fit_stain_reference#
- squidpy.experimental.im.fit_stain_reference(sdata, *, image_key, method='macenko', scale='auto', method_params=None, white_point=None, tissue_mask_key=None, max_angle_deg=45.0, canonical_reference=None)[source]#
Fit a stain reference from an image in a
SpatialDataobject.- Parameters:
- sdata
SpatialData SpatialData object containing the image.
- image_key
str|list[str] Key of the RGB image in
sdata.imagesto fit on, or a list of keys to fit one reference from the pooled tissue pixels of several images (e.g. a representative cohort). Pooled images must share a dtype. The pool is a pool of pixels: each image contributes in proportion to its tissue pixel count at the resolvedscale, not equally, so images whose pyramids bottom out at different sizes carry different weight.- method
Literal['macenko','vahadane','reinhard'] (default:'macenko') Fitting method:
"macenko"or"vahadane"(physical stain-matrix decomposition, usable by bothtransform()anddecompose()), or"reinhard"(faster statistical colour transfer, no stain separation). Macenko is the default because its one documented weakness - artifact pixels contaminating the fit - is removed by the mandatory tissue mask.- scale
str|Literal['auto'] (default:'auto') Scale level to fit on.
"auto"uses the coarsest level, which is cheap and sufficient for colour statistics.- method_params
ReinhardParams|MacenkoParams|VahadaneParams|Mapping[str,Any] |None(default:None) A mapping of
ReinhardParams/MacenkoParams/VahadaneParamskeys, orNonefor defaults. Must matchmethod.- white_point
ndarray|None(default:None) Per-channel white point
I_0(3,)for the decomposition methods. IfNone, a fixed full-white[255, 255, 255]is used (the HistomicsTK/Macenko convention), so unstained pixels round-trip to white. Passestimate_white_point()only for slides with a known non-white background. Ignored by Reinhard.- tissue_mask_key
str|list[str] |None(default:None) Key of a tissue-label element in
sdata.labels(as produced bydetect_tissue()) restricting the fit to tissue pixels. IfNone,f"{image_key}_tissue"is used. A tissue mask is required: if neither exists, aKeyErrorasks you to rundetect_tissue()first. Whenimage_keyis a list, pass a list of mask keys order-matched to it (orNonefor the{key}_tissueconvention per image).- max_angle_deg
float(default:45.0) Tolerance of the H/E sanity gate for the decomposition methods: the fit raises
StainFittingErrorif either recovered stain vector deviates more than this many degrees from its canonical reference. Default45. Ignored by Reinhard.- canonical_reference
Mapping[str,ndarray] |None(default:None) Canonical H/E reference for the decomposition methods, a mapping with
"hematoxylin"and"eosin"keys to(3,)RGB optical-density unit vectors. Drives both the H/E column ordering and the deviation gate. IfNone, the Ruifrok H&E vectors are used. Ignored by Reinhard.
- sdata
- Returns:
StainFit– The fittedStainFit. Nothing is written tosdata.