ocelli.pp.neighbors

Contents

ocelli.pp.neighbors#

ocelli.pp.neighbors(adata: AnnData, x: list | None = None, n_neighbors: int = 20, method: str = 'sklearn', n_jobs: int = -1, verbose: bool = False, copy: bool = False)#

Nearest neighbors search

Computes exact or approximate nearest neighbors using sklearn or nmslib libraries. The sklearn method calculates exact neighbors, while nmslib is faster and approximates neighbors for large datasets.

Parameters:
  • adata (anndata.AnnData) – The annotated data matrix.

  • x (list or None) – A list of keys in adata.obsm specifying embeddings to calculate neighbors for. If None, keys are loaded from adata.uns[“modalities”]. (default: None)

  • n_neighbors (int) – Number of nearest neighbors to compute. (default: 20)

  • method (str) – Method to compute nearest neighbors. Valid options are sklearn (exact) and nmslib (approximate). (default: ‘sklearn’)

  • n_jobs (int) – Number of parallel jobs to use. If -1, all CPUs are used. (default: -1)

  • verbose (bool) – Whether to print progress notifications. (default: False)

  • copy (bool) – Whether to return a copy of the AnnData object. If False, the input object is updated in-place. (default: False)

Returns:

By default (copy=False), updates adata with nearest neighbor indices and distances stored in adata.obsm. If copy=True, returns a copy of adata.

Return type:

anndata.AnnData or None

Example:
import ocelli as oci
from anndata import AnnData
import numpy as np

# Example AnnData object
adata = AnnData(X=np.random.random((100, 50)))
adata.obsm['modality1'] = np.random.random((100, 10))

# Compute neighbors
oci.pp.neighbors(adata, x=['modality1'], n_neighbors=15, method='nmslib', n_jobs=4)