skyan1002's picture
v1.0 manifest covering all 806 testbeds; docs/CLI corrected; mirror Zenodo qc/ and code/pipeline/; DOI 10.5281/zenodo.22100899
378f073 verified
Raw History Blame Contribute Delete
3.7 kB
"""verify.py — verification harness for the clean reconstruction.
Diffs the build/ outputs against the canonical ds_260603 reference at every
selection stage (membership-exact), reports the L2 availability bookkeeping drift
(reference cache/table inconsistency), and runs the L2/L3 validators.
Usage: python verify.py
"""
import subprocess
import sys
import pathlib
sys.path.insert(0, str(pathlib.Path(__file__).resolve().parent))
import pandas as pd
import config
def gid(g):
return str(int(float(g))).zfill(8)
def members(df, with_peak=False):
if with_peak and "peak_time" in df.columns:
return set((e, gid(g), str(p)) for e, g, p in zip(df.ff_episode_id, df.gage_id, df.peak_time))
return set((e, gid(g)) for e, g in zip(df.ff_episode_id, df.gage_id))
def rd(p):
return pd.read_csv(p, low_memory=False)
def line(name, ok, detail=""):
print(f" [{'PASS' if ok else 'FAIL'}] {name:34s} {detail}")
return ok
def main():
print("=== Membership verification (build vs ds_260603 reference) ===")
allok = True
# L2 selection gate (exact); availability bookkeeping may drift (documented)
r = rd(config.REF / "L2" / "l2_episodes.csv").set_index("ff_episode_id").sort_index()
n = rd(config.B_L2 / "l2_episodes.csv").set_index("ff_episode_id").sort_index()
allok &= line("L2 episode set", r.index.equals(n.index), f"{len(n)} episodes")
for c in ("observation_evidence_label", "pass_to_level3_gage_screening"):
same = (r[c].astype(str) == n.loc[r.index, c].astype(str)).all()
allok &= line(f"L2 {c}", same, "selection gate")
drift = int((r["any_availability_unknown"].astype(str) !=
n.loc[r.index, "any_availability_unknown"].astype(str)).sum())
line("L2 availability drift", True, f"{drift} episodes (reference cache/table drift; non-gating)")
# L3 stages — membership must be identical
stages = [
("L3 qualifying", "l3_testbeds.csv", False),
("L3 strict", "l3_testbeds_strict.csv", False),
("L3 dedup", "l3_testbeds_strict_dedup.csv", False),
]
for name, fn, wp in stages:
R, N = members(rd(config.REF / "L3" / fn), wp), members(rd(config.B_L3 / fn), wp)
allok &= line(name, R == N, f"ref={len(R)} new={len(N)} (+{len(N-R)}/-{len(R-N)})")
# selected flag + final set
rd_ = rd(config.REF / "L3" / "l3_testbeds_strict_dedup.csv")
nd_ = rd(config.B_L3 / "l3_testbeds_strict_dedup.csv")
rs = set((e, gid(g)) for e, g, s in zip(rd_.ff_episode_id, rd_.gage_id, rd_.selected) if bool(s))
ns = set((e, gid(g)) for e, g, s in zip(nd_.ff_episode_id, nd_.gage_id, nd_.selected) if bool(s))
allok &= line("L3 selected", rs == ns, f"ref={len(rs)} new={len(ns)}")
rfin = set((e, gid(g)) for e, g, s, rp in
zip(rd_.ff_episode_id, rd_.gage_id, rd_.selected, rd_.lp3_return_period_yr)
if bool(s) and pd.notna(rp) and rp >= config.RP_MIN_FINAL)
nf = members(rd(config.B_L3 / "l3_final_selected.csv"))
allok &= line("FINAL (selected & RP>=2)", rfin == nf,
f"ref={len(rfin)} ({len(set(e for e,_ in rfin))} ep) new={len(nf)} ({len(set(e for e,_ in nf))} ep)")
# validators (run the ported step validators against build/)
print("=== Validators ===")
for v in ("steps/s06_validate_l2.py", "steps/s12_validate_l3.py"):
rc = subprocess.run([sys.executable, v], capture_output=True, text=True).returncode
allok &= line(v, rc == 0, "exit 0" if rc == 0 else "non-zero exit")
print("\n" + ("ALL CHECKS PASSED ✓" if allok else "SOME CHECKS FAILED ✗"))
return 0 if allok else 1
if __name__ == "__main__":
raise SystemExit(main())