scanpy.metrics.gearys_c#

scanpy.metrics.gearys_c(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.gearys_c(graph, /, vals)
scanpy.metrics.gearys_c(graph, /, vals)

Calculate Geary’s C.

Specifically as used by VISION.

Geary’s C is a measure of autocorrelation for some measure on a graph. This can be to whether measures are correlated between neighboring cells. Lower values indicate greater correlation.

C=(N−1)∑i,jwi,j(xi−xj)22W∑i(xi−x¯)2
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 Geary’s C 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 Geary’s C 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.gearys_c(pbmc, A.obsm["X_pca"])

It’s equivalent to call the function directly on the underlying arrays:

alt = sc.metrics.gearys_c(pbmc.obsp["connectivities"], pbmc.obsm["X_pca"].T)
np.testing.assert_array_equal(pc_c, alt)