forked from OSIPI/TF2.4_IVIM-MRI_CodeCollection
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconftest.py
More file actions
322 lines (282 loc) · 11.2 KB
/
conftest.py
File metadata and controls
322 lines (282 loc) · 11.2 KB
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
import pytest
import pathlib
import json
import csv
# import datetime
import numpy as np
from phantoms.MR_XCAT_qMRI.sim_ivim_sig import phantom
import warnings
from tests.IVIMmodels.unit_tests.test_ivim_fit import PerformanceWarning
warnings.simplefilter("always", PerformanceWarning)
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(
"--dataFileDL",
default="tests/IVIMmodels/unit_tests/generic_DL.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"
)
parser.addoption(
"--withmatlab",
action="store_true",
default=False,
help="Run MATLAB-dependent tests"
)
@pytest.fixture(scope="session")
def eng(request):
"""Start and return a MATLAB engine session if --withmatlab is set."""
if not request.config.getoption("--withmatlab"):
return None
import matlab.engine
print("Starting MATLAB engine...")
eng = matlab.engine.start_matlab()
print("MATLAB engine started.")
return eng
@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):
config = metafunc.config
algorithms = load_filtered_algorithms(config)
if "SNR" in metafunc.fixturenames:
metafunc.parametrize("SNR", config.getoption("SNR"))
if "ivim_data" in metafunc.fixturenames:
data = data_list(config.getoption("dataFile"))
metafunc.parametrize("ivim_data", data)
if "data_ivim_fit_saved" in metafunc.fixturenames:
args = data_ivim_fit_saved(config.getoption("dataFile"), algorithms)
metafunc.parametrize("data_ivim_fit_saved", args)
if "algorithmlist" in metafunc.fixturenames:
args = algorithmlist(algorithms)
metafunc.parametrize("algorithmlist", args)
if "bound_input" in metafunc.fixturenames:
args = bound_input(config.getoption("dataFile"), algorithms)
metafunc.parametrize("bound_input", args)
if "deep_learning_algorithms" in metafunc.fixturenames:
args = deep_learning_algorithms(config.getoption("dataFileDL"), algorithms)
metafunc.parametrize("deep_learning_algorithms", args)
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
def load_filtered_algorithms(config):
algorithmFile = config.getoption("algorithmFile")
selectAlgorithm = config.getoption("selectAlgorithm")
dropAlgorithm = config.getoption("dropAlgorithm")
current_folder = pathlib.Path.cwd()
algorithm_path = current_folder / algorithmFile
with algorithm_path.open() as f:
algorithm_info = json.load(f)
algorithms = algorithm_info["algorithms"]
if selectAlgorithm and selectAlgorithm != [""]:
algorithms = [alg for alg in algorithms if alg in selectAlgorithm]
else:
algorithms = [alg for alg in algorithms if alg not in dropAlgorithm]
return {
"algorithms": algorithms,
**{k: v for k, v in algorithm_info.items() if k != "algorithms"}
}
def data_ivim_fit_saved(datafile, algorithms):
# Find the algorithms from algorithms.json
current_folder = pathlib.Path.cwd()
# Load generic test data generated from the included phantom: phantoms/MR_XCAT_qMRI
generic = current_folder / datafile
with generic.open() as f:
all_data = json.load(f)
bvals = all_data.pop('config')
bvals = bvals['bvalues']
for algorithm in algorithms["algorithms"]:
first = True
for name, data in all_data.items():
algorithm_dict = algorithms.get(algorithm, {})
if not algorithm_dict.get('deep_learning',False):
xfail = {"xfail": name in algorithm_dict.get("xfail_names", {}),
"strict": algorithm_dict.get("xfail_names", {}).get(name, True)}
kwargs = algorithm_dict.get("options", {})
tolerances = algorithm_dict.get("tolerances", {})
skiptime=False
if first:
if algorithm_dict.get("fail_first_time", False):
skiptime = True
first = False
requires_matlab = algorithm_dict.get("requires_matlab", False)
yield name, bvals, data, algorithm, xfail, kwargs, tolerances, skiptime, requires_matlab
def algorithmlist(algorithms):
for algorithm in algorithms["algorithms"]:
algorithm_dict = algorithms.get(algorithm, {})
requires_matlab = algorithm_dict.get("requires_matlab", False)
yield algorithm, requires_matlab, algorithm_dict.get('deep_learning', False)
def bound_input(datafile, algorithms):
current_folder = pathlib.Path.cwd()
# Load generic test data generated from the included phantom: phantoms/MR_XCAT_qMRI
generic = current_folder / datafile
with generic.open() as f:
all_data = json.load(f)
bvals = all_data.pop('config')
bvals = bvals['bvalues']
for name, data in all_data.items():
for algorithm in algorithms["algorithms"]:
algorithm_dict = algorithms.get(algorithm, {})
if not algorithm_dict.get('deep_learning',False):
xfail = {"xfail": name in algorithm_dict.get("xfail_names", {}) or "bounds" in algorithm_dict.get("xfail_names", {}),
"strict": algorithm_dict.get("xfail_names", {}).get("bounds", algorithm_dict.get("xfail_names", {}).get(name,True))}
kwargs = algorithm_dict.get("options", {})
tolerances = algorithm_dict.get("tolerances", {})
requires_matlab = algorithm_dict.get("requires_matlab", False)
yield name, bvals, data, algorithm, xfail, kwargs, tolerances, requires_matlab
def deep_learning_algorithms(datafile, algorithms):
# Find the algorithms from algorithms.json
current_folder = pathlib.Path.cwd()
# Load generic test data generated from the included phantom: phantoms/MR_XCAT_qMRI
generic = current_folder / datafile
with generic.open() as f:
all_data = json.load(f)
bvals = all_data.pop('config')
bvals = bvals['bvalues']
for algorithm in algorithms["algorithms"]:
algorithm_dict = algorithms.get(algorithm, {})
if algorithm_dict.get('deep_learning',False):
kwargs = algorithm_dict.get("options", {})
requires_matlab = algorithm_dict.get("requires_matlab", False)
tolerances = algorithm_dict.get("tolerances", {"atol":{"f": 2e-1, "D": 8e-4, "Dp": 8e-2},"rtol":{"f": 0.2, "D": 0.3, "Dp": 0.4}})
yield algorithm, all_data, bvals, kwargs, requires_matlab, tolerances
@pytest.fixture(scope="session")
def threeddata(request):
current_folder = pathlib.Path.cwd()
datafile = request.config.getoption("dataFile")
generic = current_folder / datafile
with generic.open() as f:
all_data = json.load(f)
bvals = all_data.pop('config')
bvals = np.array(bvals['bvalues'])
sig, _, Dim, fim, Dpim, _=phantom(bvals, 1/1000, TR=3000, TE=40, motion=False, rician=False, interleaved=False, T1T2=True)
return sig[::16,::8,::6,:], Dim[::16,::8,::6], fim[::16,::8,::6], Dpim[::16,::8,::6], bvals