Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tools/build_benchmarks.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 4.84 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tools/build_benchmarks.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tools/build_benchmarks.py
-
curl -L -o build_benchmarks.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tools/build_benchmarks.py
4.84 kB
| """Regenerate src/ai_coach/knowledge/benchmarks.yaml from the OBP contract v1 benchmark aggregates. | |
| usage: python tools/build_benchmarks.py [BENCHMARKS_JSON] [--check] | |
| BENCHMARKS_JSON defaults to ../data_ref/calibration/contract_v1/contract_v1_benchmarks.json (written by | |
| data_ref/scripts/compute_contract_v1_benchmarks.py). Intervals per metric, batting hand and OBP level: | |
| on_target = [P25, P75], satisfactory = [P10, P90]. Values keep the contract's raw sign convention (separation is NOT | |
| mirrored for left-handed hitters), so each hand has its own cells. Cells with fewer than MIN_ATHLETES athletes are | |
| dropped; the report then falls back to the same hand's college cell and says so in data_quality. | |
| --check exits 1 when the committed YAML differs from what would be generated. | |
| """ | |
| from __future__ import annotations | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import yaml | |
| ROOT = Path(__file__).resolve().parents[1] | |
| DEFAULT_JSON = ROOT.parent / "data_ref" / "calibration" / "contract_v1" / "contract_v1_benchmarks.json" | |
| OUT = ROOT / "src" / "ai_coach" / "knowledge" / "benchmarks.yaml" | |
| VERSION = "2026-10-31.1" | |
| MIN_ATHLETES = 5 | |
| HANDS = ("right", "left") | |
| OBP_LEVELS = ("high_school", "college", "pro") | |
| LEVEL_NAMES = {"high_school": "High school", "college": "College", "pro": "Professional"} | |
| SOURCE_REFERENCE = "OpenBiomechanics Project hitting dataset (Driveline), percentiles P25-P75 and P10-P90" | |
| # App athlete level -> OBP population. Unknown levels use high school (the report adds level_assumed). | |
| LEVEL_MAP = {"high_school": "high_school", "college": "college", "adult": "college", "youth": "high_school", | |
| "unknown": "high_school"} | |
| FALLBACK_LEVEL = "college" | |
| UNITS = {"hip_shoulder_separation_at_foot_down": "deg", "hip_shoulder_separation_at_contact": "deg", | |
| "stride_relative_length": "cm", "torso_inclination_at_stride_start": "deg", | |
| "hip_shoulder_separation_at_stride_start": "deg", "pelvis_rotation_to_contact": "deg", | |
| "lead_knee_extension_to_contact": "deg", "torso_inclination_at_contact": "deg", | |
| "shoulder_rotation_after_contact": "deg"} | |
| EXTENDED_JSON = ROOT.parent / "data_ref" / "calibration" / "contract_v1_1" / "contract_v1_1_benchmarks.json" | |
| def build(data: dict) -> dict: | |
| data = {**data, "metrics": {**data["metrics"], **json.loads(EXTENDED_JSON.read_text())["metrics"]}} | |
| metrics: dict = {} | |
| for metric_id, unit in UNITS.items(): | |
| cells = data["metrics"][metric_id] | |
| out: dict = {"unit": unit, "cells": {}} | |
| for hand in HANDS: | |
| for level in OBP_LEVELS: | |
| cell = cells.get(f"{hand}/{level}") or {} | |
| n = int(cell.get("n_athletes", 0)) | |
| if n < MIN_ATHLETES or any(f"p{p}" not in cell for p in (10, 25, 75, 90)): | |
| continue | |
| out["cells"][f"{hand}/{level}"] = { | |
| "benchmark_id": f"obp-contract-v1.{metric_id}.{hand}.{level}", | |
| "population": f"{LEVEL_NAMES[level]} {hand}-handed hitters ({n} athletes)", | |
| "n_athletes": n, | |
| "n_swings": int(cell["n_swings"]), | |
| "on_target": {"lower": float(cell["p25"]), "upper": float(cell["p75"])}, | |
| "satisfactory": {"lower": float(cell["p10"]), "upper": float(cell["p90"])}, | |
| "median": float(cell["p50"]), | |
| } | |
| metrics[metric_id] = out | |
| return { | |
| "version": VERSION, | |
| "kind": "reference_interval", | |
| "source_reference": SOURCE_REFERENCE, | |
| "sign_convention": "raw contract values: wrap180(shoulder_heading - hip_heading), not mirrored by batting hand", | |
| "front_foot_down_definition": "first touch (OBP fp_10_time, 10% bodyweight)", | |
| "min_athletes": MIN_ATHLETES, | |
| "level_map": LEVEL_MAP, | |
| "fallback_level": FALLBACK_LEVEL, | |
| "metrics": metrics, | |
| } | |
| HEADER = ( | |
| "# GENERATED by tools/build_benchmarks.py from data_ref/calibration/contract_v1 and contract_v1_1.\n" | |
| "# Do not edit by hand: change the generator or the source data and regenerate.\n" | |
| ) | |
| def render(data: dict) -> str: | |
| return HEADER + yaml.safe_dump(build(data), sort_keys=False, width=120) | |
| def main(argv: list[str]) -> int: | |
| check = "--check" in argv | |
| args = [a for a in argv if a != "--check"] | |
| source = Path(args[0]) if args else DEFAULT_JSON | |
| text = render(json.loads(source.read_text(encoding="utf-8"))) | |
| if check: | |
| same = OUT.is_file() and OUT.read_text(encoding="utf-8") == text | |
| print("benchmarks.yaml is up to date" if same else "benchmarks.yaml is stale: run tools/build_benchmarks.py") | |
| return 0 if same else 1 | |
| OUT.write_text(text, encoding="utf-8") | |
| print(f"wrote {OUT}") | |
| return 0 | |
| if __name__ == "__main__": | |
| raise SystemExit(main(sys.argv[1:])) | |