File size: 13,381 Bytes
f9b628d | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | """
Phase 3 β Clinical Logic & Verification Engine
===============================================
Implements deterministic clinical metric extraction from segmentation masks.
No ML here β pure geometry and ophthalmology math.
Mask convention (from Phase 2 training):
0 = Background
1 = Optic Disc
2 = Optic Cup
"""
import numpy as np
import cv2
from dataclasses import dataclass, field
from typing import Tuple, Dict, Optional
from enum import Enum
class RiskLevel(Enum):
HEALTHY = "Healthy"
SUSPECT = "Glaucoma Suspect"
HIGH = "High Risk"
class SanityError(Exception):
"""Raised when mask geometry violates anatomical constraints."""
pass
@dataclass
class ISNTResult:
inferior: float = 0.0
superior: float = 0.0
nasal: float = 0.0
temporal: float = 0.0
rule_satisfied: bool = False
def to_dict(self) -> Dict[str, float]:
return {
'inferior': round(self.inferior, 4),
'superior': round(self.superior, 4),
'nasal': round(self.nasal, 4),
'temporal': round(self.temporal, 4),
'rule_satisfied': self.rule_satisfied,
}
@dataclass
class ClinicalResult:
vcdr: float = 0.0
isnt: ISNTResult = field(default_factory=ISNTResult)
disc_area_px: int = 0
cup_area_px: int = 0
disc_center: Tuple[int, int] = (0, 0)
cup_center: Tuple[int, int] = (0, 0)
uncertainty: float = 0.0
high_uncertainty: bool = False
risk_level: RiskLevel = RiskLevel.HEALTHY
sanity_passed: bool = False
warnings: list = field(default_factory=list)
def to_dict(self) -> dict:
return {
'vcdr': round(self.vcdr, 4),
'isnt': self.isnt.to_dict(),
'disc_area_px': self.disc_area_px,
'cup_area_px': self.cup_area_px,
'disc_center': self.disc_center,
'cup_center': self.cup_center,
'uncertainty': round(self.uncertainty, 6),
'high_uncertainty': self.high_uncertainty,
'risk_level': self.risk_level.value,
'sanity_passed': self.sanity_passed,
'warnings': self.warnings,
}
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Step 1 β Sanity checks
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_sanity_checks(disc_mask: np.ndarray, cup_mask: np.ndarray) -> None:
"""
Enforce anatomical constraints. Raises SanityError on any violation.
Checks:
1. Masks are binary (0/1 values only).
2. Disc region is non-empty.
3. Cup is 100 % contained inside the Disc.
4. Neither mask has disconnected 'islands' outside the main region.
"""
# 1. Binary check
for name, mask in [('disc', disc_mask), ('cup', cup_mask)]:
unique_vals = np.unique(mask)
if not set(unique_vals).issubset({0, 1}):
raise SanityError(f"{name} mask contains non-binary values: {unique_vals}")
# 2. Non-empty disc
disc_area = int(disc_mask.sum())
if disc_area == 0:
raise SanityError("Optic Disc mask is empty β segmentation failure.")
# 3. Cup β Disc (every cup pixel must be a disc pixel)
cup_outside_disc = np.logical_and(cup_mask == 1, disc_mask == 0)
if cup_outside_disc.any():
n_violation = int(cup_outside_disc.sum())
raise SanityError(
f"Cup extends outside Disc boundary ({n_violation} pixels). "
"Anatomically impossible β reject segmentation."
)
# 4. Single connected component check (no 'floating islands')
for name, mask in [('disc', disc_mask), ('cup', cup_mask)]:
if mask.sum() == 0:
continue
n_labels, _ = cv2.connectedComponents(mask.astype(np.uint8))
if n_labels > 2: # 1 background + 1 foreground = 2
raise SanityError(
f"{name} mask has {n_labels - 1} disconnected regions. "
"Expected a single contiguous structure."
)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Step 2 β vCDR
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def calculate_vcdr(
disc_mask: np.ndarray,
cup_mask: np.ndarray
) -> Tuple[float, dict]:
"""
Calculate vertical Cup-to-Disc Ratio using vertical extrema method.
Clinical basis:
Horizontal disc expansion is less indicative of early glaucoma.
The vertical ratio (vCDR) is the primary screening metric.
Returns:
vcdr (float): ratio in [0, 1]
details (dict): raw pixel measurements
"""
disc_rows = np.where(disc_mask.any(axis=1))[0]
cup_rows = np.where(cup_mask.any(axis=1))[0]
if disc_rows.size == 0:
return 0.0, {}
disc_v_diam = int(disc_rows.max() - disc_rows.min() + 1)
cup_v_diam = int(cup_rows.max() - cup_rows.min() + 1) if cup_rows.size > 0 else 0
vcdr = cup_v_diam / disc_v_diam if disc_v_diam > 0 else 0.0
details = {
'disc_top_px': int(disc_rows.min()),
'disc_bottom_px': int(disc_rows.max()),
'disc_v_diam_px': disc_v_diam,
'cup_top_px': int(cup_rows.min()) if cup_rows.size > 0 else None,
'cup_bottom_px': int(cup_rows.max()) if cup_rows.size > 0 else None,
'cup_v_diam_px': cup_v_diam,
}
return round(vcdr, 4), details
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Step 3 β ISNT Rule
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def _disc_centroid(disc_mask: np.ndarray) -> Tuple[int, int]:
M = cv2.moments(disc_mask.astype(np.uint8))
if M['m00'] == 0:
h, w = disc_mask.shape
return h // 2, w // 2
cy = int(M['m01'] / M['m00'])
cx = int(M['m10'] / M['m00'])
return cy, cx
def _rim_thickness_in_quadrant(
quadrant_mask: np.ndarray,
disc_mask: np.ndarray,
cup_mask: np.ndarray
) -> float:
"""
Mean Euclidean distance from cup boundary to disc boundary,
measured inside a specific quadrant.
Uses distance transform on the inverse disc mask so that each
pixel inside the disc gets its distance to the disc edge.
Then we sample only rim pixels (disc=1, cup=0) in this quadrant.
"""
rim = np.logical_and(disc_mask == 1, cup_mask == 0).astype(np.uint8)
rim_in_quad = np.logical_and(rim, quadrant_mask).astype(np.uint8)
if rim_in_quad.sum() == 0:
return 0.0
# Distance transform on disc interior β distance to disc *boundary*
dist_to_disc_edge = cv2.distanceTransform(
disc_mask.astype(np.uint8), cv2.DIST_L2, 5
)
thicknesses = dist_to_disc_edge[rim_in_quad == 1]
return float(np.mean(thicknesses))
def calculate_isnt(
disc_mask: np.ndarray,
cup_mask: np.ndarray
) -> ISNTResult:
"""
Calculate neuro-retinal rim thickness in the four ISNT quadrants.
Quadrant definition (image coordinates):
Superior β top half (rows < cy)
Inferior β bottom half (rows >= cy)
Nasal β right half (cols >= cx) [standard right eye convention]
Temporal β left half (cols < cx)
ISNT rule is satisfied when:
Inferior > Superior > Nasal > Temporal
Violation is a known early indicator of glaucomatous damage.
"""
h, w = disc_mask.shape
cy, cx = _disc_centroid(disc_mask)
superior_q = np.zeros((h, w), dtype=bool)
inferior_q = np.zeros((h, w), dtype=bool)
nasal_q = np.zeros((h, w), dtype=bool)
temporal_q = np.zeros((h, w), dtype=bool)
superior_q[:cy, :] = True
inferior_q[cy:, :] = True
nasal_q[:, cx:] = True
temporal_q[:, :cx] = True
I = _rim_thickness_in_quadrant(inferior_q, disc_mask, cup_mask)
S = _rim_thickness_in_quadrant(superior_q, disc_mask, cup_mask)
N = _rim_thickness_in_quadrant(nasal_q, disc_mask, cup_mask)
T = _rim_thickness_in_quadrant(temporal_q, disc_mask, cup_mask)
rule_ok = (I > S > N > T)
return ISNTResult(inferior=I, superior=S, nasal=N, temporal=T,
rule_satisfied=rule_ok)
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Step 4 β Risk classification
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def classify_risk(
vcdr: float,
isnt: ISNTResult,
uncertainty: float,
uncertainty_threshold: float = 0.05
) -> Tuple[RiskLevel, list]:
"""
Rule-based risk stratification.
Thresholds derived from clinical literature:
vCDR < 0.65 β Healthy
vCDR 0.65β0.80 + ISNT violation β Suspect
vCDR > 0.80 β High Risk
High uncertainty overrides to Suspect.
"""
warnings = []
if uncertainty > uncertainty_threshold:
warnings.append(
f"High model uncertainty ({uncertainty:.4f}) β result may be unreliable."
)
return RiskLevel.SUSPECT, warnings
if vcdr > 0.80:
risk = RiskLevel.HIGH
warnings.append(f"vCDR {vcdr:.2f} exceeds 0.80 β urgent referral recommended.")
elif vcdr > 0.65:
risk = RiskLevel.SUSPECT
warnings.append(f"vCDR {vcdr:.2f} in borderline range (0.65β0.80).")
else:
risk = RiskLevel.HEALTHY
if not isnt.rule_satisfied:
warnings.append(
"ISNT rule violated β neuro-retinal rim thinning detected."
)
if risk == RiskLevel.HEALTHY:
risk = RiskLevel.SUSPECT
return risk, warnings
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
# Main pipeline entry point
# βββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββββ
def run_clinical_pipeline(
disc_mask: np.ndarray,
cup_mask: np.ndarray,
uncertainty: float = 0.0,
uncertainty_threshold: float = 0.05
) -> ClinicalResult:
"""
Execute complete Phase 3 pipeline on binary masks.
Args:
disc_mask: uint8 binary array (1 = disc, 0 = background)
cup_mask: uint8 binary array (1 = cup, 0 = background)
uncertainty: scalar from Phase 2 MC-Dropout
uncertainty_threshold: flag above this value as high uncertainty
Returns:
ClinicalResult dataclass
"""
result = ClinicalResult()
result.uncertainty = float(uncertainty)
result.high_uncertainty = uncertainty > uncertainty_threshold
disc_mask = (disc_mask > 0).astype(np.uint8)
cup_mask = (cup_mask > 0).astype(np.uint8)
# ββ Sanity checks ββ
try:
run_sanity_checks(disc_mask, cup_mask)
result.sanity_passed = True
except SanityError as e:
result.warnings.append(f"SANITY FAIL: {e}")
result.risk_level = RiskLevel.SUSPECT
return result
# ββ Structural measurements ββ
result.disc_area_px = int(disc_mask.sum())
result.cup_area_px = int(cup_mask.sum())
dy, dx = _disc_centroid(disc_mask)
result.disc_center = (int(dx), int(dy))
if cup_mask.sum() > 0:
M = cv2.moments(cup_mask)
if M['m00'] > 0:
result.cup_center = (
int(M['m10'] / M['m00']),
int(M['m01'] / M['m00'])
)
# ββ vCDR ββ
result.vcdr, _ = calculate_vcdr(disc_mask, cup_mask)
# ββ ISNT ββ
result.isnt = calculate_isnt(disc_mask, cup_mask)
# ββ Risk ββ
result.risk_level, warnings = classify_risk(
result.vcdr, result.isnt, uncertainty, uncertainty_threshold
)
result.warnings.extend(warnings)
return result
|