Skip to content

spatialAggregate

spatialAggregate

spatialAggregate(
    data,
    xCoordinate="X_centroid",
    yCoordinate="Y_centroid",
    zCoordinate=None,
    purity=60,
    phenotype="phenotype",
    method="radius",
    radius=30,
    knn=10,
    imageId="imageid",
    subset=None,
    verbose=True,
    label="spatialAggregate",
    outputDir=None,
    maxWorkers=None,
    streamData=False,
    sdataTable=None,
)

Find local aggregates of a single cell type.

For every cell, look at its neighbourhood and take the most common phenotype there. When that phenotype accounts for at least purity percent of the neighbourhood, the cell is labelled with it; otherwise it is labelled 'non-significant'. The result is a map of where each cell type forms coherent patches rather than scattered individuals.

purity is the knob that matters: at 60 you get generous, contiguous regions; at 90 only dense cores survive.

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
purity int

Minimum percentage of the neighbourhood that the dominant phenotype must occupy for a cell to be labelled. Between 1 and 100.

60
phenotype str

Column in obs holding the cell type labels.

'phenotype'
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'
subset str

Process only this image.

None
label str

Column in obs the aggregate labels are written to.

'spatialAggregate'
maxWorkers int

Cap on parallel workers. Defaults to max(1, cpu_count() - 1). Lower it to cut peak memory.

None
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 obs[label] naming the dominant phenotype, or 'non-significant'. None in streaming mode.

Example
adata = sp.tl.spatialAggregate(
    adata, phenotype="phenotype", method="radius", radius=30, purity=60
)
sp.pl.spatialScatterPlot(adata, colorBy="spatialAggregate", s=3)

# Only dense cores.
adata = sp.tl.spatialAggregate(adata, purity=90, label="dense_aggregates")