Repository navigation
Closed
Description
- I have checked that this issue has not already been reported.
- I have confirmed this bug exists on the latest version of scanpy.
- (optional) I have confirmed this bug exists on the master branch of scanpy.
Hey!
I've run into a weird issue where I can't plot categorical data in scanpy, it always gives an error (NotImplementedError: isna is not defined for MultiIndex) while assigning colors (see below for the full error). Plotting numerical data works just fine and also plotting categorical data with scvelo works fine.
Manually specifying a color palette works, but results in a weird choice of colors.
Example output of sc.pl.umap(pbmc, color = 'phase', palette = 'tab10')

Any ideas of what could be the issue here?
Minimal code sample (that we can copy&paste without having any data)
import scanpy as sc
pbmc = sc.datasets.pbmc68k_reduced()
sc.pl.umap(pbmc, color = 'phase')Traceback
---------------------------------------------------------------------------
NotImplementedError Traceback (most recent call last)
<ipython-input-39-d43e888a7389> in <module>
----> 1 sc.pl.umap(adata, color = 'phase')
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/scanpy/plotting/_tools/scatterplots.py in umap(adata, **kwargs)
601 If `show==False` a :class:`~matplotlib.axes.Axes` or a list of it.
602 """
--> 603 return embedding(adata, 'umap', **kwargs)
604
605
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/scanpy/plotting/_tools/scatterplots.py in embedding(adata, basis, color, gene_symbols, use_raw, sort_order, edges, edges_width, edges_color, neighbors_key, arrows, arrows_kwds, groups, components, layer, projection, scale_factor, color_map, cmap, palette, na_color, na_in_legend, size, frameon, legend_fontsize, legend_fontweight, legend_loc, legend_fontoutline, vmax, vmin, add_outline, outline_width, outline_color, ncols, hspace, wspace, title, show, save, ax, return_fig, **kwargs)
244 groups=groups,
245 )
--> 246 color_vector, categorical = _color_vector(
247 adata,
248 value_to_plot,
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/scanpy/plotting/_tools/scatterplots.py in _color_vector(adata, values_key, values, palette, na_color)
1129 else: # is_categorical_dtype(values)
1130 color_map = _get_palette(adata, values_key, palette=palette)
-> 1131 color_vector = values.map(color_map).map(to_hex)
1132
1133 # Set color to 'missing color' for all missing values
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/arrays/categorical.py in map(self, mapper)
1196 new_categories = self.categories.map(mapper)
1197 try:
-> 1198 return self.from_codes(
1199 self._codes.copy(), categories=new_categories, ordered=self.ordered
1200 )
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/arrays/categorical.py in from_codes(cls, codes, categories, ordered, dtype)
567 Categories (2, object): ['a' < 'b']
568 """
--> 569 dtype = CategoricalDtype._from_values_or_dtype(
570 categories=categories, ordered=ordered, dtype=dtype
571 )
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/dtypes.py in _from_values_or_dtype(cls, values, categories, ordered, dtype)
271 # Note: This could potentially have categories=None and
272 # ordered=None.
--> 273 dtype = CategoricalDtype(categories, ordered)
274
275 return dtype
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/dtypes.py in __init__(self, categories, ordered)
158
159 def __init__(self, categories=None, ordered: Ordered = False):
--> 160 self._finalize(categories, ordered, fastpath=False)
161
162 @classmethod
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/dtypes.py in _finalize(self, categories, ordered, fastpath)
312
313 if categories is not None:
--> 314 categories = self.validate_categories(categories, fastpath=fastpath)
315
316 self._categories = categories
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/dtypes.py in validate_categories(categories, fastpath)
505 if not fastpath:
506
--> 507 if categories.hasnans:
508 raise ValueError("Categorical categories cannot be null")
509
pandas/_libs/properties.pyx in pandas._libs.properties.CachedProperty.__get__()
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/indexes/base.py in hasnans(self)
2193 """
2194 if self._can_hold_na:
-> 2195 return bool(self._isnan.any())
2196 else:
2197 return False
pandas/_libs/properties.pyx in pandas._libs.properties.CachedProperty.__get__()
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/indexes/base.py in _isnan(self)
2172 """
2173 if self._can_hold_na:
-> 2174 return isna(self)
2175 else:
2176 # shouldn't reach to this condition by checking hasnans beforehand
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/missing.py in isna(obj)
125 Name: 1, dtype: bool
126 """
--> 127 return _isna(obj)
128
129
~/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/pandas/core/dtypes/missing.py in _isna(obj, inf_as_na)
154 # hack (for now) because MI registers as ndarray
155 elif isinstance(obj, ABCMultiIndex):
--> 156 raise NotImplementedError("isna is not defined for MultiIndex")
157 elif isinstance(obj, type):
158 return False
NotImplementedError: isna is not defined for MultiIndex
Versions
Details
anndata 0.7.5
scanpy 1.7.2
sinfo 0.3.1
MulticoreTSNE NA
PIL 8.0.1
anndata 0.7.5
annoy NA
backcall 0.2.0
bbknn NA
bottleneck 1.3.2
cairo 1.19.1
cffi 1.14.0
colorama 0.4.4
cycler 0.10.0
cython_runtime NA
dateutil 2.8.1
decorator 4.4.2
fcsparser 0.2.1
future_fstrings NA
get_version 2.1
google NA
h5py 2.10.0
igraph 0.7.1
ipykernel 5.3.4
ipython_genutils 0.2.0
ipywidgets 7.5.1
jedi 0.17.2
joblib 0.17.0
kiwisolver 1.2.0
legacy_api_wrap 0.0.0
leidenalg 0.8.2
llvmlite 0.34.0
lxml 4.6.1
matplotlib 3.3.2
mkl 2.3.0
mpl_toolkits NA
natsort 7.0.1
networkx 2.5
numba 0.51.2
numexpr 2.7.1
numpy 1.19.2
packaging 20.4
palantir 1.0.0
pandas 1.2.4
parso 0.7.0
pexpect 4.8.0
phenograph 1.5.7
pickleshare 0.7.5
pkg_resources NA
prompt_toolkit 3.0.8
psutil 5.8.0
ptyprocess 0.6.0
pycparser 2.20
pygments 2.7.1
pyparsing 2.4.7
pytz 2020.1
scanpy 1.7.2
scipy 1.5.2
scvelo 0.2.3
seaborn 0.11.1
setuptools_scm NA
sinfo 0.3.1
six 1.15.0
sklearn 0.23.2
sphinxcontrib NA
statsmodels 0.12.0
storemagic NA
tables 3.6.1
tornado 6.0.4
traitlets 5.0.5
typing_extensions NA
umap 0.4.6
wcwidth 0.2.5
yaml 5.3.1
zipp NA
zmq 19.0.2
IPython 7.18.1
jupyter_client 6.1.7
jupyter_core 4.6.3
notebook 6.1.4
Python 3.8.0 | packaged by conda-forge | (default, Nov 22 2019, 19:11:38) [GCC 7.3.0]
Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.10
4 logical CPU cores, x86_64
Session information updated at 2021-05-24 12:06
Reacted by Xin Xiong
Activity
ivirshup commentedon May 24, 2021
Could you install the newest bugfix release of anndata and try again? Failing that, I'd recommend upgrading
h5pyand seeing if that works.karlann commentedon May 24, 2021
The issue persists with anndata 0.7.6. I've also been trying to update h5py, but it has conflicts with other packages. I'll post an update if I get it updated.
ivirshup commentedon May 25, 2021
What packages are conflicting with
h5py3.0? The 2 -> 3 update had some fairly hard to manage changes to how string dtypes are handled, and it'd be nice to drop 2.0 support once the ecosystem is caught up.I'm actually not so sure this is h5py or anndata though, those are just common culprits. I've tried this in a conda environment with h5py 2.10.0 and it doesn't reproduce. I've even tried to make a conda environment from your
sinfoand could not reproduce.Here's how I tried to create a replicate environment
Could you create a fresh environment, and try again? I'm really confused about how you are ending up with a multi index anywhere.
karlann commentedon May 25, 2021
I created a new environment (see below for package details) and there everything works as it should.
Versions in the new working environment
anndata 0.7.6
scanpy 1.7.2
sinfo 0.3.1
PIL 7.2.0
anndata 0.7.6
anyio NA
argon2 20.1.0
attr 21.2.0
babel 2.9.1
backcall 0.2.0
bottleneck 1.3.2
brotli NA
certifi 2020.12.05
cffi 1.14.5
chardet 4.0.0
cloudpickle 1.6.0
colorama 0.4.4
cycler 0.10.0
cython_runtime NA
cytoolz 0.11.0
dask 2021.05.0
dateutil 2.8.1
decorator 5.0.9
fsspec 2021.05.0
get_version 2.1
h5py 3.2.1
idna 2.10
igraph 0.9.1
ipykernel 5.5.5
ipython_genutils 0.2.0
ipywidgets 7.6.3
jedi 0.17.2
jinja2 3.0.1
joblib 1.0.1
json5 NA
jsonschema 3.2.0
jupyter_server 1.8.0
jupyterlab_server 2.5.2
kiwisolver 1.3.1
legacy_api_wrap 0.0.0
leidenalg 0.8.4
llvmlite 0.36.0
louvain 0.7.0
markupsafe 2.0.1
matplotlib 3.4.2
mpl_toolkits NA
natsort 7.1.1
nbclassic NA
nbformat 5.1.3
numba 0.53.1
numexpr 2.7.3
numpy 1.18.5
packaging 20.9
pandas 1.2.4
parso 0.7.0
pexpect 4.8.0
pickleshare 0.7.5
pkg_resources NA
prometheus_client NA
prompt_toolkit 3.0.18
psutil 5.8.0
ptyprocess 0.7.0
pvectorc NA
pygments 2.9.0
pyparsing 2.4.7
pyrsistent NA
pytz 2021.1
requests 2.25.1
scanpy 1.7.2
scipy 1.5.3
seaborn 0.11.1
send2trash NA
setuptools_scm NA
sinfo 0.3.1
six 1.16.0
sklearn 0.24.2
sniffio 1.2.0
socks 1.7.1
sphinxcontrib NA
statsmodels 0.12.2
storemagic NA
tables 3.6.1
tblib 1.7.0
terminado 0.10.0
texttable 1.6.3
tlz 0.11.0
toolz 0.11.1
tornado 6.1
traitlets 5.0.5
typing_extensions NA
urllib3 1.26.4
wcwidth 0.2.5
websocket 0.57.0
yaml 5.4.1
zmq 22.0.3
zope NA
IPython 7.23.1
jupyter_client 6.1.12
jupyter_core 4.7.1
jupyterlab 3.0.16
notebook 6.4.0
Python 3.8.10 (default, May 19 2021, 18:05:58) [GCC 7.3.0]
Linux-4.4.0-19041-Microsoft-x86_64-with-glibc2.10
4 logical CPU cores, x86_64
Session information updated at 2021-05-25 15:50
I'm still trying to update h5py in the old environment, which has quite some inconsistencies in it, considerably slowing everything down. At some point it looked like I had success with installing h5py 3.2.1 from conda-forge after running
conda update anacondaandconda update --all(as per here). But now this environment leads to an ImportError when importing scanpy:ImportError: /home/karl/anaconda3/envs/scanpy1_7/lib/python3.8/site-packages/h5py/defs.cpython-38-x86_64-linux-gnu.so: undefined symbol: H5Pset_fapl_ros3Can it be that pip version of scanpy doesn't see the updated conda version of h5py?
Inconsistencies in the old environment
Package versions in old environment
ImportError traceback
Before, when I was trying to update
h5pyand getting lots of conflicts, I let conda try to figure out all of them, printing out a huge list that reached the output limit of my console (~6000 lines). Let me know if I should post the list.ivirshup commentedon May 26, 2021
Can you use this new environment to do your analysis?
I expect that the previous environment managed to get into a messy state, which can lead to very strange errors. Because of this, I generally avoid trying to update old environments much and instead opt for creating fresh ones frequently.
karlann commentedon May 26, 2021
I might have overextended the old environment a bit too much indeed, so I'll just continue with a fresh one.
If it's of any help to you, then before the problems started I wanted to try out some of the newer additions to scanpy ecosystem by installing triku, dorothea and progeny.
Thanks for your help!
OnlyBelter commentedon Jun 17, 2021
Same problem. And I use
h5pyv3.1.0,anndatav0.7.6 in conda environment, windowns 10 system.ivirshup commentedon Jun 17, 2021
@karlann yeah, conda environments can run into these problems, especially if you've installed some packages with
pipand some withconda. I generally try to create fresh environments very frequently instead of updating old ones.mamba(faster conda) definitely makes this less painful.@OnlyBelter have you tried the same solution of trying a fresh environment?
OnlyBelter commentedon Jun 17, 2021
It's weird! If I save the processed dataset in
.h5adformat, then import this file into a new jupyter notebook, I can usesc.pl.umapcorrectly. But in the original jupyter notebook which I used to process this.h5adfile (also contains many other steps before this step), I cannot use some specific columns to set parametercolorin this function (some columns can be used correctly). I even cannot run the example like below:It seems like that some steps executed before contaminate this function.
ivirshup commentedon Jun 17, 2021
That does sound weird and worrying. If you're able to figure out which steps are causing the issue, that would be helpful. It does sound like it could be a bug in a different library. Also, are you sure you're using the same kernels for the notebooks?
OnlyBelter commentedon Jun 17, 2021
I think this problem may cause by
seaborn!The following code should reproduce the error:
ivirshup commentedon Jun 18, 2021
@OnlyBelter I'm not getting an error from that, but I am getting a bunch of warnings, which makes it seem like something weird is going on.
I'm going to split this into a new issue while we investigate, since it seems a bit different.
jkobject commentedon Feb 15, 2022
I am getting a similar issue. seems to come from seaborn as well...