rapids_singlecell.gr.calculate_niche#

rapids_singlecell.gr.calculate_niche(adata, *, table_key=None, flavor, groups=None, n_neighbors=15, resolutions=None, distance=None, n_hop_weights=None, abs_nhood=False, scale=True, min_niche_size=None, mask=None, library_key=None, aggregation='mean', n_components=10, use_rep=None, spatial_connectivities_key='spatial_connectivities', random_state=42, inplace=True, copy=None, **kwargs)[source]#

Compute spatial niches on the GPU.

Deprecated since version 0.17.0: calculate_niche is deprecated and will be removed in a future release, following squidpy. Use the flavor-specific functions instead:

Deprecated since version 0.17.0: Use calculate_niche_neighborhood, calculate_niche_utag, or calculate_niche_cellcharter instead.

The spatial graph in adata.obsp[spatial_connectivities_key] must be precomputed (e.g. via spatial_neighbors_knn()), except for flavor="cellcharter" when use_rep is provided.

Parameters:
adata AnnData | SpatialData

Annotated data matrix or a SpatialData object containing the selected table.

table_key str | None (default: None)

Key in SpatialData.tables; required for SpatialData input. All reads and writes use this table; ignored for AnnData input.

flavor Literal['neighborhood', 'utag', 'cellcharter']

  • "neighborhood" cluster cell-type frequency profiles among spatial neighbors [B+23].

  • "utag" cluster gene expression smoothed across spatial neighbors [KRM+22].

  • "cellcharter" shell-aggregate gene expression over n-hop neighborhoods, PCA-reduce, then cluster with a Gaussian mixture [VTSM+24].

groups str | None (default: None)

Column in adata.obs with cell-type labels. Required for flavor="neighborhood".

n_neighbors int (default: 15)

Neighbors for the post-aggregation kNN graph passed to leiden.

resolutions float | Sequence[float] | None (default: None)

Resolution(s) for leiden, defaulting to (0.5,). A label column is written for each value. Ignored for flavor="cellcharter".

distance int | None (default: None)

Number of n-hop neighborhoods to include. Defaults to 3 for cellcharter, 1 for neighborhood.

n_hop_weights Sequence[float] | None (default: None)

Per-hop weights when distance > 1 (flavor="neighborhood" only).

abs_nhood bool (default: False)

Use absolute neighbor counts instead of per-cell relative frequencies (flavor="neighborhood" only).

scale bool (default: True)

Z-score the neighborhood profile before clustering (flavor="neighborhood" only).

min_niche_size int | None (default: None)

Discard niches with fewer cells than this; relabel as "not_a_niche".

mask Series | None (default: None)

Boolean Series indexed like adata.obs. Cells that are False are labeled "not_a_niche".

library_key str | None (default: None)

Column in adata.obs identifying samples. If given, niches are computed per sample and labels are prefixed with lib=<id>_.

aggregation Literal['mean', 'variance'] (default: 'mean')

Per-shell aggregation for flavor="cellcharter". "mean" (default) or "variance".

n_components int (default: 10)

Number of mixture components for flavor="cellcharter".

use_rep str | None (default: None)

Key in adata.obsm to use as the embedding for flavor="cellcharter"; if provided, the first n_components columns are used and the shell-aggregation + PCA step is skipped.

spatial_connectivities_key str (default: 'spatial_connectivities')

Key in adata.obsp with the spatial connectivity matrix. Not used for flavor="cellcharter" when use_rep is provided.

random_state int (default: 42)

Random seed for the GMM (flavor="cellcharter" only).

inplace bool (default: True)

Write the niche columns to adata (the selected table for SpatialData). If False, return a modified AnnData copy of that table.

copy bool | None (default: None)

Deprecated alias for inplace; copy=True is inplace=False.

kwargs

Accepts the removed gmm_init argument, which is ignored with a warning.

Return type:

AnnData | None