dynamo.tl.neighbors(adata, X_data=None, genes=None, basis='pca', layer=None, n_pca_components=30, n_neighbors=30, method=None, metric='euclidean', metric_kwads=None, cores=1, seed=19491001, result_prefix='', **kwargs)[source]

Search nearest neighbors of the adata object.

  • adata (AnnData) – An AnnData object.

  • X_data (Optional[ndarray]) – The user supplied data that will be used for nearest neighbor search directly. Defaults to None.

  • genes (Optional[List[str]]) – The list of genes that will be used to subset the data for nearest neighbor search. If None, all genes will be used. Defaults to None.

  • basis (str) – The space that will be used for nearest neighbor search. Valid names includes, for example, pca, umap, velocity_pca or X (that is, you can use velocity for clustering), etc. Defaults to “pca”.

  • layer (Optional[str]) – The layer to be used for nearest neighbor search. Defaults to None.

  • n_pca_components (int) – Number of PCA components. Applicable only if you will use pca basis for nearest neighbor search. Defaults to 30.

  • n_neighbors (int) – Number of nearest neighbors. Defaults to 30.

  • method (Optional[str]) – The method used for nearest neighbor search. If umap or pynn, it relies on pynndescent package’s NNDescent for fast nearest neighbor search. Defaults to None.

  • metric (Union[str, Callable]) – The distance metric to use for the tree. The default metric is euclidean, and with p=2 is equivalent to the standard Euclidean metric. See the documentation of DistanceMetric for a list of available metrics. If metric is “precomputed”, X is assumed to be a distance matrix and must be square during fit. X may be a sparse graph, in which case only “nonzero” elements may be considered neighbors. Defaults to “euclidean”.

  • metric_kwads (Optional[Dict[str, Any]]) – Additional keyword arguments for the metric function. Defaults to None.

  • cores (int) – The number of parallel jobs to run for neighbors search. None means 1 unless in a joblib.parallel_backend context. -1 means using all processors. Defaults to 1.

  • seed (int) – Random seed to ensure the reproducibility of each run. Defaults to 19491001.

  • result_prefix (str) – The key that will be used as the prefix of the connectivity, distance and neighbor keys in the returning adata. Defaults to “”.

  • kwargs – Additional arguments that will be passed to each nearest neighbor search algorithm.


ImportErrormethod is invalid.

Return type:



An updated anndata object that are updated with the indices, connectivity, distance to the .obsp, as well as a new neighbors key in .uns.