rapids_singlecell.tl.leiden#

rapids_singlecell.tl.leiden(adata, resolution=1.0, *, rng=None, theta=1.0, restrict_to=None, n_iterations=100, use_weights=True, dtype=<class 'numpy.float32'>, use_dask=False, use=None, out=False, key_added=None, adjacency=None, neighbors_key=None, obsp=None, copy=False)[source]#

Cluster cells into subgroups using the Leiden algorithm [TWvE19].

Performs Leiden clustering using cuGraph, an improved version of the Louvain algorithm [BGLL08].

Parameters:
adata AnnData

annData object

resolution float | list[float] (default: 1.0)

A parameter value or a list of parameter values controlling the coarseness of the clustering. (called gamma in the modularity formula). Higher values lead to more clusters. If a list of values is provided, the Leiden algorithm will be run for each value in the list.

rng int | integer | Sequence[int] | SeedSequence | Generator | BitGenerator | None (default: None)

Random seed or Generator changing the initialization of the optimization. Defaults to 0. The superseded random_state argument is still accepted.

theta float (default: 1.0)

Called theta in the Leiden algorithm, this is used to scale modularity gain in Leiden refinement phase, to compute the probability of joining a random leiden community.

restrict_to tuple[str, Sequence[str]] | None (default: None)

Restrict the clustering to the categories within the key for

sample annotation, tuple needs to contain (obs_key, list_of_categories).

n_iterations

This controls the maximum number of levels/iterations of the Leiden algorithm. When specified, the algorithm will terminate after no more than the specified number of iterations. No error occurs when the algorithm terminates early in this manner.

use_weights

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

dtype

Data type to use for the adjacency matrix.

use_dask

If True, use Dask to create the graph and cluster. This will use all GPUs available. This feature is experimental. For datasets with less than 10 Million cells, it is recommended to use use_dask=False.

use

The neighborhood graph (connectivities) to cluster, e.g. A.obsp['k']. Strings are resolved by anndata.acc.AdAcc.resolve(), e.g. 'obsp.k'. If None, the one neighbors() wrote by default.

out

Where to write the cluster labels, e.g. A.obs['clusters']; the parameters go to .uns under the same key. If True, the labels are returned instead (a dict by resolution for several resolutions). If False, they are written to .obs['leiden'] and .uns['leiden'] ('leiden_R' with restrict_to). Several resolutions are written to .obs[f'{key}_{resolution}'].

Return type:

AnnData | Categorical | dict[float, Categorical] | None