squidpy.gr.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 AnnData as input and works with two layers; one latent space and one spatial layer. Adapted from https://github.com/HiDiHlabs/SpatialLeiden/.

Parameters:
  • adata – Annotated data object.

  • latent_connectivities_key (str) – Key in adata.obsp containing the latent-space connectivity matrix.

  • spatial_connectivities_key (str) – Key in anndata.AnnData.obsp containing 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) – Relative weight assigned to the latent and spatial layers.

  • n_iterations (int) – Number of optimization iterations used by SpatialLeiden.

  • use_weights (bool | tuple[bool, bool]) – Whether to use edge weights during clustering.

  • rng (int | integer | Sequence[int] | SeedSequence | Generator | BitGenerator | None) – Pseudorandom number generator state, following SPEC 7. When None, a new numpy.random.Generator is created using entropy from the operating system. Types other than numpy.random.Generator are passed to numpy.random.default_rng() to instantiate a generator. Each resolution is clustered with an independent rng derived from it.

  • min_niche_size (int | None) – Minimum number of observations required for a niche. Niches with fewer observations are relabeled 'not_a_niche'.

  • key_added (str) – Stem of the anndata.AnnData.obs columns the labels are written to, one per resolution, named f"{key_added}_res={resolution}".

  • copy (bool) – If True, return the result, otherwise save it to the adata object.

  • table_key (str | None) – Key in spatialdata.SpatialData.tables where the table is stored. Required when adata is a spatialdata.SpatialData object and ignored otherwise.

Return type:

AnnData | None

Returns:

If copy = True, returns a copy of adata with niche annotations added to .obs. Otherwise, modifies adata in place and returns None.