Spaces:
Running on T4
Running on T4
Download tests/test_advanced_metrics_feedback.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 2.9 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tests/test_advanced_metrics_feedback.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/tests/test_advanced_metrics_feedback.py
-
curl -L -o test_advanced_metrics_feedback.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/tests/test_advanced_metrics_feedback.py
2.9 kB
| from pathlib import Path | |
| import numpy as np | |
| import yaml | |
| from baseball_batting.advanced_metrics import augment | |
| from baseball_batting.assessment import assess | |
| from baseball_batting.contracts import Event,Metric | |
| from baseball_batting.storage import atomic_json | |
| def test_independent_bat_velocity_and_contact_gates(tmp_path): | |
| n=5;times=np.arange(n)*.01;sweet=np.c_[2*times,np.zeros(n),np.ones(n)*4] | |
| np.savez_compressed(tmp_path/"bat.npz",sweet_spot_world_m=sweet) | |
| atomic_json(tmp_path/"bat3d_status.json",{"status":"complete","trajectory_path":"bat.npz","geometry_source":"calibrated_multiview"}) | |
| capture={"frame_count":n,"metadata":{},"physical_clock_known":True,"timing_source":{"kind":"unretimed"}, | |
| "frames":[{"physical_s":float(t),"flags":[]} for t in times]} | |
| curves={f"{side}_{joint}_projected_flexion_deg":np.zeros(n) for side in ("left","right") for joint in ("knee","elbow")} | |
| curves["body_pixels"]=np.full((n,133,2),np.nan) | |
| quality=yaml.safe_load((Path(__file__).resolve().parents[1]/"configs/quality.yaml").read_text()) | |
| events=[Event("contact_fixture","contact","confirmed",[2,2],[2],"independent_synthetic_fixture")] | |
| metrics=augment(tmp_path,curves,[],events,capture,quality,{"bat3d":True}) | |
| values={m.metric_id:m for m in metrics} | |
| assert np.isclose(values["bat_sweet_spot_speed_at_contact"].value,2) | |
| assert np.isclose(values["bat_attack_angle_at_contact"].value,0) | |
| assert values["bat_sweet_spot_speed_at_contact"].evidence_frames==[2] | |
| for event_state,enabled in (("unknown",True),("confirmed",False)): | |
| events[0].state=event_state | |
| metrics=augment(tmp_path,curves,[],events,capture,quality,{"bat3d":enabled}) | |
| assert next(m for m in metrics if m.metric_id=="bat_sweet_spot_speed_at_contact").value is None | |
| def test_educational_rule_requires_matched_repeated_context(): | |
| path=Path(__file__).resolve().parents[1]/"rules/coaching_rules.yaml" | |
| metrics=[Metric(side+"_knee_projected_flexion_at_contact","1.1",20.,"deg_projected","supported",["manual"],[2],"oriented_image_projection","unretimed") for side in ("left","right")] | |
| events=[Event("reviewed","contact","confirmed",[2,2],[2],"human_review","reviewed")] | |
| single=assess(metrics,{"swing_task":"tee"},events,[],path,"evidence_linked_experimental") | |
| assert all(f.research_exercise is None for f in single) | |
| trial={"metrics":[{"metric_id":m.metric_id,"value":m.value} for m in metrics]} | |
| repeated=assess(metrics,{"swing_task":"tee"},events,[trial,trial],path,"evidence_linked_experimental") | |
| exercises=[f.research_exercise for f in repeated if f.research_exercise] | |
| assert len(exercises)==1 and exercises[0]["expected_benefit"]=="not_established" | |
| different=assess(metrics,{"swing_task":"live_pitch"},events,[trial,trial],path,"evidence_linked_experimental") | |
| assert all(f.research_exercise is None for f in different) | |