File size: 1,752 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
"""Release invariants: integrity, anonymisation, and shape."""

from __future__ import annotations

import re

import polars as pl
import pytest

from cystobench.audit import audit
from cystobench.baselines import still_predictions
from cystobench.harness import score
from cystobench.loader import load_clips, load_stills

ID_PATTERNS = {"sample_id": r"^s\d{6}$", "patient_id": r"^p\d{3}$"}


@pytest.fixture(scope="module")
def stills():
    return load_stills()


def test_release_passes_audit():
    assert audit(check_media=False) == []


def test_identifiers_are_anonymised(stills):
    for column, pattern in ID_PATTERNS.items():
        assert all(re.match(pattern, v) for v in stills[column].unique().to_list()), column


def test_oracle_scores_one():
    assert score(level="stills", axis="2c", preds=still_predictions("oracle", load_stills()))["f1"] == 1.0


def test_majority_class_floor():
    result = score(
        level="stills", axis="2c", preds=still_predictions("always_non_roi", load_stills())
    )
    assert result["f1"] == pytest.approx(0.2851, abs=5e-4)


def test_stills_shape(stills):
    assert stills.height == 5807
    assert stills["patient_id"].n_unique() == 113
    assert set(stills["class_label"].unique().to_list()) == {
        "malignant_tumor",
        "non_malignant_lesion",
        "non_roi",
    }


def test_clip_shape():
    clips = load_clips()
    assert clips.height == 161
    assert clips["patient_id"].n_unique() == 27


def test_stills_and_clips_share_no_patient(stills):
    assert not set(stills["patient_id"].to_list()) & set(load_clips()["patient_id"].to_list())


def test_no_split_column_remains(stills):
    assert "split" not in stills.columns
    assert "split" not in load_clips().columns