Skip to content

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.

A Masson's trichrome mouse kidney crop, then the same crop split into an aniline-blue channel (collagen and nuclei), a Biebrich-scarlet (cytoplasm) channel and a nearly empty Residual, and the collagen mask from the aniline-blue channel with the nuclei excluded
pyHisto measures each stain's colour from the slide itself and separates the stains: here aniline blue (collagen) and Biebrich scarlet (cytoplasm) on an open Masson's trichrome slide. Trichrome has a third dye, the nuclear hematoxylin; being blue-black, it lands in the aniline-blue channel rather than in the Residual. The last panel therefore removes the nuclei before counting collagen: nuclei = the darkest class of a 4-class multi-Otsu of the aniline-blue amount, dilated by 2 px; collagen = aniline-blue amount above the first multi-Otsu threshold, larger than the scarlet amount, and not a nucleus (20.0 % of the tissue in this crop; an illustration, not a validated collagen measurement). Image: OpenSlide test data, Zeiss-5 (Masson's trichrome, mouse kidney), Venklab, Pathology, UTHSCSA — CC0 1.0.
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 pipeline on one real slide, in two rows. Top: the CZI slide overview; a 3-D view of the OME-Zarr pyramid after tanoa, four levels with their 1024-pixel chunk grid in yellow and the corners of consecutive levels joined by straight lines; the overview with the 12 regions histo calibrate sampled. Bottom: a crop of the QC sheet with the overview and one region; a crop with its aniline-blue and scarlet channels; and the crop with the collagen mask in yellow
The same steps on one real slide: CZI scene, OME-Zarr pyramid written by tanoa (four levels; yellow = the 1024 px chunk grid of each level), 12 calibration regions (vector spread between regions: median 2.7° aniline blue, 3.1° Biebrich scarlet), a crop of the QC sheet, the stain separation of one crop and its collagen mask (aniline blue, nuclei excluded, as above). Image: OpenSlide test data, Zeiss-5 (Masson's trichrome, mouse kidney), Venklab, Pathology, UTHSCSA — CC0 1.0.

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.

    Install

  • Your first calibration


    From a CZI file to a QC report in three commands.

    Tutorial

  • Understand it


    Slides, stain vectors, why many regions, and what "variance" means.

    Concepts

  • Use the API


    import pyHisto as histo: every function, generated from the code.

    Reference

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.io are 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.