Calibrate a new dataset¶
The rule
Every new dataset gets its own calibration, with regions sampled from several of its files, and a person checks the QC sheets before the vectors are used for deconvolution. Stain vectors are never copied from another dataset, another staining batch or an older JSON file.
1. Convert every file¶
Convert the CZI files of the dataset to OME-Zarr with tanoa
(tanoa convert file.czi zarr/file.ome.zarr --series all writes one store per
scene) into one folder, for example zarr/.
2. Choose which files to sample¶
Pick files that span the dataset: different animals or patients,
different groups (e.g. control and treated), different staining days. With a
small dataset, use all of them. Sampling is per slide, so every slide you pass
gets --n-per-slide regions.
3. Run the calibration¶
from pathlib import Path
import pyHisto as histo
slides = [histo.open_slide(p) for p in sorted(Path("zarr").glob("*.ome.zarr"))]
reference = histo.stain.reference("psr-fg") # your staining protocol
regions = histo.stain.propose_regions(slides, n_per_slide=12, size_px=512,
strategy="stain", reference=reference)
cal = histo.stain.calibrate(slides, regions, reference=reference)
histo.stain.qc.report(cal, slides, "qc/calibration_2026-11-30")
Replace psr-fg by your staining protocol
(psr, masson-trichrome, he, h-dab, ...), or pass --reference FILE
for a stain that is not built in.
Every slide is opened lazily; only the sampled boxes and a coarse level per
slide (for the tissue mask, the stain map and the background) are read, so
this works over a network share too. A slide with very little glass makes pyHisto
read finer levels for the background (slower); if even level 0 has fewer
than --min-glass-px glass pixels, the run stops and names the slide: pass
its glass colour with --background R G B (measured in QuPath or an image
viewer on empty glass).
Sampling parameters¶
| Parameter | CLI | Default | Effect |
|---|---|---|---|
n_per_slide |
--n-per-slide |
10 | Boxes per slide. Fewer are returned (with a warning) if the tissue cannot hold them. |
size_px |
--size |
512 | Box side in pixels of the sampling level. |
level |
--level |
0 | Pyramid level of the boxes. Level 1 with the same size_px covers 4× the area. |
min_tissue_frac |
--min-tissue-frac |
0.8 | Minimum fraction of tissue in a box (tissue = any channel ≤ 0.9 × background). |
strategy |
--sampling |
"random" (Python) / stain (CLI) |
stain: the boxes where both stains are most abundant and mixed, on a coarse amount map, spread over the tissue (needs reference); random: stratified random. See Sampling. |
seed |
--seed |
0 | Same seed, same boxes (random only; stain is deterministic). |
min_glass_px |
--min-glass-px |
50,000 | Glass pixels needed to measure each slide's background. |
Lung and other sparse tissue
Lung is mostly air spaces. With 512 px boxes (88 µm at 0.17 µm/px) and 80 % tissue required, boxes can only land in dense areas (bronchi, vessels), which biases the sample. For lung, use larger regions and a lower tissue fraction, for example:
pixi run histo calibrate zarr/*.ome.zarr --n-per-slide 12 \
--size 1024 --min-tissue-frac 0.5 --out qc/lung_calibration
or sample at level 1 (--level 1 --size 512) for the same area with fewer
pixels.
4. Check the result¶
- Read the variance table in
report.html(what it means). - Look at every QC sheet (how): each stain where it belongs (for PSR + FG: PSR on collagen, FG on cytoplasm and muscle), Residual nearly empty.
- Look at the regions marked not used: they should be single-stain areas. If many are, choose larger regions so that each one contains both stains.
5. Keep the bundle¶
Keep the whole output folder with the analysis: calibration.json records
the vectors, the per-region results, the background of every slide, the
regions and all parameters, and the run is reproducible from it (same inputs
+ same seed give the same JSON, which has no timestamps). Use stain_map.json
as the stain map of the deconvolution step.