Spaces:
Running on T4
Running on T4
Download AI-coach-agent/tests/test_pose_spec.py from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 6.91 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_pose_spec.py
- Command line
-
hf download hf://spaces/modventures/baseball-engine/AI-coach-agent/tests/test_pose_spec.py
-
curl -L -o test_pose_spec.py https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/AI-coach-agent/tests/test_pose_spec.py
6.91 kB
| """Pose-data contract: schema and example are in sync, the example is valid, the checker catches mistakes.""" | |
| import copy | |
| import json | |
| import sys | |
| from pathlib import Path | |
| import pytest | |
| TOOLS = Path(__file__).resolve().parents[1] / "tools" | |
| sys.path.insert(0, str(TOOLS)) | |
| import build_pose_spec as spec # noqa: E402 | |
| import check_pose_json as checker # noqa: E402 | |
| DOCS = Path(__file__).resolve().parents[1] / "docs" | |
| def example(): | |
| return json.loads((DOCS / "examples" / "pose_track_example.json").read_text()) | |
| def codes(data, level=None): | |
| return {i.code for i in checker.check(data) if level is None or i.level == level} | |
| def test_published_files_match_the_generator(): | |
| """If this fails run: python tools/build_pose_spec.py (the ML engineer reads these files).""" | |
| assert (DOCS / "pose_track.schema.json").read_text() == json.dumps(spec.build_schema(), indent=2, sort_keys=True) + "\n" | |
| assert (DOCS / "examples" / "pose_track_example.json").read_text() == json.dumps( | |
| spec.build_example(), separators=(",", ":") | |
| ) + "\n" | |
| def test_example_has_no_errors_and_only_the_expected_warning(example): | |
| assert codes(example, "error") == set() and codes(example, "warning") == {"few_reps"} | |
| def test_example_models_a_short_occlusion_correctly(example): | |
| wrist = spec.COCO17.index("left_wrist") | |
| assert example["frames"]["kp"][41][wrist] is None and example["frames"]["conf"][41][wrist] == 0 | |
| def test_hitting_and_pitching_event_lists_are_the_documented_ones(): | |
| assert spec.HITTING_EVENTS == [ | |
| "stance_set", | |
| "load_start", | |
| "stride", | |
| "foot_contact", | |
| "foot_plant", | |
| "swing_start", | |
| "contact", | |
| "finish", | |
| ] | |
| assert spec.PITCHING_EVENTS == ["first_move", "leg_lift", "hand_break", "foot_contact", "foot_plant", "release", "finish"] | |
| assert len(spec.COCO17) == 17 and len(spec.FOOT6) == 6 | |
| def mutate(example, fn): | |
| data = copy.deepcopy(example) | |
| fn(data) | |
| return data | |
| def test_checker_catches_mistakes(example, name, fn, code, level): | |
| assert code in codes(mutate(example, fn), level), name | |
| def test_low_joint_coverage_is_reported_per_joint(example): | |
| ankle = spec.COCO17.index("right_ankle") | |
| def drop(d): | |
| for i in range(len(d["frames"]["conf"])): | |
| d["frames"]["conf"][i][ankle] = 0.0 | |
| d["frames"]["kp"][i][ankle] = None | |
| issues = [i for i in checker.check(mutate(example, drop)) if i.code == "low_joint_coverage"] | |
| assert len(issues) == 1 and "right_ankle" in issues[0].detail | |
| def test_nan_and_infinity_tokens_are_rejected_when_loading(tmp_path): | |
| path = tmp_path / "bad.json" | |
| path.write_text('{"frames": {"t_s": [NaN]}}') | |
| with pytest.raises(ValueError, match="not valid JSON"): | |
| checker.load(path) | |
| def test_cli_exit_codes(tmp_path, capsys): | |
| good = DOCS / "examples" / "pose_track_example.json" | |
| assert checker.main([str(good)]) == 0 | |
| assert checker.main([str(good), "--strict"]) == 1 # the example has one warning | |
| broken = tmp_path / "broken.json" | |
| broken.write_text("{not json") | |
| assert checker.main([str(broken)]) == 1 | |
| assert "error" in capsys.readouterr().out | |