Your first calibration¶
In this tutorial you take one Axioscan CZI file of a PSR + Fast Green section
(staining protocol psr-fg), convert it to OME-Zarr, calibrate its stain
vectors and read the QC report. It takes three commands. For another stain,
pick its staining protocol; the steps are
the same.
1. Convert the CZI to OME-Zarr with tanoa¶
pyHisto reads OME-Zarr only. tanoa converts the CZI and, importantly, records which stored channel is Red, Green and Blue:
By default tanoa converts the first scene (series 0). For a file with several
scenes use --series 1, --series largest, or --series all (one store per
scene, <stem>_s<i>.ome.zarr). In a batch, tanoa.to_ome_zarr() does the same
from Python.
Why not read the CZI directly?
Zeiss stores brightfield colour as Bgr24: blue, green, red. tanoa
writes the channel labels Blue, Green, Red into the OME-Zarr metadata
and pyHisto uses them to return true RGB. pyHisto's older CZI readers
returned BGR and treated it as RGB, so the stain vectors were applied to
swapped colours. See Slides and OME-Zarr.
2. Calibrate¶
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,
reference=histo.stain.reference("psr-fg"))
histo.stain.qc.report(cal, [slide], "qc/slide")
The reference only names and orders the two estimated stains (it is
QuPath's swap rule); psr-fg is the CLI's default protocol, so
--protocol psr-fg could be left out. For PSR without Fast Green use psr,
for Masson's trichrome masson-trichrome, for H&E he
(histo calibrate --list-protocols shows all).
pyHisto places 12 boxes of 512 × 512 pixels inside the tissue, estimates the stain vectors in each box with QuPath's algorithm and combines them. On a mouse lung scene (37298 × 35806 px at 0.1725 µm/px) the command prints:
stain vectors:
PSR: [0.4913, 0.7150, 0.4974]
FG: [0.9333, 0.1923, 0.3032]
Residual: [0.1822, 0.4741, -0.8614]
stain variance across regions (deg):
stain median mean sd p95 max n_used
PSR 6.39 6.39 3.71 12.72 12.84 12
FG 2.00 2.17 1.13 3.92 4.96 12
Residual 2.80 3.51 2.28 7.16 10.15 12
report: qc/slide/report.html
- stain vectors are the result: unit vectors in RGB optical density.
- stain variance says how far, in degrees, the vectors of the individual regions were from that result. Here the Fast Green colour was very consistent (median 2.0°), PSR less so (median 6.4°, two regions near 13°). This is a measurement, not a grade: see Stain variance is a measurement.
- If a region contained only one stain it is listed as not used (none here).
The run is easier to judge with two pictures of it, both written into the QC
folder or drawn from calibration.json:
overlay_<slide>.png, made by
histo.stain.qc.region_overlay): white-corrected thumbnail, tissue outline
in green, the scanner's no-data area in black, region boxes numbered as in the
QC sheet, 1 mm scale bar. PSR/FG-stained mouse lung, week 16.
Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.
deviations in calibration.json). FG regions sit tightly around 2°; PSR regions spread out to
13°. Same mouse lung as above.
Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.3. Look at the QC report¶
Open qc/slide/report.html in a browser. The folder contains:
| File | What it is |
|---|---|
report.html |
The page to read: variance table, overviews, region strips, vectors, parameters |
qc_sheet_<slide>_01.png, _02.png, ... |
One printable sheet per 8 regions (below) |
overlay_<slide>.png |
Slide thumbnail with tissue outline and numbered boxes |
separation_<NNN>_<slide>.png |
One full-resolution strip per region |
stain_vectors.png |
Pixel optical densities with the stain vectors, as in QuPath's Visual Stain Editor (how to read it) |
stain_map.json |
The vectors, in the format the deconvolution step reads |
calibration.json |
Everything: vectors, variance, per-region results, parameters, regions |
The QC sheet is the quickest check:
- Top: the slide, cropped to the tissue and spanning the full sheet width, with the tissue outline in green, the scanner's uncovered area (no-data) in black with a magenta outline, and the numbered region boxes. A 1 mm scale bar sits in the corner.
- Below: 4 rows × 2 regions. Each region is a strip of four squares,
RGB | PSR | FG | Residual, under a caption bar with its number,
usedornot used, its position, and how far its own PSR and FG vectors were from the result. - The RGB square is white-corrected: each channel is scaled so that the slide's background becomes pure white, \(I'_c = \min\!\big(255,\ \operatorname{round}(255 \cdot I_c / I_{0,c})\big)\) (no-data pixels stay black). This is what the deconvolution "sees"; it is for display only. Its scale bar shows the region size (here 10 µm).
- PSR, FG and Residual show each stain's amount re-drawn alone in its own colour: white means none. A stain amount \(c\) (clipped to \([0, 3]\)) with stain vector \(\mathbf{s}\) is drawn as \(255 \cdot 10^{-c\,s_k}\) in each channel \(k\).
What good looks like
Collagen fibres appear in the PSR square and not in the FG square; cytoplasm and muscle appear in FG and not in PSR; the Residual square is nearly empty. Strong structure in the Residual square means the two vectors do not explain the colours well.
Read Interpret the QC sheet for what to do when it does not look like this.
4. Use the vectors¶
stain_map.json has the same format as the stain-colour maps the collagen
pipeline already reads (here {"PSR": [r, g, b], "FG": [...], "Residual": [...]};
the keys are the protocol's stain names),
so it can replace a hand-measured QuPath file directly.
One slide is not a dataset
This tutorial used one scene. For real work, calibrate across several files of the dataset: see Calibrate a new dataset.