calculate_niche_spatialleiden#
- squidpy.gr.calculate_niche_spatialleiden(data, *, resolutions, latent_connectivities_key='connectivities', spatial_connectivities_key='spatial_connectivities', layer_ratio=1.0, n_iterations=-1, use_weights=True, rng=None, key_added='spatialleiden', min_niche_size=None, copy=False, table_key=None)[source]#
Compute niche assignments using the SpatialLeiden algorithm.
This is a wrapper around SpatialLeiden [Müller-Bötticher et al., 2025], which takes
AnnDataas input and works with two layers; one latent space and one spatial layer. Adapted from HiDiHlabs/SpatialLeiden.- Parameters:
- adata
Annotated data object.
- latent_connectivities_key
str(default:'connectivities') Key in
adata.obspcontaining the latent-space connectivity matrix.- spatial_connectivities_key
str(default:'spatial_connectivities') Key in
anndata.AnnData.obspcontaining the spatial connectivity matrix.- resolutions
float|tuple[float,float] |list[float|tuple[float,float]] Resolution parameter(s) for the Leiden optimization. Can be a single float or a list of floats.
- layer_ratio
float(default:1.0) Relative weight assigned to the latent and spatial layers.
- n_iterations
int(default:-1) Number of optimization iterations used by SpatialLeiden.
- use_weights
bool|tuple[bool,bool] (default:True) Whether to use edge weights during clustering.
- rng
int|integer|Sequence[int] |SeedSequence|Generator|BitGenerator|None(default:None) Pseudorandom number generator state, following SPEC 7. When None, a new
numpy.random.Generatoris created using entropy from the operating system. Types other thannumpy.random.Generatorare passed tonumpy.random.default_rng()to instantiate a generator. Each resolution is clustered with an independent rng derived from it.- min_niche_size
int|None(default:None) Minimum number of observations required for a niche. Niches with fewer observations are relabeled
'not_a_niche'.- key_added
str(default:'spatialleiden') Stem of the
anndata.AnnData.obscolumns the labels are written to, one per resolution, namedf"{key_added}_res={resolution}".- copy
bool(default:False) If
True, return the result, otherwise save it to theadataobject.- table_key
str|None(default:None) Key in
spatialdata.SpatialData.tableswhere the table is stored. Required whenadatais aspatialdata.SpatialDataobject and ignored otherwise.
- Returns:
AnnData|None– Ifcopy = True, returns a copy ofadatawith niche annotations added to.obs. Otherwise, modifiesadatain place and returnsNone.