Spaces:
Sleeping
Sleeping
File size: 5,725 Bytes
e04bbc2 | 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 | import math
import pytest
from analysis.scoring import (
MATCHER_FIXED_PX,
MATCHER_RELATIVE_RADIUS,
MATCH_FRAC_DEFAULT,
equivalent_radius,
match,
score,
score_relative,
)
def square(cx: float, cy: float, side: float) -> dict:
"""A GT bundle shaped as an axis-aligned square of a known area."""
h = side / 2
return {
"center": [cx, cy],
"contour": [[cx - h, cy - h], [cx + h, cy - h], [cx + h, cy + h], [cx - h, cy + h]],
"area": side * side,
}
def tolerance_of(side: float, frac: float = MATCH_FRAC_DEFAULT) -> float:
return math.sqrt(side * side / math.pi) * frac
# --------------------------------------------------------------------------- #
# fixed-pixel matcher (legacy)
# --------------------------------------------------------------------------- #
def test_all_match_one_to_one():
run = [(10, 10), (100, 100), (200, 200)]
gt = [(11, 9), (98, 101), (201, 198)]
s = score(run, gt, match_radius=20)
assert (s["tp"], s["fp"], s["fn"]) == (3, 0, 0)
assert s["precision"] == 1.0 and s["recall"] == 1.0
def test_false_positive_when_run_has_extra():
s = score([(10, 10), (50, 50)], [(11, 11)], match_radius=20)
assert (s["tp"], s["fp"], s["fn"]) == (1, 1, 0)
assert s["precision"] == 0.5 and s["recall"] == 1.0
def test_false_negative_when_gt_has_extra():
s = score([(10, 10)], [(11, 11), (200, 200)], match_radius=20)
assert (s["tp"], s["fp"], s["fn"]) == (1, 0, 1)
assert s["precision"] == 1.0 and s["recall"] == 0.5
def test_no_double_matching():
s = score([(10, 10), (12, 12)], [(11, 11)], match_radius=20)
assert (s["tp"], s["fp"]) == (1, 1)
def test_match_boundary_at_radius():
assert score([(0, 0)], [(20, 0)], match_radius=20)["tp"] == 1
s_out = score([(0, 0)], [(21, 0)], match_radius=20)
assert (s_out["tp"], s_out["fp"], s_out["fn"]) == (0, 1, 1)
# --------------------------------------------------------------------------- #
# undefined ratios must not read as success
# --------------------------------------------------------------------------- #
def test_nothing_to_find_and_nothing_claimed_is_perfect():
assert score([], [], match_radius=20) == {
"tp": 0, "fp": 0, "fn": 0, "precision": 1.0, "recall": 1.0
}
def test_predictions_against_empty_gt_score_zero_recall():
"""Everything predicted is a false positive; recall is undefined, not 1.0."""
assert score([(1, 1)], [], match_radius=20) == {
"tp": 0, "fp": 1, "fn": 0, "precision": 0.0, "recall": 0.0
}
def test_no_predictions_against_real_gt_scores_zero_precision():
"""A model that finds nothing used to report precision 1.0."""
assert score([], [(1, 1)], match_radius=20) == {
"tp": 0, "fp": 0, "fn": 1, "precision": 0.0, "recall": 0.0
}
# --------------------------------------------------------------------------- #
# relative-radius matcher
# --------------------------------------------------------------------------- #
def test_equivalent_radius_of_a_square():
r = equivalent_radius(square(0, 0, 200)["contour"])
assert r == pytest.approx(math.sqrt(40000 / math.pi), rel=1e-4)
def test_relative_match_boundary_at_frac_times_radius():
gt = [square(500, 500, 200)]
tol = tolerance_of(200)
inside = score_relative([(500 + tol - 1, 500)], gt)
outside = score_relative([(500 + tol + 1, 500)], gt)
assert inside["tp"] == 1
assert (outside["tp"], outside["fp"], outside["fn"]) == (0, 1, 1)
def test_tolerance_scales_with_object_size():
"""The whole point: the same offset hits a big object and misses a small one."""
offset = 40.0
assert score_relative([(500 + offset, 500)], [square(500, 500, 400)])["tp"] == 1
assert score_relative([(500 + offset, 500)], [square(500, 500, 40)])["tp"] == 0
def test_a_detection_inside_a_large_bundle_is_a_hit_unlike_fixed_20px():
"""Regression guard for the mis-calibration this matcher replaces."""
gt = [square(1000, 1000, 400)]
dead_inside = [(1060.0, 1000.0)]
assert score(dead_inside, [(1000, 1000)], match_radius=20)["tp"] == 0
assert score_relative(dead_inside, gt)["tp"] == 1
def test_degenerate_contours_are_dropped_from_both_sides():
gt = [square(100, 100, 50), {"center": [900, 900], "contour": [[9, 9], [9, 9]], "area": 0}]
s = score_relative([(100, 100)], gt)
assert (s["tp"], s["fp"], s["fn"]) == (1, 0, 0)
assert s["precision"] == 1.0 and s["recall"] == 1.0
def test_relative_no_double_matching():
gt = [square(500, 500, 200)]
s = score_relative([(500, 500), (505, 505)], gt)
assert (s["tp"], s["fp"], s["fn"]) == (1, 1, 0)
def test_relative_frac_is_honoured():
gt = [square(500, 500, 200)]
far = [(500 + tolerance_of(200, 0.5) + 5, 500)]
assert score_relative(far, gt, frac=0.5)["tp"] == 0
assert score_relative(far, gt, frac=1.0)["tp"] == 1
# --------------------------------------------------------------------------- #
# dispatch
# --------------------------------------------------------------------------- #
def test_match_dispatches_to_relative():
gt = [square(500, 500, 400)]
assert match([(560, 500)], gt, MATCHER_RELATIVE_RADIUS, 0.5)["tp"] == 1
def test_match_dispatches_to_fixed_px():
gt = [square(500, 500, 400)]
assert match([(500, 500)], gt, MATCHER_FIXED_PX, 20.0)["tp"] == 1
assert match([(600, 500)], gt, MATCHER_FIXED_PX, 20.0)["tp"] == 0
def test_match_rejects_an_unknown_kind():
with pytest.raises(ValueError):
match([(0, 0)], [], "iou", 0.5)
|