baseball-engine / AI-coach-agent /tools /make_examples.py
github-actions[bot]
Automated Deploy: f4f01388c7b26e818cffbd64414eac11381a7891
35e80ff
Raw History Blame Contribute Delete
6.23 kB
"""Generate the synthetic example sessions in examples/inputs/ (measurements only, contract v2).
Pure Python, deterministic, no dependency on any pipeline code. Typical values sit inside the agent's own typical ranges; each
scenario moves one or two metrics so the coach has something to find. `real_hitting_30fps_low_confidence.json` is a real clip's
output (converted to v2) and is not generated here.
python tools/make_examples.py
"""
import json
import random
from pathlib import Path
OUT = Path(__file__).resolve().parents[1] / "examples" / "inputs"
# key: (unit, typical value, rep-to-rep sigma, confidence at >=120 fps, is_timing)
# Typical values sit inside the agent's calibrated ranges (college): stride is foot-to-foot at foot plant.
HITTING = {
"stride_pct_height": ("%", 49.0, 1.2, "high", False),
"foot_plant_to_contact_ms": ("ms", 128.0, 6.0, "medium", True),
"load_tempo_s": ("s", 1.0, 0.05, "high", True),
"load_launch_ratio": ("ratio", 8.0, 0.5, "medium", True),
"head_movement_pct_height": ("%", 2.2, 0.4, "high", False),
"lead_knee_angle_contact_deg": ("deg", 168.0, 2.0, "medium", False),
"lateral_trunk_tilt_contact_deg": ("deg", 14.0, 2.0, "medium", False),
"hip_shoulder_separation_foot_plant_deg": ("deg", 41.0, 2.0, "est", False),
"com_drift_pct_height": ("%", -24.5, 1.5, "medium", False),
}
PITCHING = {
"stride_pct_height": ("%", 74.0, 1.5, "high", False),
"lead_knee_flexion_fp_deg": ("deg", 46.0, 2.5, "medium", False),
"lead_knee_flexion_release_deg": ("deg", 41.0, 3.0, "medium", False),
"lead_knee_block_deg": ("deg", 5.0, 2.0, "medium", False),
"trunk_forward_tilt_release_deg": ("deg", 31.0, 1.5, "medium", False),
"trunk_lateral_tilt_release_deg": ("deg", 27.0, 2.0, "medium", False),
"arm_slot_deg": ("deg", 59.0, 2.0, "medium", False),
"leg_lift_to_fp_ms": ("ms", 640.0, 25.0, "high", True),
"fp_to_release_ms": ("ms", 134.0, 6.0, "high", True),
"head_stability_pct_height": ("%", 22.0, 1.5, "high", False),
"hip_shoulder_sep_fp": ("deg", 45.0, 3.0, "est", False),
}
EVENTS = {
"hitting": {
"stance_set": 0,
"load_start": 130,
"stride": 300,
"foot_plant": 560,
"swing_start": 610,
"contact": 770,
"finish": 1250,
},
"pitching": {
"first_move": 0,
"leg_lift": 640,
"hand_break": 790,
"foot_plant": 1240,
"release": 1390,
"finish": 1800,
},
}
def build(name, discipline, n_reps, fps, shifts=None, sigmas=None, nulls=(), capture=None, level="hs_varsity", seed=7):
"""One session: typical values, plus `shifts` (add to the typical value) and `sigmas` (replace rep-to-rep spread)."""
rng = random.Random(seed)
table = HITTING if discipline == "hitting" else PITCHING
reps = []
for i in range(n_reps):
metrics = []
for key, (unit, base, sigma, conf, timing) in table.items():
if key in nulls or (timing and fps < 60):
note = "timing needs >= 60 fps" if timing and fps < 60 else "not computed: event not found"
metrics.append({"key": key, "value": None, "unit": unit, "confidence": "est", "note": note})
continue
value = base + (shifts or {}).get(key, 0.0) + rng.gauss(0, (sigmas or {}).get(key, sigma))
if fps < 120 and conf == "high" and timing:
conf = "medium"
if fps < 60 and conf != "est":
conf = "medium" if conf == "high" else "est"
metrics.append({"key": key, "value": round(value, 1), "unit": unit, "confidence": conf})
reps.append(
{
"rep": i + 1,
"events_ms": EVENTS[discipline],
"metrics": metrics,
"quality": {"pose_conf": round(rng.uniform(0.78, 0.9), 2), "blur": round(rng.uniform(0.08, 0.2), 2)},
}
)
session = {
"schema_version": "2.0",
"discipline": discipline,
"view": "face_on" if discipline == "hitting" else "open_side",
"fps": float(fps),
"level": level,
"capture": capture or {"ok": True, "reasons": []},
"reps": reps,
}
if discipline == "pitching": # pitch counts come from the app's pitch log, not from the pose pipeline
session["arm_care"] = {
"known": True,
"pitch_count_today": n_reps,
"daily_max": 95,
"next_eligible_date": "2026-10-11",
"alerts": [],
}
(OUT / f"{name}.json").write_text(json.dumps(session, indent=1) + "\n")
print(f"{name}: {n_reps} reps at {fps} fps")
if __name__ == "__main__":
OUT.mkdir(parents=True, exist_ok=True)
build("hitting_ok_240fps", "hitting", 6, 240, level="college")
build("hitting_overstride_240fps", "hitting", 6, 240, level="college", shifts={"stride_pct_height": 16.0})
build(
"hitting_stride_and_lunging_240fps",
"hitting",
6,
240,
level="college",
shifts={"stride_pct_height": 11.0, "com_drift_pct_height": 13.0},
)
build("hitting_lunging_120fps", "hitting", 6, 120, level="college", shifts={"com_drift_pct_height": 10.0})
build("hitting_late_launch_hs_240fps", "hitting", 6, 240, shifts={"foot_plant_to_contact_ms": -45.0})
build("hitting_30fps_geometry_only", "hitting", 5, 30, level="college", capture={"ok": True, "reasons": ["very_low_fps"]})
build("hitting_one_bad_rep_240fps", "hitting", 6, 240, level="college", sigmas={"stride_pct_height": 7.0}, seed=3)
build("pitching_ok_240fps", "pitching", 8, 240, level="college")
build("pitching_short_stride_240fps", "pitching", 8, 240, level="college", shifts={"stride_pct_height": -18.0})
build(
"pitching_short_stride_soft_knee_240fps",
"pitching",
8,
240,
level="college",
shifts={"stride_pct_height": -13.0, "lead_knee_flexion_release_deg": 25.0},
)
build("pitching_short_stride_hs_rough_range_240fps", "pitching", 8, 240, shifts={"stride_pct_height": -14.0})
build("pitching_slot_inconsistent_240fps", "pitching", 8, 240, level="college", sigmas={"arm_slot_deg": 14.0})