scanpy.metrics.morans_i#
- scanpy.metrics.morans_i(adata_or_graph, /, vals=None, *, use_graph=None, layer=None, obsm=None, obsp=None, use_raw=False)[source]#
- Calculate Moran’s I Global Autocorrelation Statistic. - Moran’s I is a global autocorrelation statistic for some measure on a graph. It is commonly used in spatial data analysis to assess autocorrelation on a 2D grid. It is closely related to Geary’s C, but not identical. More info can be found here. \[I = \frac{ N \sum_{i, j} w_{i, j} z_{i} z_{j} }{ S_{0} \sum_{i} z_{i}^{2} }\]- Parameters:
- adata_or_graph AnnData|csr_matrix|csr_array
- AnnData object containing a graph (see - use_graph) or the graph itself. See the examples for more info.
- vals ndarray[tuple[int,...],dtype[TypeVar(_ScalarType_co, bound=generic, covariant=True)]] |spmatrix|sparray|Array|DataFrame|Series|None(default:None)
- Values to calculate Moran’s I for. If this is two dimensional, should be of shape - (n_features, n_cells). Otherwise should be of shape- (n_cells,). This matrix can be selected from elements of the anndata object by using key word arguments:- layer,- obsm,- obsp, or- use_raw.
- use_graph str|None(default:None)
- Key to use for graph in anndata object. If not provided, default neighbors connectivities will be used instead. 
- layer str|None(default:None)
- Key for - adata.layersto choose- vals.
- obsm str|None(default:None)
- Key for - adata.obsmto choose- vals.
- obsp str|None(default:None)
- Key for - adata.obspto choose- vals.
- use_raw bool(default:False)
- Whether to use - adata.raw.Xfor- vals.
 
- adata_or_graph 
- Return type:
- Returns:
- If vals is two dimensional, returns a 1 dimensional ndarray array. Returns a scalar if - valsis 1d.
 - Examples - Calculate Moran’s I for each components of a dimensionality reduction: - import scanpy as sc, numpy as np pbmc = sc.datasets.pbmc68k_processed() pc_c = sc.metrics.morans_i(pbmc, obsm="X_pca") - It’s equivalent to call the function directly on the underlying arrays: - alt = sc.metrics.morans_i(pbmc.obsp["connectivities"], pbmc.obsm["X_pca"].T) np.testing.assert_array_equal(pc_c, alt)