"""render3 stage-1 trainers: the render2 stage-1 detector (cascade2_render2_nnunet._C2R2DetBase, C2R2DataLoader labelling: 251 -> label 1, 250 -> ignore, decoys per the 35n rule) with the renderer swapped for Render3 (cascade2_render3: render2-B + procedural lesion field). NEW module (2026-11-18, renderer designer); no existing module is modified. Shim: nnUNetTrainer_D2Cascade2R3.py (names start with nnUNetTrainer_C2R3). nnUNetTrainer_C2R3DetA_iv_e40 renderer v3 only, 40 epochs (paired screen vs C2R2DetA_iv_e40 / C2R2V1DetA_iv_e40) nnUNetTrainer_C2R3DetA_iv renderer v3 only, 150 epochs (scale-up) nnUNetTrainer_C2R3DetA_ii G1 + renderer v3 + CONTRA (for the record / mix with v3) Env C2_R3_KNOBS / C2_R2B_KNOBS override knobs (printed to the training log).""" from __future__ import annotations import os, sys from pathlib import Path _D2 = str(Path(__file__).resolve().parent) if _D2 not in sys.path: sys.path.insert(0, _D2) import torch # noqa: E402 (main 2026-11-20: needed by the explicit __init__ default) import cascade2_render2_nnunet as R2N # noqa: E402 class _C2R3DetBase(R2N._C2R2DetBase): R3_KNOBS = "" # baked-in C2_R3_KNOBS for named variants (own results folder); env value is appended R3_VK = "" # baked-in C2_R3_VK (v1-engine acquisition / appearance overrides) R2B_KNOBS = "" # baked-in C2_R2B_KNOBS (render2-B small-family / gate overrides) EXTRA_IGNORE_CODES = () # extra v1 decoy codes -> IGNORE (e.g. 5 sulcal, 6 edge: analyst item 5, juxtacortical ablation) SAVE_EVERY = 1 # opportunistic MIG preempts ~hourly and an epoch takes ~13 min: checkpoint every epoch def __init__(self, plans, configuration, fold, dataset_json, device=torch.device("cuda")): # main 2026-11-20: explicit signature (nnUNetTrainer.__init__ records its args via locals(); *a/**k raised KeyError 'a' # and killed every Render3 pilot at start, e.g. 45047) super().__init__(plans, configuration, fold, dataset_json, device) self.save_every = self.SAVE_EVERY def _decoy_codes(self): codes = super()._decoy_codes() if codes is not None and self.EXTRA_IGNORE_CODES: codes = sorted(set(codes) | set(self.EXTRA_IGNORE_CODES)) self.print_to_log_file(f"C2R3: extra decoy codes -> IGNORE {sorted(self.EXTRA_IGNORE_CODES)}; now {codes}") return codes def _render_hook(self): if "cg" not in self.GROUPS: return None if self.R3_KNOBS: env = os.environ.get("C2_R3_KNOBS", "") os.environ["C2_R3_KNOBS"] = ",".join(x for x in (self.R3_KNOBS, env) if x) if self.R2B_KNOBS: os.environ["C2_R2B_KNOBS"] = ",".join(x for x in (self.R2B_KNOBS, os.environ.get("C2_R2B_KNOBS", "")) if x) if self.R3_VK: os.environ["C2_R3_VK"] = ",".join(x for x in (self.R3_VK, os.environ.get("C2_R3_VK", "")) if x) self.print_to_log_file(f"C2R3 C2_R3_VK={os.environ.get('C2_R3_VK', '')!r}") import cascade2_render3 as R3 self.print_to_log_file(f"C2R3: renderer render3 (render2-B + procedural lesion field), R3 knobs {R3._knobs_from_env()}, " f"R2B knobs {R3._r2b_knobs_from_env()}; env C2_R3_KNOBS={os.environ.get('C2_R3_KNOBS', '')!r} " f"C2_R2B_KNOBS={os.environ.get('C2_R2B_KNOBS', '')!r}") return R3.make_online_hook3(return_negmap=True) _GEN = {} for _d in ("ii", "iv"): _n = f"nnUNetTrainer_C2R3DetA_{_d}" _GEN[_n] = type(_n, (_C2R3DetBase,), {"DATA_OPTION": _d, "WARM": "d331", "OVERSAMPLE": True, "__module__": __name__, "__doc__": f"stage-1 detector, renderer v3, data option {_d}, D331 warm start"}) _GEN["nnUNetTrainer_C2R3DetA_iv_e40"] = type("nnUNetTrainer_C2R3DetA_iv_e40", (_GEN["nnUNetTrainer_C2R3DetA_iv"],), {"EPOCHS": 40, "__module__": __name__, "__doc__": "renderer v3 only, 40 epochs (paired screen)"}) # r3b = DOSE variant of the procedural field (more foci, more tiny, multifocal more often); same everything else _GEN["nnUNetTrainer_C2R3bDetA_iv_e40"] = type("nnUNetTrainer_C2R3bDetA_iv_e40", (_GEN["nnUNetTrainer_C2R3DetA_iv_e40"],), {"R3_KNOBS": "p_field=1.0,p_multifocal=0.5,mf_lambda=20:80,p_tiny=0.85", "__module__": __name__, "__doc__": "renderer v3, high-dose procedural field, 40 epochs"}) # r3c = R3a + EXTREME acquisition / appearance breadth (robustness): SNR 3-100, bias up to 0.9, thick 1.5-8 mm in any axis # more often off-axial, 3D iso 0.7-2.5 mm, stronger gamma, more ghost / Gibbs, random contrast 70 %, lesion texture 0-1.5 _GEN["nnUNetTrainer_C2R3cDetA_iv_e40"] = type("nnUNetTrainer_C2R3cDetA_iv_e40", (_GEN["nnUNetTrainer_C2R3DetA_iv_e40"],), {"R3_VK": "snr=3:100,bias_max=0.9,thick_mm=1.5:8,p_thick_axial=0.5,iso_mm=0.7:2.5,gamma_sd=0.5," "p_ghost=0.3,p_gibbs=0.3,p_realistic=0.3,lesion_texture=0:1.5", "__module__": __name__, "__doc__": "renderer v3 + extreme acquisition breadth, 40 epochs"}) # r3d = FAINT-TINY emphasis (user 2026-11-19: remaining LAB gap = T1-faint tiny specks; parity on stroke-like): most foci tiny # (8-60 vox), multifocal half the time, all locations; the small-component family repaints tiny comps (own + procedural) with # p 0.8 at FAINT-to-moderate amplitude 0.8-3 local SD (the gate still makes sub-visible ones IGNORE). FLAIR appearance is # independent of T1 (FLAIR-only stage 1), so these are T1-faint by construction. _GEN["nnUNetTrainer_C2R3dDetA_iv_e40"] = type("nnUNetTrainer_C2R3dDetA_iv_e40", (_GEN["nnUNetTrainer_C2R3DetA_iv_e40"],), {"R3_KNOBS": "p_field=0.95,p_multifocal=0.5,mf_lambda=10:50,p_tiny=0.9,tiny_vox=8:60", "R2B_KNOBS": "p_small_family=0.8,tiny_amp=0.8:3.0", "__module__": __name__, "__doc__": "renderer v3, faint-tiny emphasis, 40 epochs"}) # ---- 2026-11-19 detector-analyst dispatch (analysts/DETECTOR_FINDINGS.md) ----------------------------------------------- def _v(name, base=None, **attrs): _GEN[name] = type(name, (base or _GEN["nnUNetTrainer_C2R3DetA_iv_e40"],), dict(attrs, __module__=__name__)) _CONSPIC = "p_tiny=0.85,p_conspic=1.0" _LOC8 = "loc_p=0.15:0.2:0.2:0.1:0.05:0.15:0.1:0.05" # + posterior fossa .10, CSF-adjacent .05 _v("nnUNetTrainer_C2R3eDetA_iv_e40", R3_KNOBS=_CONSPIC, __doc__="(1) conspicuous tiny: procedural foci < 150 vox repainted at 2.5-8 local SD") _v("nnUNetTrainer_C2R3hDetA_iv_e40", R3_KNOBS="band_p=0.5,bg_lambda=0:6", __doc__="(2) faint perilesional band + faint background patches, label 0") _v("nnUNetTrainer_C2R3jDetA_iv_e40", EXTRA_IGNORE_CODES=(5, 6), __doc__="(3) sulcal / edge decoys -> IGNORE (juxtacortical)") _v("nnUNetTrainer_C2R3j2DetA_iv_e40", EXTRA_IGNORE_CODES=(4, 5, 6), __doc__="(3b) ALL v1 decoys -> IGNORE (flow 4 + sulcal 5 + edge 6; every other decoy class already IGNORE): liberal FLAIR finder, classifier does T1 confirmation") _v("nnUNetTrainer_C2R3kDetA_iv_e40", R3_KNOBS="p_cavity=0.4," + _LOC8, __doc__="(4)+(5) CSF-isointense cavity family + posterior-fossa / CSF-adjacent sites") _v("nnUNetTrainer_C2R3xDetA_iv_e40", R3_KNOBS="p_tiny=0.85,p_conspic=0.7,band_p=0.5,bg_lambda=0:6,p_cavity=0.3," + _LOC8, EXTRA_IGNORE_CODES=(5, 6), __doc__="ALL dispatch items combined (fast package test; ablations h/j/k/e)") # 2x2 at e40 (renderer dose x detector tiny-lesion loss), with the detector agent's ivT bundle (cascade2_det3.C2D3_ivT: # MIL 0.2 + lesion-max deep supervision + sampling floor 0.1 mL). Cells: v1 / base = r2v1_detA_iv_e40 (41573); # R3e / base = C2R3eDetA_iv_e40; v1 / ivT = C2R3V1T_iv_e40; R3e / ivT = C2R3eT_iv_e40. try: import cascade2_det3 as _D3 from cascade2_det2 import _C2D2Base _GEN["nnUNetTrainer_C2R3V1T_iv_e40"] = type("nnUNetTrainer_C2R3V1T_iv_e40", (_D3.nnUNetTrainer_C2D3_ivT,), {"EPOCHS": 40, "__module__": __name__, "__doc__": "v1 renderer + ivT bundle, 40 ep (2x2 cell)"}) _GEN["nnUNetTrainer_C2R3eT_iv_e40"] = type("nnUNetTrainer_C2R3eT_iv_e40", (_GEN["nnUNetTrainer_C2R3eDetA_iv_e40"], _C2D2Base), {"W_MIL": 0.2, "DS_LESION_MAX": True, "VOL_FLOOR": 100, "__module__": __name__, "__doc__": "R3e renderer + ivT bundle, 40 ep (2x2 cell)"}) except Exception as _e: # pragma: no cover print(f"[cascade2_render3_nnunet] 2x2 ivT cells unavailable: {_e!r}") globals().update(_GEN) __all__ = sorted(_GEN)