Spaces:
Running on T4
Running on T4
|
Download backend/docs/reference/ml_contract_v1.md from modventures/baseball-engine: direct link, hf CLI and curl.
- Browser
- Download file 10.7 kB
-
https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/docs/reference/ml_contract_v1.md
- Command line
-
hf download hf://spaces/modventures/baseball-engine/backend/docs/reference/ml_contract_v1.md
-
curl -L -o ml_contract_v1.md https://huggingface.co/spaces/modventures/baseball-engine/resolve/main/backend/docs/reference/ml_contract_v1.md
10.7 kB
| # ML engineer's swing analysis payload contract, version 1.0 | |
| This is the source text the ML engineer shared. The app sends this payload, unchanged, to `POST /v1/sessions/{id}/swing-report`. | |
| ## Delivery | |
| - Send one JSON object per completed swing analysis. | |
| - Identify an immutable result with `analysis_id` and `analysis_revision`. | |
| - Increment `analysis_revision` when points, event selections or calculated results change. | |
| - `swing_id` stays stable across revisions of the same recorded swing. | |
| - Repeated deliveries of the same analysis id and revision are duplicates. An older revision must not replace newer coaching results. | |
| - A completed analysis can contain unavailable measurements. | |
| ## Example | |
| ```json | |
| { | |
| "schema_version": "1.0", | |
| "analysis_id": "analysis_001", | |
| "analysis_revision": 1, | |
| "swing_id": "swing_001", | |
| "status": "completed", | |
| "athlete": { "athlete_id": "player_001", "batting_hand": "right", "lead_foot": "left", "age_years": null, "height_cm": null }, | |
| "context": { "sport": "baseball", "practice_type": "tee" }, | |
| "measurement_definitions_version": "1.0", | |
| "events": { | |
| "stride_start": { "video_time_s": 1.0, "capture_time_s": 1.0, "status": "available", "source": "manual_review" }, | |
| "front_foot_down": { "video_time_s": 1.2, "capture_time_s": 1.2, "status": "available", "source": "manual_review" }, | |
| "contact": { "video_time_s": null, "capture_time_s": null, "status": "unavailable", "source": null, "reasons": ["contact_not_verified"] } | |
| }, | |
| "measurements": [ | |
| { "metric_id": "hip_shoulder_separation_at_foot_down", "group": "stride", "value": 28, "unit": "deg", | |
| "event_id": "front_foot_down", "reference": "signed_horizontal_shoulder_minus_hip_heading", | |
| "status": "available", "confidence": null, "uses_interpolated_input": false, "reasons": [], "benchmark": null }, | |
| { "metric_id": "stride_relative_length", "group": "stride", "value": 24, "unit": "cm", | |
| "start_event_id": "stride_start", "end_event_id": "front_foot_down", | |
| "reference": "lead_ankle_displacement_relative_to_rear_ankle", | |
| "status": "limited", "confidence": null, "uses_interpolated_input": false, "reasons": ["scale_is_model_estimated"], "benchmark": null }, | |
| { "metric_id": "hip_shoulder_separation_at_contact", "group": "contact_follow_through", "value": null, "unit": "deg", | |
| "event_id": "contact", "reference": "signed_horizontal_shoulder_minus_hip_heading", | |
| "status": "unavailable", "confidence": null, "uses_interpolated_input": false, "reasons": ["contact_not_verified"], "benchmark": null } | |
| ] | |
| } | |
| ``` | |
| ## Types | |
| ```typescript | |
| export type SchemaVersion = "1.0"; | |
| export type MeasurementDefinitionsVersion = "1.0"; | |
| export type AnalysisStatus = "completed"; | |
| export type Sport = "baseball"; | |
| export type Side = "left" | "right"; | |
| export type PracticeType = "tee" | "front_toss" | "side_toss" | "pitching_machine" | "live_pitching" | "game" | "dry_swing" | "unknown"; | |
| export type MeasurementGroup = "balance_load" | "stride" | "power_move" | "contact_follow_through"; | |
| export type ResultStatus = "available" | "limited" | "unavailable"; | |
| export type MeasurementUnit = "deg" | "cm"; | |
| export type EventId = "stride_start" | "front_foot_down" | "contact"; | |
| export type EventSource = "manual_review" | "pose_estimated" | "bat_ball_detector"; | |
| export type MetricId = "hip_shoulder_separation_at_foot_down" | "stride_relative_length" | "hip_shoulder_separation_at_contact"; | |
| export type MeasurementReference = "signed_horizontal_shoulder_minus_hip_heading" | "lead_ankle_displacement_relative_to_rear_ankle"; | |
| export type ReasonCode = | |
| | "contact_not_verified" | "contact_not_applicable" | "event_not_detected" | "event_timing_uncertain" | |
| | "required_event_unavailable" | "required_event_limited" | "lead_foot_unknown" | "required_landmarks_unusable" | |
| | "ground_plane_unknown" | "scale_is_model_estimated" | "scale_unknown" | "excessive_interpolation" | |
| | "occlusion_unresolved" | "temporal_inconsistency"; | |
| export type NonEmptyReasons = [ReasonCode, ...ReasonCode[]]; | |
| export interface Athlete { athlete_id: string; batting_hand: Side | null; lead_foot: Side | null; age_years: number | null; height_cm: number | null; } | |
| export interface SwingContext { sport: Sport; practice_type: PracticeType; } | |
| export interface EventTiming { video_time_s: number; capture_time_s: number | null; source: EventSource; } | |
| export type SwingEvent = | |
| | (EventTiming & { status: "available"; reasons?: [] }) | |
| | (EventTiming & { status: "limited"; reasons: NonEmptyReasons }) | |
| | { status: "unavailable"; video_time_s: null; capture_time_s: null; source: null; reasons: NonEmptyReasons }; | |
| export interface ApprovedBenchmark { | |
| benchmark_id: string; version: string; kind: "reference_interval"; | |
| lower: number | null; upper: number | null; unit: MeasurementUnit; source_reference: string; | |
| } | |
| export type MeasurementResult = | |
| | { status: "available"; value: number; confidence: number | null; reasons: [] } | |
| | { status: "limited"; value: number; confidence: number | null; reasons: NonEmptyReasons } | |
| | { status: "unavailable"; value: null; confidence: null; reasons: NonEmptyReasons }; | |
| export type MetricSpecification = | |
| | { metric_id: "hip_shoulder_separation_at_foot_down"; group: "stride"; unit: "deg"; | |
| reference: "signed_horizontal_shoulder_minus_hip_heading"; event_id: "front_foot_down"; start_event_id?: never; end_event_id?: never; } | |
| | { metric_id: "stride_relative_length"; group: "stride"; unit: "cm"; | |
| reference: "lead_ankle_displacement_relative_to_rear_ankle"; event_id?: never; start_event_id: "stride_start"; end_event_id: "front_foot_down"; } | |
| | { metric_id: "hip_shoulder_separation_at_contact"; group: "contact_follow_through"; unit: "deg"; | |
| reference: "signed_horizontal_shoulder_minus_hip_heading"; event_id: "contact"; start_event_id?: never; end_event_id?: never; }; | |
| export type Measurement = MetricSpecification & MeasurementResult & { uses_interpolated_input: boolean; benchmark: ApprovedBenchmark | null; }; | |
| export interface SwingAnalysisPayload { | |
| schema_version: SchemaVersion; analysis_id: string; analysis_revision: number; swing_id: string; status: AnalysisStatus; | |
| athlete: Athlete; context: SwingContext; measurement_definitions_version: MeasurementDefinitionsVersion; | |
| events: Record<EventId, SwingEvent>; measurements: Measurement[]; | |
| } | |
| ``` | |
| - Every field is required unless it is marked `?`. | |
| - A field typed with `null` must still be present. | |
| - `never` means the field must be absent for that metric. | |
| ## Field constraints | |
| - `analysis_id`, `swing_id` and `athlete_id` are non-empty. | |
| - `analysis_revision` is a positive integer. | |
| - `age_years` is a non-negative integer or null. | |
| - `height_cm` is positive or null. | |
| - All 3 event keys must be present. | |
| - `measurements` has exactly one entry for each of the 3 registered metric ids, including unavailable ones. Duplicate or unregistered metric ids are rejected. | |
| - `confidence` is a finite number from 0 to 1, or null. | |
| ## Runtime validation rules (receiver) | |
| 1. Accept only `schema_version` and `measurement_definitions_version` equal to "1.0". Reject anything else with a clear error. | |
| 2. Numbers must be finite. Ids must be non-empty. The revision must be a positive integer. Age must be a non-negative integer and height positive when supplied. | |
| 3. All three event keys must be present, with exactly one entry per registered metric. | |
| 4. Each metric must match its exact unit, group, reference and event scope (`MetricSpecification`). A point measurement cannot also carry an interval. | |
| 5. Status combinations: | |
| - available and limited need a numeric value; | |
| - unavailable needs a null value and a null confidence; | |
| - available has an empty reasons list; | |
| - limited and unavailable need at least one registered reason code. | |
| 6. Timing: | |
| - Available or limited event `video_time_s` must be non-negative. | |
| - `capture_time_s`, when known, must be non-negative. | |
| - For a usable stride interval, `stride_start` must come before `front_foot_down`. | |
| - When both landing and contact are available, landing must not follow contact. | |
| 7. Event quality propagates: | |
| - A missing required event makes the dependent measurement unavailable. | |
| - A limited event makes the dependent measurement limited or unavailable, never available. | |
| 8. Contact evidence: `pose_estimated` cannot establish contact, and `bat_ball_detector` is valid only for the contact event. | |
| 9. Physical interpretation: | |
| - Unknown scale makes cm measurements unavailable. | |
| - Model-estimated, unverified scale needs `limited` with `scale_is_model_estimated`. | |
| - An unknown lead foot makes relative stride unavailable with `lead_foot_unknown`. | |
| 10. Ranges: | |
| - Signed separation must be in [-180, 180). | |
| - Relative stride length must be non-negative. | |
| 11. Benchmarks: the backend sends `benchmark: null`, because the coach owns the ranges. If one is present, its unit must match, at least one bound must be non-null, and `lower <= upper`. | |
| 12. Traceability: every finding cites its metric ids and the analysis revision, and the coach never invents measurements, contact events, scores or improvement claims. | |
| ## Metric definitions (measurement_definitions_version 1.0) | |
| | Metric id | Scope | Definition | | |
| |---|---|---| | |
| | `hip_shoulder_separation_at_foot_down` | `front_foot_down` | `wrap180(shoulder_heading - hip_heading)`, in degrees, on the horizontal plane. Positive means the shoulder line is counter-clockwise from the hip line when seen from above. The same convention is used for both batting sides. | | |
| | `stride_relative_length` | `stride_start` to `front_foot_down` | `100 * length(horizontal(r(end) - r(start)))` cm, where `r = lead_ankle - rear_ankle`. It is never negative. | | |
| | `hip_shoulder_separation_at_contact` | `contact` | The same signed heading difference, at verified bat-ball contact. | | |
| ## Measurement groups (display names) | |
| | Group | Display name | | |
| |---|---| | |
| | `balance_load` | Balance and Load | | |
| | `stride` | Stride | | |
| | `power_move` | Power Move | | |
| | `contact_follow_through` | Contact and Follow-Through | | |
| ## Plain-language meanings (from the ML engineer's glossary doc) | |
| - **Hip Shoulder Separation at Foot Down:** when your front foot lands, how differently your hips and shoulders are pointing. | |
| - **Stride Length:** how far your front foot steps forward. | |
| - **Hip Shoulder Separation at Contact:** when the bat meets the ball, how differently your hips and shoulders are pointing. | |
| - **Groups:** | |
| - **Balance and Load** is how you prepare before swinging. | |
| - **Stride** is the step toward the pitcher. | |
| - **Power Move** is how your body turns to drive the swing. | |
| - **Contact and Follow-Through** is hitting and finishing. | |