forked from paulienvoorter/TF2.4_IVIM-MRI_CodeCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
/
conftest.py
180 lines (162 loc) · 5.42 KB
/
conftest.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
import pytest
import pathlib
import json
import csv
# import datetime
def pytest_addoption(parser):
parser.addoption(
"--SNR",
default=[100],
nargs="+",
type=int,
help="Evaluation test SNR",
)
parser.addoption(
"--ricianNoise",
default=False,
type=bool,
help="Use Rician noise, non-rician is gaussian",
)
parser.addoption(
"--usePrior",
default=False,
type=bool,
help="Use a prior where accepted",
)
parser.addoption(
"--algorithmFile",
default="tests/IVIMmodels/unit_tests/algorithms.json",
type=str,
help="Algorithm file name",
)
parser.addoption(
"--dataFile",
default="tests/IVIMmodels/unit_tests/generic.json",
type=str,
help="Default data file name",
)
parser.addoption(
"--saveFileName",
default="",
type=str,
help="Saved results file name",
)
parser.addoption(
"--rtol",
default=1,
type=float,
help="Relative tolerance",
)
parser.addoption(
"--atol",
default=1,
type=float,
help="Absolute tolerance",
)
parser.addoption(
"--fitCount",
default=10,
type=int,
help="Number of fits to perform on the same parameters",
)
parser.addoption(
"--saveDurationFileName",
default="",
type=str,
help="Saved duration results file name",
)
parser.addoption(
"--selectAlgorithm",
default=[""],
nargs="+",
type=str,
help="Drop all algorithms except for these from the list"
)
parser.addoption(
"--dropAlgorithm",
default=[""],
nargs="+",
type=str,
help="Drop this algorithm from the list"
)
@pytest.fixture(scope="session")
def save_file(request):
filename = request.config.getoption("--saveFileName")
if filename:
current_folder = pathlib.Path.cwd()
filename = current_folder / filename
# print(filename)
# filename.unlink(missing_ok=True)
filename = filename.as_posix()
data = data_list(request.config.getoption("--dataFile")) # TODO: clean up this hacky way to get bvalues
[_, bvalues, _] = next(data)
bvalue_string = ["bval_" + str(bvalue) for bvalue in bvalues]
# bvalue_string = ["b_0.0","b_1.0","b_2.0","b_5.0","b_10.0","b_20.0","b_30.0","b_50.0","b_75.0","b_100.0","b_150.0","b_250.0","b_350.0","b_400.0","b_550.0","b_700.0","b_850.0","b_1000.0"]
with open(filename, "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(("Algorithm", "Region", "SNR", "index", "f", "Dp", "D", "f_fitted", "Dp_fitted", "D_fitted", *bvalue_string))
yield writer
# writer.writerow(["", datetime.datetime.now()])
else:
yield None
# return filename
@pytest.fixture(scope="session")
def save_duration_file(request):
filename = request.config.getoption("--saveDurationFileName")
if filename:
current_folder = pathlib.Path.cwd()
filename = current_folder / filename
# print(filename)
# filename.unlink(missing_ok=True)
filename = filename.as_posix()
with open(filename, "w") as csv_file:
writer = csv.writer(csv_file, delimiter=',')
writer.writerow(("Algorithm", "Region", "SNR", "Duration [us]", "Count"))
yield writer
# writer.writerow(["", datetime.datetime.now()])
else:
yield None
# return filename
@pytest.fixture(scope="session")
def rtol(request):
return request.config.getoption("--rtol")
@pytest.fixture(scope="session")
def atol(request):
return request.config.getoption("--atol")
@pytest.fixture(scope="session")
def fit_count(request):
return request.config.getoption("--fitCount")
@pytest.fixture(scope="session")
def rician_noise(request):
return request.config.getoption("--ricianNoise")
@pytest.fixture(scope="session")
def use_prior(request):
return request.config.getoption("--usePrior")
def pytest_generate_tests(metafunc):
if "SNR" in metafunc.fixturenames:
metafunc.parametrize("SNR", metafunc.config.getoption("SNR"))
if "ivim_algorithm" in metafunc.fixturenames:
algorithms = algorithm_list(metafunc.config.getoption("algorithmFile"), metafunc.config.getoption("selectAlgorithm"), metafunc.config.getoption("dropAlgorithm"))
metafunc.parametrize("ivim_algorithm", algorithms)
if "ivim_data" in metafunc.fixturenames:
data = data_list(metafunc.config.getoption("dataFile"))
metafunc.parametrize("ivim_data", data)
def algorithm_list(filename, selected, dropped):
current_folder = pathlib.Path.cwd()
algorithm_path = current_folder / filename
with algorithm_path.open() as f:
algorithm_information = json.load(f)
algorithms = set(algorithm_information["algorithms"])
algorithms = algorithms - set(dropped)
if len(selected) > 0 and selected[0]:
algorithms = algorithms & set(selected)
return list(algorithms)
def data_list(filename):
current_folder = pathlib.Path.cwd()
data_path = current_folder / filename
with data_path.open() as f:
all_data = json.load(f)
bvals = all_data.pop('config')
bvals = bvals['bvalues']
for name, data in all_data.items():
yield name, bvals, data