ocelli.tl.neighbors_graph#
- ocelli.tl.neighbors_graph(adata: AnnData, x: str, n_edges: int = 10, out: str = 'graph', verbose: bool = False, copy: bool = False)#
Nearest neighbors-based graph construction
Constructs a nearest neighbors-based graph from a precomputed nearest neighbors search. Each graph node (cell) is connected to n_edges nearest neighbors in the embedding space.
Note
Before using this function, you must run ocelli.pp.neighbors to compute the nearest neighbors for the specified embedding.
- Parameters:
adata (anndata.AnnData) – The annotated data matrix.
x (str) – Key in adata.obsm corresponding to the embedding for which nearest neighbors were computed.
n_edges (int) – Number of edges (connections) per graph node. (default: 10)
out (str) – Key in adata.obsm where the constructed graph is saved. (default: ‘graph’)
verbose (bool) – Whether to print progress notifications. (default: False)
copy (bool) – Whether to return a copy of adata. If False, updates are made in-place. (default: False)
- Returns:
If copy=False: Updates adata with the constructed graph stored in adata.obsm[out].
If copy=True: Returns a modified copy of adata with the constructed graph.
- Return type:
anndata.AnnData or None
- Example:
import ocelli as oci from anndata import AnnData import numpy as np # Example data adata = AnnData(X=np.random.rand(100, 50)) adata.obsm['embedding'] = np.random.rand(100, 10) # Compute nearest neighbors oci.pp.neighbors(adata, x=['embedding'], n_neighbors=20) # Construct nearest neighbors-based graph oci.tl.neighbors_graph(adata, x='embedding', n_edges=10, verbose=True)