|
10 | 10 |
|
11 | 11 | #run using python -m pytest from the root folder
|
12 | 12 |
|
| 13 | + |
| 14 | +# @pytest.fixture |
| 15 | +# def algorithm_fixture() |
| 16 | +# def test_fixtures() |
| 17 | + |
| 18 | +# use a fixture to generate data |
| 19 | +# either read a config file for the test or perhaps hard code a few fixtures and usefixtures in the config? |
| 20 | +# use a fixture to save data |
| 21 | + |
| 22 | +# def algorithm_list(): |
| 23 | +# # Find the algorithms from algorithms.json |
| 24 | +# file = pathlib.Path(__file__) |
| 25 | +# algorithm_path = file.with_name('algorithms.json') |
| 26 | +# with algorithm_path.open() as f: |
| 27 | +# algorithm_information = json.load(f) |
| 28 | +# return algorithm_information["algorithms"] |
| 29 | + |
| 30 | +# @pytest.fixture(params=algorithm_list()) |
| 31 | +# def algorithm_fixture(request): |
| 32 | +# # assert request.param == "algorithms" |
| 33 | +# yield request.param |
| 34 | + |
| 35 | + |
| 36 | + |
| 37 | +# @pytest.fixture(params=SNR) |
| 38 | +# def noise_fixture(request): |
| 39 | +# return request.config.getoption("--noise") |
| 40 | + |
| 41 | +# @pytest.fixture |
| 42 | +# def noise_fixture(request): |
| 43 | +# yield request.param |
| 44 | + |
| 45 | +# @pytest.mark.parametrize("S", [SNR]) |
| 46 | +# @pytest.mark.parametrize("D, Dp, f, bvals", [[0.0015, 0.1, 0.11000000000000007,[0, 5, 10, 50, 100, 200, 300, 500, 1000]]]) |
| 47 | +# def test_generated(ivim_algorithm, ivim_data, SNR): |
| 48 | +# S0 = 1 |
| 49 | +# gd = GenerateData() |
| 50 | +# name, bvals, data = ivim_data |
| 51 | +# D = data["D"] |
| 52 | +# f = data["f"] |
| 53 | +# Dp = data["Dp"] |
| 54 | +# if "data" not in data: |
| 55 | +# signal = gd.ivim_signal(D, Dp, f, S0, bvals, SNR) |
| 56 | +# else: |
| 57 | +# signal = data["data"] |
| 58 | +# fit = OsipiBase(algorithm=ivim_algorithm) |
| 59 | +# [f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals) |
| 60 | +# npt.assert_allclose([f, D, Dp], [f_fit, D_fit, Dp_fit]) |
| 61 | + |
| 62 | + |
| 63 | + |
13 | 64 | # test_linear_data = [
|
14 | 65 | # pytest.param(0, np.linspace(0, 1000, 11), id='0'),
|
15 | 66 | # pytest.param(0.01, np.linspace(0, 1000, 11), id='0.1'),
|
|
57 | 108 | #if not np.allclose(f, 0):
|
58 | 109 | #npt.assert_allclose(Dp, Dp_fit, rtol=1e-2, atol=1e-3)
|
59 | 110 |
|
| 111 | + |
| 112 | +# convert the algorithm list and signal list to fixtures that read from the files into params (scope="session") |
| 113 | +# from that helpers can again parse the files? |
| 114 | + |
| 115 | +def signal_helper(signal): |
| 116 | + signal = np.asarray(signal) |
| 117 | + signal = np.abs(signal) |
| 118 | + signal /= signal[0] |
| 119 | + ratio = 1 / signal[0] |
| 120 | + return signal, ratio |
| 121 | + |
| 122 | +def tolerances_helper(tolerances, ratio, noise): |
| 123 | + if "dynamic_rtol" in tolerances: |
| 124 | + dyn_rtol = tolerances["dynamic_rtol"] |
| 125 | + scale = dyn_rtol["offset"] + dyn_rtol["ratio"]*ratio + dyn_rtol["noise"]*noise + dyn_rtol["noiseCrossRatio"]*ratio*noise |
| 126 | + tolerances["rtol"] = {"f": scale*dyn_rtol["f"], "D": scale*dyn_rtol["D"], "Dp": scale*dyn_rtol["Dp"]} |
| 127 | + else: |
| 128 | + tolerances["rtol"] = tolerances.get("rtol", {"f": 5, "D": 5, "Dp": 25}) |
| 129 | + if "dynamic_atol" in tolerances: |
| 130 | + dyn_atol = tolerances["dynamic_atol"] |
| 131 | + scale = dyn_atol["offset"] + dyn_atol["ratio"]*ratio + dyn_atol["noise"]*noise + dyn_atol["noiseCrossRatio"]*ratio*noise |
| 132 | + tolerances["atol"] = {"f": scale*dyn_atol["f"], "D": scale*dyn_atol["D"], "Dp": scale*dyn_atol["Dp"]} |
| 133 | + else: |
| 134 | + tolerances["atol"] = tolerances.get("atol", {"f": 1e-2, "D": 1e-2, "Dp": 1e-1}) |
| 135 | + return tolerances |
| 136 | + |
60 | 137 | def data_ivim_fit_saved():
|
61 | 138 | # Find the algorithms from algorithms.json
|
62 | 139 | file = pathlib.Path(__file__)
|
@@ -88,40 +165,8 @@ def test_ivim_fit_saved(name, bvals, data, algorithm, xfail, kwargs, tolerances,
|
88 | 165 | mark = pytest.mark.xfail(reason="xfail", strict=xfail["strict"])
|
89 | 166 | request.node.add_marker(mark)
|
90 | 167 | fit = OsipiBase(algorithm=algorithm, **kwargs)
|
91 |
| - signal = np.asarray(data['data']) |
92 |
| - signal = np.abs(signal) |
93 |
| - signal /= signal[0] |
94 |
| - ratio = 1 / signal[0] |
95 |
| - # has_negatives = np.any(signal<0) |
96 |
| - if "dynamic_rtol" in tolerances: |
97 |
| - dyn_rtol = tolerances["dynamic_rtol"] |
98 |
| - scale = dyn_rtol["offset"] + dyn_rtol["ratio"]*ratio + dyn_rtol["noise"]*data["noise"] + dyn_rtol["noiseCrossRatio"]*ratio*data["noise"] |
99 |
| - tolerances["rtol"] = {"f": scale*dyn_rtol["f"], "D": scale*dyn_rtol["D"], "Dp": scale*dyn_rtol["Dp"]} |
100 |
| - else: |
101 |
| - tolerances["rtol"] = tolerances.get("rtol", {"f": 5, "D": 5, "Dp": 25}) |
102 |
| - if "dynamic_atol" in tolerances: |
103 |
| - dyn_atol = tolerances["dynamic_atol"] |
104 |
| - scale = dyn_atol["offset"] + dyn_atol["ratio"]*ratio + dyn_atol["noise"]*data["noise"] + dyn_atol["noiseCrossRatio"]*ratio*data["noise"] |
105 |
| - tolerances["atol"] = {"f": scale*dyn_atol["f"], "D": scale*dyn_atol["D"], "Dp": scale*dyn_atol["Dp"]} |
106 |
| - else: |
107 |
| - tolerances["atol"] = tolerances.get("atol", {"f": 1e-2, "D": 1e-2, "Dp": 1e-1}) |
108 |
| - # if tolerances: |
109 |
| - # # signal = np.abs(signal) |
110 |
| - # # ratio = 1 / signal[0] |
111 |
| - # # signal /= signal[0] |
112 |
| - # # tolerance = 1e-2 + 25 * data['noise'] * ratio # totally empirical |
113 |
| - # # tolerance = 1 |
114 |
| - # tolerances = {"rtol": {"f": 5, "D": 5, "Dp": 25}, |
115 |
| - # "atol": {"f": 1e-2, "D": 1e-2, "Dp": 1e-1}} |
116 |
| - #if has_negatives: # this fitting doesn't do well with negatives |
117 |
| - # tolerance += 1 |
118 |
| - #if data['f'] == 1.0: |
119 |
| - #linear_fit = fit.ivim_fit(np.log(signal), bvals, linear_fit_option=True) |
120 |
| - #npt.assert_allclose([data['f'], data['Dp']], linear_fit, atol=tolerance) |
121 |
| - #else: |
122 |
| - #[f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals) |
123 |
| - #npt.assert_allclose([data['f'], data['D']], [f_fit, D_fit], atol=tolerance) |
124 |
| - #npt.assert_allclose(data['Dp'], Dp_fit, atol=1e-1) # go easy on the perfusion as it's a linear fake |
| 168 | + signal, ratio = signal_helper(data["data"]) |
| 169 | + tolerances = tolerances_helper(tolerances, ratio, data["noise"]) |
125 | 170 | [f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals)
|
126 | 171 | npt.assert_allclose(data['f'], f_fit, rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
|
127 | 172 | npt.assert_allclose(data['D'], D_fit, rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
|
|
0 commit comments