Spaces:
Running on T4
Running on T4
Download tools/evaluation/evaluate_events.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.53 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tools/evaluation/evaluate_events.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/tools/evaluation/evaluate_events.py
-
curl -L -o evaluate_events.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tools/evaluation/evaluate_events.py
2.53 kB
| """Interval-aware event errors; absent/missed/extra/abstained cases stay separate.""" | |
| import argparse | |
| import json | |
| from pathlib import Path | |
| import numpy as np | |
| def evaluate(predicted, reference, physical_times=None): | |
| rows = [] | |
| for r in reference: | |
| candidates = [e for e in predicted if e["type"] == r["type"] and e["state"] in {"confirmed", "candidate"} and e.get("frame_interval")] | |
| if r["state"] == "absent": | |
| rows.append({"type": r["type"], "status": "extra" if candidates else "correct_absence"}) | |
| continue | |
| if r["state"] != "confirmed": | |
| rows.append({"type": r["type"], "status": "reference_unknown"}) | |
| continue | |
| if not candidates: | |
| rows.append({"type": r["type"], "status": "missed_or_abstained"}) | |
| continue | |
| if len(candidates) != 1: | |
| rows.append({"type": r["type"], "status": "multiple_predictions"}) | |
| continue | |
| p = candidates[0]["frame_interval"] | |
| a, b = r["frame_interval"] | |
| midpoint = sum(p)/2 | |
| error = max(a-midpoint, midpoint-b, 0) | |
| row = {"type": r["type"], "status": "matched", "absolute_error_frames": error, | |
| "predicted_interval_width_frames": p[1]-p[0], "reference_interval_width_frames": b-a} | |
| if physical_times is not None: | |
| t = np.asarray(physical_times, float) | |
| pred_time = float(np.interp(midpoint, np.arange(len(t)), t)) | |
| if np.isfinite(t[[a,b]]).all() and np.isfinite(pred_time): | |
| row["absolute_error_ms"] = 1000*max(t[a]-pred_time, pred_time-t[b], 0) | |
| rows.append(row) | |
| known = {r["type"] for r in reference} | |
| rows.extend({"type": e["type"], "status": "extra"} for e in predicted | |
| if e["type"] not in known and e["state"] in {"confirmed", "candidate"}) | |
| return {"rows": rows, "counts": {s: sum(r["status"] == s for r in rows) for s in sorted({r["status"] for r in rows})}, | |
| "validation": "evaluation output only; thresholds must be preregistered"} | |
| if __name__ == "__main__": | |
| p = argparse.ArgumentParser() | |
| p.add_argument("result"); p.add_argument("reference"); p.add_argument("--output", required=True) | |
| args = p.parse_args() | |
| result = json.loads(Path(args.result).read_text()) | |
| refs = json.loads(Path(args.reference).read_text()) | |
| report = evaluate(result["events"], refs["events"], [f["physical_s"] for f in result["capture"]["frames"]]) | |
| Path(args.output).write_text(json.dumps(report, indent=2)) | |