squidpy.gr.nhood_enrichment
- squidpy.gr.nhood_enrichment(adata, *, cluster_key, library_key=None, connectivity_key=None, n_perms=1000, rng=None, copy=False, n_jobs=None, show_progress_bar=True, normalization='none', min_cell_count=0, handle_nan='keep', table_key=None)[source]
Compute neighborhood enrichment by permutation test.
Changed in version 1.8.4: Every permutation now uses an independent
numpy.random.Generatorspawned from anumpy.random.SeedSequence. Consequently the permutation-based results no longer depend onn_jobs/backend, but results obtained with a given seed differ from those produced by squidpy < 1.8.4. See #1232 and #1233.Changed in version 1.8.4:
seed/random_statewere renamed torng, which also accepts anumpy.random.Generator(SPEC 7). The old names still work but emit aFutureWarning.- Parameters:
adata (
AnnData|SpatialData) – Annotated data object.table_key (
str|None) – Key inspatialdata.SpatialData.tableswhere the table is stored. Required whenadatais aspatialdata.SpatialDataobject and ignored otherwise.cluster_key (
str) – Key inanndata.AnnData.obswhere clustering is stored.library_key (
str|None) – If multiple library_id, column inanndata.AnnData.obswhich stores mapping betweenlibrary_idand obs.connectivity_key (
str|None) – Key inanndata.AnnData.obspwhere spatial connectivities are stored. Default is:anndata.AnnData.obsp['spatial_connectivities'].n_perms (
int) – Number of permutations for the permutation test.rng (
int|integer|Sequence[int] |SeedSequence|Generator|BitGenerator|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.copy (
bool) – IfTrue, return the result, otherwise save it to theadataobject.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.show_progress_bar (
bool) – Whether to show the progress bar or not.normalization (
str) –Normalization mode to use, as compared in [Schiller et al., 2025]:
'none': No normalization of neighbor counts.'total': Divide by the number of cells of the index cluster (SEA). Cluster sizes are unchanged by the permutation, so this rescales the statistic without changing the z-score.'conditional': Divide by the number of index-cluster cells having at least one neighbor of the given type (COZI).
min_cell_count (
int) – Minimum number of cells a cluster must contain to be included. Clusters with fewer cells are dropped before counting (default0keeps all clusters) and their z-scores are NaN whateverhandle_nansays. Worth raising: a cluster too small to give the permutation null a spread of values yields an unreliable z-score, and in'conditional'mode a rare cluster pair can leave the denominator at zero, which is reported as0.handle_nan (
Literal['keep','zero']) –How to handle NaN values in z-scores:
'zero': Replace NaN values with 0'keep': Keep NaN values (undefined enrichment)
- Return type:
- Returns:
If
copy = True, returns aNhoodEnrichmentResultwith the z-score and the enrichment count. If normalization = “conditional”, also contains the conditional ratio, otherwise it is None.Otherwise, modifies the
adatawith the following keys:anndata.AnnData.uns['{cluster_key}_nhood_enrichment']['zscore']- the enrichment z-score.anndata.AnnData.uns['{cluster_key}_nhood_enrichment']['count']- the enrichment count.anndata.AnnData.uns['{cluster_key}_nhood_enrichment']['conditional_ratio']- the ratio of cells of type A that neighbor type B.