scripts: publish what the READMEs tell you to run
Browse files- scripts/test_splits.py +146 -0
scripts/test_splits.py
ADDED
|
@@ -0,0 +1,146 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 1 |
+
"""Held-out intervals are actually held out, and the guard is not decorative.
|
| 2 |
+
|
| 3 |
+
Before this, every motherboard episode was `split: train` — the release had no
|
| 4 |
+
held-out data at all, and the probe set's start frames were training frames.
|
| 5 |
+
|
| 6 |
+
The failure this guards against leaves no trace. A training window starting
|
| 7 |
+
shortly before a held-out interval still contains its frames; the metric just
|
| 8 |
+
comes out better and nothing says why. So the checks below enumerate ACTUAL
|
| 9 |
+
window starts rather than reasoning about the intervals.
|
| 10 |
+
|
| 11 |
+
python scripts/test_splits.py
|
| 12 |
+
"""
|
| 13 |
+
from __future__ import annotations
|
| 14 |
+
|
| 15 |
+
import json
|
| 16 |
+
import sys
|
| 17 |
+
from pathlib import Path
|
| 18 |
+
|
| 19 |
+
sys.path.insert(0, str(Path(__file__).resolve().parents[2]))
|
| 20 |
+
|
| 21 |
+
from react_paths import release_root # noqa: E402
|
| 22 |
+
|
| 23 |
+
import numpy as np # noqa: E402
|
| 24 |
+
|
| 25 |
+
RESULTS: list[tuple[bool, str, str]] = []
|
| 26 |
+
REL = release_root("motherboard")
|
| 27 |
+
|
| 28 |
+
|
| 29 |
+
def check(ok: bool, name: str, evidence: str) -> None:
|
| 30 |
+
RESULTS.append((bool(ok), name, evidence))
|
| 31 |
+
|
| 32 |
+
|
| 33 |
+
def main() -> int:
|
| 34 |
+
from twm.splits import (assert_window_fits, build_splits, forbidden_starts,
|
| 35 |
+
test_starts)
|
| 36 |
+
|
| 37 |
+
eps = [json.loads(l) for l in (REL / "episodes.jsonl").read_text().splitlines() if l.strip()]
|
| 38 |
+
bad = json.loads((REL / "bad_frames.json").read_text())["episodes"]
|
| 39 |
+
S = build_splits(eps, bad, seed=0)
|
| 40 |
+
W = S["max_train_window"]
|
| 41 |
+
|
| 42 |
+
# 1 — NO TRAIN WINDOW TOUCHES A TEST FRAME. Enumerated, not argued.
|
| 43 |
+
leaks, n_win = [], 0
|
| 44 |
+
for e in eps:
|
| 45 |
+
key, N = e["episode"], e["n_frames"]
|
| 46 |
+
info = S["episodes"][key]
|
| 47 |
+
test = np.zeros(N, bool)
|
| 48 |
+
for a, b in info["test"]:
|
| 49 |
+
test[a:b + 1] = True
|
| 50 |
+
forb = forbidden_starts(S, key, W)
|
| 51 |
+
for s in range(0, N - W + 1):
|
| 52 |
+
if any(lo <= s <= hi for lo, hi in forb):
|
| 53 |
+
continue
|
| 54 |
+
n_win += 1
|
| 55 |
+
if test[s:s + W].any():
|
| 56 |
+
leaks.append(f"{key}: window at {s}")
|
| 57 |
+
if len(leaks) > 3:
|
| 58 |
+
break
|
| 59 |
+
check(not leaks, "no admissible train window contains a held-out frame",
|
| 60 |
+
f"{n_win} train windows of {W} frames enumerated across "
|
| 61 |
+
f"{len(eps)} episodes, 0 touch a test interval"
|
| 62 |
+
+ (f"; leaks {leaks[:3]}" if leaks else ""))
|
| 63 |
+
|
| 64 |
+
# 2 — AND THE GUARD IS LOAD-BEARING. Shrinking it to the interval alone
|
| 65 |
+
# must produce leaks, or the guard was never doing anything.
|
| 66 |
+
naive = []
|
| 67 |
+
for e in eps[:6]:
|
| 68 |
+
key, N = e["episode"], e["n_frames"]
|
| 69 |
+
info = S["episodes"][key]
|
| 70 |
+
if info["whole"]:
|
| 71 |
+
continue
|
| 72 |
+
test = np.zeros(N, bool)
|
| 73 |
+
for a, b in info["test"]:
|
| 74 |
+
test[a:b + 1] = True
|
| 75 |
+
for s in range(0, N - W + 1):
|
| 76 |
+
if any(a <= s <= b for a, b in info["test"]): # interval only
|
| 77 |
+
continue
|
| 78 |
+
if test[s:s + W].any():
|
| 79 |
+
naive.append(s)
|
| 80 |
+
check(len(naive) > 0, "the guard is load-bearing, not decorative",
|
| 81 |
+
f"excluding only the intervals (no guard) leaks {len(naive)} windows "
|
| 82 |
+
f"in the first 6 episodes; with the guard it is 0")
|
| 83 |
+
|
| 84 |
+
# 3 — a too-long window is REFUSED, not silently allowed
|
| 85 |
+
try:
|
| 86 |
+
assert_window_fits(S, S["guard_frames"] + 2)
|
| 87 |
+
raised = False
|
| 88 |
+
except ValueError:
|
| 89 |
+
raised = True
|
| 90 |
+
ok_small = True
|
| 91 |
+
try:
|
| 92 |
+
assert_window_fits(S, W)
|
| 93 |
+
except ValueError:
|
| 94 |
+
ok_small = False
|
| 95 |
+
check(raised and ok_small, "a window longer than the guard is refused",
|
| 96 |
+
f"span {W} accepted, span {S['guard_frames']+2} raises")
|
| 97 |
+
|
| 98 |
+
# 4 — deterministic, and it moves when the seed does
|
| 99 |
+
a = build_splits(eps, bad, seed=0)
|
| 100 |
+
b = build_splits(eps, bad, seed=1)
|
| 101 |
+
same = a["episodes"] == S["episodes"]
|
| 102 |
+
diff = sum(1 for k in a["episodes"]
|
| 103 |
+
if a["episodes"][k]["test"] != b["episodes"][k]["test"])
|
| 104 |
+
check(same and diff > len(eps) // 2,
|
| 105 |
+
"the split is reproducible and seed-dependent",
|
| 106 |
+
f"seed 0 reproduces exactly; seed 1 moves {diff}/{len(eps)} episodes")
|
| 107 |
+
|
| 108 |
+
# 5 — no test interval sits on known-bad frames
|
| 109 |
+
onbad = []
|
| 110 |
+
for e in eps:
|
| 111 |
+
key, N = e["episode"], e["n_frames"]
|
| 112 |
+
if S["episodes"][key]["whole"]:
|
| 113 |
+
continue
|
| 114 |
+
m = np.zeros(N, bool)
|
| 115 |
+
for k in ("intensity_spikes", "pose_teleports_L", "pose_teleports_R",
|
| 116 |
+
"ot_loss_L", "ot_loss_R"):
|
| 117 |
+
for x, y in bad.get(key, {}).get(k, []):
|
| 118 |
+
m[max(0, x):min(N, y + 1)] = True
|
| 119 |
+
for x, y in S["episodes"][key]["test"]:
|
| 120 |
+
if m[x:y + 1].any():
|
| 121 |
+
onbad.append(f"{key}[{x},{y}]")
|
| 122 |
+
check(not onbad, "held-out intervals avoid known-bad frames",
|
| 123 |
+
f"{S['stats']['n_test_intervals']} intervals, none on flagged "
|
| 124 |
+
f"dropouts" + (f"; {onbad[:2]}" if onbad else ""))
|
| 125 |
+
|
| 126 |
+
# 6 — the numbers the docstring quotes are the numbers it produces
|
| 127 |
+
st = S["stats"]
|
| 128 |
+
check(0.10 <= st["test_fraction"] <= 0.15 and st["n_test_intervals"] > 100,
|
| 129 |
+
"the split holds out a usable fraction",
|
| 130 |
+
f"test {st['test_fraction']*100:.1f}% guard "
|
| 131 |
+
f"{st['guard_fraction']*100:.1f}% train "
|
| 132 |
+
f"{(1-st['test_fraction']-st['guard_fraction'])*100:.1f}% over "
|
| 133 |
+
f"{st['n_test_intervals']} intervals + {st['n_whole_test_episodes']} "
|
| 134 |
+
f"whole episodes")
|
| 135 |
+
|
| 136 |
+
w = max(len(x) for _, x, _ in RESULTS)
|
| 137 |
+
print()
|
| 138 |
+
for ok, name, ev in RESULTS:
|
| 139 |
+
print(f" [{'ok' if ok else 'FAIL'}] {name:<{w}} {ev}")
|
| 140 |
+
n = sum(not ok for ok, _, _ in RESULTS)
|
| 141 |
+
print(f"\nsplits: {len(RESULTS)} checks, {n} failing")
|
| 142 |
+
return 1 if n else 0
|
| 143 |
+
|
| 144 |
+
|
| 145 |
+
if __name__ == "__main__":
|
| 146 |
+
raise SystemExit(main())
|