File size: 2,378 Bytes
326d3cc
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import numpy as np
import pytest
from baseball_batting.events import detect_events
from baseball_batting.metrics import continuous_curves
from baseball_batting.objects import possible_impact_reset

def inputs():
    frames = [{"frame_id":i,"body_points":[[0,0]]*133,"body_states":["model_inferred"]*133,
               "bat":{"points":[[10,10],[20,20]],"states":["propagated"]*2},
               "ball":{"point":[20,20],"state":"model_inferred"}} for i in range(8)]
    capture = {"frames":[{"physical_s":None,"playback_s":i/30,"flags":[]} for i in range(8)],
               "physical_clock_known":False,"camera_stability":"fixed_verified"}
    curves = {"left_hand_path_pixels":np.zeros((8,2)),"right_hand_path_pixels":np.zeros((8,2))}
    params = {"motion_threshold_px_per_frame":3,"sustained_frames":3,"min_visible_frames":4}
    return {"frames":frames},capture,curves,params

def test_projected_overlap_is_not_contact():
    obs,capture,curves,params = inputs()
    events = detect_events(curves,obs,capture,{"events":[]},params)
    assert next(e for e in events if e.type == "contact").state == "unknown"
    assert possible_impact_reset([{"class_id":32,"box":[18,18,22,22]}], obs["frames"][0]["bat"])

def test_propagated_bat_is_not_direct_measurement():
    obs,capture,_,_ = inputs()
    curves = continuous_curves(obs,capture)
    assert np.isnan(curves["bat_endpoints_pixels"]).all()

@pytest.mark.parametrize("outcome", ["contact", "miss", "dry_swing"])
def test_reviewed_outcome_preserved(outcome):
    obs,capture,curves,params = inputs()
    labels = {"annotator":"fixture", "events":[{"type":outcome,"state":"confirmed","frame_interval":[2,3],
              "evidence_frames":[2,3],"evidence_description":"Test fixture"}]}
    events = detect_events(curves,obs,capture,labels,params)
    event = next(e for e in events if e.type == outcome)
    assert event.state == "confirmed" and event.annotator == "fixture"
    assert event.physical_interval_s == [None,None]

def test_conflicting_reviewed_outcomes_rejected():
    obs,capture,curves,params = inputs()
    labels = {"annotator":"fixture", "events":[{"type":outcome,"state":"confirmed","frame_interval":[2,3]}
                                                for outcome in ("contact","miss")]}
    with pytest.raises(ValueError,match="Conflicting"):
        detect_events(curves,obs,capture,labels,params)