squidpy.gr.calculate_niche_cellcharter
- squidpy.gr.calculate_niche_cellcharter(data, *, distance=3, aggregation='mean', rng=None, spatial_connectivities_key='spatial_connectivities', n_clusters=10, n_pca_components=None, n_jobs=None, use_rep=None, embedding_key_added='niche_embedding', key_added='cellcharter_niche', min_niche_size=None, cluster_mask=None, copy=False, table_key=None)[source]
Compute spatial niches using a CellCharter-style embedding and GMM [Varrone et al., 2023].
CellCharter recommends a dimensionality-reduced embedding such as scVI, passed as
use_rep; PCA ofadata.Xis the fallback when it is not given. The mixture model is scikit-learn’s, not CellCharter’s torchgmm, so partitions will not match theirs.This method identifies niches by clustering an embedding that represents each observation together with information from its surrounding spatial neighborhood. Unlike
calculate_niche_neighborhood(), which builds an embedding from categorical cell-type composition, this approach uses a continuous feature representation and a Gaussian mixture model (GMM) for clustering.A spatial connectivity graph must already be present in
adata.obsp[spatial_connectivities_key]. This function does not construct the graph itself.The method:
Takes a reduced representation of every observation:
adata.obsm[use_rep]if given, otherwise the firstn_pca_componentsprincipal components ofadata.X, restricted toadata.var["highly_variable"]when that column is present.Aggregates that representation over each disjoint hop ring of the spatial graph, from direct neighbors through
distancegraph hops, according toaggregation.Concatenates the observation’s own representation with every ring’s aggregate.
Fits a Gaussian mixture model with
n_clustersmixture components.Uses the GMM component assignments as niche labels.
Consequently, each niche corresponds to a probabilistic cluster in a feature space that encodes both an observation’s features and its broader spatial context. Increasing
distanceallows the embedding to reflect larger tissue-scale neighborhoods, whereas smaller values emphasize local microenvironments.- Parameters:
adata – Annotated data object.
distance (
int) – Maximum number of graph hops included when constructing the CellCharter-style spatial embedding.distance=1emphasizes direct neighbors; larger values incorporate progressively more distal observations in the spatial graph.aggregation (
str) –Aggregation statistic used to summarize features across spatial neighborhoods. Typical options include
"mean"and"variance"."mean"emphasizes the average local feature state, such as the average local expression program."variance"emphasizes local heterogeneity in the feature representation.rng (
int|integer|Sequence[int] |SeedSequence|Generator|BitGenerator|None) – Pseudorandom number generator state, following SPEC 7. When None, a newnumpy.random.Generatoris created using entropy from the operating system. Types other thannumpy.random.Generatorare passed tonumpy.random.default_rng()to instantiate a generator. Seeds the Gaussian mixture clustering step.spatial_connectivities_key (
str) – Key inanndata.AnnData.obspcontaining the spatial connectivity matrix.n_clusters (
int) – Number of Gaussian mixture components, and therefore the number of niche labels produced per library or dataset.n_pca_components (
int|None) – Number of principal components ofadata.Xaggregated over the hop rings.Noneuses 10, the latent width scVI defaults to, or one fewer than the smallest dimension ofadata.Xwhen that is narrower. Named for the PCA becauseGaussianMixturespells its cluster countn_componentstoo; here that isn_clusters. Rejected together withuse_rep, which supplies an already reduced embedding and so skips the PCA.n_jobs (
int|None) – Number of parallel threads to use. None and-1use numba’s default thread count (NUMBA_NUM_THREADS); asking for more threads than that warns and falls back to it, while0and values below-1raise, since scanpy only supportsn_jobs >= -1.use_rep (
str|None) – Key inanndata.AnnData.obsmholding a reduced representation, such as an scVI latent, or'X'forX. Aggregated over the hop rings in place of the PCA ofadata.X.cluster_mask (
Series|None) – Booleanpandas.Seriesindexed likeanndata.AnnData.obs.Falseobservations are labeled'not_a_niche'and take no part in the clustering, though they still reach their neighbors through the graph. Observations it omits are kept. To drop them from the graph as well, subset and rebuild it withspatial_neighbors().key_added (
str) – Name of theanndata.AnnData.obscolumn the labels are written to.embedding_key_added (
str) – Controls the name of the key to be added inanndata.AnnData.obsm, which will hold the niche embedding computed.min_niche_size (
int|None) – Minimum number of observations required for a niche. Niches with fewer observations are relabeled'not_a_niche'.copy (
bool) – IfTrue, return the result, otherwise save it to theadataobject.table_key (
str|None) – Key inspatialdata.SpatialData.tableswhere the table is stored. Required whenadatais aspatialdata.SpatialDataobject and ignored otherwise.
- Return type:
- Returns:
If
copy=True, returns a copy ofadatawith the embedding stored in.obsm[embedding_key_added]and GMM-based niche assignments added to.obs. Otherwise, modifiesadatain place and returnsNone.