scanpy.metrics.modularity

Contents

scanpy.metrics.modularity#

scanpy.metrics.modularity(adata_or_connectivities, /, labels='leiden', *, neighbors_key=None, is_directed=None, mode='calculate')[source]#

Compute the modularity of a graph given its connectivities and labels.

Parameters:
adata_or_connectivities AnnData | AnyArrayLike | spmatrix | sparray

The AnnData object containing the data or a weighted adjacency matrix representing the graph.

labels str | AnyArrayLike (default: 'leiden')

Cluster labels for each node in the graph. When AnnData is provided, this can be the key in adata.obs that contains the clustering labels and defaults to "leiden".

neighbors_key str | None (default: None)

When AnnData is provided, the key in adata.obsp that contains the connectivities.

is_directed bool | None (default: None)

Whether the connectivities are directed or undirected. Always False if AnnData is provided, as connectivities are derived from (symmetric) neighbors.

mode Literal['calculate', 'update', 'retrieve'] (default: 'calculate')

When AnnData is provided, this controls if the stored modularity is retrieved, or if we should calculate it (and optionally update it in adata.uns[labels]).

Return type:

float

Returns:

The modularity of the graph based on the provided clustering.