Skip to content

pyHisto.stain.qc

Visual QC for stain calibration. Every function returns uint8 RGB NumPy arrays (or writes files, for report). Requires Pillow; imported lazily.

Visual QC for stain calibration (spec item 6).

  • :func:region_overlay -- slide thumbnail with the tissue / no-data outline and numbered region boxes (used / not-used regions marked when a calibration is given).
  • :func:channel_separation -- RGB | stain 1 | stain 2 | residual panels of one region, deconvolved with a pure-NumPy inverse of the 3x3 OD stain matrix.
  • :func:vector_plots -- QuPath-style stain-vector plots (pixel OD scatter in the R-G, R-B, G-B planes with the stain vectors); :func:calibration_pixels collects the pixels of the regions a calibration used.
  • :func:stain_density_map -- coarse per-pixel amount of stain 1 (the map stain-guided sampling scores boxes on); :func:stain_density_image draws it as a heat map with the slide's region boxes.
  • :func:report -- writes calibration.json, stain_map.json, the PNGs and one self-contained report.html (relative links) into out_dir.

Memory is bounded: only slide thumbnails and the sampled regions are read. NumPy + Pillow (imported lazily); matplotlib only inside :func:vector_plots; no histomicstk.

report

report(calibration, slides, out_dir) -> Path

Write the QC bundle for calibration into out_dir; returns the path of report.html.

Files: calibration.json (= calibration.to_json()), stain_map.json (legacy {"PSR":..,"FG":..,"Residual":..} of calibration.stains), overlay_<slide>.png and stain_density_<slide>.png (:func:stain_density_image with the reference vectors of the calibration, or its stains when it has none) per slide, separation_<NNN>_<slide>.png per enabled region (NNN = index in regions.enabled()), stain_vectors.png (:func:vector_plots of the used regions' pixels, each region's pixels taken against its own slide's background) and report.html.

region_sheets

region_sheets(slide, regions, calibration, *, cols: int = 2, rows: int = 4, width: int = SHEET_WIDTH_PX)

QC sheets for one slide on a uniform grid of square panels, 4*cols wide x 2+rows tall (default 8 x 6): the full slide overview (numbered region boxes, letterboxed) spans the top two panel rows; below, rows rows of cols regions, each a 1x4 strip RGB | stain 1 | stain 2 | residual under a caption bar coloured by status, row-major in regions.enabled() order. One sheet per cols*rows regions (>= 1).

Returns [(image, layout), ...]; layout = {"overview": {x,y,w,h}, "cells": [{"index", "x", "y", "w", "h"}, ...], "scale_bars": [...], "panel_px", "caption_px", "grid": {"cols", "rows"}} in sheet pixels.

region_overlay

region_overlay(slide, regions, calibration=None, *, max_px: int = THUMB_MAX_PX, white_balance: bool = True, scale_bar: bool = True) -> np.ndarray

Thumbnail of slide (long side <= max_px) with the tissue outline (green), no-data outline (magenta) and the slide's region boxes, labelled with their index in regions.enabled(). Colours: cyan used, light grey not used (needs calibration), dark grey disabled. Regions of other slides are ignored. Returns (h, w, 3) uint8.

channel_separation

channel_separation(rgb, stains: StainMatrix, background=(255, 255, 255), *, white_balance: bool = True, um_per_px=None) -> np.ndarray

Four equal-width panels side by side: RGB | stain 1 | stain 2 | residual.

Each stain panel is that stain's concentration re-rendered alone in its own colour (white = none). The RGB panel is white-corrected against background unless white_balance=False. Returns (H, 4*W, 3) uint8.

deconvolve

deconvolve(rgb, stains: StainMatrix, background=(255, 255, 255)) -> np.ndarray

Per-pixel stain concentrations (H, W, 3) by OD-space inversion.

OD = -log10(I / I0) with I clipped to >= 1; OD = C @ V (V: stain vectors as rows) => C = OD @ inv(V). Order of the last axis = stains.names.

white_correct

white_correct(rgb, background) -> np.ndarray

Display-only white correction: scale each channel so background (the slide's measured glass colour) maps to 255. No-data pixels (all 0) stay black. The same background is the I0 of the OD transform, so a white-balanced view is what the deconvolution "sees".

add_scale_bar

add_scale_bar(img, um_per_px, *, frac: float = 0.2, colour=(0, 0, 0))

Draw a scale bar (1-2-5 length, ~frac of the width) with its label in the bottom-right corner of img (H, W, 3) uint8, on a white backing so it reads on tissue and glass. um_per_px is for the image AS SHOWN. Returns (new_image, {"um", "px", "label", "x", "y"}).