spatial_autocorr#
- squidpy.gr.spatial_autocorr(adata, *, connectivity_key='spatial_connectivities', genes=None, mode='moran', transformation=True, n_perms=None, two_tailed=False, corr_method='fdr_bh', attr='X', layer=None, rng=None, use_raw=False, copy=False, n_jobs=None, backend='loky', show_progress_bar=True, table_key=None)[source]#
Calculate Global Autocorrelation Statistic (Moran’s I or Geary’s C).
See [Rey and Anselin, 2010] for reference.
Changed in version 1.8.2: The analytic (normality-assumption) variance for Geary’s C was corrected; previously the Moran’s I variance was reused for
mode = 'geary'. As a result,'var_norm'and'pval_norm'for Geary’s C differ from earlier versions. Permutation-based p-values ('pval_sim','pval_z_sim') are unaffected. See #1183.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(default:None) Key in
spatialdata.SpatialData.tableswhere the table is stored. Required whenadatais aspatialdata.SpatialDataobject and ignored otherwise.- connectivity_key
str(default:'spatial_connectivities') Key in
anndata.AnnData.obspwhere spatial connectivities are stored. Default is:anndata.AnnData.obsp['spatial_connectivities'].- genes
str|int|Sequence[str] |Sequence[int] |None(default:None) Depending on the
attr:if
attr = 'X', it corresponds to genes stored inanndata.AnnData.var_names. If None, it’s computedanndata.AnnData.var['highly_variable'], if present. Otherwise, it’s computed for all genes.if
attr = 'obs', it corresponds to a list of columns inanndata.AnnData.obs. If None, use all numerical columns.if
attr = 'obsm', it corresponds to indices inanndata.AnnData.obsm['{layer}']. If None, all indices are used.
- mode
Literal['moran','geary'] (default:'moran') Mode of score calculation:
’moran’ - Moran’s I autocorrelation.
’geary’ - Geary’s C autocorrelation.
- transformation
bool(default:True) If True, weights in
anndata.AnnData.obsp['spatial_connectivities']are row-normalized, advised for analytic p-value calculation.- n_perms
int|None(default:None) Number of permutations for the permutation test. If None, only p-values under normality assumption are computed.
- two_tailed
bool(default:False) If True, p-values are two-tailed, otherwise they are one-tailed.
- corr_method
str|None(default:'fdr_bh') Correction method for multiple testing. See
statsmodels.stats.multitest.multipletests()for valid options.- use_raw
bool(default:False) Whether to access
anndata.AnnData.raw. Only used whenattr = 'X'.- layer
str|None(default:None) Depending on
attr: Layer inanndata.AnnData.layersto use. If None, useanndata.AnnData.X.- attr
Literal['obs','X','obsm'] (default:'X') Which attribute of
AnnDatato access. Seegenesparameter for more information.- 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.- copy
bool(default:False) If
True, return the result, otherwise save it to theadataobject.- n_jobs
int|None(default:None) Number of parallel jobs to use. None is serial and
-1uses all available cores; asking for more cores than are available warns and falls back to all of them, while0and values below-1raise, since scanpy only supportsn_jobs >= -1. Forbackend="loky", the number of cores used by numba for each job spawned by the backend will be set to 1 in order to overcome the oversubscription issue in case you run numba in your function to parallelize. To set the absolute maximum number of threads in numba for your python program, set the environment variable:NUMBA_NUM_THREADSbefore running the program.- backend
str(default:'loky') Parallelization backend to use. See
joblib.Parallelfor available options.- show_progress_bar
bool(default:True) Whether to show the progress bar or not.
- adata
- Returns:
DataFrame|None– Ifcopy = True, returns apandas.DataFramewith the following keys:’I’ or ‘C’ - Moran’s I or Geary’s C statistic.
’pval_norm’ - p-value under normality assumption.
’var_norm’ - variance of ‘score’ under normality assumption.
’{p_val}_{corr_method}’ - the corrected p-values if
corr_method != None.
If
n_perms != None, additionally returns the following columns:’pval_z_sim’ - p-value based on standard normal approximation from permutations.
’pval_sim’ - p-value based on permutations.
’var_sim’ - variance of ‘score’ from permutations.
Otherwise, modifies the
adatawith the following key:anndata.AnnData.uns['moranI']- the above mentioned dataframe, ifmode = 'moran'.anndata.AnnData.uns['gearyC']- the above mentioned dataframe, ifmode = 'geary'.