workspace / tests /test_experiments /test_hypotheses.py
AntonioJun's picture
Replace tests with local workspace contents
e8055cf verified
Raw
History Blame Contribute Delete
9.29 kB
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)