Spaces:
Sleeping
Sleeping
Download tests/test_scoring.py from DmitryPlant/plants-analysis-api: direct link, hf CLI and curl.
- Browser
- Download file 5.73 kB
-
https://huggingface.co/spaces/DmitryPlant/plants-analysis-api/resolve/main/tests/test_scoring.py
- Command line
-
hf download hf://spaces/DmitryPlant/plants-analysis-api/tests/test_scoring.py
-
curl -L -o test_scoring.py https://huggingface.co/spaces/DmitryPlant/plants-analysis-api/resolve/main/tests/test_scoring.py
5.73 kB
| 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) | |