pnw-subtask-benchmark / grader /test_grade.py
tomas-duarte's picture
P&W subtask segmentation benchmark
63f0b54
Raw History Blame Contribute Delete
3.44 kB
#!/usr/bin/env -S uv run --script
# /// script
# requires-python = ">=3.9"
# dependencies = ["numpy"]
# ///
"""Tiny hand cases for grade.py. Run: uv run test_grade.py (or python test_grade.py with numpy installed, or pytest)."""
from grade import normalise, episode_counts
S = lambda a, b, l, r="excellent": {"startSec": a, "endSec": b, "label": l, "rating": r}
E = lambda *s: {"subtasks": list(s)}
def test_cleanup_gap_overlap_duplicate():
g = normalise([S(5, 10, "B"), S(0, 4, "A"), S(0, 4, "A"), S(9, 12, "C")])
assert g == [(0, 4, "A", False), (4, 10, "B", False), (10, 12, "C", False)], g
def test_sort_colours_merged():
g = normalise([S(0, 2, "Pick up a single black screw from the table.")])
assert g[0][2] == "Pick up a single screw from the table."
def test_perfect():
g = E(S(0, 4, "A"), S(4, 10, "B", "failed"))
c = episode_counts(g, g)
assert (c["seg_tp"], c["seg_fp"], c["seg_fn"], c["b_err"], c["f_tp"], c["f_fp"], c["f_fn"]) == (2, 0, 0, 0, 1, 0, 0)
def test_shift_iou():
g = E(S(0, 4, "A"), S(4, 10, "B"))
c = episode_counts(g, E(S(0, 5, "A"), S(5, 10, "B"))) # IoU .8 and .83: both hits, 1 s boundary error
assert c["seg_tp"] == 2 and abs(c["b_err"] - 1) < 1e-9 and c["b_matched"] == 1
c = episode_counts(g, E(S(0, 6, "A"), S(6, 10, "B"))) # IoU .67: misses; boundary 2 s still within window
assert c["seg_tp"] == 0 and c["b_matched"] == 1
c = episode_counts(g, E(S(0, 7, "A"), S(7, 10, "B"))) # boundary 3 s off: unmatched
assert c["b_matched"] == 0
def test_boundary_precision():
g = E(S(0, 4, "A"), S(4, 10, "B"))
c = episode_counts(g, E(S(0, 5, "A"), S(5, 8, "B"), S(8, 10, "C"))) # 2 predicted boundaries (5, 8), 1 within 2 s of gold 4
assert (c["b_gold"], c["b_pred"], c["b_matched"]) == (1, 2, 1), c
c = episode_counts(g, E(S(0, 10, "A"))) # no predicted boundary
assert (c["b_pred"], c["b_matched"]) == (0, 0), c
def test_retry_attempts_not_merged_for_failure():
g = E(S(0, 4, "A", "failed"), S(4, 8, "A"), S(8, 10, "B"))
p = E(S(0, 4, "A", "failed"), S(4, 8, "A"), S(8, 10, "B", "failed"))
c = episode_counts(g, p)
assert (c["f_tp"], c["f_fp"], c["f_fn"]) == (1, 1, 0), c
assert c["seg_tp"] == 2 # A runs merged for segment F1
def test_empty_prediction():
c = episode_counts(E(S(0, 4, "A", "failed")), E())
assert c["seg_fn"] == 1 and c["f_fn"] == 1
def test_seg_tol():
g = E(S(0, 4, "A"), S(4, 10, "B"), S(10, 30, "A"))
p = E(S(0, 4.9, "A"), S(4.9, 10, "B"), S(10, 28.5, "A")) # A: end +0.9; B: start +0.9; last A: end -1.5
c = lambda t: (lambda d: (d["seg_tp"], d["seg_fp"], d["seg_fn"]))(episode_counts(g, p, seg_tol=t))
assert c(1.0) == (2, 1, 1), c(1.0)
assert c(1.5) == (3, 0, 0), c(1.5)
assert c(0.5) == (0, 3, 3), c(0.5)
# same-label merge: two A pieces form one run
assert c(1.0) == (lambda d: (d["seg_tp"], d["seg_fp"], d["seg_fn"]))(episode_counts(g, E(S(0, 2, "A"), S(2, 4.9, "A"), S(4.9, 10, "B"), S(10, 28.5, "A")), seg_tol=1.0))
if __name__ == "__main__":
import sys, traceback
failed = 0
for k, f in list(globals().items()):
if k.startswith("test_") and callable(f):
try:
f(); print("ok ", k)
except Exception:
failed += 1; print("FAIL", k); traceback.print_exc()
print(f"{failed} failed" if failed else "all tests passed")
sys.exit(1 if failed else 0)