from __future__ import annotations
from collections.abc import Iterator, Mapping, Sequence
from typing import Any, Literal, cast
import dask.array as da
import numpy as np
import spatialdata as sd
import xarray as xr
from dask.base import is_dask_collection
from dask_image.ndinterp import affine_transform as da_affine
from skimage import feature, future, measure
from skimage.filters import gaussian, threshold_otsu
from skimage.morphology import closing, disk, remove_small_holes
from skimage.segmentation import felzenszwalb
from skimage.util import img_as_float
from sklearn.ensemble import RandomForestClassifier
from sklearn.linear_model import LogisticRegression
from spatialdata._logging import logger
from spatialdata.models import Labels2DModel
from spatialdata.transformations import get_transformation
from squidpy._params import resolve_params
from squidpy._utils import (
_ensure_dim_order,
_get_scale_factors,
_yx_from_shape,
legacy_random,
)
from squidpy._validators import normalize_choice, options_of
from squidpy.types import (
FelzenszwalbParams,
WekaParams,
)
from ._utils import flatten_channels, get_element_data
#: The segmentation methods :func:`detect_tissue` accepts.
DetectTissueMethod = Literal["otsu", "felzenszwalb", "weka"]
#: Background flag per corner: ``(top-left, top-right, bottom-left, bottom-right)``.
_Corners = tuple[bool, bool, bool, bool]
def _normalize_corners(corners_are_background: bool | Sequence[bool]) -> _Corners:
"""Broadcast a single flag to all four corners, or validate four of them (sequence or array)."""
if isinstance(corners_are_background, str): # `bool("False")` is True
raise TypeError("`corners_are_background` must be a bool or 4 bools, not a string.")
# any scalar or 0-d array is read by truthiness (as on chore/kwonly-args); a generator is not a scalar
if np.ndim(corners_are_background) == 0 and not isinstance(corners_are_background, Iterator):
return (bool(corners_are_background),) * 4
if np.shape(corners_are_background) != (4,):
raise ValueError(
"`corners_are_background` must be a bool or a sequence of 4 bools "
"(top-left, top-right, bottom-left, bottom-right)."
)
return cast(_Corners, tuple(bool(c) for c in corners_are_background))
#: The params type each method takes. OTSU is absent: it accepts none.
_METHOD_PARAMS: dict[DetectTissueMethod, type[FelzenszwalbParams | WekaParams]] = {
"felzenszwalb": FelzenszwalbParams,
"weka": WekaParams,
}
def _normalize_margins(
margins_px: int | Sequence[int],
shape: tuple[int, int],
) -> tuple[int, int, int, int]:
"""
Normalize border margins to a 4-tuple (top, bottom, left, right) of non-negative ints.
If the requested margins would eclipse the image, returns all zeros.
"""
if isinstance(margins_px, (str, bytes)):
raise TypeError("`border_margin_px` must be an int or a sequence of 4 ints, not a string.")
if isinstance(margins_px, Sequence):
margins_list = list(margins_px)
if len(margins_list) != 4:
raise ValueError("`border_margin_px` must be an int or a sequence of 4 ints (top, bottom, left, right).")
try:
t, b, l, r = (int(x) for x in margins_list)
except (TypeError, ValueError) as e: # pragma: no cover - defensive
raise TypeError("`border_margin_px` entries must be convertible to int.") from e
else:
t = b = l = r = int(margins_px)
if any(v < 0 for v in (t, b, l, r)):
raise ValueError("`border_margin_px` values must be non-negative.")
H, W = shape
if t + b >= H or l + r >= W:
return (0, 0, 0, 0)
return (t, b, l, r)
def _build_inner_mask(shape: tuple[int, int], margins: tuple[int, int, int, int]) -> np.ndarray:
"""
Mask that excludes a border defined by (top, bottom, left, right).
"""
H, W = shape
t, b, l, r = margins
mask = np.ones((H, W), dtype=bool)
if t > 0:
mask[:t, :] = False
if b > 0:
mask[-b:, :] = False
if l > 0:
mask[:, :l] = False
if r > 0:
mask[:, -r:] = False
return mask
def _apply_border_margin(mask: np.ndarray, margins: tuple[int, int, int, int]) -> np.ndarray:
"""
Zero-out a border defined by (top, bottom, left, right) around a boolean mask.
"""
if margins == (0, 0, 0, 0):
return mask
t, b, l, r = margins
out = np.array(mask, copy=True)
if t > 0:
out[:t, :] = False
if b > 0:
out[-b:, :] = False
if l > 0:
out[:, :l] = False
if r > 0:
out[:, -r:] = False
return out
def _rescale_margins(
margins: tuple[int, int, int, int],
from_shape: tuple[int, int],
to_shape: tuple[int, int],
) -> tuple[int, int, int, int]:
"""
Rescale margins defined for `from_shape` so they apply to `to_shape`.
"""
fy, fx = from_shape
ty, tx = to_shape
if fy == 0 or fx == 0:
return (0, 0, 0, 0)
sy = ty / float(fy)
sx = tx / float(fx)
t, b, l, r = margins
scaled = (
int(round(t * sy)),
int(round(b * sy)),
int(round(l * sx)),
int(round(r * sx)),
)
# Ensure margins do not eclipse the target shape
ty_eff = max(0, ty - (scaled[0] + scaled[1]))
tx_eff = max(0, tx - (scaled[2] + scaled[3]))
if ty_eff <= 0 or tx_eff <= 0:
return (0, 0, 0, 0)
return scaled
[docs]
def detect_tissue(
sdata: sd.SpatialData,
*,
image_key: str,
scale: str = "auto",
method: DetectTissueMethod = "otsu",
method_params: FelzenszwalbParams | WekaParams | Mapping[str, Any] | None = None,
channel_format: Literal["infer", "rgb", "rgba", "multichannel"] = "infer",
corners_are_background: bool | Sequence[bool] = True,
corner_size_pct: float = 0.01,
border_margin_px: int | Sequence[int] = 0,
min_specimen_area_frac: float = 0.01,
n_samples: int | None = None,
auto_max_pixels: int = 5_000_000,
close_holes_smaller_than_frac: float = 0.0001,
mask_smoothing_cycles: int = 0,
new_labels_key: str | None = None,
inplace: bool = True,
) -> np.ndarray | None:
"""
Detect tissue regions in an image and optionally store an integer-labeled mask.
Parameters
----------
sdata
SpatialData object containing the image.
image_key
Key of the image in ``sdata.images`` to detect tissue from.
scale
Scale level to use for processing. If `"auto"`, uses the smallest available scale.
Otherwise, must be a valid scale level present in the image.
method
Tissue detection method. Valid options are:
- `"otsu"` - Otsu thresholding with background detection.
- `"felzenszwalb"` - Felzenszwalb superpixel segmentation.
- `"weka"` - Trainable segmentation with corner background priors and RGB multiscale features.
method_params
Optional parameters specific to the selected method. For `"felzenszwalb"`, provide a
mapping of ``FelzenszwalbParams`` keys. For `"weka"`, provide a mapping of ``WekaParams``
keys. Passing values when ``method="otsu"`` is not supported.
channel_format
Expected format of image channels. Valid options are:
- `"infer"` - Automatically infer from image shape.
- `"rgb"` - RGB image.
- `"rgba"` - RGBA image.
- `"multichannel"` - Multi-channel image.
corners_are_background
Whether corners are considered background regions. Used for orienting threshold.
Pass four bools to set ``(top-left, top-right, bottom-left, bottom-right)`` separately.
If no corners are flagged True, orientation falls back to bright background.
corner_size_pct
Corner box size as a fraction of height/width.
border_margin_px
Ignore a border when seeding and predicting tissue. Can be:
- a single int applied to all sides, or
- a sequence of four ints ``(top, bottom, left, right)``.
Useful for masking out fiducial rings or slide edges. Applied consistently across all methods.
min_specimen_area_frac
Minimum fraction of image area for a region to be considered a specimen.
n_samples
Maximum number of specimen regions to keep. If `None`, uses Otsu thresholding
on log10(area) to separate specimens from artifacts.
auto_max_pixels
Maximum number of pixels to process automatically. Images larger than this
will be downscaled before processing.
close_holes_smaller_than_frac
Fraction of image area below which holes in the tissue mask are filled.
mask_smoothing_cycles
Number of morphological closing cycles to apply for boundary smoothing.
new_labels_key
Key to store the resulting labels in ``sdata.labels``. If `None`, uses
`"{image_key}_tissue"`.
inplace
If `True`, stores labels in ``sdata.labels``. If `False`, returns the mask array.
Returns
-------
If `inplace = False`, returns a NumPy array of shape `(y, x)` with integer labels
where `0` represents background and `1..K` represent different specimen regions.
Otherwise, returns `None` and stores the labels in ``sdata.labels``.
Notes
-----
The function produces an integer-labeled mask where:
- Label `0` represents background.
- Labels `1..K` represent different specimen regions.
Processing is performed at an appropriate resolution and then upscaled to match
the original image dimensions.
"""
method = normalize_choice(method, options_of(DetectTissueMethod), name="method")
logger.info(f"Detecting tissue with method: {method}")
corners = _normalize_corners(corners_are_background)
if not 0 < corner_size_pct <= 1:
raise ValueError(f"`corner_size_pct` must be in (0, 1], got {corner_size_pct}.")
if method == "weka" and not any(corners):
raise ValueError("WEKA tissue detection requires corner background priors; set corners_are_background=True.")
if method == "otsu":
if method_params is not None:
raise ValueError("`method_params` are not supported for OTSU tissue detection.")
resolved_method_params = None
else:
spec = _METHOD_PARAMS.get(method)
if spec is None:
raise ValueError(f"Unsupported method: {method}")
resolved_method_params = resolve_params(method_params, spec)
manual_scale = scale.lower() != "auto"
# Load smallest available or explicit scale
img_node = sdata.images[image_key]
img_da = get_element_data(img_node, scale if manual_scale else "auto", "image", image_key)
img_src = _ensure_dim_order(img_da, "yxc")
src_h = int(img_src.sizes["y"])
src_w = int(img_src.sizes["x"])
n_src_px = src_h * src_w
target_shape = _get_target_upscale_shape(sdata, image_key=image_key)
normalized_margins_target = _normalize_margins(border_margin_px, target_shape)
# Decide working resolution
need_downscale = (not manual_scale) and (n_src_px > auto_max_pixels)
# Channel flattening (greyscale) for threshold-based methods
img_grey = None
if method != "weka":
img_grey_da: xr.DataArray = flatten_channels(img=img_src, channel_format=channel_format)
if need_downscale:
logger.info("Downscaling for faster computation.")
img_grey = _downscale_with_dask(img_grey=img_grey_da, target_pixels=auto_max_pixels)
else:
img_grey = img_grey_da.values # may compute
# Prepare color image for WEKA (keeps channels)
if method == "weka":
if need_downscale:
logger.info("Downscaling for faster computation.")
img_weka = _downscale_with_dask_multichannel(img_rgb=img_src, target_pixels=auto_max_pixels)
else:
img_weka = np.asarray(_dask_compute(_ensure_dask(img_src)))
working_shape = img_weka.shape[:2]
else:
working_shape = img_grey.shape if img_grey is not None else (src_h, src_w)
normalized_margins = _rescale_margins(
normalized_margins_target,
from_shape=target_shape,
to_shape=working_shape,
)
# First-pass foreground
if method == "otsu":
img_fg_mask_bool = _segment_otsu(img_grey=img_grey, corners=corners, corner_size_pct=corner_size_pct)
img_fg_mask_bool = _apply_border_margin(img_fg_mask_bool, normalized_margins)
elif method == "weka":
wp = cast(WekaParams, resolved_method_params)
img_fg_mask_bool = _segment_weka(
img=img_weka,
corners=corners,
corner_size_pct=corner_size_pct,
weka_params=wp,
border_margins_px=normalized_margins,
)
else:
p = cast(FelzenszwalbParams, resolved_method_params)
labels_sp = _segment_felzenszwalb(img_grey=img_grey, params=p)
img_fg_mask_bool = _mask_from_labels_via_corners(
img_grey=img_grey, labels=labels_sp, corners=corners, corner_size_pct=corner_size_pct
)
img_fg_mask_bool = _apply_border_margin(img_fg_mask_bool, normalized_margins)
logger.info("Finished segmentation.")
# Solidify holes
if close_holes_smaller_than_frac > 0:
img_fg_mask_bool = _make_solid(img_fg_mask_bool, close_holes_smaller_than_frac)
# Keep specimen-sized components → integer labels
img_fg_labels = _filter_by_area(
mask=img_fg_mask_bool,
min_specimen_area_frac=min_specimen_area_frac,
n_samples=n_samples,
)
img_fg_labels = _smooth_mask(img_fg_labels, mask_smoothing_cycles)
img_fg_labels = _apply_border_margin(img_fg_labels, normalized_margins)
# Upscale to full resolution
scale_matrix = _get_scaling_matrix(img_fg_labels.shape, target_shape)
img_fg_labels_up = _affine_upscale_nearest(img_fg_labels, scale_matrix, target_shape)
if inplace:
lk = new_labels_key or f"{image_key}_tissue"
sf = _get_scale_factors(sdata.images[image_key])
sdata.labels[lk] = Labels2DModel.parse(
data=img_fg_labels_up,
dims=("y", "x"),
transformations=get_transformation(sdata.images[image_key], get_all=True),
scale_factors=sf,
)
logger.info(f"Saved tissue mask to `sdata.labels['{lk}']`.")
return None
# If dask-backed, return a NumPy array to honor the signature
if is_dask_collection(img_fg_labels_up):
return np.asarray(img_fg_labels_up.compute())
return np.asarray(img_fg_labels_up)
# Line 182 - convert dask array to numpy
def _affine_upscale_nearest(labels: np.ndarray, scale_matrix: np.ndarray, target_shape: tuple[int, int]) -> np.ndarray:
"""
Nearest-neighbor affine upscaling using dask-image. Returns dask array if available, else NumPy.
"""
logger.info("Upscaling mask back to original size.")
try:
lbl_da = da.from_array(labels, chunks="auto")
result = da_affine(
lbl_da,
matrix=scale_matrix,
offset=(0.0, 0.0),
output_shape=target_shape,
order=0,
mode="constant",
cval=0,
output=np.int32,
)
return np.asarray(result)
except (ImportError, AttributeError, TypeError):
sy = target_shape[0] / labels.shape[0]
sx = target_shape[1] / labels.shape[1]
yi = np.clip((np.arange(target_shape[0]) / sy).round().astype(int), 0, labels.shape[0] - 1)
xi = np.clip((np.arange(target_shape[1]) / sx).round().astype(int), 0, labels.shape[1] - 1)
return np.asarray(labels[yi[:, None], xi[None, :]].astype(np.int32, copy=False))
def _get_scaling_matrix(current_shape: tuple[int, int], target_shape: tuple[int, int]) -> np.ndarray:
"""
Affine matrix mapping output coords to input coords for scipy/dask-image.
"""
cy, cx = current_shape
ty, tx = target_shape
scale_y = cy / float(ty)
scale_x = cx / float(tx)
return np.array([[scale_y, 0.0], [0.0, scale_x]], dtype=float)
def _get_target_upscale_shape(sdata: sd.SpatialData, *, image_key: str) -> tuple[int, int]:
"""
Select the first multiscale level (assumed largest) or the single-scale shape.
"""
img = sdata.images[image_key]
# Image2DModel-like
if hasattr(img, "image"):
return _yx_from_shape(img.image.shape)
# Multiscale dict-like: first key is largest by convention
if hasattr(img, "keys"):
keys = list(img.keys())
target_scale = keys[0]
h, w = _yx_from_shape(img[target_scale].image.shape)
return (h, w)
# Raw array fallback
return _yx_from_shape(img.shape)
def _downscale_with_dask(img_grey: xr.DataArray, target_pixels: int) -> np.ndarray:
"""
Downscale (y, x) with xarray.coarsen(mean) until H*W <= target_pixels. Returns NumPy array.
"""
h, w = img_grey.shape
n = h * w
if n <= target_pixels:
return _dask_compute(_ensure_dask(img_grey))
scale = float(np.sqrt(target_pixels / float(n))) # 0 < scale < 1
target_h = max(1, int(h * scale))
target_w = max(1, int(w * scale))
fy = max(1, int(np.ceil(h / target_h)))
fx = max(1, int(np.ceil(w / target_w)))
da_small = _ensure_dask(img_grey).coarsen(y=fy, x=fx, boundary="trim").mean()
return np.asarray(_dask_compute(da_small))
def _downscale_with_dask_multichannel(img_rgb: xr.DataArray, target_pixels: int) -> np.ndarray:
"""
Downscale multichannel (y, x, c) with xarray.coarsen(mean) until H*W <= target_pixels. Returns NumPy array.
"""
h = int(img_rgb.sizes["y"]) if hasattr(img_rgb, "sizes") and "y" in img_rgb.sizes else img_rgb.shape[0]
w = int(img_rgb.sizes["x"]) if hasattr(img_rgb, "sizes") and "x" in img_rgb.sizes else img_rgb.shape[1]
n = h * w
if n <= target_pixels:
return np.asarray(_dask_compute(_ensure_dask(img_rgb)))
scale = float(np.sqrt(target_pixels / float(n))) # 0 < scale < 1
target_h = max(1, int(h * scale))
target_w = max(1, int(w * scale))
fy = max(1, int(np.ceil(h / target_h)))
fx = max(1, int(np.ceil(w / target_w)))
da_small = _ensure_dask(img_rgb).coarsen(y=fy, x=fx, boundary="trim").mean()
return np.asarray(_dask_compute(da_small))
def _ensure_dask(arr: xr.DataArray) -> xr.DataArray:
"""
Ensure DataArray is dask-backed. If not, chunk to reasonable tiles.
"""
try:
if hasattr(arr, "data") and isinstance(arr.data, da.Array):
return arr
return arr.chunk({"y": 2048, "x": 2048})
except (ImportError, AttributeError):
return arr
def _dask_compute(img_da: xr.DataArray) -> np.ndarray:
"""
Compute an xarray DataArray (possibly dask-backed) to a NumPy array with a ProgressBar if available.
"""
try:
from dask.diagnostics import ProgressBar
if hasattr(img_da, "data") and isinstance(img_da.data, da.Array):
with ProgressBar():
computed = img_da.data.compute()
return np.asarray(computed)
return np.asarray(img_da.values)
except (ImportError, AttributeError, TypeError):
return np.asarray(img_da.values)
def _segment_otsu(img_grey: np.ndarray, corners: _Corners, corner_size_pct: float) -> np.ndarray:
"""
Otsu binarization with orientation from background corners.
"""
img_f = img_as_float(img_grey)
t = threshold_otsu(img_f)
bright_bg = _background_is_bright(img_f, corners, corner_size_pct)
return np.array((img_f <= t) if bright_bg else (img_f >= t))
def _segment_felzenszwalb(img_grey: np.ndarray, params: FelzenszwalbParams) -> np.ndarray:
"""
Felzenszwalb superpixels with size-aware parameters.
"""
h, w = img_grey.shape
short = min(h, w)
sigma = float(np.clip(params["sigma_frac"] * short, 1.0, 5.0))
img_s = img_as_float(gaussian(img_grey, sigma=sigma))
target_regions = max(1, params["grid_rows"] * params["grid_cols"])
target_area = (h * w) / float(target_regions)
scale = float(max(1.0, params["scale_coef"] * target_area))
min_size = int(max(1, params["min_size_coef"] * target_area))
return np.array(
felzenszwalb(
img_s,
scale=scale,
sigma=sigma,
min_size=min_size,
channel_axis=None,
).astype(np.int32)
)
def _segment_weka(
img: np.ndarray,
corners: _Corners,
corner_size_pct: float,
weka_params: WekaParams,
border_margins_px: tuple[int, int, int, int] = (0, 0, 0, 0),
) -> np.ndarray:
"""
Trainable segmentation using multiscale features and a RandomForest classifier.
Logic:
- Use corners as reliable background seeds (label 1).
- Compute a distance-from-background "zmap" over the image.
- Mark the top `pseudo_tissue_percentile` of non-corner pixels as tissue seeds (label 2),
enforcing at least `pseudo_min_pixels` seeds.
- All remaining pixels stay 0 (unlabeled) and are ignored during training.
- Train a RandomForest on these seeds in multiscale feature space and predict tissue (class 2).
- Optionally refine the resulting mask with a second-stage classifier that
sees inside-mask pixels as candidate tissue and outside as background.
"""
img_f = img_as_float(img)
if img_f.ndim == 2:
channel_axis = None
img_mono = img_f
elif img_f.ndim == 3 and img_f.shape[2] <= 4:
channel_axis = -1
img_mono = img_f.mean(axis=2)
else: # pragma: no cover - defensive
raise ValueError("WEKA segmentation expects 2D or 3D (RGB/RGBA) input.")
H, W = img_mono.shape
inner_mask = _build_inner_mask((H, W), border_margins_px)
# Multiscale features (WEKA-like)
feats = feature.multiscale_basic_features(
img_f,
intensity=True,
edges=weka_params["edges"],
texture=True,
sigma_min=weka_params["sigma_min"],
sigma_max=weka_params["sigma_max"],
channel_axis=channel_axis,
)
# Label image: 0 = unlabeled, 1 = background, 2 = tissue
training_labels = np.zeros((H, W), dtype=np.uint8)
# Background seeds from corners
# never empty: detect_tissue requires a flagged corner and _corner_mask marks >= 1x1 per corner
corner_mask = _corner_mask((H, W), corners, corner_size_pct)
training_labels[corner_mask] = 1
if any(border_margins_px):
# Treat excluded border as background seeds to down-weight fiducial rings.
training_labels[~inner_mask] = 1
non_bg = (~corner_mask) & inner_mask
# Background prototype from corners
if img_f.ndim == 2:
# Grayscale: scalar mean/std, simple z-score magnitude
bg_mean = float(img_f[corner_mask].mean())
bg_std = float(img_f[corner_mask].std())
bg_std = max(bg_std, 1e-6)
zmap = np.abs((img_f - bg_mean) / bg_std)
else:
# Multichannel: per-channel z-score, then L2 norm across channels
bg_mean = img_f[corner_mask].mean(axis=0)
bg_std = img_f[corner_mask].std(axis=0)
bg_std = np.maximum(bg_std, 1e-6)
zmap = np.linalg.norm((img_f - bg_mean) / bg_std, axis=-1)
# Pseudo tissue seeds from most non-background-like pixels
if np.any(non_bg):
perc = float(np.clip(weka_params["pseudo_tissue_percentile"], 0.0, 100.0))
thr = np.percentile(zmap[non_bg], perc)
tissue_mask = (zmap >= thr) & non_bg
# Ensure minimum number of tissue seeds
if tissue_mask.sum() < weka_params["pseudo_min_pixels"]:
flat_non_bg = np.flatnonzero(non_bg)
if flat_non_bg.size > 0:
z_flat = zmap.ravel()[flat_non_bg]
order = np.argsort(z_flat)[::-1]
n_take = min(flat_non_bg.size, weka_params["pseudo_min_pixels"])
chosen = flat_non_bg[order[:n_take]]
seed_mask_flat = np.zeros_like(non_bg.ravel(), dtype=bool)
seed_mask_flat[chosen] = True
tissue_mask = seed_mask_flat.reshape(non_bg.shape)
training_labels[tissue_mask] = 2
# Ensure a tissue class exists for training (background always does: corner_mask is never empty)
if not (training_labels == 2).any():
# No tissue seeds found: pick the most different non-corner pixel if possible
flat_non_bg = np.flatnonzero(non_bg)
training_labels_flat = training_labels.ravel()
if flat_non_bg.size > 0:
z_flat = zmap.ravel()[flat_non_bg]
idx_rel = int(np.argmax(z_flat))
idx = int(flat_non_bg[idx_rel])
else:
# Full-corner image: arbitrary fallback (center pixel)
idx = int((H * W) // 2)
training_labels_flat[idx] = 2
training_labels = training_labels_flat.reshape(H, W)
# one generator for the whole segmentation, so the forest and the refinement step
# are seeded independently instead of sharing a single seed
rng = np.random.default_rng(weka_params["rng"])
clf = RandomForestClassifier(
n_estimators=weka_params["rf_estimators"],
n_jobs=-1,
max_depth=weka_params["rf_max_depth"],
max_samples=weka_params["rf_max_samples"],
random_state=legacy_random(rng),
)
clf = future.fit_segmenter(training_labels, feats, clf)
result = future.predict_segmenter(feats, clf)
prior_mask = np.asarray(result == 2) & inner_mask
# Optional second-stage refinement: inside-vs-outside mask classification
if weka_params["refine_with_classifier"]:
prior_mask = _refine_with_background_classifier(
feats=feats,
prior_mask=prior_mask,
n_samples_per_class=weka_params["refine_n_samples_per_class"],
bg_prob_threshold=weka_params["refine_bg_prob_threshold"],
rng=rng,
)
return prior_mask
def _refine_with_background_classifier(
feats: np.ndarray,
prior_mask: np.ndarray,
n_samples_per_class: int,
bg_prob_threshold: float,
rng: np.random.Generator,
) -> np.ndarray:
"""
Refine a prior tissue mask using a simple classifier on multiscale features.
We:
- treat pixels inside the prior mask as candidate tissue, outside as background,
- sample up to `n_samples_per_class` pixels from each class,
- train a logistic regression to discriminate background(1) vs tissue(0),
- compute p(background | x) for inside-mask pixels,
- turn to background those prior-mask pixels with high background probability.
Parameters
----------
feats
Feature array of shape (H, W, F) from `multiscale_basic_features`.
prior_mask
Boolean array (H, W) with the initial tissue mask.
n_samples_per_class
Maximum number of training samples to draw from each class.
bg_prob_threshold
Background probability threshold above which an inside-mask pixel
is reclassified as background.
rng
Generator used to subsample the training pixels.
Returns
-------
refined_mask : bool ndarray, same shape as prior_mask.
"""
logger.info("Refining mask with background classifier.")
H, W, F = feats.shape
prior_flat = prior_mask.ravel()
# Define classes based on prior mask
idx_tissue = np.flatnonzero(prior_flat)
idx_bg = np.flatnonzero(~prior_flat)
if idx_tissue.size == 0 or idx_bg.size == 0:
# Nothing to refine
return prior_mask
# Subsample for training
n_tissue = min(n_samples_per_class, idx_tissue.size)
n_bg = min(n_samples_per_class, idx_bg.size)
idx_tissue_sample = rng.choice(idx_tissue, size=n_tissue, replace=False)
idx_bg_sample = rng.choice(idx_bg, size=n_bg, replace=False)
feats_flat = feats.reshape(-1, F)
X_tissue = feats_flat[idx_tissue_sample]
X_bg = feats_flat[idx_bg_sample]
# y=1 for background, y=0 for tissue
X_train = np.vstack([X_bg, X_tissue])
y_train = np.concatenate(
[
np.ones(n_bg, dtype=np.int32),
np.zeros(n_tissue, dtype=np.int32),
]
)
# Standardize features for better optimizer behavior
mean = X_train.mean(axis=0)
std = X_train.std(axis=0)
std = np.maximum(std, 1e-6)
def _z(x: np.ndarray) -> np.ndarray:
return (x - mean) / std
X_train_z = _z(X_train)
# Simple logistic regression classifier
clf = LogisticRegression(
max_iter=10_000,
solver="lbfgs",
)
clf.fit(X_train_z, y_train)
# Predict p(background) only for inside-mask pixels
p_bg_tissue = clf.predict_proba(_z(feats_flat[idx_tissue]))[:, 1]
refined_flat = prior_flat.copy()
# Demote high-confidence background inside the prior mask
refined_flat[idx_tissue[p_bg_tissue >= bg_prob_threshold]] = False
return refined_flat.reshape(H, W)
def _mask_from_labels_via_corners(
img_grey: np.ndarray, labels: np.ndarray, corners: _Corners, corner_size_pct: float
) -> np.ndarray:
"""
Turn superpixels into a mask via Otsu on per-label mean intensity, oriented by corners.
"""
labels = labels.astype(np.int32, copy=False)
max_lab = int(labels.max())
if max_lab <= 0:
return np.zeros_like(img_grey, dtype=bool)
flat = labels.ravel()
imgf = img_as_float(img_grey).ravel()
counts = np.bincount(flat, minlength=max_lab + 1).astype(np.float64)
sums = np.bincount(flat, weights=imgf, minlength=max_lab + 1)
means = np.zeros(max_lab + 1, dtype=np.float64)
nz = counts > 0
means[nz] = sums[nz] / counts[nz]
valid = means[1:]
if valid.size > 1:
thr = threshold_otsu(valid)
elif valid.size == 1:
thr = float(valid[0]) - 1.0
else:
thr = 0.0
bright_bg = _background_is_bright(img_as_float(img_grey), corners, corner_size_pct)
keep = (means <= thr) if bright_bg else (means >= thr)
keep[0] = False
return np.array(keep[labels], dtype=bool)
def _corner_mask(shape: tuple[int, int], corners: _Corners, corner_size_pct: float) -> np.ndarray:
"""
Build a boolean mask for selected corners.
"""
H, W = shape
ch = max(1, int(corner_size_pct * H))
cw = max(1, int(corner_size_pct * W))
top_left, top_right, bottom_left, bottom_right = corners
mask = np.zeros((H, W), dtype=bool)
if top_left:
mask[:ch, :cw] = True
if top_right:
mask[:ch, -cw:] = True
if bottom_left:
mask[-ch:, :cw] = True
if bottom_right:
mask[-ch:, -cw:] = True
return mask
def _background_is_bright(img_grey: np.ndarray, corners: _Corners, corner_size_pct: float) -> bool:
"""
Decide if background is bright using flagged corners.
If none are flagged or mask ends up empty, return True.
"""
if not any(corners):
return True
corner_mask = _corner_mask(img_grey.shape, corners, corner_size_pct)
if not corner_mask.any():
return True
corner_mean = float(img_grey[corner_mask].mean())
global_median = float(np.median(img_grey))
return corner_mean >= global_median
def _make_solid(mask: np.ndarray, close_holes_smaller_than_frac: float = 0.01) -> np.ndarray:
"""
Fill holes smaller than the provided fraction of image area.
"""
if mask.dtype != bool:
mask = mask.astype(bool, copy=False)
max_hole_area = int(close_holes_smaller_than_frac * mask.size)
return np.array(remove_small_holes(mask, max_size=max_hole_area))
def _smooth_mask(mask: np.ndarray, cycles: int) -> np.ndarray:
"""
Apply morphological closing cycles to smooth boundaries, then relabel once.
"""
if cycles <= 0:
return mask.astype(np.int32, copy=False)
logger.info(f"Smoothing mask for {cycles} cycle(s).")
binary = mask > 0
H, W = mask.shape
r0 = max(1, min(5, min(H, W) // 100))
sm = binary
for i in range(cycles):
sm = closing(sm, disk(r0 + i))
return np.asarray(measure.label(sm, connectivity=2).astype(np.int32, copy=False))
def _filter_by_area(
mask: np.ndarray,
min_specimen_area_frac: float,
n_samples: int | None = None,
) -> np.ndarray:
"""
Keep specimen-sized connected components. Returns int32 labels.
If n_samples is given, keep top-n by area after min-area filtering.
Else, Otsu on log10(area) separates specimens from small artifacts.
"""
labels = measure.label(mask.astype(bool, copy=False), connectivity=2)
n = int(labels.max())
if n == 0:
return np.zeros_like(labels, dtype=np.int32)
areas = np.bincount(labels.ravel(), minlength=n + 1)[1:].astype(np.int64)
ids = np.arange(1, n + 1, dtype=np.int32)
H, W = labels.shape
min_area = max(1, int(min_specimen_area_frac * H * W))
big_enough = areas >= min_area
if not np.any(big_enough):
return np.zeros_like(labels, dtype=np.int32)
areas_big = areas[big_enough]
ids_big = ids[big_enough]
if n_samples is not None:
order = np.argsort(areas_big)[::-1]
keep = ids_big[order[:n_samples]]
out = np.zeros_like(labels, dtype=np.int32)
for new_id, old_id in enumerate(keep, 1):
out[labels == old_id] = new_id
return out
la = np.log10(areas_big + 1e-9)
thr = threshold_otsu(la) if la.size > 1 else la.min() - 1.0
keep_ids = ids_big[la > thr]
if keep_ids.size == 0:
return np.zeros_like(labels, dtype=np.int32)
out = np.zeros_like(labels, dtype=np.int32)
for new_id, old_id in enumerate(keep_ids, 1):
out[labels == old_id] = new_id
return out