File size: 3,701 Bytes
378f073
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
"""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())