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', use_leidenalg=False, weighted=False, inplace=True)[source]#

Cluster cells into subgroups [Traag18].

Cluster cells using the Leiden algorithm [Traag18], an improved version of the Louvain algorithm [Blondel08]. It has been proposed for single-cell analysis by [Levine15]. This requires having ran knn.

Parameters:
  • adata (AnnData | AnnDataSet | spmatrix) – The annotated data matrix or sparse adjacency matrix of the graph, defaults to neighbors connectivities.

  • resolution (float) – A parameter value controlling the coarseness of the clustering. Higher values lead to more clusters. Set to None if overriding partition_type to one that doesn’t accept a resolution_parameter.

  • objective_function (Literal['CPM', 'modularity', 'RBConfiguration']) – whether to use the Constant Potts Model (CPM) or modularity. Must be either “CPM”, “modularity” or “RBConfiguration”.

  • min_cluster_size (int) – The minimum size of clusters.

  • n_iterations (int) – How many iterations of the Leiden clustering algorithm to perform. Positive values above 2 define the total number of iterations to perform, -1 has the algorithm run until it reaches its optimal clustering.

  • random_state (int) – Change the initialization of the optimization.

  • key_added (str) – adata.obs key under which to add the cluster labels.

  • use_leidenalg (bool) – If True, leidenalg package is used. Otherwise, python-igraph is used.

  • weighted (bool) – Whether to use the edge weights in the graph

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

Returns:

If inplace=True, update adata.obs[key_added] to store an array of dim (number of samples) that stores the subgroup id ('0', '1', …) for each cell. Otherwise, returns the array directly.

Return type:

np.ndarray | None