Use the Python API¶
Everything the CLI does is available from Python as import pyHisto as histo.
The top level gives you histo.open_slide, histo.Slide, histo.StainMatrix
and the subpackage histo.stain.
Open slides¶
import pyHisto as histo
slides = [histo.open_slide(p) for p in ["a.ome.zarr", "b.ome.zarr"]]
rgb = slides[0].read_region(level=2, x=1000, y=800, w=512, h=512) # (512, 512, 3) uint8 RGB
thumb = slides[0].thumbnail(1024)
slide_id defaults to the store's folder name and identifies the slide in
regions and results; two stores with the same name need distinct
slide_id=s.
thumbnail() gives the whole slide; read_region() returns only the pixels you ask for (orange box), as true RGB. Mouse lung, week 16. Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.Stain matrices¶
sm = histo.StainMatrix(["PSR", "FG"], [(0.20, 0.85, 0.48), (0.90, 0.25, 0.36)])
sm.names # ('PSR', 'FG', 'Residual') -- residual added as cross product
sm.vectors # (3, 3), one unit vector per row
sm.as_matrix() # (3, 3), vectors as columns, for deconvolution
sm.to_json() # '{"PSR": [...], "FG": [...], "Residual": [...]}'
histo.StainMatrix.from_json("stain_map.json")
histo.stain.angle_deg(sm.vectors[0], sm.vectors[1]) # angle in degrees
Built-in staining protocols give you a reference matrix by name instead of typing vectors:
histo.stain.protocols() # ['alcian-blue-h', 'azan-mallory', 'h-dab', 'he', ...]
ref = histo.stain.reference("masson-trichrome") # StainMatrix: 2 named stains + Residual
histo.stain.protocol_info("psr") # {name, title, stains, description, third_dye_note, source}
histo.stain.reference("psr-fg") is the same matrix as sm above.
Estimate from pixels¶
rgb is (H, W, 3) or (N, 3) uint8 true RGB. Without background, the
per-channel mode of the pixels is used (QuPath GUI behaviour). smooth=True
applies QuPath's 2× 3×3 mean filter first.
estimate() finds: the pixels' optical densities and the two stain vectors that bound them (QuPath-style view, see Stain vectors and calibration). Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.Sample, review, calibrate¶
regions = histo.stain.propose_regions(slides, n_per_slide=12, size_px=512, seed=0)
open("regions.json", "w").write(regions.to_json()) # edit by hand or in a viewer
regions = histo.stain.RegionSet.from_json("regions.json")
regions[3].enabled = False # drop a region
regions = histo.stain.RegionSet(list(regions) + [
histo.stain.Region("a.ome.zarr", level=0, x=20000, y=9000, w=1024, h=1024, source="user")])
cal = histo.stain.calibrate(slides, regions, reference=histo.stain.reference("psr-fg"))
cal.stains, cal.variance, cal.excluded, cal.deviations, cal.pooled
RegionSet JSON is the exchange format for reviewing regions (the planned
review app edits this file). Only enabled regions are used; indices in the
result (excluded, deviations, per_region) refer to positions in
regions.enabled().
Left: the regions from propose_regions() on the slide. Right: cal.variance — how far each region's vectors are from the calibrated ones. Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.
QC images¶
from pyHisto.stain import qc
overlay = qc.region_overlay(slides[0], regions, cal) # thumbnail + boxes
strip = qc.channel_separation(rgb, cal.stains, background=(212, 214, 212))
conc = qc.deconvolve(rgb, cal.stains, background=(212, 214, 212)) # (H, W, 3) amounts
sheets = qc.region_sheets(slides[0], regions, cal) # [(image, layout), ...]
px = qc.calibration_pixels(slides, cal) # (N, 3) pixels of the used regions
plot = qc.vector_plots(px, cal.stains, background=(212, 214, 212)) # QuPath-style OD scatter
qc.report(cal, slides, "qc/out") # the whole bundle
All QC functions return uint8 RGB NumPy arrays; save them with Pillow or show
them with matplotlib. qc.deconvolve is a plain NumPy inverse of the 3×3
matrix, \(\mathbf{C} = \mathbf{OD}\,V^{-1}\) with the stain vectors as the rows
of \(V\) (details), intended for QC, not for whole
slides. qc.vector_plots draws the pixels' optical densities in the Red–Green,
Red–Blue and Green–Blue planes with the stain vectors, like QuPath's Visual
Stain Editor (the figure above).
qc.channel_separation(): one region as RGB | PSR | FG | Residual. qc.region_sheets() and qc.report() put the overview and all regions on one page (see Interpret the QC sheet). Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.Save and reload¶
open("calibration.json", "w").write(cal.to_json())
stains = histo.StainMatrix.from_json(cal.stains.to_json())
Full signatures: API reference.

