|
| 1 | +from __future__ import annotations |
| 2 | + |
| 3 | +import numpy as np |
| 4 | + |
| 5 | +# --- Intensity metrics (grayscale input) --- |
| 6 | + |
| 7 | + |
| 8 | +def brightness_mean(block: np.ndarray) -> np.ndarray: |
| 9 | + """Mean pixel intensity of a grayscale tile.""" |
| 10 | + return np.array([[float(block.mean())]], dtype=np.float32) |
| 11 | + |
| 12 | + |
| 13 | +def brightness_std(block: np.ndarray) -> np.ndarray: |
| 14 | + """Standard deviation of pixel intensity of a grayscale tile.""" |
| 15 | + return np.array([[float(block.std())]], dtype=np.float32) |
| 16 | + |
| 17 | + |
| 18 | +def entropy(block: np.ndarray) -> np.ndarray: |
| 19 | + """Shannon entropy of pixel intensity histogram.""" |
| 20 | + arr = block.ravel() |
| 21 | + lo, hi = float(arr.min()), float(arr.max()) |
| 22 | + if hi - lo < 1e-10: |
| 23 | + return np.array([[0.0]], dtype=np.float32) |
| 24 | + # Quantize to 256 bins directly without storing intermediate normalized array |
| 25 | + bins = np.clip(((arr - lo) * (255.0 / (hi - lo))).astype(np.int32), 0, 255) |
| 26 | + counts = np.bincount(bins, minlength=256) |
| 27 | + probs = counts[counts > 0].astype(np.float64) |
| 28 | + probs /= probs.sum() |
| 29 | + ent = -float(np.dot(probs, np.log2(probs))) |
| 30 | + return np.array([[ent]], dtype=np.float32) |
| 31 | + |
| 32 | + |
| 33 | +# --- Staining metrics (RGB input, H&E only) --- |
| 34 | + |
| 35 | + |
| 36 | +def rgb_to_hed(block_rgb: np.ndarray) -> np.ndarray: |
| 37 | + """Convert RGB tile to HED colour space using Beer-Lambert deconvolution. |
| 38 | +
|
| 39 | + Parameters |
| 40 | + ---------- |
| 41 | + block_rgb |
| 42 | + (ty, tx, 3) float32 array in [0, 1]. |
| 43 | +
|
| 44 | + Returns |
| 45 | + ------- |
| 46 | + (ty, tx, 3) float64 array with channels H, E, D. |
| 47 | + """ |
| 48 | + from skimage.color import rgb2hed |
| 49 | + |
| 50 | + rgb_clipped = np.clip(block_rgb, 0.0, 1.0) |
| 51 | + return rgb2hed(rgb_clipped) |
| 52 | + |
| 53 | + |
| 54 | +def hed_metrics(block: np.ndarray) -> np.ndarray: |
| 55 | + """Return all HED-derived metrics for one RGB tile.""" |
| 56 | + hed = rgb_to_hed(block) |
| 57 | + h = hed[..., 0] |
| 58 | + e = hed[..., 1] |
| 59 | + |
| 60 | + return np.array( |
| 61 | + [ |
| 62 | + [ |
| 63 | + [ |
| 64 | + float(h.mean()), |
| 65 | + float(h.std()), |
| 66 | + float(e.mean()), |
| 67 | + float(e.std()), |
| 68 | + float(np.abs(h).mean() / (np.abs(e).mean() + 1e-10)), |
| 69 | + ] |
| 70 | + ] |
| 71 | + ], |
| 72 | + dtype=np.float32, |
| 73 | + ) |
| 74 | + |
| 75 | + |
| 76 | +def hematoxylin_mean(block: np.ndarray) -> np.ndarray: |
| 77 | + """Mean hematoxylin channel intensity.""" |
| 78 | + hed = rgb_to_hed(block) |
| 79 | + return np.array([[float(hed[..., 0].mean())]], dtype=np.float32) |
| 80 | + |
| 81 | + |
| 82 | +def hematoxylin_std(block: np.ndarray) -> np.ndarray: |
| 83 | + """Std of hematoxylin channel intensity.""" |
| 84 | + hed = rgb_to_hed(block) |
| 85 | + return np.array([[float(hed[..., 0].std())]], dtype=np.float32) |
| 86 | + |
| 87 | + |
| 88 | +def eosin_mean(block: np.ndarray) -> np.ndarray: |
| 89 | + """Mean eosin channel intensity.""" |
| 90 | + hed = rgb_to_hed(block) |
| 91 | + return np.array([[float(hed[..., 1].mean())]], dtype=np.float32) |
| 92 | + |
| 93 | + |
| 94 | +def eosin_std(block: np.ndarray) -> np.ndarray: |
| 95 | + """Std of eosin channel intensity.""" |
| 96 | + hed = rgb_to_hed(block) |
| 97 | + return np.array([[float(hed[..., 1].std())]], dtype=np.float32) |
| 98 | + |
| 99 | + |
| 100 | +def he_ratio(block: np.ndarray) -> np.ndarray: |
| 101 | + """Ratio of hematoxylin to eosin mean intensity.""" |
| 102 | + hed = rgb_to_hed(block) |
| 103 | + h_mean = float(np.abs(hed[..., 0]).mean()) |
| 104 | + e_mean = float(np.abs(hed[..., 1]).mean()) |
| 105 | + ratio = h_mean / (e_mean + 1e-10) |
| 106 | + return np.array([[ratio]], dtype=np.float32) |
| 107 | + |
| 108 | + |
| 109 | +# --- Artifact metrics (RGB input, H&E only) --- |
| 110 | + |
| 111 | + |
| 112 | +def fold_fraction(block: np.ndarray) -> np.ndarray: |
| 113 | + """Fraction of pixels identified as tissue folds. |
| 114 | +
|
| 115 | + Uses HSV thresholds tuned for H&E staining: saturation > 0.4 and |
| 116 | + value < 0.3 captures the dark, saturated appearance of folded tissue. |
| 117 | + """ |
| 118 | + from skimage.color import rgb2hsv |
| 119 | + |
| 120 | + rgb_clipped = np.clip(block, 0.0, 1.0) |
| 121 | + hsv = rgb2hsv(rgb_clipped) |
| 122 | + sat = hsv[..., 1] |
| 123 | + val = hsv[..., 2] |
| 124 | + fold_mask = (sat > 0.4) & (val < 0.3) |
| 125 | + frac = float(fold_mask.sum()) / max(fold_mask.size, 1) |
| 126 | + return np.array([[frac]], dtype=np.float32) |
| 127 | + |
| 128 | + |
| 129 | +# --- Tissue coverage (mask input) --- |
| 130 | + |
| 131 | + |
| 132 | +def tissue_fraction(block: np.ndarray) -> np.ndarray: |
| 133 | + """Fraction of pixels that are tissue (nonzero) in a binary mask tile.""" |
| 134 | + return np.array([[float(block.mean())]], dtype=np.float32) |
0 commit comments