baseball-engine / tools /annotation /export_dataset.py
github-actions[bot]
Automated Deploy: a19ede424a80b7b8d7f6978585141d1bb45de69c
326d3cc
Raw History Blame Contribute Delete
1.55 kB
"""Export reviewed image labels; participant identity groups must not leak across splits."""
import argparse
import json
from pathlib import Path
def grouped_split(records):
groups = sorted({r["player_id"] for r in records})
if len(groups) < 3:
raise ValueError("At least three distinct athletes are needed for train/validation/test separation")
# Deterministic grouped split. Samples from an athlete never cross partitions.
assignment = {g: ("test" if i == len(groups)-1 else "validation" if i == len(groups)-2 else "train")
for i,g in enumerate(groups)}
return [{**r, "split": assignment[r["player_id"]]} for r in records]
if __name__ == "__main__":
p = argparse.ArgumentParser()
p.add_argument("index", help="JSON list of reviewed annotation files, player IDs and session IDs")
p.add_argument("--output", required=True)
args = p.parse_args()
records = json.loads(Path(args.index).read_text())
for r in records:
if not r.get("player_id") or not r.get("session_id"):
raise ValueError("Dataset records require explicit athlete/session groups")
labels = json.loads(Path(r["annotations_path"]).read_text())
if not labels.get("annotator") or not labels.get("input_sha256"):
raise ValueError("Reviewed label provenance is missing")
r["input_sha256"] = labels["input_sha256"]
r["reviewed_frame_count"] = len(labels.get("frames", {}))
Path(args.output).write_text(json.dumps(grouped_split(records), indent=2))