Skip to content

"IndexError: boolean index did not match indexed array along dimension" when working with subsetted anndata object #699

Closed

Description

@brennerUT

When I run this

import scanpy as sc

sc.logging.print_versions()

pbmc = sc.datasets.pbmc68k_reduced()
pbmc = pbmc[pbmc.obs['louvain'] == '0', :]
sc.pp.scale(pbmc)

I get this

scanpy==1.4.3 anndata==0.6.21 umap==0.3.9 numpy==1.16.2 scipy==1.3.0 pandas==0.24.2 scikit-learn==0.21.2 statsmodels==0.9.0
/stor/home/ericb123/miniconda3/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py:1120: RuntimeWarning: invalid value encountered in sqrt
  scale = np.sqrt(var)
Trying to set attribute `.X` of view, making a copy.
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/stor/home/ericb123/miniconda3/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py", line 860, in scale
    scale(adata.X, zero_center=zero_center, max_value=max_value, copy=False)
File "/stor/home/ericb123/miniconda3/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py", line 876, in scale
    _scale(X, zero_center)
File "/stor/home/ericb123/miniconda3/lib/python3.7/site-packages/scanpy/preprocessing/_simple.py", line 1126, in _scale
    scale[scale == 0] = 1e-12
File "/stor/home/ericb123/miniconda3/lib/python3.7/site-packages/anndata/base.py", line 392, in __setitem__
    getattr(adata_view, attr_name)[idx] = value
IndexError: boolean index did not match indexed array along dimension 0; dimension is 130 but corresponding boolean dimension is 765

Any idea what's going on?

Thanks,

-Eric

Activity

  1. ivirshup commented on Jun 22, 2019

    @ivirshup
    Member

    It looks like it's a bug in how we handle views of objects. If you copy the view you get from subsetting, that should give you the result you want, i.e.:

    pbmc = sc.datasets.pbmc68k_reduced()
    pbmc = pbmc[pbmc.obs['louvain'] == '0', :].copy()
    sc.pp.scale(pbmc)

    On our side, I think we should change the behavior of ArrayViews so they return np.ndarrays when operations are performed on them. Maybe we need to override __array_ufunc__?

  2. self-assigned this
    on Jul 5, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions