Skip to content

neighNMF

neighNMF

neighNMF(
    data,
    xCoordinate="X_centroid",
    yCoordinate="Y_centroid",
    zCoordinate=None,
    phenotype="phenotype",
    method="radius",
    radius=30,
    knn=10,
    imageId="imageid",
    subset=None,
    verbose=True,
    label="neighNMF",
    maxWorkers=None,
    outputDir=None,
    streamData=False,
    inferenceMode="phenotype_driven",
    layer=None,
    subsetMarker=None,
    nmfComponents=5,
    nmfRandomState=0,
    sdataTable=None,
)

Non-negative matrix factorisation of neighbourhood composition.

Builds the same cell x category neighbourhood matrix as sp.tl.neighCount, then factorises it into nmfComponents parts. Each cell gets a weight per component, and each component is a recognisable mixture of cell types — so a cell can sit partly in two neighbourhoods, which k-means cannot express.

Only the factorisation is kept: uns[label] holds the cell factors and uns[label + "_nmf_components"] the component definitions. The full neighbourhood matrix is discarded, which keeps the object small on big slides — run sp.tl.neighCount as well if you want it.

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

Key in uns the cell factors are written to. label + "_nmf_components" holds the components.

'neighNMF'
maxWorkers int

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

None
inferenceMode str

'phenotype_driven' builds the neighbourhood matrix from phenotype labels; 'marker_driven' builds it from marker expression, reading the matrix selected by layer and binarising it if needed.

'phenotype_driven'
layer str

Which matrix to read for inferenceMode='marker_driven'. None uses .X.

None
subsetMarker list

Restrict 'marker_driven' counting to these markers.

None
nmfComponents int

Number of components to factor into. The main thing to tune.

5
nmfRandomState int

Seed for the NMF initialisation.

0
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 uns[label] and uns[label + "_nmf_components"]. None in streaming mode.

Example
adata = sp.tl.neighNMF(
    adata, phenotype="phenotype", radius=30, nmfComponents=6
)

# What is each component made of?
adata.uns["neighNMF_nmf_components"]

# And where does each cell sit?
adata.uns["neighNMF"].head()