-
Notifications
You must be signed in to change notification settings - Fork 57
/
Copy pathtest_interpret.py
82 lines (67 loc) · 2.22 KB
/
test_interpret.py
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
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
# coding: utf8
import json
import os
import shutil
from pathlib import Path
import pytest
from clinicadl import MapsManager
from tests.testing_tools import clean_folder, compare_folders
@pytest.fixture(params=["classification", "regression"])
def test_name(request):
return request.param
def test_interpret(cmdopt, tmp_path, test_name):
base_dir = Path(cmdopt["input"])
input_dir = base_dir / "interpret" / "in"
ref_dir = base_dir / "interpret" / "ref"
tmp_out_dir = tmp_path / "interpret" / "out"
tmp_out_dir.mkdir(parents=True)
labels_dir_str = str(input_dir / "labels_list" / "2_fold")
maps_tmp_out_dir = str(tmp_out_dir / "maps")
if test_name == "classification":
cnn_input = [
"train",
"classification",
str(input_dir / "caps_image"),
"t1-linear_mode-image.json",
labels_dir_str,
maps_tmp_out_dir,
"--architecture Conv5_FC3",
"--epochs",
"1",
"--n_splits",
"2",
"--split",
"0",
]
elif test_name == "regression":
cnn_input = [
"train",
"regression",
str(input_dir / "caps_patch"),
"t1-linear_mode-patch.json",
labels_dir_str,
maps_tmp_out_dir,
"--architecture Conv5_FC3",
"--epochs",
"1",
"--n_splits",
"2",
"--split",
"0",
]
else:
raise NotImplementedError(f"Test {test_name} is not implemented.")
if cmdopt["no-gpu"]:
cnn_input.append("--no-gpu")
run_interpret(cnn_input, tmp_out_dir, ref_dir)
def run_interpret(cnn_input, tmp_out_dir, ref_dir):
from clinicadl.interpret.gradients import method_dict
maps_path = tmp_out_dir / "maps"
if maps_path.is_dir():
shutil.rmtree(maps_path)
train_error = not os.system("clinicadl " + " ".join(cnn_input))
assert train_error
maps_manager = MapsManager(maps_path, verbose="debug")
for method in method_dict.keys():
maps_manager.interpret("train", f"test-{method}", method)
interpret_map = maps_manager.get_interpretation("train", f"test-{method}")