scverse / scanpy

Single-cell analysis in Python. Scales to >1M cells.
https://scanpy.readthedocs.io
BSD 3-Clause "New" or "Revised" License
1.9k stars 597 forks source link

`scanpy` doesn't plot labels in UMAP #1701

Closed cartal closed 3 years ago

cartal commented 3 years ago

Hi

I'm trying to do a standard analysis of cells with scanpy?; however, I'm having a weird issue with plotting the results.

For instance, I get the following error when I try to visualise the cell states on my data:

sc.pl.umap(combined_bbknn, color = ['scNym'], size = 1, legend_fontsize = 6, color_map = 'RdPu', frameon = False)

Screenshot 2021-03-01 at 09 57 24

As you can see, the labels are there, but somehow the colours do not make it to the UMAP. When I look for a specific subpopulation, it seems that it finds it, but somehow it doesn't display it.

sc.pl.umap(combined_bbknn, color = ['scNym'], groups = ['NKT'], size = 3, legend_fontsize = 6, color_map = 'RdPu', frameon = False)

Screenshot 2021-03-01 at 10 06 21

I checked if there was something odd with the labels, but they are there.

combined_bbknn.obs['scNym'].cat.categories

Index(['Adip1', 'B_memory', 'CD4+T_cytox', 'CD8+T_tem', 'CD14+Monocyte',
       'DOCK4+MØ1', 'EC1_cap', 'EC3_cap', 'EC4_immune', 'EC5_art', 'EC6_ven',
       'EC7_atria', 'FB1', 'FB2', 'FB3', 'FB4', 'FB5', 'Mast', 'Meso', 'MØ',
       'NC1', 'NK', 'NKT', 'PC1_vent', 'PC2_atria', 'PC3_str', 'SMC1_basic',
       'SMC2_art', 'aCM1', 'aCM3', 'vCM1', 'vCM2', 'vCM3'],
      dtype='object')

They even have assigned colours:

('scNym_colors', ['#FFFF00', '#1CE6FF', '#FF34FF', '#FF4A46', '#008941', '#006FA6', '#A30059', '#FFDBE5', '#7A4900', '#0000A6', '#63FFAC', '#B79762', '#004D43', '#8FB0FF', '#997D87', '#5A0007', '#809693', '#6A3A4C', '#1B4400', '#4FC601', '#3B5DFF', '#4A3B53', '#FF2F80', '#61615A', '#BA0900', '#6B7900', '#00C2A0', '#FFAA92', '#FF90C9', '#B903AA', '#D16100', '#DDEFFF', '#000035'])])

Then I tried to plot some markers using the dotplot function, but then I got this error:

marker_genes = ['PERM1', 'GAB3', 'G6PD']
combined_bbknn.var_names_make_unique()
sc.pl.dotplot(combined_bbknn, marker_genes, groupby = 'scNym')
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-17-3392793686cd> in <module>
      1 marker_genes = ['PERM1', 'GAB3', 'G6PD']
      2 combined_bbknn.var_names_make_unique()
----> 3 sc.pl.dotplot(combined_bbknn, marker_genes, groupby = 'scNym')

/opt/conda/lib/python3.8/site-packages/scanpy/plotting/_dotplot.py in dotplot(adata, var_names, groupby, use_raw, log, num_categories, expression_cutoff, mean_only_expressed, cmap, dot_max, dot_min, standard_scale, smallest_dot, title, colorbar_title, size_title, figsize, dendrogram, gene_symbols, var_group_positions, var_group_labels, var_group_rotation, layer, swap_axes, dot_color_df, show, save, ax, return_fig, **kwds)
    949         return dp
    950     else:
--> 951         dp.make_figure()
    952         savefig_or_show(DotPlot.DEFAULT_SAVE_PREFIX, show=show, save=save)
    953         show = settings.autoshow if show is None else show

/opt/conda/lib/python3.8/site-packages/scanpy/plotting/_baseplot_class.py in make_figure(self)
    730         if self.legends_width > 0:
    731             legend_ax = self.fig.add_subplot(gs[0, 1])
--> 732             self._plot_legend(legend_ax, return_ax_dict, normalize)
    733 
    734         self.ax_dict = return_ax_dict

/opt/conda/lib/python3.8/site-packages/scanpy/plotting/_dotplot.py in _plot_legend(self, legend_ax, return_ax_dict, normalize)
    490         if self.show_size_legend:
    491             size_legend_ax = fig.add_subplot(legend_gs[1])
--> 492             self._plot_size_legend(size_legend_ax)
    493             return_ax_dict['size_legend_ax'] = size_legend_ax
    494 

/opt/conda/lib/python3.8/site-packages/scanpy/plotting/_dotplot.py in _plot_size_legend(self, size_legend_ax)
    418         # a descending range that is afterwards inverted is used
    419         # to guarantee that dot_max is in the legend.
--> 420         size_range = np.arange(self.dot_max, self.dot_min, step * -1)[::-1]
    421         if self.dot_min != 0 or self.dot_max != 1:
    422             dot_range = self.dot_max - self.dot_min

ValueError: arange: cannot compute length

and this figure:

Screenshot 2021-03-01 at 10 02 44

I have tried this in a MacOS and two Linux machines with the same error. I have downgraded to scanpy==1.6 and the error seems to persist.

Any help would be appreciated.

----- anndata 0.7.4 scanpy 1.7.1 sinfo 0.3.1 ----- OpenSSL 20.0.1 PIL 8.1.0 anndata 0.7.4 annoy NA anyio NA attr 20.3.0 babel 2.9.0 backcall 0.2.0 bbknn NA brotli NA certifi 2020.12.05 cffi 1.14.4 chardet 3.0.4 cryptography 3.3.1 cycler 0.10.0 cython_runtime NA dateutil 2.8.1 decorator 4.4.2 get_version 2.1 google NA h5py 2.10.0 idna 2.10 igraph 0.9.0 ipykernel 5.4.2 ipython_genutils 0.2.0 jedi 0.17.2 jinja2 2.11.2 joblib 1.0.1 json5 NA jsonschema 3.2.0 jupyter_server 1.2.2 jupyterlab_server 2.1.3 kiwisolver 1.3.1 legacy_api_wrap 1.2 leidenalg 0.8.0 llvmlite 0.32.1 louvain 0.7.0 markupsafe 1.1.1 matplotlib 3.3.4 mpl_toolkits NA natsort 7.1.1 nbclassic NA nbformat 5.1.2 numba 0.49.1 numexpr 2.7.2 numpy 1.18.2 packaging 20.8 pandas 1.0.4 parso 0.7.1 pexpect 4.8.0 pickleshare 0.7.5 pkg_resources NA prometheus_client NA prompt_toolkit 3.0.14 psutil 5.8.0 ptyprocess 0.7.0 pvectorc NA pycparser 2.20 pygments 2.7.4 pyparsing 2.4.7 pyrsistent NA pytz 2020.5 requests 2.23.0 ruamel NA scanpy 1.7.1 scipy 1.4.1 send2trash NA setuptools_scm NA sinfo 0.3.1 six 1.14.0 sklearn 0.22.2.post1 sniffio 1.2.0 socks 1.7.1 storemagic NA tables 3.6.1 texttable 1.6.3 tornado 6.1 traitlets 5.0.5 typing_extensions NA umap 0.3.10 urllib3 1.25.8 wcwidth 0.2.5 yaml 5.3.1 zmq 21.0.1 ----- IPython 7.19.0 jupyter_client 6.1.11 jupyter_core 4.7.0 jupyterlab 3.0.5 notebook 6.2.0 ----- Python 3.8.6 | packaged by conda-forge | (default, Dec 26 2020, 05:05:16) [GCC 9.3.0] Linux-4.15.0-112-generic-x86_64-with-glibc2.10 60 logical CPU cores, x86_64 ----- Session information updated at 2021-03-01 09:45
ivirshup commented 3 years ago

Can you show us the values of combined_bbknn.obs['scNym']?

Also, if you create a conda environment, does your problems still occur? I'm wondering if some dependencies like pandas could be out of date.

cartal commented 3 years ago

Hi,

Thanks for the quick reply!

I'm attaching the output for combined_bbknn.obs['scNym']:

Screenshot 2021-03-01 at 11 09 39.

This is really weird. When I tested it on my macbook I created a new environment and the problem persisted. However, there I downgraded to scanpy==1.6 as well, the problem persisted, but the NAs weren't there.

ivirshup commented 3 years ago

Do you know how that entry could have been filled with NaN?

The plots and errors you were showing above are consistent with all the values being "null".

cartal commented 3 years ago

The only issue I can think of was when I was creating the object. Before I used to transfer the adata.obs dataframe to a new one by doing adata_new.obs = adata_old.obs. When I did this in scanpy==1.7.1 the transfer didnÄt show any errors, but it didn't copy. This was fixed when I added the .copy() to that command.

When I ran the same thing on a macbook pro, the labels somehow disappeared after calculating highly variable genes.

I have been using this notebook since scanpy==1.6 and it didn't give me any problems until I upgraded to scanpy==1.7.1.

ivirshup commented 3 years ago

Could you come up with the minimum amount of commands you'd need to run to reproduce this? It would also be helpful if this could be done using generated or publicly available data. Something like this would be great.

ivirshup commented 3 years ago

Just checking back on this, it's concerning if you are getting null values unexpectedly, but it's difficult for me to figure out why that could be happening without more information. It would be great if you're able to give us any update on this.

cartal commented 3 years ago

Hi,

Yes, I will do it. Please bear with me while I make it.

giovp commented 3 years ago

@cartal this seems like a merge/concat gone wrong and indexes got messed up. happens to me a lot. I'm gonna close this because as @ivirshup mentioned the problem is proabbly due to some anndata operation rather than the plotting itself. feel free to reopen this or open a new issue in case this is still a problem.

cartal commented 3 years ago

I think this was somehow related, yes. After making an new anndata the problem was resolved.

Thank you guys