scanpy.tl.louvain
- scanpy.tl.louvain(adata, resolution=None, random_state=0, restrict_to=None, key_added='louvain', adjacency=None, flavor='vtraag', directed=True, use_weights=False, partition_type=None, partition_kwargs=mappingproxy({}), neighbors_key=None, obsp=None, copy=False)
Cluster cells into subgroups [Blondel08] [Levine15] [Traag17].
Cluster cells using the Louvain algorithm [Blondel08] in the implementation of [Traag17]. The Louvain algorithm has been proposed for single-cell analysis by [Levine15].
This requires having ran
neighbors()
orbbknn()
first, or explicitly passing aadjacency
matrix.- Parameters
- adata :
AnnData
AnnData
The annotated data matrix.
- resolution :
float
|None
Optional
[float
] (default:None
) For the default flavor (
'vtraag'
), you can provide a resolution (higher resolution means finding more and smaller clusters), which defaults to 1.0. See “Time as a resolution parameter” in [Lambiotte09].- random_state :
None
|int
|RandomState
Union
[None
,int
,RandomState
] (default:0
) Change the initialization of the optimization.
- restrict_to :
Tuple
[str
,Sequence
[str
]] |None
Optional
[Tuple
[str
,Sequence
[str
]]] (default:None
) Restrict the clustering to the categories within the key for sample annotation, tuple needs to contain
(obs_key, list_of_categories)
.- key_added :
str
str
(default:'louvain'
) Key under which to add the cluster labels. (default:
'louvain'
)- adjacency :
spmatrix
|None
Optional
[spmatrix
] (default:None
) Sparse adjacency matrix of the graph, defaults to neighbors connectivities.
- flavor : {‘vtraag’, ‘igraph’, ‘rapids’}
Literal
[‘vtraag’, ‘igraph’, ‘rapids’] (default:'vtraag'
) Choose between to packages for computing the clustering.
'vtraag'
is much more powerful, and the default.- directed :
bool
bool
(default:True
) Interpret the
adjacency
matrix as directed graph?- use_weights :
bool
bool
(default:False
) Use weights from knn graph.
- partition_type :
Type
[MutableVertexPartition
] |None
Optional
[Type
[MutableVertexPartition
]] (default:None
) Type of partition to use. Only a valid argument if
flavor
is'vtraag'
.- partition_kwargs :
Mapping
Mapping
[str
,Any
] (default:mappingproxy({})
) Key word arguments to pass to partitioning, if
vtraag
method is being used.- neighbors_key :
str
|None
Optional
[str
] (default:None
) Use neighbors connectivities as adjacency. If not specified, louvain looks .obsp[‘connectivities’] for connectivities (default storage place for pp.neighbors). If specified, louvain looks .obsp[.uns[neighbors_key][‘connectivities_key’]] for connectivities.
- obsp :
str
|None
Optional
[str
] (default:None
) Use .obsp[obsp] as adjacency. You can’t specify both
obsp
andneighbors_key
at the same time.- copy :
bool
bool
(default:False
) Copy adata or modify it inplace.
- adata :
- Return type
- Returns
None
By default (
copy=False
), updatesadata
with the following fields:adata.obs['louvain']
(pandas.Series
, dtypecategory
)Array of dim (number of samples) that stores the subgroup id (
'0'
,'1'
, …) for each cell.
AnnData
When
copy=True
is set, a copy ofadata
with those fields is returned.