Skip to content

Commit

Permalink
fix broken unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
NicolasGensollen committed Jul 11, 2024
1 parent 2fb77c9 commit d1540dc
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 12 deletions.
21 changes: 12 additions & 9 deletions clinica/utils/caps.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,13 @@
from cattr.gen import make_dict_unstructure_fn, override
from cattr.preconf.json import make_converter

# from clinica.utils.input import DatasetType
from clinica.utils.bids import BIDS_VERSION
from clinica.utils.exceptions import ClinicaCAPSError
from clinica.utils.inputs import DatasetType

CAPS_VERSION = "1.0.0"


class DatasetType(str, Enum):
RAW = "raw"
DERIVATIVE = "derivative"


@define
class CAPSDatasetDescription:
"""Model representing a CAPS dataset description."""
Expand Down Expand Up @@ -55,7 +51,7 @@ def from_file(cls, json_file: Path):
DatasetType(parsed["DatasetType"]),
)
except KeyError:
raise ValueError(
raise ClinicaCAPSError(
f"CAPS dataset_description.json file {json_file} is not valid and "
"cannot be parsed as a CAPSDatasetDescription. "
"Please verify that the file is well formatted."
Expand Down Expand Up @@ -103,17 +99,24 @@ def write_caps_dataset_description(
caps_version: Optional[str] = None,
) -> None:
"""Write `dataset_description.json` at the root of the CAPS directory."""
from clinica.utils.stream import cprint

new_desc = CAPSDatasetDescription.from_values(name, bids_version, caps_version)
if (caps_dir / "dataset_description.json").exists():
print(f"The CAPS dataset already contains a dataset_description.json file.")
cprint(
f"The CAPS dataset {name} already contains a dataset_description.json file.",
lvl="info",
)
previous_desc = CAPSDatasetDescription.from_file(
caps_dir / "dataset_description.json"
)
if not previous_desc.is_compatible_with(new_desc):
raise ValueError(
msg = (
f"Impossible to write the dataset_description.json file in {caps_dir} "
"because it already exists and it contains incompatible metadata."
)
cprint(msg, lvl="error")
raise ClinicaCAPSError(msg)
if previous_desc.name != new_desc.name:
new_desc.name = f"{previous_desc.name} + {new_desc.name}"
with open(caps_dir / "dataset_description.json", "w") as f:
Expand Down
5 changes: 5 additions & 0 deletions clinica/utils/testing_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,11 @@ def build_caps_directory(directory: os.PathLike, configuration: dict) -> None:
This function is a simple prototype for creating fake datasets for testing.
"""
directory = Path(directory)
with open(directory / "dataset_description.json", "w") as fp:
json.dump(
{"Name": "Example dataset", "BIDSVersion": "1.0.2", "CAPSVersion": "1.0.0"},
fp,
)
_build_groups(directory, configuration)
_build_subjects(directory, configuration)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ def test_anat_linear_pipeline_single_bids_input_error(tmp_path):
with pytest.raises(
ClinicaBIDSError,
match=re.escape(
f"The BIDS directory ({tmp_path}) you provided "
f"The raw directory ({tmp_path}) you provided "
"is missing a dataset_description.json file."
),
):
Expand All @@ -37,7 +37,7 @@ def test_anat_linear_pipeline_single_caps_input_error(tmp_path):
with pytest.raises(
ClinicaCAPSError,
match=re.escape(
f"The CAPS directory ({tmp_path}) you provided "
f"The derivative directory ({tmp_path}) you provided "
"is missing a dataset_description.json file."
),
):
Expand Down
3 changes: 2 additions & 1 deletion test/unittests/utils/test_caps.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,7 @@ def test_write_caps_dataset_description_error(tmp_path):
DatasetType,
write_caps_dataset_description,
)
from clinica.utils.exceptions import ClinicaCAPSError

caps_dir = tmp_path / "caps"
caps_dir.mkdir()
Expand All @@ -91,7 +92,7 @@ def test_write_caps_dataset_description_error(tmp_path):

# But re-writing a different description raises an error
with pytest.raises(
ValueError,
ClinicaCAPSError,
match=(
f"Impossible to write the dataset_description.json file in {caps_dir} "
"because it already exists and it contains incompatible metadata."
Expand Down
18 changes: 18 additions & 0 deletions test/unittests/utils/test_utils_inputs.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import json
import os
import re
from pathlib import Path
Expand Down Expand Up @@ -354,6 +355,18 @@ def test_check_caps_folder(tmp_path):

(tmp_path / "subjects").mkdir()
(tmp_path / "subjects" / "foo.txt").mkdir()
with pytest.raises(
ClinicaCAPSError,
match=re.escape(
f"The derivative directory ({tmp_path}) you provided is missing a dataset_description.json file."
),
):
check_caps_folder(tmp_path)
with open(tmp_path / "dataset_description.json", "w") as fp:
json.dump(
{"Name": "Example dataset", "BIDSVersion": "1.0.2", "CAPSVersion": "1.0.0"},
fp,
)
assert check_caps_folder(tmp_path) is None
(tmp_path / "sub-01").mkdir()
with pytest.raises(
Expand Down Expand Up @@ -709,6 +722,11 @@ def test_clinica_file_reader_dwi_dti(tmp_path):
/ "native_space"
)
dti_folder.mkdir(parents=True)
with open(tmp_path / "dataset_description.json", "w") as fp:
json.dump(
{"Name": "Example dataset", "BIDSVersion": "1.0.2", "CAPSVersion": "1.0.0"},
fp,
)
for measure in DTIBasedMeasure:
(dti_folder / f"sub-01_ses-M000_space-T1w_{measure.value}.nii.gz").touch()
query = dwi_dti("FA", space="T1w")
Expand Down

0 comments on commit d1540dc

Please sign in to comment.