File size: 2,911 Bytes
d37698e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""Unit tests for localization construction and scoring (no API deps)."""

from __future__ import annotations

from localization.construct import (
    label_specs_from_segments,
    localization_prompt,
    multiplicity_phrase,
)
from localization.schema import GoldSegment, PredictedInterval, PredictionResult
from localization.score import optimal_group_assignment, score_episode


def _segments(labels: list[str]) -> list[GoldSegment]:
    return [
        GoldSegment(float(index), float(index + 1), label)
        for index, label in enumerate(labels)
    ]


def test_multiplicity_phrasing_handles_singletons_and_duplicates():
    gold = _segments(["pick", "place", "pick"])
    specs = sorted(
        label_specs_from_segments("homer_1", gold, seed=0),
        key=lambda spec: spec.label,
    )

    assert [multiplicity_phrase(spec) for spec in specs] == [
        '"pick" (occurs 2 times)',
        '"place"',
    ]
    prompt = localization_prompt("stack the blocks", specs)
    assert '"pick" (occurs 2 times)' in prompt
    assert '- "place"\n' in prompt
    assert "occurs 1" not in prompt


def test_label_shuffle_is_deterministic_under_seed():
    gold = _segments(["a", "b", "c", "d"])

    first = [spec.label for spec in label_specs_from_segments("homer_1", gold, seed=3)]
    second = [spec.label for spec in label_specs_from_segments("homer_1", gold, seed=3)]
    alternatives = {
        tuple(spec.label for spec in label_specs_from_segments("homer_1", gold, seed=seed))
        for seed in range(10)
    }

    assert first == second
    assert len(alternatives) > 1


def test_optimal_group_assignment_ties_are_deterministic():
    golds = [
        GoldSegment(0.0, 2.0, "repeat"),
        GoldSegment(2.0, 4.0, "repeat"),
    ]
    preds = [
        PredictedInterval(label_echo="repeat", start_sec=1.0, end_sec=3.0),
        PredictedInterval(label_echo="repeat", start_sec=1.0, end_sec=3.0),
    ]

    assert optimal_group_assignment(golds, preds) == {0: 0, 1: 1}


def test_score_episode_exact_match_is_perfect():
    gold = _segments(["pick", "place"])
    specs = label_specs_from_segments("ep", gold, seed=0)
    prediction = PredictionResult(
        labels=[
            {
                "label": "pick",
                "intervals": [
                    {"label_echo": "pick", "start_sec": 0.0, "end_sec": 1.0},
                ],
            },
            {
                "label": "place",
                "intervals": [
                    {"label_echo": "place", "start_sec": 1.0, "end_sec": 2.0},
                ],
            },
        ]
    )
    rows, diagnostics = score_episode(
        episode_id="ep",
        family="homer",
        gold_segments=gold,
        specs=specs,
        prediction=prediction,
    )
    assert diagnostics["events_total"] == 2
    assert all(row["iou"] == 1.0 for row in rows)
    assert all(row["hit_0_75"] for row in rows)