snapatac2.tl.leiden#

snapatac2.tl.leiden(adata, resolution=1, objective_function='modularity', min_cluster_size=5, n_iterations=-1, random_state=0, key_added='leiden', weighted=False, inplace=True)[source]#

Cluster cells with the Leiden community detection algorithm.

Use this function after building a nearest-neighbor graph with snapatac2.pp.knn, or pass a sparse adjacency matrix directly.

Anti-Patterns#

  • Do NOT pass raw embeddings as adata; pass an AnnData object with adata.obsp["distances"] or a sparse graph matrix.

  • Do NOT expect clusters smaller than min_cluster_size to keep their original labels; they are relabeled as "-1".

param adata:

Annotated data object containing adata.obsp["distances"], or a sparse graph adjacency matrix.

type adata:

AnnData | AnnDataSet | spmatrix

param resolution:

Clustering resolution. Larger values usually produce more clusters.

type resolution:

float

param objective_function:

Leiden objective function.

type objective_function:

Literal['CPM', 'modularity']

param min_cluster_size:

Minimum retained cluster size. Smaller clusters are labeled "-1".

type min_cluster_size:

int

param n_iterations:

Number of Leiden optimization iterations. Use -1 to run until convergence.

type n_iterations:

int

param random_state:

Seed for Leiden initialization.

type random_state:

int

param key_added:

Key in adata.obs used to store cluster labels.

type key_added:

str

param weighted:

If True, transform graph distances into edge weights before clustering.

type weighted:

bool

param inplace:

If True, store labels in adata.obs[key_added]; if False, return them.

type inplace:

bool

returns:

If inplace=True, stores categorical labels in adata.obs[key_added] and returns None. If inplace=False, returns a string array of labels.

rtype:

ndarray | None

Examples

>>> import snapatac2 as snap
>>> adata = snap.datasets.pbmc5k(type="annotated_h5ad")
>>> snap.pp.knn(adata, use_rep="X_spectral")
>>> snap.tl.leiden(adata, resolution=1.0)
>>> "leiden" in adata.obs
True