bench / tests /test_scoring.py
milkyroad's picture
Upload folder using huggingface_hub (part 12)
11ec37f verified
Raw History Blame Contribute Delete
5.66 kB
"""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)