scanpy.api.tl.leiden

scanpy.api.tl.leiden(adata, resolution=1, *, random_state=0, key_added='leiden', adjacency=None, directed=True, use_weights=True, n_iterations=-1, partition_type=None, copy=False, **partition_kwargs)

Cluster cells into subgroups [Traag18].

Cluster cells using the Leiden algorithm [Traag18], an improved version of the Louvain algorithm [Blondel08]. The Louvain algorithm has been proposed for single-cell analysis by [Levine15].

This requires having ran neighbors() or bbknn() first.

Parameters:
adata : AnnDataAnnData

The annotated data matrix.

resolution : floatfloat

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.

random_state : intint

Change the initialization of the optimization.

key_added : strstr

adata.obs key under which to add the cluster labels.

adjacency : spmatrix, NoneOptional[spmatrix]

Sparse adjacency matrix of the graph, defaults to adata.uns['neighbors']['connectivities'].

directed : boolbool

Whether to treat the graph as directed or undirected.

use_weights : boolbool

If True, edge weights from the graph are used in the computation (placing more emphasis on stronger edges).

n_iterations : intint

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.

partition_type : Type[MutableVertexPartition], NoneOptional[Type[MutableVertexPartition]]

Type of partition to use. Defaults to RBConfigurationVertexPartition. For the available options, consult the documentation for find_partition().

copy : boolbool

Whether to copy adata or modify it inplace.

**partition_kwargs

Any further arguments to pass to ~leidenalg.find_partition (which in turn passes arguments to the partition_type).

Return type:

AnnData, NoneOptional[AnnData]

Returns:

  • None – By default (copy=False), updates adata with the following fields:

    adata.obs[key_added] (pandas.Series, dtype category)

    Array of dim (number of samples) that stores the subgroup id ('0', '1', …) for each cell.

    adata.uns['leiden']['params']

    A dict with the values for the parameters resolution, random_state, and n_iterations.

  • AnnData – When copy=True is set, a copy of adata with those fields is returned.