scvelo.pp.neighbors(adata, n_neighbors=30, n_pcs=None, use_rep=None, use_highly_variable=True, knn=True, random_state=0, method='umap', metric='euclidean', metric_kwds=None, num_threads=- 1, copy=False)

Compute a neighborhood graph of observations.

The neighbor graph methods (umap, hnsw, sklearn) only differ in runtime and yield the same result as Scanpy [Wolf et al., 2018]. Connectivities are computed with adaptive kernel width as proposed in Haghverdi et al. 2016 (doi:10.1038/nmeth.3971).

adata : AnnData

Annotated data matrix.

n_neighbors : int

The size of local neighborhood (in terms of number of neighboring data points) used for manifold approximation. Larger values result in more global views of the manifold, while smaller values result in more local data being preserved. In general values should be in the range 2 to 100. If knn is True, number of nearest neighbors to be searched. If knn is False, a Gaussian kernel width is set to the distance of the n_neighbors neighbor.

n_pcs : int or None (default: None)

Number of principal components to use. If not specified, the full space is used of a pre-computed PCA, or 30 components are used when PCA is computed internally.

use_rep : None, ‘X’ or any key for .obsm (default: None)

Use the indicated representation. If None, the representation is chosen automatically: for .n_vars < 50, .X is used, otherwise ‘X_pca’ is used.

use_highly_variable : bool (default: True)

Whether to use highly variable genes only, stored in .var[‘highly_variable’].

knn : bool

If True, use a hard threshold to restrict the number of neighbors to n_neighbors, that is, consider a knn graph. Otherwise, use a Gaussian Kernel to assign low weights to neighbors more distant than the n_neighbors nearest neighbor.

random_state : int

A numpy random seed.

method : {{‘umap’, ‘hnsw’, ‘sklearn’}} (default: ‘umap’)

Method to compute neighbors, only differs in runtime. The ‘hnsw’ method is most efficient and requires to pip install hnswlib. Connectivities are computed with adaptive kernel.

metric : str

A known metric’s name or a callable that returns a distance.

metric_kwds : Dict, None

Options for the metric.

num_threads : int

Number of threads to be used (for runtime).

copy : bool

Return a copy instead of writing to adata.


  • connectivities (.obsp) – Sparse weighted adjacency matrix of the neighborhood graph of data points. Weights should be interpreted as connectivities.

  • distances (.obsp) – Sparse matrix of distances for each pair of neighbors.