Skip to content

Commit

Permalink
Fix issue in compare_folders (#610)
Browse files Browse the repository at this point in the history
* add FileNotFound error in tree
  • Loading branch information
thibaultdvx authored Jun 4, 2024
1 parent f20e7fb commit f6f382a
Show file tree
Hide file tree
Showing 9 changed files with 128 additions and 76 deletions.
4 changes: 2 additions & 2 deletions tests/test_generate.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,12 @@ def test_generate(cmdopt, tmp_path, test_name):
"t1-linear",
]
elif test_name == "hypometabolic_example":
output_folder = str(tmp_out_dir / test_name)
output_folder = tmp_out_dir / test_name
test_input = [
"generate",
"hypometabolic",
data_caps_pet,
output_folder,
str(output_folder),
"--n_subjects",
"2",
"--pathology",
Expand Down
69 changes: 47 additions & 22 deletions tests/test_predict.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
# coding: utf8
import json
import os
import shutil
from os.path import exists
from pathlib import Path

import pytest

from clinicadl import MapsManager
from tests.testing_tools import clean_folder, compare_folders

from .testing_tools import compare_folders, modify_maps


@pytest.fixture(
Expand All @@ -33,46 +33,71 @@ def test_predict(cmdopt, tmp_path, test_name):
tmp_out_dir.mkdir(parents=True)

if test_name == "predict_image_classification":
model_folder = input_dir / "maps_image_cnn"
maps_name = "maps_image_cnn"
modes = ["image"]
use_labels = True
elif test_name == "predict_slice_classification":
model_folder = input_dir / "maps_slice_cnn"
maps_name = "maps_slice_cnn"
modes = ["image", "slice"]
use_labels = True
elif test_name == "predict_patch_regression":
model_folder = input_dir / "maps_patch_cnn"
maps_name = "maps_patch_cnn"
modes = ["image", "patch"]
use_labels = False
elif test_name == "predict_roi_regression":
model_folder = input_dir / "maps_roi_cnn"
maps_name = "maps_roi_cnn"
modes = ["image", "roi"]
use_labels = False
elif test_name == "predict_patch_multi_classification":
model_folder = input_dir / "maps_patch_multi_cnn"
maps_name = "maps_patch_multi_cnn"
modes = ["image", "patch"]
use_labels = False
elif test_name == "predict_roi_reconstruction":
model_folder = input_dir / "maps_roi_ae"
maps_name = "maps_roi_ae"
modes = ["roi"]
use_labels = False
else:
raise NotImplementedError(f"Test {test_name} is not implemented.")

out_dir = str(model_folder / "split-0/best-loss/test-RANDOM")
shutil.copytree(input_dir / maps_name, tmp_out_dir / maps_name)
model_folder = tmp_out_dir / maps_name

if cmdopt["adapt-base-dir"]:
with open(model_folder / "maps.json", "r") as f:
config = json.load(f)
config = modify_maps(
maps=config,
base_dir=base_dir,
no_gpu=cmdopt["no-gpu"],
adapt_base_dir=cmdopt["adapt-base-dir"],
)
with open(model_folder / "maps.json", "w") as f:
json.dump(config, f, skipkeys=True, indent=4)

with open(model_folder / "groups/test-RANDOM/maps.json", "r") as f:
config = json.load(f)
config = modify_maps(
maps=config,
base_dir=base_dir,
no_gpu=False,
adapt_base_dir=cmdopt["adapt-base-dir"],
)
with open(model_folder / "groups/test-RANDOM/maps.json", "w") as f:
json.dump(config, f, skipkeys=True, indent=4)

if exists(out_dir):
shutil.rmtree(out_dir)
tmp_out_subdir = str(model_folder / "split-0/best-loss/test-RANDOM")
if exists(tmp_out_subdir):
shutil.rmtree(tmp_out_subdir)

# Correction of JSON file for ROI
if "roi" in modes:
json_path = model_folder / "maps.json"
with open(json_path, "r") as f:
parameters = json.load(f)
parameters["roi_list"] = ["leftHippocampusBox", "rightHippocampusBox"]
json_data = json.dumps(parameters, skipkeys=True, indent=4)
with open(json_path, "w") as f:
f.write(json_data)
# # Correction of JSON file for ROI
# if "roi" in modes:
# json_path = model_folder / "maps.json"
# with open(json_path, "r") as f:
# parameters = json.load(f)
# parameters["roi_list"] = ["leftHippocampusBox", "rightHippocampusBox"]
# json_data = json.dumps(parameters, skipkeys=True, indent=4)
# with open(json_path, "w") as f:
# f.write(json_data)

maps_manager = MapsManager(model_folder, verbose="debug")
maps_manager.predict(
Expand All @@ -91,7 +116,7 @@ def test_predict(cmdopt, tmp_path, test_name):
maps_manager.get_metrics(data_group="test-RANDOM", mode=mode)

assert compare_folders(
tmp_out_dir / test_name,
ref_dir / test_name,
tmp_out_dir / maps_name,
input_dir / maps_name,
tmp_out_dir,
)
14 changes: 7 additions & 7 deletions tests/test_qc.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,29 +22,29 @@ def test_qc(cmdopt, tmp_path, test_name):
tmp_out_dir.mkdir(parents=True)

if test_name == "t1-linear":
out_tsv = str(tmp_out_dir / "QC.tsv")
out_tsv = tmp_out_dir / "QC.tsv"
test_input = [
"t1-linear",
str(input_dir / "caps"),
out_tsv,
str(out_tsv),
"--no-gpu",
]

elif test_name == "t1-volume":
out_dir = str(tmp_out_dir / "QC_T1V")
out_dir = tmp_out_dir / "QC_T1V"
test_input = [
"t1-volume",
str(input_dir / "caps_T1V"),
out_dir,
str(out_dir),
"Ixi549Space",
]

elif test_name == "pet-linear":
out_tsv = str(tmp_out_dir / "QC_pet.tsv")
out_tsv = tmp_out_dir / "QC_pet.tsv"
test_input = [
"pet-linear",
str(input_dir / "caps_pet"),
out_tsv,
str(out_tsv),
"18FFDG",
"cerebellumPons2",
"--threshold",
Expand Down Expand Up @@ -73,7 +73,7 @@ def test_qc(cmdopt, tmp_path, test_name):
assert out_df.equals(ref_df)

elif test_name == "t1-volume":
assert compare_folders(out_dir, str(ref_dir / "QC_T1V"), tmp_out_dir)
assert compare_folders(out_dir, ref_dir / "QC_T1V", tmp_out_dir)

elif test_name == "pet-linear":
out_df = pd.read_csv(out_tsv, sep="\t")
Expand Down
24 changes: 11 additions & 13 deletions tests/test_random_search.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@

import pytest

from .testing_tools import change_gpu_in_toml, compare_folders
from .testing_tools import compare_folders, modify_toml


# random searxh for ROI with CNN
Expand All @@ -25,6 +25,9 @@ def test_random_search(cmdopt, tmp_path, test_name):
input_dir = base_dir / "randomSearch" / "in"
ref_dir = base_dir / "randomSearch" / "ref"
tmp_out_dir = tmp_path / "randomSearch" / "out"

if os.path.exists(tmp_out_dir):
shutil.rmtree(tmp_out_dir)
tmp_out_dir.mkdir(parents=True)

if test_name == "rs_roi_cnn":
Expand All @@ -33,21 +36,16 @@ def test_random_search(cmdopt, tmp_path, test_name):
else:
raise NotImplementedError(f"Test {test_name} is not implemented.")

run_test_random_search(
toml_path, generate_input, tmp_out_dir, ref_dir, cmdopt["no-gpu"]
)


def run_test_random_search(toml_path, generate_input, tmp_out_dir, ref_dir, no_gpu):
if os.path.exists(tmp_out_dir):
shutil.rmtree(tmp_out_dir)

# Write random_search.toml file
os.makedirs(tmp_out_dir, exist_ok=True)
shutil.copy(toml_path, tmp_out_dir)

if no_gpu:
change_gpu_in_toml(tmp_out_dir / "random_search.toml")
if cmdopt["no-gpu"] or cmdopt["adapt-base-dir"]:
modify_toml(
toml_path=tmp_out_dir / "random_search.toml",
base_dir=base_dir,
no_gpu=cmdopt["no-gpu"],
adapt_base_dir=cmdopt["adapt-base-dir"],
)

flag_error_generate = not os.system("clinicadl " + " ".join(generate_input))
performances_flag = os.path.exists(
Expand Down
2 changes: 1 addition & 1 deletion tests/test_resume.py
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ def test_resume(cmdopt, tmp_path, test_name):
adapt_base_dir=cmdopt["adapt-base-dir"],
)
with open(maps_stopped / "maps.json", "w") as f:
json.dump(config, f)
json.dump(config, f, skipkeys=True, indent=4)

flag_error = not system(f"clinicadl -vv train resume {maps_stopped}")
assert flag_error
Expand Down
13 changes: 6 additions & 7 deletions tests/test_train_ae.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,10 @@ def test_train_ae(cmdopt, tmp_path, test_name):

labels_path = str(input_dir / "labels_list" / "2_fold")
config_path = str(input_dir / "train_config.toml")
split = 0

if test_name == "image_ae":
split = [0, 0]
split = 1
test_input = [
"train",
"reconstruction",
Expand All @@ -45,10 +47,9 @@ def test_train_ae(cmdopt, tmp_path, test_name):
"-c",
config_path,
"--split",
"1",
str(split),
]
elif test_name == "patch_multi_ae":
split = [0, 0]
test_input = [
"train",
"reconstruction",
Expand All @@ -61,7 +62,6 @@ def test_train_ae(cmdopt, tmp_path, test_name):
"--multi_network",
]
elif test_name == "roi_ae":
split = [0, 0]
test_input = [
"train",
"reconstruction",
Expand All @@ -73,7 +73,6 @@ def test_train_ae(cmdopt, tmp_path, test_name):
config_path,
]
elif test_name == "slice_ae":
split = [0, 0]
test_input = [
"train",
"reconstruction",
Expand Down Expand Up @@ -116,7 +115,7 @@ def test_train_ae(cmdopt, tmp_path, test_name):
tmp_path,
)
assert compare_folders(
tmp_out_dir / f"split-{split[0]}" / "best-loss",
ref_dir / ("maps_" + test_name) / f"split-{split[1]}" / "best-loss",
tmp_out_dir / f"split-{split}" / "best-loss",
ref_dir / ("maps_" + test_name) / f"split-{split}" / "best-loss",
tmp_path,
)
2 changes: 1 addition & 1 deletion tests/test_train_from_json.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def test_json_compatibility(cmdopt, tmp_path):
adapt_base_dir=cmdopt["adapt-base-dir"],
)
with open(config_json, "w+") as f:
json.dump(config, f)
json.dump(config, f, skipkeys=True, indent=4)

flag_error = not system(
f"clinicadl train from_json {str(config_json)} {str(reproduced_maps_dir)} -s {split}"
Expand Down
25 changes: 14 additions & 11 deletions tests/test_transfer_learning.py
Original file line number Diff line number Diff line change
Expand Up @@ -152,20 +152,23 @@ def test_transfer_learning(cmdopt, tmp_path, test_name):
with open(ref_dir / ("maps_roi_" + name) / "maps.json", "r") as ref:
json_data_ref = json.load(ref)

ref_source_dir = Path(json_data_ref["transfer_path"]).parent
json_data_ref["transfer_path"] = str(
tmp_out_dir / Path(json_data_ref["transfer_path"]).relative_to(ref_source_dir)
)
if cmdopt["no-gpu"] or cmdopt["adapt-base-dir"]:
json_data_ref = modify_maps(
maps=json_data_ref,
base_dir=base_dir,
no_gpu=cmdopt["no-gpu"],
adapt_base_dir=cmdopt["adapt-base-dir"],
)
# TODO : uncomment when CI data are correct
# ref_source_dir = Path(json_data_ref["transfer_path"]).parent
# json_data_ref["transfer_path"] = str(
# tmp_out_dir / Path(json_data_ref["transfer_path"]).relative_to(ref_source_dir)
# )
# if cmdopt["no-gpu"] or cmdopt["adapt-base-dir"]:
# json_data_ref = modify_maps(
# maps=json_data_ref,
# base_dir=base_dir,
# no_gpu=cmdopt["no-gpu"],
# adapt_base_dir=cmdopt["adapt-base-dir"],
# )
# TODO: remove and update data
json_data_ref["caps_directory"] = json_data_out["caps_directory"]
json_data_ref["gpu"] = json_data_out["gpu"]
json_data_ref["transfer_path"] = json_data_out["transfer_path"]
json_data_ref["tsv_path"] = json_data_out["tsv_path"]
###
assert json_data_out == json_data_ref # ["mode"] == mode

Expand Down
Loading

0 comments on commit f6f382a

Please sign in to comment.