scverse / scverse/scanpy

pp.neighbors returns edgeless graph for n_neighbors=1

Open
#1,706 3 comments 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

  • 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.

I worked on a very small bulk dataset recently (n_obs = 15), and wanted to try a 1-nearest-neighbor graph for the UMAP. However, edgeless graphs are returned for n_neighbors=1. I would expect that each node would have at least one edge in this case.

Minimal code sample (that we can copy&paste without having any data)
import scanpy as sc
adata = sc.datasets.blobs(n_observations=5)
sc.pp.neighbors(adata, n_neighbors=1)
print('Connectivities:\n', adata.uns['neighbors']['connectivities'].A)
sc.tl.umap(adata)
Connectivities:
 [[0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0.]]
/opt/conda/lib/python3.7/site-packages/ipykernel_launcher.py:4: FutureWarning: This location for 'connectivities' is deprecated. It has been moved to .obsp[connectivities], and will not be accesible here in a future version of anndata.
  after removing the cwd from sys.path.

---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-28-2906b54049c5> in <module>
      3 sc.pp.neighbors(adata, n_neighbors=1)
      4 print('Connectivities:\n', adata.uns['neighbors']['connectivities'].A)
----> 5 sc.tl.umap(adata)

/opt/conda/lib/python3.7/site-packages/scanpy/tools/_umap.py in umap(adata, min_dist, spread, n_components, maxiter, alpha, gamma, negative_sample_rate, init_pos, random_state, a, b, copy, method, neighbors_key)
    194             neigh_params.get('metric', 'euclidean'),
    195             neigh_params.get('metric_kwds', {}),
--> 196             verbose=settings.verbosity > 3,
    197         )
    198     elif method == 'rapids':

/opt/conda/lib/python3.7/site-packages/umap/umap_.py in simplicial_set_embedding(data, graph, n_components, initial_alpha, a, b, gamma, negative_sample_rate, n_epochs, init, random_state, metric, metric_kwds, output_metric, output_metric_kwds, euclidean_output, parallel, verbose)
   1022             n_epochs = 200
   1023 
-> 1024     graph.data[graph.data < (graph.data.max() / float(n_epochs))] = 0.0
   1025     graph.eliminate_zeros()
   1026 

/opt/conda/lib/python3.7/site-packages/numpy/core/_methods.py in _amax(a, axis, out, keepdims, initial, where)
     37 def _amax(a, axis=None, out=None, keepdims=False,
     38           initial=_NoValue, where=True):
---> 39     return umr_maximum(a, axis, None, out, keepdims, initial, where)
     40 
     41 def _amin(a, axis=None, out=None, keepdims=False,

ValueError: zero-size array to reduction operation maximum which has no identity


Versions

For me, scanpy.logging.print_versions() crashes due to the importlib_metadata issue.

scanpy==1.7.1 anndata==0.7.5 umap==0.4.6 numpy==1.20.0 scipy==1.6.0 pandas==1.2.1 scikit-learn==0.24.1 statsmodels==0.12.2 python-igraph==0.8.3 louvain==0.7.0 leidenalg==0.8.3

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

Reproduce the issue with the minimal sc.datasets.blobs example, then trace sc.pp.neighbors with n_neighbors=1 and the subsequent sc.tl.umap call. Confirm the fix produces a graph with edges and allows UMAP to run without the zero-size reduction error.

Written by the indexing model from the issue text.

Assessment

Tech stack
python
Domain
bioinformatics, machine-learning
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.