question-mark
Stuck on an issue?

Lightrun Answers was designed to reduce the constant googling that comes with debugging 3rd party libraries. It collects links to all the places you might be looking at while hunting down a tough bug.

And, if you’re still stuck at the end, we’re happy to hop on a call to see how we can help out.

subsetting isn't working after updating scanpy

See original GitHub issue

subsetting doesn’t work: I’ve this object

>>> tiss
AnnData object with n_obs × n_vars = 29322 × 19860
>>> tiss[tiss.obs['cell_ontology_class']=='B cell']
IndexError                                Traceback (most recent call last)
<ipython-input-269-28b4524131cb> in <module>()
----> 1 tiss[tiss.obs['cell_ontology_class']=='B cell']

~/anaconda3/lib/python3.6/site-packages/anndata/base.py in __getitem__(self, index)
   1299     def __getitem__(self, index):
   1300         """Returns a sliced view of the object."""
-> 1301         return self._getitem_view(index)
   1302 
   1303     def _getitem_view(self, index):

~/anaconda3/lib/python3.6/site-packages/anndata/base.py in _getitem_view(self, index)
   1303     def _getitem_view(self, index):
   1304         oidx, vidx = self._normalize_indices(index)
-> 1305         return AnnData(self, oidx=oidx, vidx=vidx, asview=True)
   1306 
   1307     def _remove_unused_categories(self, df_full, df_sub, uns):

~/anaconda3/lib/python3.6/site-packages/anndata/base.py in __init__(self, X, obs, var, uns, obsm, varm, layers, raw, dtype, shape, filename, filemode, asview, oidx, vidx)
    662             if not isinstance(X, AnnData):
    663                 raise ValueError('`X` has to be an AnnData object.')
--> 664             self._init_as_view(X, oidx, vidx)
    665         else:
    666             self._init_as_actual(

~/anaconda3/lib/python3.6/site-packages/anndata/base.py in _init_as_view(self, adata_ref, oidx, vidx)
    713             raise KeyError('Unknown Index type')
    714         # fix categories
--> 715         self._remove_unused_categories(adata_ref.obs, obs_sub, uns_new)
    716         self._remove_unused_categories(adata_ref.var, var_sub, uns_new)
    717         # set attributes

~/anaconda3/lib/python3.6/site-packages/anndata/base.py in _remove_unused_categories(self, df_full, df_sub, uns)
   1318                     uns[k + '_colors'] = np.array(uns[k + '_colors'])[
   1319                         np.where(np.in1d(
-> 1320                             all_categories, df_sub[k].cat.categories))[0]]
   1321 
   1322     def rename_categories(self, key, categories):

IndexError: index 7 is out of bounds for axis 1 with size 7

even though it’s part of the set:

>>> set(tiss.obs['cell_ontology_class'])
{'B cell',
 'NA',
 'T cell',
 'dendritic cell',
 'macrophage',
 'natural killer cell'}

it does work for louvain though:

>>> tiss[tiss.obs['louvain']=='0']`
View of AnnData object with n_obs × n_vars = 5862 × 19860`

Issue Analytics

  • State:open
  • Created 5 years ago
  • Comments:13 (11 by maintainers)

github_iconTop GitHub Comments

1reaction
HYsxecommented, Feb 19, 2020

Hi @aopisco ! @falexwolf I ran into the same problem but got everything to work by deleting all the unnecessary items in adata.uns.

keep = ['neighbors', ]
keys = list(adata.uns.keys())
for key in keys:
    if key not in keep:
        del adata.uns[key]

I don’t get errors anymore but I fear that this might cause other problems I’m currently unaware of.

0reactions
simonewebbcommented, Feb 18, 2020

I do get this issue from time to time and @HYsxe 's solution works for me (thanks!). Any idea why this is? Thanks!

Read more comments on GitHub >

github_iconTop Results From Across the Web

subsetting isn't working after updating scanpy #363 - GitHub
subsetting doesn't work: I've this object >>> tiss AnnData object with n_obs ... subsetting isn't working after updating scanpy #363.
Read more >
new-10kPBMC-Scanpy
We start the analysis after two preliminary steps have been completed: 1) ambient RNA correction using soupX ; 2) doublet detection using scrublet...
Read more >
integrating single-cell datasets - Swarup Lab
Introduction. In this notebook I will go over several integration techniques for single-cell -omics data. These techniques are suitable for a variety of ......
Read more >
Integrating spatial data with scRNA-seq using scanorama
This tutorial shows how to work with multiple Visium datasets and perform ... When using your own Visium data, use Scanpy's read_visium() ...
Read more >
subsetting anndata on basis of louvain clusters - Stack Overflow
If your adata.obs has a "louvain" column that I'd expect after running tl.louvain , you could do the subsetting as ...
Read more >

github_iconTop Related Medium Post

No results found

github_iconTop Related StackOverflow Question

No results found

github_iconTroubleshoot Live Code

Lightrun enables developers to add logs, metrics and snapshots to live code - no restarts or redeploys required.
Start Free

github_iconTop Related Reddit Thread

No results found

github_iconTop Related Hackernoon Post

No results found

github_iconTop Related Tweet

No results found

github_iconTop Related Dev.to Post

No results found

github_iconTop Related Hashnode Post

No results found