"""Write the locked held-out split. Run ONCE, before any modelling. Stratified 30% holdout by (primary label x delivery mode), fixed seed. Writes splits/holdout_ids.txt, splits/dev_ids.txt and splits/holdout_ids.sha256. """ import hashlib import sys from pathlib import Path from sklearn.model_selection import train_test_split sys.path.insert(0, str(Path(__file__).resolve().parent.parent)) from ctg.data import ROOT, load_clinical # noqa: E402 SEED = 20261204 OUT = ROOT / "splits" def main(): hold = OUT / "holdout_ids.txt" if hold.exists() and "--force" not in sys.argv: sys.exit(f"{hold} already exists; the split is locked. Refusing to overwrite.") clin = load_clinical() strata = clin["y_primary"].astype(str) + "_" + clin["caesarean"].astype(str) dev, test = train_test_split(clin.index.tolist(), test_size=0.30, random_state=SEED, stratify=strata) OUT.mkdir(exist_ok=True) hold.write_text("\n".join(sorted(test, key=int)) + "\n") (OUT / "dev_ids.txt").write_text("\n".join(sorted(dev, key=int)) + "\n") digest = hashlib.sha256(hold.read_bytes()).hexdigest() (OUT / "holdout_ids.sha256").write_text(f"{digest} holdout_ids.txt\n") c = clin.loc[test] print(f"dev={len(dev)} holdout={len(test)}") print(f"holdout: pH<7.15 {c.y_primary.sum()}, pH<7.05 {c.y_secondary.sum()}, caesarean {c.caesarean.sum()}") print(f"sha256 {digest}") if __name__ == "__main__": main()