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 withadata.obsp["distances"]or a sparse graph matrix.Do NOT expect clusters smaller than
min_cluster_sizeto 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:
- 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:
- param n_iterations:
Number of Leiden optimization iterations. Use
-1to run until convergence.- type n_iterations:
- param random_state:
Seed for Leiden initialization.
- type random_state:
- param key_added:
Key in
adata.obsused to store cluster labels.- type key_added:
- param weighted:
If True, transform graph distances into edge weights before clustering.
- type weighted:
- param inplace:
If True, store labels in
adata.obs[key_added]; if False, return them.- type inplace:
- returns:
If
inplace=True, stores categorical labels inadata.obs[key_added]and returns None. Ifinplace=False, returns a string array of labels.- rtype:
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