StainFit.decompose

StainFit.decompose#

StainFit.decompose(sdata, *, image_key, scale='auto', image_key_added=None, inplace=True, output_dtype=<class 'numpy.float16'>, include_residual=True)[source]#

Decompose an image in sdata into separate per-stain concentration maps.

Requires a decomposition reference (method="macenko" or "vahadane"): its stain matrix and white point are projected onto the image as-is, so this reference is the provenance record of how the maps were produced.

Parameters:
sdata SpatialData

The SpatialData object and the RGB image key to decompose.

image_key str

The SpatialData object and the RGB image key to decompose.

scale str | Literal['auto'] (default: 'auto')

Scale level to decompose. "auto" uses the finest level.

image_key_added str | None (default: None)

Key prefix for the written images when inplace=True. If None, image_key is used, so each stain is written as its own single-channel image sdata.images[f"{image_key}_{stain}"] (e.g. f"{image_key}_hematoxylin"). Ignored when inplace=False.

inplace bool (default: True)

If True, write each stain as a separate single-channel image under the image_key_added prefix and return None; the write is atomic (all target keys are validated free before any is written). If False, leave sdata untouched and return the maps as a dict.

output_dtype DTypeLike (default: <class 'numpy.float16'>)

Dtype of the concentration maps. Defaults to float16 (half the storage; ~3 significant figures, adequate for concentrations); pass float32 for strict quantification.

include_residual bool (default: True)

If True, also produce the "residual" map. The residual is the absorbance along the complement direction - a diagnostic of decomposition quality (extra chromogen, artifacts, or a poor fit), not a biological stain. Set False to keep only hematoxylin/eosin.

Returns:

dict[str, DataArray] | None – None if inplace=True (the maps are written as separate images), otherwise a dict mapping each stain name to its (y, x) concentration DataArray ("hematoxylin", "eosin", and "residual" unless dropped).