Spaces:
Running on T4
Running on T4
File size: 2,525 Bytes
326d3cc | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 | """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))
|