Skip to content

neighExp

neighExp

neighExp(
    data,
    xCoordinate="X_centroid",
    yCoordinate="Y_centroid",
    zCoordinate=None,
    method="radius",
    radius=30,
    knn=10,
    imageId="imageid",
    layer="raw",
    log=True,
    subset=None,
    label="neighExp",
    verbose=True,
    outputDir=None,
    streamData=False,
    sdataTable=None,
)

Neighbourhood-weighted expression (spatial lag) for every cell.

For each cell, average the expression of its neighbours, producing a cell x marker matrix in which each value describes the cell's surroundings. Clustering it with cluster(mode="spatial") gives Recurrent Cellular Neighbourhoods defined by expression rather than by cell type.

The neighbour weights are assembled directly as row/column/value arrays and multiplied through a sparse matrix, so the lag is one csr_matrix product rather than a loop over cells.

Parameters:

Name Type Description Default
data (AnnData | SpatialData | str, required)

The cell table. An AnnData, a path to an .h5ad file, a SpatialData object, or a path to a .zarr SpatialData store.

required
xCoordinate str

Column in obs holding x positions.

'X_centroid'
yCoordinate str

Column in obs holding y positions.

'Y_centroid'
zCoordinate str

Column in obs holding z positions. Leave as None for 2D data; set it and the neighbourhood search becomes three-dimensional.

None
method str

How neighbourhoods are defined. 'radius' takes every cell within radius pixels; 'knn' takes the knn nearest cells regardless of distance.

'radius'
radius float

Neighbourhood radius in the units of the coordinate columns, normally pixels. Used when method='radius'.

30
knn int

Number of nearest neighbours. Used when method='knn'.

10
imageId str

Column in obs holding image identifiers. Neighbourhoods never cross images.

'imageid'
layer str

Which matrix to average. 'raw' uses .raw.X, None uses .X, any other string names a key in .layers.

'raw'
log bool

Apply log1p before averaging.

True
subset str

Process only this image.

None
label str

Key in uns the lagged matrix is written to.

'neighExp'
verbose bool

Print progress messages.

True
outputDir str

Directory to write the updated object to. With None the object is returned instead.

None
streamData bool

Run out of core against an .h5ad path with CAP-AnnData, reading only the sections this function needs and writing back only what it changes. Requires data to be a path; the file is updated in place and None is returned.

False
sdataTable str

Which SpatialData table to work on. Ignored for AnnData input, and optional when the store has exactly one table.

None

Returns:

Name Type Description
adata AnnData | SpatialData | None

The updated object with the cell x marker lagged matrix in uns[label]. None in streaming mode.

Example
adata = sp.tl.neighExp(adata, method="radius", radius=30, layer="raw")

# Cluster the environment rather than the cell.
adata = sp.tl.cluster(
    adata, mode="spatial", layer="neighExp", method="kmeans", k=8,
    label="expressionNeighbourhood",
)
sp.pl.spatialScatterPlot(adata, colorBy="expressionNeighbourhood", s=3)