Matthew Ford commited on
Commit ·
0ade44a
1
Parent(s): 083a17a
Precommit PIMT v8 multiseed split seeds
Browse filesFormer-commit-id: 443b07e6c13bf3af2d6995af2479e04b47726a95
Former-commit-id: aaff6f46f64484c4029801d349b9421d1de220cd
- scripts/pimt_v8_multiseed.py +261 -0
scripts/pimt_v8_multiseed.py
ADDED
|
@@ -0,0 +1,261 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
#!/usr/bin/env python3
|
| 2 |
+
"""PIMT v8 repeated scaffold-split robustness sweep for the Delta R2 claim.
|
| 3 |
+
|
| 4 |
+
Pre-declared primary statistics:
|
| 5 |
+
1. Mean Delta R2 across the hard-coded split seeds, with the across-split
|
| 6 |
+
2.5/97.5 percentile range.
|
| 7 |
+
2. Fraction of splits where structure_full_gbm R2 exceeds
|
| 8 |
+
baseline_physical_gbm R2.
|
| 9 |
+
|
| 10 |
+
Delta Spearman rho is reported the same way for transparency, but it is not
|
| 11 |
+
the headline statistic. Model specs and hyperparameters are fixed by reusing
|
| 12 |
+
train_substantivity.fit_predict for baseline_physical_gbm and
|
| 13 |
+
structure_full_gbm; there is no per-split tuning.
|
| 14 |
+
"""
|
| 15 |
+
from __future__ import annotations
|
| 16 |
+
|
| 17 |
+
import argparse
|
| 18 |
+
import json
|
| 19 |
+
import math
|
| 20 |
+
from pathlib import Path
|
| 21 |
+
from typing import Any
|
| 22 |
+
|
| 23 |
+
import numpy as np
|
| 24 |
+
|
| 25 |
+
import train_substantivity as train
|
| 26 |
+
|
| 27 |
+
|
| 28 |
+
SPLIT_SEEDS: list[int] = [
|
| 29 |
+
20260710,
|
| 30 |
+
20260711,
|
| 31 |
+
20260712,
|
| 32 |
+
20260713,
|
| 33 |
+
20260714,
|
| 34 |
+
20260715,
|
| 35 |
+
20260716,
|
| 36 |
+
20260717,
|
| 37 |
+
20260718,
|
| 38 |
+
20260719,
|
| 39 |
+
20260720,
|
| 40 |
+
20260721,
|
| 41 |
+
20260722,
|
| 42 |
+
20260723,
|
| 43 |
+
20260724,
|
| 44 |
+
20260725,
|
| 45 |
+
20260726,
|
| 46 |
+
20260727,
|
| 47 |
+
20260728,
|
| 48 |
+
20260729,
|
| 49 |
+
]
|
| 50 |
+
|
| 51 |
+
STRUCTURE_KEY = "structure_full_gbm"
|
| 52 |
+
BASELINE_KEY = "baseline_physical_gbm"
|
| 53 |
+
GATE_SEED = 20260710
|
| 54 |
+
GATE_TOLERANCES = {
|
| 55 |
+
"rho_struct": 0.002,
|
| 56 |
+
"rho_base": 0.002,
|
| 57 |
+
"delta_r2": 0.002,
|
| 58 |
+
}
|
| 59 |
+
GATE_EXPECTED = {
|
| 60 |
+
"rho_struct": 0.6633801733693918,
|
| 61 |
+
"rho_base": 0.6130893034049266,
|
| 62 |
+
"delta_r2": 0.15844577805518212,
|
| 63 |
+
}
|
| 64 |
+
|
| 65 |
+
|
| 66 |
+
def finite_percentiles(values: list[float]) -> list[float]:
|
| 67 |
+
arr = np.asarray([v for v in values if math.isfinite(v)], dtype=float)
|
| 68 |
+
if arr.size == 0:
|
| 69 |
+
return [float("nan"), float("nan")]
|
| 70 |
+
return [float(np.percentile(arr, 2.5)), float(np.percentile(arr, 97.5))]
|
| 71 |
+
|
| 72 |
+
|
| 73 |
+
def summarize(splits: list[dict[str, Any]], field: str, win_field: str) -> dict[str, Any]:
|
| 74 |
+
values = [float(row[field]) for row in splits]
|
| 75 |
+
finite = [v for v in values if math.isfinite(v)]
|
| 76 |
+
wins = sum(1 for row in splits if bool(row[win_field]))
|
| 77 |
+
return {
|
| 78 |
+
"mean": float(np.mean(finite)) if finite else float("nan"),
|
| 79 |
+
"percentile_2_5_97_5": finite_percentiles(values),
|
| 80 |
+
"wins": wins,
|
| 81 |
+
"n_splits": len(splits),
|
| 82 |
+
"fraction_wins": wins / len(splits) if splits else float("nan"),
|
| 83 |
+
}
|
| 84 |
+
|
| 85 |
+
|
| 86 |
+
def prepare_payload(fetch_pubchem: bool) -> dict[str, Any]:
|
| 87 |
+
rows = train.enrich_rows(fetch_pubchem)
|
| 88 |
+
ready = [
|
| 89 |
+
row
|
| 90 |
+
for row in rows
|
| 91 |
+
if row.get("smiles") or any(row["features"].get(name) is not None for name in train.FEATURE_NAMES)
|
| 92 |
+
]
|
| 93 |
+
y = np.asarray([row["target_log10"] for row in ready], dtype=float)
|
| 94 |
+
x_base, base_names = train.design_matrix(ready, include_structural=False)
|
| 95 |
+
x_full, full_names = train.design_matrix(ready, include_structural=True)
|
| 96 |
+
return {
|
| 97 |
+
"rows": ready,
|
| 98 |
+
"y": y,
|
| 99 |
+
"x_base": x_base,
|
| 100 |
+
"x_full": x_full,
|
| 101 |
+
"base_names": base_names,
|
| 102 |
+
"full_names": full_names,
|
| 103 |
+
}
|
| 104 |
+
|
| 105 |
+
|
| 106 |
+
def run_split(payload: dict[str, Any], seed: int, test_fraction: float) -> dict[str, Any]:
|
| 107 |
+
ready = payload["rows"]
|
| 108 |
+
y = payload["y"]
|
| 109 |
+
train_idx, test_idx = train.greedy_scaffold_split(ready, test_fraction, seed)
|
| 110 |
+
pred_base = train.fit_predict("gbm", payload["x_base"], y, train_idx, test_idx)
|
| 111 |
+
pred_struct = train.fit_predict("gbm", payload["x_full"], y, train_idx, test_idx)
|
| 112 |
+
base_metrics = train.metrics(y[test_idx], pred_base)
|
| 113 |
+
struct_metrics = train.metrics(y[test_idx], pred_struct)
|
| 114 |
+
delta_r2 = float(struct_metrics["r2"] - base_metrics["r2"])
|
| 115 |
+
delta_rho = float(struct_metrics["spearman_rho"] - base_metrics["spearman_rho"])
|
| 116 |
+
return {
|
| 117 |
+
"seed": seed,
|
| 118 |
+
"n_train": int(len(train_idx)),
|
| 119 |
+
"n_test": int(len(test_idx)),
|
| 120 |
+
"train_scaffolds": int(len({ready[int(i)]["scaffold"] for i in train_idx})),
|
| 121 |
+
"test_scaffolds": int(len({ready[int(i)]["scaffold"] for i in test_idx})),
|
| 122 |
+
"scaffold_overlap": sorted(
|
| 123 |
+
{ready[int(i)]["scaffold"] for i in train_idx}
|
| 124 |
+
& {ready[int(i)]["scaffold"] for i in test_idx}
|
| 125 |
+
),
|
| 126 |
+
"rho_struct": float(struct_metrics["spearman_rho"]),
|
| 127 |
+
"rho_base": float(base_metrics["spearman_rho"]),
|
| 128 |
+
"r2_struct": float(struct_metrics["r2"]),
|
| 129 |
+
"r2_base": float(base_metrics["r2"]),
|
| 130 |
+
"delta_r2": delta_r2,
|
| 131 |
+
"delta_rho": delta_rho,
|
| 132 |
+
"structure_r2_gt_baseline_r2": bool(delta_r2 > 0),
|
| 133 |
+
"structure_rho_gt_baseline_rho": bool(delta_rho > 0),
|
| 134 |
+
}
|
| 135 |
+
|
| 136 |
+
|
| 137 |
+
def gate_status(split: dict[str, Any]) -> dict[str, Any]:
|
| 138 |
+
observed = {
|
| 139 |
+
"rho_struct": split["rho_struct"],
|
| 140 |
+
"rho_base": split["rho_base"],
|
| 141 |
+
"delta_r2": split["delta_r2"],
|
| 142 |
+
}
|
| 143 |
+
checks = {
|
| 144 |
+
key: {
|
| 145 |
+
"expected": GATE_EXPECTED[key],
|
| 146 |
+
"observed": observed[key],
|
| 147 |
+
"abs_error": abs(observed[key] - GATE_EXPECTED[key]),
|
| 148 |
+
"tolerance": GATE_TOLERANCES[key],
|
| 149 |
+
"passes": abs(observed[key] - GATE_EXPECTED[key]) <= GATE_TOLERANCES[key],
|
| 150 |
+
}
|
| 151 |
+
for key in GATE_EXPECTED
|
| 152 |
+
}
|
| 153 |
+
return {
|
| 154 |
+
"seed": GATE_SEED,
|
| 155 |
+
"status": "pass" if all(item["passes"] for item in checks.values()) else "fail",
|
| 156 |
+
"checks": checks,
|
| 157 |
+
}
|
| 158 |
+
|
| 159 |
+
|
| 160 |
+
def write_markdown(report: dict[str, Any], path: Path) -> None:
|
| 161 |
+
lines = [
|
| 162 |
+
"# PIMT v8 multi-seed scaffold robustness sweep",
|
| 163 |
+
"",
|
| 164 |
+
f"Gate: **{report['gate']['status']}**.",
|
| 165 |
+
"",
|
| 166 |
+
"## Pre-declared summary",
|
| 167 |
+
"",
|
| 168 |
+
(
|
| 169 |
+
f"- Delta R2: mean {report['summary']['delta_r2']['mean']:.6f}, "
|
| 170 |
+
f"across-split 2.5/97.5 percentile range "
|
| 171 |
+
f"[{report['summary']['delta_r2']['percentile_2_5_97_5'][0]:.6f}, "
|
| 172 |
+
f"{report['summary']['delta_r2']['percentile_2_5_97_5'][1]:.6f}], "
|
| 173 |
+
f"structure wins {report['summary']['delta_r2']['wins']}/"
|
| 174 |
+
f"{report['summary']['delta_r2']['n_splits']} "
|
| 175 |
+
f"({report['summary']['delta_r2']['fraction_wins']:.3f})."
|
| 176 |
+
),
|
| 177 |
+
(
|
| 178 |
+
f"- Delta rho: mean {report['summary']['delta_rho']['mean']:.6f}, "
|
| 179 |
+
f"across-split 2.5/97.5 percentile range "
|
| 180 |
+
f"[{report['summary']['delta_rho']['percentile_2_5_97_5'][0]:.6f}, "
|
| 181 |
+
f"{report['summary']['delta_rho']['percentile_2_5_97_5'][1]:.6f}], "
|
| 182 |
+
f"structure wins {report['summary']['delta_rho']['wins']}/"
|
| 183 |
+
f"{report['summary']['delta_rho']['n_splits']} "
|
| 184 |
+
f"({report['summary']['delta_rho']['fraction_wins']:.3f})."
|
| 185 |
+
),
|
| 186 |
+
"",
|
| 187 |
+
"## Per-seed table",
|
| 188 |
+
"",
|
| 189 |
+
"| Seed | n_test | rho_struct | rho_base | R2_struct | R2_base | Delta R2 | Delta rho |",
|
| 190 |
+
"| ---: | ---: | ---: | ---: | ---: | ---: | ---: | ---: |",
|
| 191 |
+
]
|
| 192 |
+
for row in report["splits"]:
|
| 193 |
+
lines.append(
|
| 194 |
+
f"| {row['seed']} | {row['n_test']} | {row['rho_struct']:.6f} | "
|
| 195 |
+
f"{row['rho_base']:.6f} | {row['r2_struct']:.6f} | {row['r2_base']:.6f} | "
|
| 196 |
+
f"{row['delta_r2']:.6f} | {row['delta_rho']:.6f} |"
|
| 197 |
+
)
|
| 198 |
+
lines.append("")
|
| 199 |
+
path.write_text("\n".join(lines), encoding="utf-8")
|
| 200 |
+
|
| 201 |
+
|
| 202 |
+
def main() -> None:
|
| 203 |
+
parser = argparse.ArgumentParser()
|
| 204 |
+
parser.add_argument("--test-fraction", type=float, default=0.2)
|
| 205 |
+
parser.add_argument("--fetch-pubchem", action="store_true")
|
| 206 |
+
parser.add_argument("--json-output", default="results/pimt_v8_multiseed.json")
|
| 207 |
+
parser.add_argument("--md-output", default="results/pimt_v8_multiseed.md")
|
| 208 |
+
args = parser.parse_args()
|
| 209 |
+
|
| 210 |
+
payload = prepare_payload(args.fetch_pubchem)
|
| 211 |
+
splits = [run_split(payload, seed, args.test_fraction) for seed in SPLIT_SEEDS]
|
| 212 |
+
gate_split = next(row for row in splits if row["seed"] == GATE_SEED)
|
| 213 |
+
gate = gate_status(gate_split)
|
| 214 |
+
if gate["status"] != "pass":
|
| 215 |
+
print(json.dumps({"gate": gate}, indent=2, sort_keys=True))
|
| 216 |
+
raise SystemExit("Reproduction gate failed; stopping before writing sweep results.")
|
| 217 |
+
|
| 218 |
+
report = {
|
| 219 |
+
"pimt_version": train.PIMT_VERSION,
|
| 220 |
+
"analysis": "pre-committed repeated scaffold-disjoint split robustness check for Delta R2",
|
| 221 |
+
"seed_list_precommitted": SPLIT_SEEDS,
|
| 222 |
+
"model_specs": {
|
| 223 |
+
"structure": STRUCTURE_KEY,
|
| 224 |
+
"baseline": BASELINE_KEY,
|
| 225 |
+
"source": "scripts/train_substantivity.py fit_predict(model_name='gbm')",
|
| 226 |
+
"fixed_hyperparameters": {
|
| 227 |
+
"model": "HistGradientBoostingRegressor",
|
| 228 |
+
"max_iter": 200,
|
| 229 |
+
"learning_rate": 0.04,
|
| 230 |
+
"l2_regularization": 0.1,
|
| 231 |
+
"random_state": 17,
|
| 232 |
+
"imputer": "SimpleImputer(strategy='median')",
|
| 233 |
+
},
|
| 234 |
+
},
|
| 235 |
+
"target": "Poucher measured log10 coefficient only; no inferred, OpenPOM, or volatility-derived substantivity labels used.",
|
| 236 |
+
"n_model_ready": len(payload["rows"]),
|
| 237 |
+
"test_fraction": args.test_fraction,
|
| 238 |
+
"gate": gate,
|
| 239 |
+
"summary": {
|
| 240 |
+
"delta_r2": summarize(splits, "delta_r2", "structure_r2_gt_baseline_r2"),
|
| 241 |
+
"delta_rho": summarize(splits, "delta_rho", "structure_rho_gt_baseline_rho"),
|
| 242 |
+
},
|
| 243 |
+
"splits": splits,
|
| 244 |
+
}
|
| 245 |
+
|
| 246 |
+
json_path = Path(args.json_output)
|
| 247 |
+
md_path = Path(args.md_output)
|
| 248 |
+
json_path.parent.mkdir(parents=True, exist_ok=True)
|
| 249 |
+
json_path.write_text(json.dumps(report, indent=2, sort_keys=True) + "\n", encoding="utf-8")
|
| 250 |
+
write_markdown(report, md_path)
|
| 251 |
+
print(json.dumps({
|
| 252 |
+
"gate": report["gate"]["status"],
|
| 253 |
+
"delta_r2": report["summary"]["delta_r2"],
|
| 254 |
+
"delta_rho": report["summary"]["delta_rho"],
|
| 255 |
+
"json_output": str(json_path),
|
| 256 |
+
"md_output": str(md_path),
|
| 257 |
+
}, indent=2, sort_keys=True))
|
| 258 |
+
|
| 259 |
+
|
| 260 |
+
if __name__ == "__main__":
|
| 261 |
+
main()
|