snapatac2.pp.knn#

snapatac2.pp.knn(adata, n_neighbors=50, use_dims=None, use_rep='X_spectral', method='kdtree', inplace=True, random_state=0)[source]#

Compute a neighborhood graph of observations.

Computes a neighborhood graph of observations stored in adata using the method specified by method. The distance metric used is Euclidean.

Parameters:
  • adata (AnnData | AnnDataSet | ndarray) – Annotated data matrix or numpy array.

  • n_neighbors (int) – The number of nearest neighbors to be searched.

  • use_dims (Union[int, list[int], None]) – The dimensions used for computation.

  • use_rep (str) – The key for the matrix

  • method (Literal['kdtree', 'hora', 'pynndescent']) – Can be one of the following: - ‘kdtree’: use the kdtree algorithm to find the nearest neighbors. - ‘hora’: use the HNSW algorithm to find the approximate nearest neighbors. - ‘pynndescent’: use the pynndescent algorithm to find the approximate nearest neighbors.

  • inplace (bool) – Whether to store the result in the anndata object.

  • random_state (int) – Random seed for approximate nearest neighbor search. Note that this is only used when method='pynndescent'. Currently ‘hora’ does not support random seed, so the result of ‘hora’ is not reproducible.

Returns:

if inplace=True, store KNN in .obsp['distances']. Otherwise, return a sparse matrix.

Return type:

csr_matrix | None