scanpy.metrics.modularity#
- scanpy.metrics.modularity(adata_or_connectivities, /, labels='leiden', *, use=None, is_directed=None, mode='calculate', neighbors_key=None)[source]#
Compute the modularity of a graph given its connectivities and labels.
- Overloads:
connectivities (
AnyArrayLike|spmatrix|sparray), /, labels (AnyArrayLike), *, is_directed (bool) →floatadata (
AnnData), /, labels (str|AnyArrayLike) ='leiden', *, use (GraphAcc|str|None) =None, mode (Literal['calculate','update','retrieve']) ='calculate', neighbors_key (str|None) =None→float
- 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
AnnDatais provided, this can be the key inadata.obsthat contains the clustering labels and defaults to"leiden".- use
GraphAcc|str|None(default:None) The neighborhood graph (connectivities) to use, e.g.
A.obsp['k']aftersc.pp.neighbors(adata, out=A.obsp['k']), or any other graph.strs areresolve()d, e.g.'obsp.k'. IfNone, the oneneighbors()wrote by default. Only used whenAnnDatais provided.- is_directed
bool|None(default:None) Whether the connectivities are directed or undirected. Always
FalseifAnnDatais provided, as connectivities are derived from (symmetric) neighbors.- mode
Literal['calculate','update','retrieve'] (default:'calculate') When
AnnDatais provided, this controls if the stored modularity is retrieved, or if we should calculate it (and optionally update it inadata.uns[labels]).- neighbors_key
str|None(default:None) Deprecated since version 1.13.0: Use
useinstead, e.g.use=A.obsp['k_connectivities']forneighbors_key='k'.When
AnnDatais provided, the key inadata.unsnaming the connectivities.
- adata_or_connectivities
- Returns:
The modularity of the graph based on the provided clustering.