File size: 9,286 Bytes
e8055cf
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
import numpy as np

from experiments import loader


def test_every_hypothesis_has_required_interface():
    names = loader.list_hypotheses()
    assert names
    for name in names:
        module = loader.load_hypothesis(name)
        for callable_name in loader.REQUIRED_CALLABLES:
            assert callable(
                getattr(module, callable_name, None)
            ), f"{name} is missing {callable_name}()"


def test_size_percentile_hypotheses_preserve_baseline_centroid():
    smaller = np.stack(
        [np.linspace(0.0, 1.0, 20), np.zeros(20), np.zeros(20)], axis=1
    ).astype(np.float32)
    larger = np.stack(
        [np.linspace(10.0, 12.0, 30), np.zeros(30), np.zeros(30)], axis=1
    ).astype(np.float32)
    observations = [smaller, larger]
    for name, percentile in (
        ("Estimate Object Size From the 65th Percentile Across Frames", 65),
        ("Estimate Object Size From the 75th Percentile Across Frames", 75),
        ("Estimate Object Size From the 90th Percentile Across Frames", 90),
        ("Estimate Object Size From the 95th Percentile Across Frames", 95),
        ("Estimate Object Size From the Maximum Across Frames", 100),
    ):
        module = loader.load_hypothesis(name)
        expected, _, _ = module.robust_centroid_extent(larger, None)
        actual, size, dims = module.estimate_track_geometry(observations, None)
        frame_dims = np.stack(
            [module.robust_centroid_extent(points, None)[2] for points in observations]
        )
        expected_dims = np.sort(np.percentile(frame_dims, percentile, axis=0))[::-1]
        np.testing.assert_allclose(actual, expected)
        np.testing.assert_allclose(dims, expected_dims)
        assert size == expected_dims.max()


def test_symmetric_surface_percentile_distance_is_density_independent():
    module = loader.load_hypothesis(
        "Measure Absolute Object Distance Using Symmetric Surface Percentiles"
    )
    points_a = np.asarray([[0.0, 0.0, 0.0], [10.0, 0.0, 0.0]], dtype=np.float32)
    points_b = np.asarray([[1.0, 0.0, 0.0]], dtype=np.float32)
    instances_a = [{"pts": points_a, "n": len(points_a)}]
    instances_b = [{"pts": points_b, "n": len(points_b)}]
    expected = 0.5 * (np.percentile([1.0, 9.0], 1.0) + 1.0)

    forward = module.answer_closest_distance(instances_a, instances_b)
    reverse = module.answer_closest_distance(instances_b, instances_a)
    canonical = module._canonical_answer_closest_distance(instances_a, instances_b)

    np.testing.assert_allclose(forward, expected)
    np.testing.assert_allclose(reverse, expected)
    np.testing.assert_allclose(canonical, expected)


def test_multi_view_oriented_box_distance_uses_complete_boxes():
    module = loader.load_hypothesis(
        "Estimate Absolute Object Distance From Multi View Oriented Bounding Boxes"
    )
    signs = np.asarray(
        [[x, y, z] for x in (-1.0, 1.0) for y in (-0.5, 0.5) for z in (-0.25, 0.25)],
        dtype=np.float32,
    )
    angle = np.deg2rad(30.0)
    rotation = np.asarray(
        [
            [np.cos(angle), -np.sin(angle), 0.0],
            [np.sin(angle), np.cos(angle), 0.0],
            [0.0, 0.0, 1.0],
        ],
        dtype=np.float32,
    )
    direction = rotation[:, 0]
    points_a = signs @ rotation.T
    points_b = points_a + 4.0 * direction
    instances_a = [{"pts": points_a, "n": len(points_a)}]
    instances_b = [{"pts": points_b, "n": len(points_b)}]

    distance = module.answer_closest_distance(instances_a, instances_b)
    canonical = module._canonical_answer_closest_distance(instances_a, instances_b)

    np.testing.assert_allclose(distance, 2.0, atol=1e-5)
    np.testing.assert_allclose(canonical, 2.0, atol=1e-5)


def test_projected_center_line_distance_uses_robust_directional_extents():
    module = loader.load_hypothesis(
        "Estimate Absolute Object Distance Along the Line Between Object Centers"
    )
    points_a = np.stack(
        [np.linspace(-1.0, 1.0, 101), np.zeros(101), np.zeros(101)], axis=1
    ).astype(np.float32)
    points_b = np.stack(
        [np.linspace(4.0, 6.0, 101), np.zeros(101), np.zeros(101)], axis=1
    ).astype(np.float32)
    instances_a = [{"pts": points_a, "n": len(points_a)}]
    instances_b = [{"pts": points_b, "n": len(points_b)}]
    expected = np.percentile(points_b[:, 0], 2.0) - np.percentile(points_a[:, 0], 98.0)

    projected = module._projected_center_line_distance(points_a, points_b)
    forward = module.answer_closest_distance(instances_a, instances_b)
    reverse = module.answer_closest_distance(instances_b, instances_a)
    canonical = module._canonical_answer_closest_distance(instances_a, instances_b)

    np.testing.assert_allclose(projected, expected)
    np.testing.assert_allclose(reverse, forward)
    np.testing.assert_allclose(canonical, forward)


def test_half_percentile_surface_distance_is_registered():
    module = loader.load_hypothesis(
        "Measure Absolute Object Distance Using the 0.5th Surface Percentile"
    )
    assert module.SURFACE_DISTANCE_PERCENTILE == 0.5


def test_quarter_percentile_surface_distance_is_registered():
    module = loader.load_hypothesis(
        "Measure Absolute Object Distance Using the 0.25th Surface Percentile"
    )
    assert module.SURFACE_DISTANCE_PERCENTILE == 0.25


def test_maximum_object_size_scale_increases_dimensions_by_ten_percent():
    baseline = loader.load_hypothesis(
        "Estimate Object Size From the Maximum Across Frames"
    )
    scaled = loader.load_hypothesis(
        "Increase Maximum Object Size Estimates by 10 Percent"
    )
    points = np.stack(
        [np.linspace(0.0, 1.0, 20), np.zeros(20), np.zeros(20)], axis=1
    ).astype(np.float32)
    baseline_size, baseline_dims = baseline.aggregate_frame_extent([points], None)
    scaled_size, scaled_dims = scaled.aggregate_frame_extent([points], None)

    np.testing.assert_allclose(scaled_size, 1.10 * baseline_size)
    np.testing.assert_allclose(scaled_dims, 1.10 * baseline_dims)


def test_maximum_object_size_scale_increases_dimensions_by_fifteen_percent():
    baseline = loader.load_hypothesis(
        "Estimate Object Size From the Maximum Across Frames"
    )
    scaled = loader.load_hypothesis(
        "Increase Maximum Object Size Estimates by 15 Percent"
    )
    points = np.stack(
        [np.linspace(0.0, 1.0, 20), np.zeros(20), np.zeros(20)], axis=1
    ).astype(np.float32)
    baseline_size, baseline_dims = baseline.aggregate_frame_extent([points], None)
    scaled_size, scaled_dims = scaled.aggregate_frame_extent([points], None)

    np.testing.assert_allclose(scaled_size, 1.15 * baseline_size)
    np.testing.assert_allclose(scaled_dims, 1.15 * baseline_dims)


def test_maximum_object_size_scale_increases_dimensions_by_twelve_and_a_half_percent():
    baseline = loader.load_hypothesis(
        "Estimate Object Size From the Maximum Across Frames"
    )
    scaled = loader.load_hypothesis(
        "Increase Maximum Object Size Estimates by 12.5 Percent"
    )
    points = np.stack(
        [np.linspace(0.0, 1.0, 20), np.zeros(20), np.zeros(20)], axis=1
    ).astype(np.float32)
    baseline_size, baseline_dims = baseline.aggregate_frame_extent([points], None)
    scaled_size, scaled_dims = scaled.aggregate_frame_extent([points], None)

    np.testing.assert_allclose(scaled_size, 1.125 * baseline_size)
    np.testing.assert_allclose(scaled_dims, 1.125 * baseline_dims)


def test_surface_distance_blend_keeps_eighty_percent_of_the_minimum():
    module = loader.load_hypothesis(
        "Blend the Minimum Surface Distance With 20 Percent of the First Percentile"
    )
    assert module.SURFACE_PERCENTILE_BLEND == 0.20


def test_surface_distance_blend_keeps_ninety_percent_of_the_minimum():
    module = loader.load_hypothesis(
        "Blend the Minimum Surface Distance With 10 Percent of the First Percentile"
    )
    assert module.SURFACE_PERCENTILE_BLEND == 0.10


def test_minimum_surface_distance_scale_reduces_estimates_by_five_percent():
    module = loader.load_hypothesis(
        "Reduce Minimum Surface Distance Estimates by 5 Percent"
    )
    assert module.SURFACE_DISTANCE_SCALE == 0.95


def test_inconsistent_frame_rejection_removes_a_distant_observation():
    module = loader.load_hypothesis(
        "Reject Geometrically Inconsistent Frame Observations Before Measuring Object Distance"
    )
    observations = [
        np.stack([np.linspace(0.0, 1.0, 20), np.zeros(20), np.zeros(20)], axis=1)
        + offset
        for offset in (0.0, 0.01, -0.01, 20.0)
    ]
    np.testing.assert_array_equal(
        module._consistent_observation_indices(observations), [0, 1, 2]
    )


def test_consistent_frame_pair_distance_uses_lower_quartile():
    module = loader.load_hypothesis(
        "Measure Object Distance From the Lower Quartile of Consistent Frame Pairs"
    )
    first = {"pts": np.asarray([[0.0, 0.0, 0.0]], np.float32)}
    second = {
        "pts": np.asarray([[1.0, 0.0, 0.0]], np.float32),
        "distance_observations": [
            np.asarray([[distance, 0.0, 0.0]], np.float32)
            for distance in (1.0, 2.0, 3.0, 4.0)
        ],
    }
    distances = module._observation_pair_distances(first, second)
    np.testing.assert_allclose(np.percentile(distances, 25.0), 1.75)