Repository navigation
Closed
Labels
Needs info❔More information needed
Description
Hello everyone !
I would like to use a custom distance when trying to compute the neighbors graph with scanpy.pp.neighbors.
So let's suppose I have a pre-existing distance matrix somewhere, and I just want to use it in the graph generation.
Currently, I am getting an error when exceeding 4096 observations in my AnnData object.
I do not have such error when directly using theumap python package.
Full error
Traceback (most recent call last):
File "/home/paul/Documents/Curie/Immunopeptidomics/minimal_bug_scanpy.py", line 72, in <module>
sc.pp.neighbors(xd,
File "/home/paul/venv/xomx/lib/python3.9/site-packages/scanpy/neighbors/__init__.py", line 139, in neighbors
neighbors.compute_neighbors(
File "/home/paul/venv/xomx/lib/python3.9/site-packages/scanpy/neighbors/__init__.py", line 791, in compute_neighbors
knn_indices, knn_distances, forest = compute_neighbors_umap(
File "/home/paul/venv/xomx/lib/python3.9/site-packages/scanpy/neighbors/__init__.py", line 305, in compute_neighbors_umap
knn_indices, knn_dists, forest = nearest_neighbors(
File "/home/paul/venv/xomx/lib/python3.9/site-packages/umap/umap_.py", line 328, in nearest_neighbors
knn_search_index = NNDescent(
File "/home/paul/venv/xomx/lib/python3.9/site-packages/pynndescent/pynndescent_.py", line 875, in __init__
self._neighbor_graph = nn_descent(
File "/home/paul/venv/xomx/lib/python3.9/site-packages/numba/core/dispatcher.py", line 468, in _compile_for_args
error_rewrite(e, 'typing')
File "/home/paul/venv/xomx/lib/python3.9/site-packages/numba/core/dispatcher.py", line 409, in error_rewrite
raise e.with_traceback(None)
numba.core.errors.TypingError: Failed in nopython mode pipeline (step: nopython frontend)
non-precise type pyobject
During: typing of argument at /home/paul/venv/xomx/lib/python3.9/site-packages/pynndescent/pynndescent_.py (330)
File "../pynndescent/pynndescent_.py", line 330:
def nn_descent(
<source elided>
if init_graph[0].shape[0] == 1: # EMPTY_GRAPH
^
This error may have been caused by the following argument(s):
- argument 4: Cannot determine Numba type of <class 'function'>Minimal code sample to reproduce the error
import scanpy as sc
import numpy as np
def custom_distance(x1, x2):
return dist_mat[int(x1), int(x2)]
n = 4096
# generate a fake distance matrix for n elements
dist_mat = np.random.rand(n, n)
# make it symmetrical
dist_mat= np.tril(dist_mat) + np.tril(dist_mat, -1).T
# zeros on the diagonal
for i in range(len(dist_mat)):
dist_mat[i][i] = 0.
xd = sc.AnnData(shape=(n, 1))
xd.obs_names = [i for i in range(n)]
xd.X = np.empty((xd.n_obs, xd.n_vars))
for i in range(xd.n_obs):
xd.X[i, 0] = i
print("computing connectivity graph...")
rng = np.random.RandomState(0)
sc.pp.neighbors(xd,
n_neighbors=10,
n_pcs=None,
use_rep='X',
random_state=rng,
metric=custom_distance)
print("Success")Versions
Details
anndata 0.7.8
scanpy 1.8.2
sinfo 0.3.4
PIL 8.4.0
beta_ufunc NA
binom_ufunc NA
cffi 1.15.0
cycler 0.10.0
cython_runtime NA
dateutil 2.8.2
defusedxml 0.7.1
h5py 3.6.0
igraph 0.9.8
joblib 1.1.0
kiwisolver 1.3.2
leidenalg 0.8.8
llvmlite 0.38.0
louvain 0.7.1
matplotlib 3.5.1
mpl_toolkits NA
natsort 8.0.2
nbinom_ufunc NA
numba 0.55.1
numexpr 2.8.1
numpy 1.21.5
packaging 21.3
pandas 1.3.5
pkg_resources NA
pyexpat NA
pyparsing 3.0.6
pytz 2021.3
scipy 1.7.3
sitecustomize NA
six 1.16.0
sklearn 1.0.1
tables 3.6.1
texttable 1.6.4
threadpoolctl 3.0.0
typing_extensions NA
wcwidth 0.2.5
Python 3.9.9 (main, Nov 16 2021, 03:08:02) [GCC 9.3.0]
Linux-5.4.0-99-generic-x86_64-with-glibc2.31
8 logical CPU cores, x86_64
Metadata
Metadata
Assignees
Labels
Needs info❔More information needed
Activity
ivirshup commentedon Feb 23, 2022
Thanks for the report
Could you also provide an example of this working when you use UMAP directly? It looks like what you're doing is providing a precomputed distance matrix, right?
polklin commentedon Feb 25, 2022
Hello @ivirshup,
yes the goal would be to use a precomputed distance matrix exactly.
umapallows such behavior by specifyingmetric='precomputed'as an argument (see lmcinnes/umap#348).A minimal code, using
umap:ivirshup commentedon Feb 25, 2022
It should definitely be easier to use a precomputed distance matrix. I believe this has been talked about before, but not implemented. As a workaround, you should be able to get something working like this:
polklin commentedon Feb 28, 2022
thanks for your reply @ivirshup !
ivirshup commentedon Feb 28, 2022
No worries.
I'm going to close this issue as I've opened a more focused one (referenced above)
AlSzmigiel commentedon Jan 19, 2024
how to do it with a precomputed adjacency matrix and 'gauss' method for computing connectivities?