-
Daniel CARRON authoredDaniel CARRON authored
test_hivtb.py 2.92 KiB
# SPDX-FileCopyrightText: Copyright © 2023 Idiap Research Institute <contact@idiap.ch>
#
# SPDX-License-Identifier: GPL-3.0-or-later
"""Tests for HIV-TB dataset."""
import importlib
import pytest
def id_function(val):
if isinstance(val, dict):
return str(val)
return repr(val)
@pytest.mark.parametrize(
"split,lenghts",
[
("fold-0", dict(train=174, validation=44, test=25)),
("fold-1", dict(train=174, validation=44, test=25)),
("fold-2", dict(train=174, validation=44, test=25)),
("fold-3", dict(train=175, validation=44, test=24)),
("fold-4", dict(train=175, validation=44, test=24)),
("fold-5", dict(train=175, validation=44, test=24)),
("fold-6", dict(train=175, validation=44, test=24)),
("fold-7", dict(train=175, validation=44, test=24)),
("fold-8", dict(train=175, validation=44, test=24)),
("fold-9", dict(train=175, validation=44, test=24)),
],
ids=id_function, # just changes how pytest prints it
)
def test_protocol_consistency(
database_checkers, split: str, lenghts: dict[str, int]
):
from mednet.config.data.hivtb.datamodule import make_split
database_checkers.check_split(
make_split(f"{split}.json"),
lengths=lenghts,
prefixes=("HIV-TB_Algorithm_study_X-rays",),
possible_labels=(0, 1),
)
@pytest.mark.skip_if_rc_var_not_set("datadir.hivtb")
@pytest.mark.parametrize(
"dataset",
[
"train",
"validation",
"test",
],
)
@pytest.mark.parametrize(
"name",
[
"fold_0",
"fold_1",
"fold_2",
"fold_3",
"fold_4",
"fold_5",
"fold_6",
"fold_7",
"fold_8",
"fold_9",
],
)
def test_loading(database_checkers, name: str, dataset: str):
datamodule = importlib.import_module(
f".{name}", "mednet.config.data.hivtb"
).datamodule
datamodule.model_transforms = [] # should be done before setup()
datamodule.setup("predict") # sets up all datasets
loader = datamodule.predict_dataloader()[dataset]
limit = 3 # limit load checking
for batch in loader:
if limit == 0:
break
database_checkers.check_loaded_batch(
batch,
batch_size=1,
color_planes=1,
prefixes=("HIV-TB_Algorithm_study_X-rays",),
possible_labels=(0, 1),
expected_num_labels=1,
)
limit -= 1
@pytest.mark.skip_if_rc_var_not_set("datadir.hivtb")
def test_loaded_image_quality(database_checkers, datadir):
reference_histogram_file = str(
datadir / "histograms/raw_data/histograms_hivtb_fold_0.json"
)
datamodule = importlib.import_module(
".fold_0", "mednet.config.data.hivtb"
).datamodule
datamodule.model_transforms = []
datamodule.setup("predict")
database_checkers.check_image_quality(datamodule, reference_histogram_file)