File size: 6,967 Bytes
ea1a74e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
"""Bowler's and batter's review points: EXPECTED action vs PERFORMED action.

Bowler: expected = the instruction (target line, length, delivery, pace band);
        performed = what was measured or labelled (actual line/length, delivery type).
Batter: expected = the recommended response to the delivery AS BOWLED, from the
        provisional coaching rules in ontology.yaml (or a coach's per-delivery
        override); performed = shot type, contact, and event timing.

Each row: aspect, expected, performed, status (match | mismatch | needs_label | not_measured),
and where each side came from, so a wrong verdict can be traced and corrected."""

NICE = lambda v: v.replace("_", " ") if isinstance(v, str) else v


def _lab(d, k):
    x = (d.get("labels") or {}).get(k)
    return (x or {}).get("value"), ((x or {}).get("provenance") or {}).get("source_type")


def _row(aspect, expected, performed, status, src_e, src_p, note=None):
    return {"aspect": aspect, "expected": expected, "performed": performed, "status": status,
            "expected_from": src_e, "performed_from": src_p, "note": note}


def _cmp(e, p):
    if e is None or p is None:
        return "needs_label"
    return "match" if e == p else "mismatch"


def bowler_points(d):
    rows = []
    pitch = (d.get("measurements") or {}).get("pitch") or {}
    for aspect, tgt, act in [("Line", "target_line", "actual_line"), ("Length", "target_length", "actual_length")]:
        e, se = _lab(d, tgt)
        p, sp = _lab(d, act)
        perf = NICE(p)
        if aspect == "Length" and p and (pitch.get("bounce_m") or {}).get("length_from_batter_stumps_m") is not None:
            perf = f"{NICE(p)} ({pitch['bounce_m']['length_from_batter_stumps_m']:.1f} m from stumps)"
        rows.append(_row(aspect, NICE(e), perf, _cmp(e, p), "instruction" if e else None,
                         {"deterministic_rule": "pitch calibration", "annotator": "reviewer"}.get(sp, sp)))
    e, se = _lab(d, "intended_delivery")
    p, sp = _lab(d, "delivery_type")
    rows.append(_row("Delivery", NICE(e), NICE(p), _cmp(e, p), "instruction" if e else None, "reviewer" if p else None))
    e, _ = _lab(d, "intended_pace_band")
    spd = (d.get("multiview") or {}).get("speed_kmh")
    if spd:
        rows.append(_row("Pace", NICE(e), f"{spd:.0f} km/h", "measured", "instruction" if e else None,
                         f"{d['multiview']['n_views']}-camera 3D ball track",
                         "pace bands are coaching terms; compare with the bowler's usual speed"))
    else:
        rows.append(_row("Pace", NICE(e), None, "not_measured" if e else "needs_label", "instruction" if e else None, None,
                         "needs radar or a calibrated two-camera speed"))
    return rows


def expected_shots(d, onto):
    """Expected responses for the delivery as bowled, or None if line/length are unknown."""
    over, so = _lab(d, "expected_shot")
    if over:
        return [over], "coach override"
    length, _ = _lab(d, "actual_length")
    line, _ = _lab(d, "actual_line")
    table = (onto.get("expected_shots") or {}).get(length or "")
    if not table:
        return None, None
    shots = table.get(line) if line else None
    shots = shots or table.get("_any")
    status = (onto.get("expected_shots") or {}).get("status", "provisional")
    return shots, f"coaching rule ({status})"


def batter_points(d, onto):
    rows = []
    ev = d["events"]
    shots, src = expected_shots(d, onto)
    performed, sp = _lab(d, "shot_type")
    if shots is None:
        rows.append(_row("Shot", None, NICE(performed), "needs_label", None, "reviewer" if performed else None,
                         "needs the delivery's line and length (calibrate the pitch or label them)"))
    else:
        st = "needs_label" if performed is None else ("match" if performed in shots else "mismatch")
        rows.append(_row("Shot", " or ".join(NICE(x) for x in shots), NICE(performed), st, src,
                         "reviewer" if performed else None))
    leave_expected = bool(shots) and shots[0] == "leave"
    cq, _ = _lab(d, "contact_quality")
    contact = ev.get("bat_ball_contact")
    perf = NICE(cq) if cq else (f"bat contact at {contact['t_clip']:.2f} s" if contact else None)
    if shots is None:
        st = "needs_label"
    elif leave_expected:
        st = "match" if (cq in ("miss", None) and not contact and performed in (None, "leave")) else \
            ("mismatch" if (contact or cq in ("clean", "edge", "mistimed")) else "needs_label")
    else:
        st = "match" if cq == "clean" else ("mismatch" if cq in ("edge", "mistimed", "miss") else
                                            ("needs_label" if not contact else "needs_label"))
    rows.append(_row("Contact", "no contact (leave)" if leave_expected else ("clean contact" if shots else None),
                     perf, st, src if shots else None, "reviewer" if cq else ("ball track" if contact else None)))
    length, _ = _lab(d, "actual_length")
    exp_mv = (onto.get("expected_movement") or {}).get(length or "")
    mv, msrc = _lab(d, "batter_movement")
    mv_src = {"deterministic_rule": "feet on calibrated pitch", "annotator": "reviewer"}.get(msrc, msrc)
    rows.append(_row("Movement", NICE(exp_mv), NICE(mv), _cmp(exp_mv, mv) if mv != "unknown" else "needs_label",
                     "coaching rule (provisional)" if exp_mv else None, mv_src,
                     None if (exp_mv and mv) else "needs line/length and a calibrated pitch (or a movement label)"))
    trg, bounce = ev.get("batter_trigger_start"), ev.get("ball_bounce")
    if trg and bounce:
        ok = trg["t_clip"] <= bounce["t_clip"]
        rows.append(_row("Timing", "trigger movement before the ball pitches",
                         f"trigger at {trg['t_clip']:.2f} s, ball pitches at {bounce['t_clip']:.2f} s",
                         "match" if ok else "mismatch", "coaching rule (provisional)", "pose model + ball track"))
    else:
        rows.append(_row("Timing", "trigger movement before the ball pitches", None, "needs_label",
                         "coaching rule (provisional)", None, "needs batter trigger and bounce events"))
    se = ev.get("shot_followthrough_end")
    if shots and not leave_expected:
        rows.append(_row("Finish", "complete the follow-through", f"follow-through ends at {se['t_clip']:.2f} s" if se else None,
                         "match" if se else "needs_label", "coaching rule (provisional)", "pose model" if se else None))
    return rows


def review_points(d, onto):
    b, bt = bowler_points(d), batter_points(d, onto)
    score = lambda rows: {"match": sum(r["status"] == "match" for r in rows),
                          "mismatch": sum(r["status"] == "mismatch" for r in rows),
                          "open": sum(r["status"] in ("needs_label", "not_measured") for r in rows)}
    return {"bowler": b, "batter": bt, "bowler_summary": score(b), "batter_summary": score(bt)}