使用 Python API¶
CLI 能做的一切,都可以在 Python 中以 import pyHisto as histo 完成。頂層提供
histo.open_slide、histo.Slide、histo.StainMatrix,以及子套件
histo.stain。
開啟切片¶
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 預設為 store 的資料夾名稱,用來在區域和結果中識別切片;兩個同名的
store 需要不同的 slide_id=。
thumbnail() 給出整張切片;read_region() 只回傳你要求的像素(橘色方框),而且是真實 RGB。小鼠肺臟,第 16 週。Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.染色矩陣¶
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
內建的染色方法可以直接以名稱取得參考矩陣, 不必自己輸入向量:
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") 與上面的 sm 是同一個矩陣。
從像素估計¶
rgb 是 (H, W, 3) 或 (N, 3) 的 uint8 真實 RGB。若未指定 background,
就使用像素各通道的眾數(與 QuPath GUI 的行為相同)。smooth=True 會先套用
QuPath 的 2 次 3×3 平均濾波。
estimate() 找到的東西:像素的光密度,以及包住它們的兩個染色向量(QuPath 風格的呈現,見染色向量與校準)。Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.取樣、檢視、校準¶
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 是檢視區域時交換用的格式(規劃中的檢視應用程式會編輯這個
檔案)。只會使用 enabled 的區域;結果中的索引(excluded、deviations、
per_region)指的是在 regions.enabled() 中的位置。
左:propose_regions() 在切片上選出的區域。右:cal.variance——每個區域的向量與校準結果相差多少。Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.
QC 影像¶
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
所有 QC 函式都回傳 uint8 RGB 的 NumPy 陣列;可用 Pillow 儲存,或用 matplotlib
顯示。qc.deconvolve 是對 3×3 矩陣單純以 NumPy 求反矩陣,
\(\mathbf{C} = \mathbf{OD}\,V^{-1}\),其中染色向量為 \(V\) 的各列
(細節),用於 QC,不適合處理整張切片。
qc.vector_plots 會把像素的光密度畫在紅–綠、紅–藍、綠–藍三個平面上並加上染色向量,
與 QuPath 的「Visual Stain Editor」相同(即上方的圖)。
qc.channel_separation():一個區域的 RGB | PSR | FG | Residual。qc.region_sheets() 與 qc.report() 會把總覽和所有區域放在同一頁(見解讀 QC 表)。Images: Dafni Gyftaki-Venieri and Nan Yang, Nanchahal Group, Kennedy Institute of Rheumatology, University of Oxford.儲存與重新載入¶
open("calibration.json", "w").write(cal.to_json())
stains = histo.StainMatrix.from_json(cal.stains.to_json())
完整的函式簽名:API 參考。

