sddb.loader.StainNormalize#

class sddb.loader.StainNormalize(method: Literal['macenko'] = 'macenko', *, reference: StainFit | None = None)#

Normalise H&E stains to one reference with squidpy's stain module.

fit() pools the tissue pixels of a cohort's items into one reference (fit_stain_reference()); reference overrides it. build() applies it lazily per block (transform(), uint8 out, background kept) and records it in item.stain_reference for DatasetContract.stain_reference.

Both run squidpy on the source image's coarsest level, which Rasterize keeps on the item, with the tissue mask TissueMask found on that level. fit() passes every item's level and mask in one SpatialData with scale="auto" (squidpy fits on the coarsest level and takes the mask scale closest to it, _stain/_normalize.py _resolve_fit_inputs); each item weighs in proportion to its tissue area there. The level and its shape per item are recorded as fit_inputs. build() passes a two-entry multiscale image (the written image, then the source level) and the matching tissue masks: transform normalises the finest entry and takes each image's own stain statistics from the coarsest, so nothing is downsampled from the written grid. The two entries are not one pyramid (the written grid may be rotated against the source); squidpy uses the coarse one only for statistics, paired with its own mask.

Parameters:
method

The squidpy method; "macenko".

reference

A fitted reference to apply instead of fitting one.

Raises:

ValueError -- In fit() and build(), on an IF item, an item without labels["tissue"], or (in build()) without a reference.

Attributes table#

FITTED

The params a fit adds to the record.

config

The constructor kwargs, with the reference as plain lists and, after fit(), what it read per item.

name

version

Methods table#

build(sdata)

Return the item with the normalised image (uint8, lazy).

fit(items)

Fit one reference on the pooled tissue pixels of items, keep it as reference and return it.

from_record(rec)

Rebuild the step, and its reference if the record has one.

requirements()

An H&E item with labels["tissue"].

to_record()

The step as an AdapterRecord that from_record() rebuilds.

validate(sdata)

Require an H&E item with a tissue mask.

validate_meta(meta)

No catalog metadata decides these steps; the checks run on the item in validate().

Attributes#

FITTED: ClassVar[tuple[str, ...]] = ('reference', 'fit_inputs')#

The params a fit adds to the record.

property config: dict[str, Any]#

The constructor kwargs, with the reference as plain lists and, after fit(), what it read per item.

name: ClassVar[str] = 'StainNormalize'#
version: ClassVar[str] = '0.3.0'#

Methods#

build(sdata: LoaderItem) → LoaderItem#

Return the item with the normalised image (uint8, lazy).

fit(items: Sequence[LoaderItem]) → StainFit#

Fit one reference on the pooled tissue pixels of items, keep it as reference and return it.

classmethod from_record(rec: AdapterRecord) → Self#

Rebuild the step, and its reference if the record has one.

requirements() → dict[str, Any]#

An H&E item with labels["tissue"].

to_record() → AdapterRecord#

The step as an AdapterRecord that from_record() rebuilds.

validate(sdata: LoaderItem) → list[str]#

Require an H&E item with a tissue mask.

validate_meta(meta: ObjectMeta) → list[str]#

No catalog metadata decides these steps; the checks run on the item in validate().