"""Tests for iou.py. Run: python -m pytest -q""" import numpy as np import pytest from iou import average_precision, iou, iou_matrix, match_detections, nms, pr_curve, precision_recall def test_iou_worked_example() -> None: # Intersection 80 x 90 = 7200, union 10000 + 10000 - 7200 = 12800. assert iou([100, 100, 200, 200], [120, 110, 220, 210]) == pytest.approx(7200 / 12800) def test_iou_identical_disjoint_touching() -> None: assert iou([0, 0, 10, 10], [0, 0, 10, 10]) == pytest.approx(1.0) assert iou([0, 0, 10, 10], [20, 20, 30, 30]) == 0.0 assert iou([0, 0, 10, 10], [10, 0, 20, 10]) == 0.0 # shared edge, zero area def test_iou_contained_box() -> None: assert iou([0, 0, 10, 10], [0, 0, 5, 10]) == pytest.approx(0.5) def test_iou_matrix_matches_scalar_and_is_symmetric() -> None: rng = np.random.default_rng(0) xy = rng.uniform(0, 100, size=(20, 2)) wh = rng.uniform(5, 40, size=(20, 2)) boxes = np.hstack([xy, xy + wh]) m = iou_matrix(boxes, boxes) assert np.allclose(m, m.T) assert np.allclose(np.diag(m), 1.0) assert np.all((m >= 0) & (m <= 1)) for i, j in [(0, 1), (3, 7), (12, 19)]: assert m[i, j] == pytest.approx(iou(boxes[i], boxes[j])) def test_nms_worked_example() -> None: boxes = [[100, 100, 200, 200], [120, 110, 220, 210], [300, 120, 380, 200], [105, 95, 195, 205]] scores = [0.92, 0.85, 0.60, 0.40] assert nms(boxes, scores, 0.5).tolist() == [0, 2] # A threshold above 0.5625 keeps box 1, which overlaps box 0 by exactly that much. assert nms(boxes, scores, 0.6).tolist() == [0, 1, 2] def test_nms_keeps_all_disjoint_boxes_in_score_order() -> None: boxes = [[0, 0, 1, 1], [5, 5, 6, 6], [10, 10, 11, 11]] assert nms(boxes, [0.2, 0.9, 0.5]).tolist() == [1, 2, 0] def test_match_detections_duplicate_is_false_positive() -> None: gt = [[0, 0, 10, 10]] is_tp, s = match_detections([[0, 0, 10, 10], [1, 0, 10, 10]], [0.9, 0.8], gt) assert is_tp.tolist() == [True, False] assert s.tolist() == [0.9, 0.8] def test_precision_recall_worked_example() -> None: boxes = np.array([[100, 100, 200, 200], [120, 110, 220, 210], [300, 120, 380, 200], [105, 95, 195, 205]]) scores = np.array([0.92, 0.85, 0.60, 0.40]) gt = np.array([[102, 104, 204, 206], [298, 118, 382, 204], [450, 300, 520, 380]]) p, r, tp, fp, fn = precision_recall(boxes, scores, gt, score_threshold=0.5) assert (tp, fp, fn) == (2, 1, 1) assert p == pytest.approx(2 / 3) and r == pytest.approx(2 / 3) keep = nms(boxes, scores, 0.5) p, r, tp, fp, fn = precision_recall(boxes[keep], scores[keep], gt, score_threshold=0.5) assert (tp, fp, fn) == (2, 0, 1) assert p == pytest.approx(1.0) def test_pr_curve_and_ap() -> None: # Ranked results T, F, T with 2 objects: precision 1, 1/2, 2/3 at recall 1/2, 1/2, 1. p, r = pr_curve([True, False, True], [0.9, 0.8, 0.7], n_gt=2) assert np.allclose(p, [1.0, 0.5, 2 / 3]) assert np.allclose(r, [0.5, 0.5, 1.0]) # Envelope: precision 1 up to recall 0.5, then 2/3 up to recall 1. assert average_precision(p, r) == pytest.approx(0.5 * 1.0 + 0.5 * 2 / 3) def test_ap_perfect_detector() -> None: p, r = pr_curve([True] * 4, [0.9, 0.8, 0.7, 0.6], n_gt=4) assert average_precision(p, r) == pytest.approx(1.0)