Repository navigation
Closed
Description
suddenly I have this problem, maybe related to an anndata upgrade. pip says all requirements are satisfied:
scanpy==1.4.3 anndata==0.6.22rc1 umap==0.3.9 numpy==1.16.4 scipy==1.2.1 pandas==0.24.2 scikit-learn==0.21.2 statsmodels==0.10.0 python-igraph==0.7.1 louvain==0.6.1
adata
AnnData object with n_obs × n_vars = 466 × 28685
obs: 'GEO_Sample_age', 'age', 'age_unit', 'biosample_source_life_stage', 'biosample_source_gender', 'sample_category', 'biosample_cell_type', 'n_genes', 'n_counts', 'percent_mito'
var: 'n_cells'
fig1 = sc.pl.scatter(adata, x='n_counts', y='n_genes', save="_gene_count")
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in _get_obs_array(self, k, use_raw, layer)
1527 obs.keys and then var.index."""
1528 if use_raw:
-> 1529 return self.raw.obs_vector(k)
1530 else:
1531 return self.obs_vector(k=k, layer=layer)
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in obs_vector(self, k)
408 as `.obs_names`.
409 """
--> 410 a = self[:, k].X
411 if issparse(a):
412 a = a.toarray()
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in __getitem__(self, index)
331
332 def __getitem__(self, index):
--> 333 oidx, vidx = self._normalize_indices(index)
334 if self._adata is not None or not self._adata.isbacked: X = self._X[oidx, vidx]
335 else: X = self._adata.file['raw.X'][oidx, vidx]
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in _normalize_indices(self, packed_index)
360 obs, var = unpack_index(packed_index)
361 obs = _normalize_index(obs, self._adata.obs_names)
--> 362 var = _normalize_index(var, self.var_names)
363 return obs, var
364
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in _normalize_index(index, names)
153 return slice(start, stop, step)
154 elif isinstance(index, (np.integer, int, str)):
--> 155 return name_idx(index)
156 elif isinstance(index, (Sequence, np.ndarray, pd.Index)):
157 # here, we replaced the implementation based on name_idx with this
~/miniconda3/envs/py3/lib/python3.6/site-packages/anndata/core/anndata.py in name_idx(i)
140 raise IndexError(
141 'Key "{}" is not valid observation/variable name/index.'
--> 142 .format(i))
143 i = i_found[0]
144 return i
I don't understand why anndata thinks that
IndexError: Key "n_counts" is not valid observation/variable name/index.
even though it's clearly in adata.obs... any suggestions what to do? add print statements to the various functions?
Metadata
Metadata
Assignees
Labels
No labels
Activity
maximilianh commentedon Jul 4, 2019
The weirdest thing is that if I write this adata object to an h5ad file with adata.write("temp.h5ad"), load it from there and run the same command, it works.
I wonder if this indicates some issue with the .obs object or some version issue...
maximilianh commentedon Jul 4, 2019
Even something simple doesn't work anymore, without going through h5ad:
maximilianh commentedon Jul 4, 2019
I wonder if this has to do with the view discussed in #699.
The weird thing is these are very basic operations and I imagine this has come up before for someone else...
Anyhow, I'm closing this, #699 gave me the idea that this is just a very recent problem, it works fine with scanpy 1.4.1, I guess this is already on your radar via #699
ivirshup commentedon Jul 5, 2019
This is separate from that, what's happening is that
_get_obs_arrayhad a change a behaviour during a bug fix.What we should do is
Allow asking for a key of obs when use_raw=True in deprecated _get_{o…
Allow asking for a key of obs when use_raw=True in deprecated _get_{o…
maximilianh commentedon Jul 5, 2019
LuckyMD commentedon Jul 5, 2019
The max categorical error was one that I thought was addressed by anndata 0.6.18. I assume this is still on 0.6.22rc1? There was previously a switch from defaulting to ordered categoricals to unordered instead.
There are quite a few unit tests... but clearly not perfect coverage. Others will be able to say more about the coverage than me.
ivirshup commentedon Jul 6, 2019
The original bug you hit was with the
sc.pl.scatterwhich has few tests.I'd recommend trying out the master branches of
AnnDataandscanpyuntil new releases can be made in cases like these.LuckyMD commentedon Jul 17, 2019
I just got the same error with a similar situation.
I get umap coordinates from a collaborator, which I store in
adata.obs. Before the last update this worked:sc.pl.scatter(adata, x='UMAP1', y='UMAP2', color='cell_type_class')Now, this produces a
IndexError: Key "UMAP1" is not valid observation/variable name/index.error.Now I need to run this for the same plot:
sc.pl.scatter(adata, x='UMAP1', y='UMAP2', color='cell_type_class', use_raw=False)These covariates are all in
adata.obs.keys(). It seems thatuse_rawis taking precendence overxandybeing fromadata.obs.LuckyMD commentedon Jul 17, 2019
Oh, I also get
DeprecationWarningandFutureWarningaboutlayer='X'being removed in future andobs_vectorbeing used, while I assume these are just used insc.pl.scatterin the background. I guess this is in the process of being fixed though.ivirshup commentedon Jul 20, 2019
This should be fixed in the
v1.4.4. Could you try that out and see if this is fixed?maximilianh commentedon Jul 29, 2019
Hi Isaac, I've updated to v1.4.4 but I'm still getting this problem. I've finally produced a minimal test case:
output is:
The problem does not appear with the pbmc3k data. It does appear with any other expression matrix, as long as it is in text format.
I noted that the adata object when reading from a text file does not have a real .var content, the .var is a dataframe with just an index. But I have no idea if this is related to the problem.
maximilianh commentedon Jul 30, 2019
One more thing: the exception does not happen if I comment out the line:
maximilianh commentedon Jul 30, 2019
Hmm...I must admit I don't understand why a "view" exists. Views are often tricky to get right, especially in a complex datastructure like anndata. They also slow down processing, especially if users may not be aware that the object they have is a view of something else. I don't see a good use case for views in my pipeline at least. Is there a way to switch off all views in anndata and just return a copy when slicing?
LuckyMD commentedon Jul 30, 2019
I wonder if it works if you use
adata.raw = adata.copy()instead. Maybe the issue is a View inadata.raw?ivirshup commentedon Jul 31, 2019
I've just spent a while trying to replicate, before realizing I've seen this issue before over on AnnData (scverse/anndata#182). I've got some good and bad news about this. It's fixed on master, but that fix is slated to be release in
v0.7, which has intentionally breaking changes.I find views very useful when dealing with large datasets interactively. They're also important for file backed data, since copies are extremely expensive in that case.
Unlike numpy, AnnData objects should always return a view when subset. If you'd like to get copies, you could add a
.copy()to the end of your subsetting statement.maximilianh commentedon Jul 31, 2019
Zethson commentedon Nov 2, 2025
Closing this as this seems to have been fixed.