#!/usr/bin/env python3 """Attach recorded experiment intent to deliveries (standard sections 5.2 and 7.2). During a phone/academy session the operator keeps a sheet: ball number -> who bowled, who batted, and the instruction (target line, length, delivery). This maps ball 1, 2, 3 ... to the deliveries the pipeline found in that video, in time order, and stores them as reviewer labels (annotator 'intent_sheet'), so "intended vs actual" works. python code/import_intent.py --source SRC_YT_0009 --csv datasets/intent/session_2026-10-20.csv add --dry-run to check the mapping first. Template: datasets/intent/intent_template.csv""" import argparse import csv import json import sys from pathlib import Path sys.path.insert(0, str(Path(__file__).resolve().parent)) from common import absp, load_config, load_ontology, read_json # noqa: E402 from review import review_path, stamp, validate # noqa: E402 FIELDS = ["bowler_id", "batter_id", "target_line", "target_length", "intended_delivery", "intended_pace_band", "bowling_style", "batter_handedness"] def main(): ap = argparse.ArgumentParser() ap.add_argument("--source", required=True) ap.add_argument("--csv", required=True) ap.add_argument("--dry-run", action="store_true") a = ap.parse_args() cfg, onto = load_config(), load_ontology() R = cfg["_paths"]["results"] src = next((s for s in read_json(R / "registry" / "sources.json") if s["source_id"] == a.source), None) if not src: raise SystemExit(f"{a.source} not found in results/registry/sources.json") dels = sorted((read_json(p) for p in (R / "deliveries").glob(f"{src['session_id']}_D*/measurements.json")), key=lambda m: m["window"]["start_s"]) rows = list(csv.DictReader(open(absp(a.csv) if not Path(a.csv).is_absolute() else a.csv))) print(f"{len(rows)} balls on the sheet, {len(dels)} deliveries found in {a.source}") if len(rows) != len(dels): print(" ! counts differ: check for missed or extra deliveries before importing (use --dry-run)") recs, errs = [], [] for row, d in zip(rows, dels): for f in FIELDS: v = (row.get(f) or "").strip() if not v: continue r = {"kind": "label", "delivery_id": d["delivery_id"], "field": f, "value": v, "annotator_id": "intent_sheet", "note": f"ball {row.get('ball_no')}"} e = validate(r, onto) (errs.append(f"ball {row.get('ball_no')} {f}: {e}") if e else recs.append(r)) print(f" ball {row.get('ball_no', '?'):>3} -> {d['delivery_id']} release {d['window']['start_s'] + d['events'].get('ball_release', {}).get('t_clip', 0):.1f} s") if errs: raise SystemExit("not imported:\n " + "\n ".join(errs)) if a.dry_run: print(f"dry run: {len(recs)} labels would be written") return p = review_path(R); p.parent.mkdir(parents=True, exist_ok=True) with open(p, "a") as f: for r in recs: f.write(json.dumps(stamp(r, onto), ensure_ascii=False) + "\n") print(f"wrote {len(recs)} intent labels. Rebuild: python code/run_all.py --stages sft,tables,release,evaluate,web") if __name__ == "__main__": main()