pinellolab / singlecellvr

13 stars 4 forks source link

cm undefined in scv converter #12

Closed davidfstein closed 3 years ago

davidfstein commented 3 years ago

@huidongchen

There seems to be a small bug in

def get_colors(adata,ann):
    df_cell_colors = pd.DataFrame(index=adata.obs.index)
    if(is_numeric_dtype(adata.obs[ann])):
        cm = mpl.cm.get_cmap('viridis',512)
        norm = mpl.colors.Normalize(vmin=0, vmax=max(adata.obs[ann]),clip=True)
        df_cell_colors[ann+'_color'] = [mpl.colors.to_hex(cm(norm(x))) for x in adata.obs[ann]]
    else:
        df_cell_colors[ann+'_color'] = ''

        adata.obs[ann] = adata.obs[ann].astype('category')
        categories = adata.obs[ann].cat.categories
        length = len(categories)
        # check if default matplotlib palette has enough colors
        # mpl.style.use('default')
        if len(mpl.rcParams['axes.prop_cycle'].by_key()['color']) >= length:
            cc = mpl.rcParams['axes.prop_cycle']()
            palette = [next(cc)['color'] for _ in range(length)]
        else:
            if length <= 20:
                palette = palettes.default_20
            elif length <= 28:
                palette = palettes.default_28
            elif length <= len(palettes.default_102):  # 103 colors
                palette = palettes.default_102
            else:
                rgb_rainbow = cm.rainbow(np.linspace(0,1,length))
                palette = [mpl.colors.rgb2hex(rgb_rainbow[i,:-1]) for i in range(length)]
        for i,x in enumerate(categories):
            id_cells = np.where(adata.obs[ann]==x)[0]
            df_cell_colors.loc[df_cell_colors.index[id_cells],ann+'_color'] = palette[i]
    return(df_cell_colors[ann+'_color'].tolist())

In this line rgb_rainbow = cm.rainbow(np.linspace(0,1,length)) cm is not defined

huidongchen commented 3 years ago

It's solved in https://github.com/pinellolab/singlecellvr/pull/13