scverse / scverse/scanpy

Ingest: ValueError: all input arrays must have the same shape

Open
#2,635 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

Dominant language
Python
Stars
2.6k
Forks
779
Avg merge
1d 4h
Merged PRs (30d)
27

Description

Please make sure these conditions are met
  • 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.
What happened?

I am trying to run ingest in order to transfer labels from a reference dataset to a query dataset.

I have subsetted both datasets to the same features and re-run basic processing on both datasets, including pca, umap, and generating neighbor graphs.

When I run ingest I receive the following error message:
ValueError: all input arrays must have the same shape

Minimal code sample
var_names = adata_ref.var_names.intersection(adata.var_names)
adata_ref = adata_ref[:, var_names]
adata = adata[:, var_names]

sc.tl.pca(adata_ref, svd_solver='arpack')
sc.pp.neighbors(adata_ref, n_neighbors=10, n_pcs=40)
sc.tl.paga(adata_ref, groups = 'cell_type')
sc.pl.paga(adata_ref, plot=False)  # remove `plot=False` if you want to see the coarse-grained graph
sc.tl.umap(adata_ref, init_pos='paga')

adata.obs['seurat_clusters'] = adata.obs['seurat_clusters'].astype('category')
sc.tl.pca(adata, svd_solver='arpack')
sc.pp.neighbors(adata, n_neighbors=10, n_pcs=40)
sc.tl.paga(adata, groups = 'seurat_clusters')
sc.pl.paga(adata, plot=False)  # remove `plot=False` if you want to see the coarse-grained graph
sc.tl.umap(adata, init_pos='paga')

sc.tl.ingest(adata, adata_ref, obs='cell_type')
Error output
ValueError                                Traceback (most recent call last)
<ipython-input-18-6b34a6250614> in <module>
      1 # we map our tabula sapiens cell type labels onto our data
----> 2 sc.tl.ingest(adata, adata_ref, obs='cell_type')

~/.local/lib/python3.7/site-packages/scanpy/tools/_ingest.py in ingest(adata, adata_ref, obs, embedding_method, labeling_method, neighbors_key, inplace, **kwargs)
    124         labeling_method = labeling_method * len(obs)
    125 
--> 126     ing = Ingest(adata_ref, neighbors_key)
    127     ing.fit(adata)
    128 

~/.local/lib/python3.7/site-packages/scanpy/tools/_ingest.py in __init__(self, adata, neighbors_key)
    383 
    384         if neighbors_key in adata.uns:
--> 385             self._init_neighbors(adata, neighbors_key)
    386         else:
    387             raise ValueError(

~/.local/lib/python3.7/site-packages/scanpy/tools/_ingest.py in _init_neighbors(self, adata, neighbors_key)
    349         else:
    350             self._neigh_random_state = neighbors['params'].get('random_state', 0)
--> 351             self._init_pynndescent(neighbors['distances'])
    352 
    353     def _init_pca(self, adata):

~/.local/lib/python3.7/site-packages/scanpy/tools/_ingest.py in _init_pynndescent(self, distances)
    284 
    285         first_col = np.arange(distances.shape[0])[:, None]
--> 286         init_indices = np.hstack((first_col, np.stack(distances.tolil().rows)))
    287 
    288         self._nnd_idx = NNDescent(

<__array_function__ internals> in stack(*args, **kwargs)

~/.local/lib/python3.7/site-packages/numpy/core/shape_base.py in stack(arrays, axis, out)
    425     shapes = {arr.shape for arr in arrays}
    426     if len(shapes) != 1:
--> 427         raise ValueError('all input arrays must have the same shape')
    428 
    429     result_ndim = arrays[0].ndim + 1

ValueError: all input arrays must have the same shape
Versions
WARNING: If you miss a compact list, please try `print_header`!
The `sinfo` package has changed name and is now called `session_info` to become more discoverable and self-explanatory. The `sinfo` PyPI package will be kept around to avoid breaking old installs and you can downgrade to 0.3.2 if you want to use it without seeing this message. For the latest features and bug fixes, please install `session_info` instead. The usage and defaults also changed slightly, so please review the latest README at https://gitlab.com/joelostblom/session_info.
-----
anndata     0.8.0
scanpy      1.8.1
sinfo       0.3.4
-----
PIL                 7.1.2
backcall            0.2.0
beta_ufunc          NA
binom_ufunc         NA
cffi                1.15.0
colorama            0.4.4
cycler              0.10.0
cython_runtime      NA
dateutil            2.8.2
decorator           5.1.1
google              NA
h5py                3.7.0
ipykernel           5.3.0
ipython_genutils    0.2.0
ipywidgets          7.5.1
jedi                0.17.0
joblib              1.1.0
kiwisolver          1.2.0
llvmlite            0.38.1
matplotlib          3.3.0
mpl_toolkits        NA
natsort             8.1.0
nbinom_ufunc        NA
netifaces           0.11.0
numba               0.55.2
numexpr             2.8.3
numpy               1.20.0
packaging           21.3
pandas              1.1.3
parso               0.7.0
pexpect             4.8.0
pickleshare         0.7.5
pkg_resources       NA
prompt_toolkit      3.0.5
psutil              5.9.0
ptyprocess          0.6.0
pygments            2.6.1
pyparsing           3.0.8
pytz                2022.1
ruamel              NA
scipy               1.7.0
seaborn             0.11.2
setuptools          62.1.0
simplejson          3.17.6
six                 1.16.0
sklearn             1.0.1
statsmodels         0.13.2
storemagic          NA
tables              3.7.0
threadpoolctl       3.1.0
tornado             6.0.4
traitlets           4.3.3
typing_extensions   NA
wcwidth             0.2.5
yaml                6.0
zipp                NA
zmq                 19.0.1
-----
IPython             7.15.0
jupyter_client      6.1.3
jupyter_core        4.6.3
jupyterlab          2.1.4
notebook            6.1.5
-----
Python 3.7.13 (default, Aug  6 2022, 00:43:34) [GCC 9.2.0]
Linux-4.18.0-348.12.2.el8_5.x86_64-x86_64-with-centos-8.7-Green_Obsidian
192 logical CPU cores, x86_64
-----
Session information updated at 2023-08-22 16:49

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start in scanpy/tools/_ingest.py, especially Ingest._init_neighbors and _init_pynndescent, using the supplied ingest example to reproduce the failure. Inspect why distances.tolil().rows produces arrays with different shapes, then define and test the expected handling for this neighbor graph case.

Written by the indexing model from the issue text.

Assessment

Tech stack
numpy, python
Domain
data
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.