scanpy.metrics.morans_i#

scanpy.metrics.morans_i(adata_or_graph, /, vals=None, *, use_graph=None, neighbors_key=None, use=None, layer=None, obsm=None, obsp=None, use_raw=False)[source]#
scanpy.metrics.morans_i(graph, /, vals)
scanpy.metrics.morans_i(graph, /, vals)

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=N∑i,jwi,jzizjS0∑izi2
Parameters:
adata_or_graph AnnData | csr_matrix | csr_array

AnnData object containing a graph (see use) or the graph itself. See the examples for more info.

vals NDArray | spmatrix | sparray | Array | DataFrame | Series | GraphAcc | LayerAcc | MultiAcc | AdRef[Idx2D | int, AnnData] | str | 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,). If adata_or_graph is an AnnData, this can be an accessor, e.g. A.obsm['X_pca'] or the 1D A.obs['n_genes'] (strs are resolve()d, e.g. 'obsm.X_pca'), by default A.X.

layer str | None (default: None)

Deprecated since version 1.13.0: Use vals=A.... instead.

A key of layers, i.e. layer='k' is vals=A.layers['k'].

obsm str | None (default: None)

Deprecated since version 1.13.0: Use vals=A.... instead.

A key of obsm, i.e. obsm='k' is vals=A.obsm['k'].

obsp str | None (default: None)

Deprecated since version 1.13.0: Use vals=A.... instead.

A key of obsp, i.e. obsp='k' is vals=A.obsp['k'].

use_raw bool (default: False)

Deprecated since version 1.13.0: raw is deprecated. Keep the data in a layer instead.

Whether to use adata.raw.X for vals.

use_graph str | None (default: None)

Deprecated since version 1.13.0: Use use=A.obsp[...] instead.

Key of the graph in obsp, i.e. use_graph='k' is use=A.obsp['k'].

neighbors_key str | None (default: None)

Deprecated since version 1.13.0: Use use=A.obsp[...] instead.

Where to look for neighbors connectivities. If not specified, this retrieves .obsp['connectivities'] for connectivities (default storage place for neighbors()). If specified, this retrieves .obsp[.uns[neighbors_key]['connectivities_key']] for connectivities.

use GraphAcc | str | None (default: None)

The neighborhood graph (connectivities) to use, e.g. A.obsp['k'] after sc.pp.neighbors(adata, out=A.obsp['k']), or any other graph. strs are resolve()d, e.g. 'obsp.k'. If None, the one neighbors() wrote by default.

Return type:

ndarray | float

Returns:

If vals is two dimensional, returns a 1 dimensional ndarray array. Returns a scalar if vals is 1d.

Examples

Calculate Moran’s I for each components of a dimensionality reduction:

import scanpy as sc, numpy as np
from anndata.acc import A

pbmc = sc.datasets.pbmc68k_processed()
pc_c = sc.metrics.morans_i(pbmc, A.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)