pyHisto.zarr_pipeline (legacy)¶
To be deprecated
See Legacy modules. Ingestion is via tanoa OME-Zarr and
histo.open_slide; the legacy CZI readers return BGR.
Chunk-driven decon + segmentation over an OME-Zarr pyramid produced by stream_czi_to_ome_zarr.
Design (see repo discussion): tissue masking and Otsu threshold estimation run on a low-resolution pyramid level (cheap, representative of the whole slide) rather than full resolution; color deconvolution and the actual per-pixel thresholding run chunk-by-chunk at whatever target resolution the caller picks (via pick_pyramid_level), so the full-resolution image is never materialized in memory -- mirroring the memory-safety fix already applied to CZI ingestion.
pick_pyramid_level
¶
Pick the pyramid level index whose scale factor is closest to target_scale.
Args:
level_scale_factors: sequence of per-level scale factors relative to
level 0 (e.g. [1, 2, 4, 8] for a 4-level, 2x pyramid).
target_scale: desired downsampling factor, matching decon.py's
--scaling semantics (1 = full res, 2 = half res, etc).
Returns: int level index.
compute_masked_multiotsu_threshold
¶
Compute a multi-level Otsu threshold from PSR-channel pixels that fall inside the tissue mask.
Intended to run on a low-resolution pyramid level (a cheap, whole-slide- representative sample) rather than full resolution, then be applied as a single fixed scalar threshold when thresholding full/target-resolution chunks -- this avoids ever needing a full-resolution masked-pixel array in memory just to pick a threshold, which was the memory-heavy step in the previous non-chunked seg.py implementation.
Args: psr_image: 2D array, PSR (deconvolved) channel. tissue_mask: 2D boolean/0-1 array, same shape as psr_image. classes: number of Otsu classes. class_id: which threshold to return (index into the sorted thresholds array).
Returns: float threshold value.
iter_chunk_regions
¶
Yield region tuples (one slice per axis) covering shape in blocks
aligned to chunk_shape's Y/X extent (the last two axes). Channel/Z
axes are always covered in full per region (this pipeline only ever
tiles over the spatial Y/X axes).
Args:
shape: full array shape, CZYX order (or any 4-tuple where the last
two axes are the spatial ones being tiled).
chunk_shape: per-axis chunk size, same axis order as shape.
Returns: generator of 4-tuples of slice objects.
crop_and_resize_mask_for_region
¶
Extract the portion of a low-resolution tissue mask corresponding to one target-resolution chunk region, and upsample just that crop -- never the whole mask at once. Upsampling the full low-res mask to a full-resolution target shape up front would reintroduce the same kind of whole-image memory blocker that streaming ingestion was built to avoid (e.g. a single-channel boolean mask at full WSI resolution can still be several GB by itself).
Args: coarse_mask: 2D array (H_coarse, W_coarse), the low-res tissue mask. target_shape: (H_target, W_target), the full spatial shape of the target-resolution level this region belongs to. region: 4-tuple of slices as produced by iter_chunk_regions (only the last two, Y/X, slices are used).
Returns: 2D uint8 array, same H/W as the region, values 0/1.
deconvolve_chunk
¶
Color-deconvolve one RGB/A image chunk and return a single stain channel.
Args:
rgb_chunk: HxWx3 array (uint8/uint16), one image chunk.
stain_matrix: 3x3 array, columns are stain vectors -- built the same
way as decon.py's W (np.array([stain_color_map[s] for s
in stains]).T).
stain_index: which stain column's deconvolved channel to return
(0 = first stain in stains, e.g. PSR by convention).
Returns: 2D array, same H/W as rgb_chunk, the deconvolved stain channel.
threshold_chunk
¶
Binarize one PSR chunk into a collagen mask, restricted to tissue.
Mirrors seg.py's segmentation rule: pixels strictly between 0 and
threshold (background/0 excluded) and inside the tissue mask are
collagen.
Args: psr_chunk: 2D array, deconvolved PSR channel for one chunk. tissue_mask_chunk: 2D boolean/0-1 array, same shape as psr_chunk. threshold: scalar threshold (from compute_masked_multiotsu_threshold).
Returns: 2D uint8 array, same shape as psr_chunk, 1 = collagen, 0 = not.