5
5
import pathlib
6
6
import time
7
7
from src .wrappers .OsipiBase import OsipiBase
8
- from conftest import eng
9
8
#run using python -m pytest from the root folder
10
9
11
10
@@ -31,50 +30,24 @@ def tolerances_helper(tolerances, data):
31
30
tolerances ["atol" ] = tolerances .get ("atol" , {"f" : 2e-1 , "D" : 5e-4 , "Dp" : 4e-2 })
32
31
return tolerances
33
32
34
- def data_ivim_fit_saved ():
35
- # Find the algorithms from algorithms.json
36
- file = pathlib .Path (__file__ )
37
- algorithm_path = file .with_name ('algorithms.json' )
38
- with algorithm_path .open () as f :
39
- algorithm_information = json .load (f )
40
-
41
- # Load generic test data generated from the included phantom: phantoms/MR_XCAT_qMRI
42
- generic = file .with_name ('generic.json' )
43
- with generic .open () as f :
44
- all_data = json .load (f )
45
-
46
- algorithms = algorithm_information ["algorithms" ]
47
- bvals = all_data .pop ('config' )
48
- bvals = bvals ['bvalues' ]
49
- for algorithm in algorithms :
50
- first = True
51
- for name , data in all_data .items ():
52
- algorithm_dict = algorithm_information .get (algorithm , {})
53
- xfail = {"xfail" : name in algorithm_dict .get ("xfail_names" , {}),
54
- "strict" : algorithm_dict .get ("xfail_names" , {}).get (name , True )}
55
- kwargs = algorithm_dict .get ("options" , {})
56
- tolerances = algorithm_dict .get ("tolerances" , {})
57
- skiptime = False
58
- if first == True :
59
- if algorithm_dict .get ("fail_first_time" , {}) == True :
60
- skiptime = True
61
- first = False
62
- if algorithm_dict .get ("requieres_matlab" , {}) == True :
63
- if eng is None :
64
- continue
65
- else :
66
- kwargs = {** kwargs ,'eng' : eng }
67
- yield name , bvals , data , algorithm , xfail , kwargs , tolerances , skiptime
68
-
69
- @pytest .mark .parametrize ("name, bvals, data, algorithm, xfail, kwargs, tolerances, skiptime" , data_ivim_fit_saved ())
70
- def test_ivim_fit_saved (name , bvals , data , algorithm , xfail , kwargs , tolerances ,skiptime , request , record_property ):
33
+ def test_ivim_fit_saved (data_ivim_fit_saved , eng , request , record_property ):
34
+ name , bvals , data , algorithm , xfail , kwargs , tolerances , skiptime , requires_matlab = data_ivim_fit_saved
35
+ max_time = 0.5
36
+ if requires_matlab :
37
+ max_time = 2
38
+ if eng is None :
39
+ print ('test is here' )
40
+ pytest .skip (reason = "Running without matlab; if Matlab is available please run pytest --withmatlab" )
41
+ else :
42
+ print ('test is not here' )
43
+ kwargs = {** kwargs , 'eng' : eng }
71
44
if xfail ["xfail" ]:
72
45
mark = pytest .mark .xfail (reason = "xfail" , strict = xfail ["strict" ])
73
46
request .node .add_marker (mark )
74
47
signal = signal_helper (data ["data" ])
75
48
tolerances = tolerances_helper (tolerances , data )
76
- start_time = time .time () # Record the start time
77
49
fit = OsipiBase (algorithm = algorithm , ** kwargs )
50
+ start_time = time .time () # Record the start time
78
51
fit_result = fit .osipi_fit (signal , bvals )
79
52
elapsed_time = time .time () - start_time # Calculate elapsed time
80
53
def to_list_if_needed (value ):
@@ -99,29 +72,18 @@ def to_list_if_needed(value):
99
72
npt .assert_allclose (fit_result ['Dp' ],data ['Dp' ], rtol = tolerances ["rtol" ]["Dp" ], atol = tolerances ["atol" ]["Dp" ])
100
73
#assert fit_result['D'] < fit_result['Dp'], f"D {fit_result['D']} is larger than D* {fit_result['Dp']} for {name}"
101
74
if not skiptime :
102
- assert elapsed_time < 0.5 , f"Algorithm { name } took { elapsed_time } seconds, which is longer than 2 second to fit per voxel" #less than 0.5 seconds per voxel
103
-
104
-
105
- def algorithmlist ():
106
- # Find the algorithms from algorithms.json
107
- file = pathlib .Path (__file__ )
108
- algorithm_path = file .with_name ('algorithms.json' )
109
- with algorithm_path .open () as f :
110
- algorithm_information = json .load (f )
111
- algorithms = algorithm_information ["algorithms" ]
112
- for algorithm in algorithms :
113
- algorithm_dict = algorithm_information .get (algorithm , {})
114
- args = {}
115
- if algorithm_dict .get ("requieres_matlab" , {}) == True :
116
- if eng is None :
117
- continue
118
- else :
119
- args = {** args , 'eng' : eng }
120
- yield algorithm , args
121
-
122
- @pytest .mark .parametrize ("algorithm, args" , algorithmlist ())
123
- def test_default_bounds_and_initial_guesses (algorithm , args ):
124
- fit = OsipiBase (algorithm = algorithm ,** args )
75
+ assert elapsed_time < max_time , f"Algorithm { name } took { elapsed_time } seconds, which is longer than 2 second to fit per voxel" #less than 0.5 seconds per voxel
76
+
77
+ def test_default_bounds_and_initial_guesses (algorithmlist ,eng ):
78
+ algorithm , requires_matlab = algorithmlist
79
+ if requires_matlab :
80
+ if eng is None :
81
+ pytest .skip (reason = "Running without matlab; if Matlab is available please run pytest --withmatlab" )
82
+ else :
83
+ kwargs = {'eng' : eng }
84
+ else :
85
+ kwargs = {}
86
+ fit = OsipiBase (algorithm = algorithm ,** kwargs )
125
87
#assert fit.bounds is not None, f"For {algorithm}, there is no default fit boundary"
126
88
#assert fit.initial_guess is not None, f"For {algorithm}, there is no default fit initial guess"
127
89
if fit .use_bounds :
@@ -141,38 +103,13 @@ def test_default_bounds_and_initial_guesses(algorithm, args):
141
103
assert 0.9 <= fit .initial_guess [3 ] <= 1.1 , f"For { algorithm } , the default initial guess for S { fit .initial_guess [3 ]} is unrealistic; note signal is normalized"
142
104
143
105
144
- def bound_input ():
145
- # Find the algorithms from algorithms.json
146
- file = pathlib .Path (__file__ )
147
- algorithm_path = file .with_name ('algorithms.json' )
148
- with algorithm_path .open () as f :
149
- algorithm_information = json .load (f )
150
-
151
- # Load generic test data generated from the included phantom: phantoms/MR_XCAT_qMRI
152
- generic = file .with_name ('generic.json' )
153
- with generic .open () as f :
154
- all_data = json .load (f )
155
-
156
- algorithms = algorithm_information ["algorithms" ]
157
- bvals = all_data .pop ('config' )
158
- bvals = bvals ['bvalues' ]
159
- for name , data in all_data .items ():
160
- for algorithm in algorithms :
161
- algorithm_dict = algorithm_information .get (algorithm , {})
162
- xfail = {"xfail" : name in algorithm_dict .get ("xfail_names" , {}),
163
- "strict" : algorithm_dict .get ("xfail_names" , {}).get (name , True )}
164
- kwargs = algorithm_dict .get ("options" , {})
165
- tolerances = algorithm_dict .get ("tolerances" , {})
166
- if algorithm_dict .get ("requieres_matlab" , {}) == True :
167
- if eng is None :
168
- continue
169
- else :
170
- kwargs = {** kwargs , 'eng' : eng }
171
- yield name , bvals , data , algorithm , xfail , kwargs , tolerances
172
-
173
-
174
- @pytest .mark .parametrize ("name, bvals, data, algorithm, xfail, kwargs, tolerances" , bound_input ())
175
- def test_bounds (name , bvals , data , algorithm , xfail , kwargs , tolerances , request ):
106
+ def test_bounds (bound_input , eng ):
107
+ name , bvals , data , algorithm , xfail , kwargs , tolerances , requires_matlab = bound_input
108
+ if requires_matlab :
109
+ if eng is None :
110
+ pytest .skip (reason = "Running without matlab; if Matlab is available please run pytest --withmatlab" )
111
+ else :
112
+ kwargs = {** kwargs , 'eng' : eng }
176
113
bounds = ([0.0008 , 0.2 , 0.01 , 1.1 ], [0.0012 , 0.3 , 0.02 , 1.3 ])
177
114
# deliberately have silly bounds to see whether they are used
178
115
fit = OsipiBase (algorithm = algorithm , bounds = bounds , initial_guess = [0.001 , 0.25 , 0.015 , 1.2 ], ** kwargs )
0 commit comments