File size: 5,655 Bytes
11ec37f | 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 | """Scoring maths: intervals, matching, and the three populated axes."""
from __future__ import annotations
import pytest
import itertools
from cystobench.intervals import (
assignment_overlap,
match_intervals,
overlap_s,
tiou,
union_duration,
)
from cystobench.scoring import score_2a, score_2b, score_2c, score_axis1
def test_overlap_and_union():
assert overlap_s((0, 5), (3, 8)) == 2
assert overlap_s((0, 5), (5, 8)) == 0
assert union_duration([(0, 3), (2, 5), (9, 10)]) == 6
assert tiou((0, 4), (2, 6)) == pytest.approx(2 / 6)
def test_match_accepts_a_pair_above_the_threshold():
result = match_intervals([(1.0, 5.0)], [(0.5, 4.0)])
assert (result["tp"], result["fp"], result["fn"]) == (1, 0, 0)
def test_match_rejects_a_pair_below_the_threshold():
result = match_intervals([(10.0, 14.0)], [(0.0, 1.0)])
assert (result["tp"], result["fp"], result["fn"]) == (0, 1, 1)
def test_short_gold_lowers_the_threshold():
gold = [(0.0, 0.4)]
assert match_intervals(gold, [(0.0, 0.4)])["tp"] == 1
assert match_intervals(gold, [(0.0, 0.4)], strict=True)["tp"] == 0
def test_matching_maximises_total_overlap():
gold = [(0.0, 10.0), (20.0, 30.0)]
pred = [(1.0, 9.0), (21.0, 29.0)]
assert match_intervals(gold, pred)["tp"] == 2
def test_2c_macro_f1_weights_each_class_equally():
rows = [
{"class_label": "malignant_tumor", "patient_id": 1, "pred": "malignant_tumor"},
{"class_label": "non_malignant_lesion", "patient_id": 2, "pred": "non_roi"},
{"class_label": "non_roi", "patient_id": 3, "pred": "non_roi"},
{"class_label": "non_roi", "patient_id": 3, "pred": "non_roi"},
]
result = score_2c(rows)
assert result["per_class"]["malignant_tumor"]["f1"] == 1.0
assert result["per_class"]["non_malignant_lesion"]["f1"] == 0.0
assert result["per_class"]["non_roi"]["precision"] == pytest.approx(2 / 3)
assert result["f1"] == pytest.approx(0.6)
def test_2c_reports_zero_support_without_scoring_it():
rows = [
{"class_label": "malignant_tumor", "patient_id": 1, "pred": "malignant_tumor"},
{"class_label": "non_roi", "patient_id": 2, "pred": "non_roi"},
]
result = score_2c(rows)
assert result["zero_support"] == ["non_malignant_lesion"]
assert result["f1"] == 1.0
def test_2c_flags_low_support():
rows = [
{"class_label": "malignant_tumor", "patient_id": 1, "pred": "malignant_tumor"},
{"class_label": "non_malignant_lesion", "patient_id": 2, "pred": "non_malignant_lesion"},
{"class_label": "non_roi", "patient_id": 3, "pred": "non_roi"},
]
result = score_2c(rows)
assert result["per_class"]["non_malignant_lesion"]["low_support"] is True
def test_2a_pools_counts_across_clips():
clips = [
{"clip_id": "a", "gold": [(0.0, 4.0)], "pred": [(0.0, 4.0)]},
{"clip_id": "b", "gold": [(0.0, 4.0)], "pred": []},
]
result = score_2a(clips)
assert (result["tp"], result["fp"], result["fn"]) == (1, 0, 1)
assert result["f1"] == pytest.approx(2 / 3)
assert result["count_mae"] == 0.5
def test_2b_averages_only_true_positives():
clips = [
{"clip_id": "a", "gold": [(0.0, 4.0)], "pred": [(0.0, 4.0)]},
{"clip_id": "b", "gold": [(0.0, 4.0)], "pred": [(0.0, 2.0)]},
]
assert score_2b(score_2a(clips))["tiou"] == pytest.approx((1.0 + 0.5) / 2)
def test_2b_no_gold_when_corpus_has_no_lesions():
presence = score_2a([{"clip_id": "n", "gold": [], "pred": []}])
assert score_2b(presence)["status"] == "no_gold"
def test_2b_zero_tiou_when_gold_exists_but_no_tp():
presence = score_2a([{"clip_id": "a", "gold": [(0.0, 4.0)], "pred": []}])
result = score_2b(presence)
assert result["status"] == "ok"
assert result["n_tp"] == 0
assert result["tiou"] == 0.0
def test_zero_duration_gold_is_not_a_free_tp():
result = match_intervals([(5.0, 5.0)], [(4.0, 6.0)])
assert (result["tp"], result["fp"], result["fn"]) == (0, 1, 1)
def test_negative_clip_empty_pred_is_perfect_absence():
result = score_2a([{"clip_id": "n", "gold": [], "pred": []}])
assert (result["tp"], result["fp"], result["fn"]) == (0, 0, 0)
assert result["f1"] == 1.0
def test_axis1_counts_a_landmark_inspected_at_two_seconds():
procedure = {
"procedure_id": "p",
"sex": "male",
"gold": {"trigone": [(0.0, 2.5)]},
"pred": {"trigone": [(0.0, 1.5)]},
}
result = score_axis1([procedure])
assert result["per_landmark"]["trigone"]["tp"] == 0
assert result["per_landmark"]["trigone"]["fn"] == 1
def test_axis1_skips_prostatic_urethra_for_female_procedures():
procedure = {
"procedure_id": "p",
"sex": "female",
"gold": {"prostatic_urethra": [(0.0, 5.0)]},
"pred": {},
}
assert "prostatic_urethra" not in score_axis1([procedure])["per_landmark"]
def _brute_force_overlap(gold, pred):
if not gold or not pred:
return 0.0
best = 0.0
if len(pred) <= len(gold):
for choice in itertools.permutations(range(len(gold)), len(pred)):
best = max(best, sum(overlap_s(gold[g], pred[p]) for p, g in enumerate(choice)))
else:
for choice in itertools.permutations(range(len(pred)), len(gold)):
best = max(best, sum(overlap_s(gold[g], pred[choice[g]]) for g in range(len(gold))))
return best
def test_assignment_matches_brute_force():
gold = [(0, 2), (5, 8), (10, 11)]
pred = [(1, 3), (6, 9)]
assigned = assignment_overlap(gold, pred)
assert sum(item[2] for item in assigned) == _brute_force_overlap(gold, pred)
|