Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tests/test_scoring.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 6.14 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_scoring.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tests/test_scoring.py
-
curl -L -o test_scoring.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_scoring.py
6.14 kB
| """The 0-100 mechanics score: pure maths on the judged measurements, no model, fully deterministic.""" | |
| import math | |
| import pytest | |
| from ai_coach.input_builder import build_coach_input | |
| from ai_coach.schemas import CaptureInput, MetricInput | |
| from ai_coach.scoring import compute_score, metric_score, rep_score | |
| from .helpers import load_example, make_input, measurements | |
| def metric(key="stride_pct_height", per_rep=(47.0,) * 6, band=(43.0, 51.0), confidence="high", range_confidence="high", **kw): | |
| inside = all(band[0] <= v <= band[1] for v in per_rep) | |
| return MetricInput( | |
| key=key, | |
| label=key, | |
| value=sorted(per_rep)[len(per_rep) // 2], | |
| unit="%", | |
| confidence=confidence, | |
| band=band, | |
| zone="in_range" if inside else "outside_typical_range", | |
| n_reps=len(per_rep), | |
| per_rep=list(per_rep), | |
| range_status="calibrated", | |
| range_confidence=range_confidence, | |
| **kw, | |
| ) | |
| # band 43-51 is 8 wide: 4 outside = 75, 8 outside = 50, 16 outside = 0 | |
| def test_a_rep_is_perfect_inside_the_range_and_falls_to_zero_two_widths_away(value, expected): | |
| assert rep_score(value, 43.0, 51.0, 2.0) == pytest.approx(expected) | |
| def test_a_metric_averages_its_reps_so_a_repeated_fault_costs_more_than_one_bad_rep(): | |
| one_bad = metric(per_rep=(47, 47, 47, 47, 47, 55)) | |
| always_bad = metric(per_rep=(55,) * 6) | |
| assert metric_score(one_bad, 2.0) == pytest.approx((500 + 75) / 6) | |
| assert metric_score(always_bad, 2.0) == pytest.approx(75) | |
| def test_everything_in_range_scores_100(): | |
| ci = make_input(metrics=[metric(), metric("com_drift_pct_height", (-25.0,) * 6, (-30.0, -19.0))]) | |
| score = compute_score(ci) | |
| assert score.value == 100 and score.in_range == score.checked == 2 and score.band == "Very close to typical" | |
| def test_the_score_is_deterministic(): | |
| ci = build_coach_input(load_example("hitting_stride_and_lunging_240fps")) | |
| assert compute_score(ci) == compute_score(ci) | |
| def test_weights_in_the_breakdown_add_up_to_one_and_lower_body_counts_more(): | |
| ci = make_input( | |
| metrics=[metric("stride_pct_height", (70.0,) * 6), metric("head_movement_pct_height", (47.0,) * 6)], | |
| ) | |
| score = compute_score(ci) | |
| assert math.isclose(sum(i.weight for i in score.breakdown), 1.0, abs_tol=0.01) | |
| by_key = {i.metric_key: i for i in score.breakdown} | |
| assert by_key["stride_pct_height"].weight > by_key["head_movement_pct_height"].weight # lower body 1.5 vs upper 1.0 | |
| assert by_key["stride_pct_height"].score == 0 and by_key["head_movement_pct_height"].score == 100 | |
| assert score.value == 40 # (1.5 x 0 + 1.0 x 100) / 2.5 | |
| def test_estimates_and_reference_only_measurements_never_count(): | |
| est = metric("com_drift_pct_height", (-25.0,) * 6, (-30.0, -19.0), confidence="est") | |
| ref = metric("head_movement_pct_height").model_copy(update={"zone": "reference_only"}) | |
| ci = make_input(metrics=[metric(), est, ref]) | |
| score = compute_score(ci) | |
| assert score.value is None and score.reason_code == "not_enough_measurements" and score.checked == 1 | |
| def test_a_failed_capture_has_no_score_and_says_why(): | |
| ci = make_input(capture=CaptureInput(ok=False, reasons=["too_dark"])) | |
| score = compute_score(ci) | |
| assert score.value is None and score.reason_code == "capture_not_ok" and "record again" in score.reason | |
| def test_rough_high_school_ranges_lower_the_confidence_not_the_score(): | |
| two = [metric(), metric("com_drift_pct_height", (-25.0,) * 6, (-30.0, -19.0))] | |
| rough = [m.model_copy(update={"range_confidence": "low"}) for m in two] | |
| assert compute_score(make_input(metrics=two)).confidence == "medium" # two metrics: never "high" | |
| low = compute_score(make_input(metrics=rough)) | |
| assert low.confidence == "low" and low.value == compute_score(make_input(metrics=two)).value | |
| def test_high_confidence_needs_three_metrics_five_reps_and_trusted_ranges(): | |
| three = [ | |
| metric(), | |
| metric("com_drift_pct_height", (-25.0,) * 6, (-30.0, -19.0)), | |
| metric("load_tempo_s", (0.5,) * 6, (0.4, 0.7)), | |
| ] | |
| assert compute_score(make_input(metrics=three)).confidence == "high" | |
| few_reps = [m.model_copy(update={"n_reps": 3, "per_rep": m.per_rep[:3]}) for m in three] | |
| assert compute_score(make_input(metrics=few_reps)).confidence == "medium" | |
| def test_basis_names_what_the_score_rests_on(): | |
| ci = build_coach_input(load_example("hitting_stride_and_lunging_240fps")) | |
| score = compute_score(ci) | |
| assert score.basis.startswith(f"{score.checked} of ") and score.basis.endswith("swings") | |
| pitch = compute_score(build_coach_input(load_example("pitching_short_stride_240fps"))) | |
| assert pitch.basis.endswith("pitches") | |
| def test_a_real_session_with_two_faults_scores_below_a_clean_one(): | |
| faulty = compute_score(build_coach_input(load_example("hitting_stride_and_lunging_240fps"))) | |
| clean = compute_score(build_coach_input(load_example("hitting_ok_240fps"))) | |
| assert faulty.value is not None and clean.value is not None and faulty.value < clean.value | |
| def test_the_score_never_leaves_0_to_100_for_any_example(): | |
| from .helpers import example_names | |
| for name in example_names(): | |
| score = compute_score(build_coach_input(load_example(name))) | |
| assert score.value is None or 0 <= score.value <= 100, name | |
| assert (score.value is None) == (score.reason_code is not None), name | |
| def test_hitting_has_thin_evidence_and_the_score_says_so(): | |
| """Only stride and body drift have calibrated hitting ranges today, so a hitting score rests on at most two measurements.""" | |
| session = measurements( | |
| series={ | |
| "stride_pct_height": ("%", "high", [47.0] * 6), | |
| "com_drift_pct_height": ("%", "medium", [-25.0] * 6), | |
| "foot_plant_to_contact_ms": ("ms", "medium", [120.0] * 6), | |
| }, | |
| level="college", | |
| ) | |
| score = compute_score(build_coach_input(session)) | |
| assert score.checked == 2 and score.confidence != "high" | |