pyHisto: calibrated stain vectors for whole-slide histology¶
pyHisto measures the colours of your stains from your own slides, so that colour deconvolution, and every number computed from it (such as the collagen percentage of a Picrosirius Red section), rests on vectors you can check. It is not tied to one stain: PSR with or without Fast Green, Masson's trichrome, H&E, H-DAB and others are built-in staining protocols.
import pyHisto as histo
slide = histo.open_slide("slide.ome.zarr")
regions = histo.stain.propose_regions([slide], n_per_slide=12, size_px=512)
cal = histo.stain.calibrate([slide], regions)
cal.stains now holds the two stain vectors and the Residual for this slide
(or for the whole dataset, when you pass several slides), and cal.variance
tells you how much they varied between the sampled regions. Pass
reference=histo.stain.reference("<protocol>") to name them after your stains,
e.g. "psr-fg" (PSR and FG, the default of histo calibrate), "psr",
"masson-trichrome", "he" or "h-dab"; histo.stain.protocols() lists them
all (staining protocols). The reference only
names and orders the two vectors: their values always come from your slides.
Why calibrate?¶
Colour deconvolution splits each RGB pixel into the amounts of each stain. To do that it needs the stain vectors: the colour of each "pure" stain (for example PSR and Fast Green, or hematoxylin and eosin) in optical density. Until now they were measured by hand in QuPath, from one cropped region per dataset, and copied into a JSON file. That is
- fragile: one region may hold only one stain, and then the second vector is meaningless (in a synthetic test, QuPath's own estimate from a PSR-only region was 49° off the true Fast Green vector);
- unreproducible: nobody can redo the same crop;
- silent: wrong vectors still give a collagen %, just a wrong one.
pyHisto instead samples many regions inside the tissue of many slides, runs QuPath's own estimation algorithm on each one, combines them robustly and writes a visual QC report so that a person can look at the result before it is used. The CZI files are first converted to OME-Zarr by tanoa, a separate Oxford-Zeiss Centre of Excellence tool:
The files each step writes (calibration.json, stain_map.json, report.html,
QC sheets) are described in File formats.
The rule
Every new dataset gets its own calibration, sampled across several of its files, and a person looks at the QC sheets before the vectors are used.
Where do you want to start?¶
-
Install
pyHisto is installed and run with pixi.
-
Your first calibration
From a CZI file to a QC report in three commands.
-
Understand it
Slides, stain vectors, why many regions, and what "variance" means.
-
Use the API
import pyHisto as histo: every function, generated from the code.
Good to know¶
- Status: alpha. The stain calibration (
histo.open_slide,histo.stain,histo calibrate) is the stable core. The pipeline layer, vector export and HPC execution are planned. - Input is OME-Zarr only. Convert CZI (and other formats) with tanoa
first. The older CZI readers in
pyHisto.ioare legacy. - Stain variance as a measurement. Calibration reports how much the stain colour varies across the tissue, for you to interpret. See Stain variance is a measurement.
- Related projects: tanoa converts CZI and other microscopy formats into the OME-Zarr stores that pyHisto reads.