#!/usr/bin/env python3 """Check GP manuscript numbers against the reviewed revision and saved result tables. Run from any directory with the derived outputs present. See the numerical audit in docs/research/gp-referee/numerical-guard.md for scope and provenance. """ import csv import hashlib import re import subprocess from pathlib import Path ROOT = Path(__file__).resolve().parents[1] OUT = ROOT / "outputs" BASELINE = "eff975c" GP_NUMBER_SHA256 = "afb8dbed399fc5935410e3e09f789d0ed19a973561bbf100a5317022d52b4292" SI_NUMBER_SHA256 = { "paper/sections/06_experiments.tex": "7981c2a15fdde66036f8ce281549473cc8e22ee49155da94de679cfff857f8ee", "paper/sections/C_record.tex": "dc646ce7e0dd808986317b1150713594c49e865be99a97e6629978ff985fa2cc", } SETTINGS = ((r"10\,dB", "R10"), (r"0\,dB", "R0"), (r"$-5$\,dB", "Rm5")) NUMBER = re.compile(r"[+-]?(?:\d+(?:\.\d+)?|\.\d+)(?:(?:\\,\d{3})+)?(?:[eE][+-]?\d+)?(?:\\%)?") def require(condition, message): if not condition: raise SystemExit(f"FAIL: {message}") def git(*args): return subprocess.check_output(["git", *args], cwd=ROOT) def baseline(path): return git("show", f"{BASELINE}:{path}") def table(tex, label): i = tex.index(r"\label{tab:" + label + "}") return tex[i:].split(r"\end{tabular}", 1)[0].split(r"\toprule", 1)[1] def cells(tex): """Return all data rows, including text labels but excluding rules and headers.""" body = tex.split(r"\midrule", 1)[1] return [ tuple(c.strip() for c in line.strip()[:-2].split("&")) for line in body.splitlines() if line.strip().endswith(r"\\") and "&" in line ] def read(path): with (OUT / path).open(newline="") as f: return list(csv.DictReader(f)) def pick(rows, **criteria): matches = [r for r in rows if all(r[k] == v for k, v in criteria.items())] require(len(matches) == 1, f"{criteria} selected {len(matches)} source rows") return matches[0] def interval(r, lo="ratio_lo", hi="ratio_hi"): return f"{float(r['ratio']):.3f} [{float(r[lo]):.3f}, {float(r[hi]):.3f}]" def check_h2a(tex): expected = [] for status, directory in (("Hash-locked", "p2r"), ("Replication", "replication")): source = read(f"{directory}/summary/h2a.csv") for snr, regime in SETTINGS: row = [status, snr] for n in ("4", "16"): r = pick(source, regime=regime, c="1.0", n_train=n, candidate="B3d-Sa", competitors="point_ties") row.extend((interval(r), f"{float(r['dpcc']):+.3f}")) expected.append(tuple(row)) require(cells(table(tex, "h2a")) == expected, "H2a ratios, intervals, correlation or row labels") def check_point_ties(tex): point = read("point_tie/ratios.csv") optimized = read("gp_review/e4/ratios.csv") comparisons = ( ("B", "Best saved particle / best deterministic tie"), ("C", "Anchor mode / best deterministic tie"), ("A", "SMC mean / best saved particle"), ("D", "SMC mean / anchor mode"), ("E4-2", "SMC mean / optimized SMC point tie"), ) expected = [] for snr, regime in SETTINGS: for i, (cmp, name) in enumerate(comparisons): source = optimized if cmp == "E4-2" else point ratios = [interval(pick(source, regime=regime, c="1.0", n_train=n, cmp=cmp), "pct_lo", "pct_hi") for n in ("4", "16")] expected.append((snr if i == 0 else "", name, *ratios)) require(cells(table(tex, "pointties")) == expected, "point tie ratios, intervals or row labels") def check_sigma(tex): locked_ratios = read("p2r/summary/sigma_a.csv") followup_ratios = read("replication/summary/sigma_a.csv") post = read("p2r/summary/sigma_post.csv") followup_post = read("replication/summary/sigma_post.csv") nominal = read("p2r/summary/h2a.csv") arms = ( (0, "3.5", "R10_sa3.5"), (0, "3.5, fixed", "R10_sa3.5_fixhyper"), (0, "7", "R10"), (0, "14", "R10_sa14"), (2, "3.5", "Rm5_sa3.5"), (2, "7", "Rm5"), (2, "14", "Rm5_sa14"), ) expected = [] for i, sigma, setting in arms: snr, regime = SETTINGS[i] ratios = [] for n in ("4", "16"): if sigma == "7": r = pick(nominal, n_train=n, c="1.0", regime=regime, candidate="B3d-Sa", competitors="point_ties") elif sigma == "3.5, fixed": ratios.append("--") continue elif i == 0: r = pick(locked_ratios, n_train=n, c="1.0", sigma_a_reg_ms=f"{float(sigma)}") else: r = pick(followup_ratios, n_train=n, c="1.0", setting=setting) ratios.append(interval(r)) exploratory = sigma == "3.5, fixed" or (i == 2 and sigma != "7") r = pick(followup_post if exploratory else post, setting=setting, c="1.0", n_train="16") posterior = [f"{float(r[k]):.2f}" for k in ("sigma_post_ms", "warp_rmse_ms", "warp_cov90")] expected.append((snr, sigma, *ratios, *posterior, "Exploratory" if exploratory else "Locked")) require(cells(table(tex, "sigmaa")) == expected, "anchor uncertainty ratios, intervals or warp summaries") def check_e1(tex): summary = Path("gp_review/e1/summary") ratio = read(summary / "ratios.csv") posterior = read(summary / "p4_posterior.csv") expected = [] for condition in ("iid7-reg7", "iid4-reg4", "iid4-reg7", "iid2-reg2", "iid2-reg7", "iid1-reg1", "iid1-reg7", "bulk-reg2", "bulk-reg5.4", "bulk-reg7"): model, registered = condition.split("-reg") iid, bulk = (model[3:], "0") if model.startswith("iid") else ("2", "5") intervals = [interval(pick(ratio, condition=condition, n_train=n, cmp="P1/point_ties"), "pct_lo", "pct_hi") for n in ("4", "16")] r = pick(posterior, condition=condition, n_train="16") expected.append((iid, bulk, registered, *intervals, f"{float(r['SMC_warp_rmse_ms']):.2f}", f"{float(r['SMC_cov90']):.2f}")) require(cells(table(tex, "anchornoise")) == expected, "E1 ratios, intervals, warp errors or coverage") def number_tokens(source): return NUMBER.findall(re.sub(r"(?