Skip to content

Commit 441cd51

Browse files
committed
fix tests
1 parent a768dcb commit 441cd51

File tree

1 file changed

+12
-32
lines changed

1 file changed

+12
-32
lines changed

tests/IVIMmodels/unit_tests/test_ivim_fit.py

Lines changed: 12 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
import logging
88
from src.wrappers.OsipiBase import OsipiBase
99
from utilities.data_simulation.GenerateData import GenerateData
10-
11-
logging.basicConfig(level=logging.INFO)
1210
logger = logging.getLogger(__name__)
11+
#run using python -m pytest from the root folder
1312

14-
# Define a global list to hold the test results
1513
test_results = []
1614

15+
1716
def signal_helper(signal):
1817
signal = np.asarray(signal)
1918
signal = np.abs(signal)
@@ -60,57 +59,38 @@ def data_ivim_fit_saved():
6059
tolerances = algorithm_dict.get("tolerances", {})
6160
yield name, bvals, data, algorithm, xfail, kwargs, tolerances
6261

62+
6363
@pytest.mark.parametrize("name, bvals, data, algorithm, xfail, kwargs, tolerances", data_ivim_fit_saved())
6464
def test_ivim_fit_saved(name, bvals, data, algorithm, xfail, kwargs, tolerances, request):
6565
global test_results
6666
if xfail["xfail"]:
67-
logger.info(xfail["xfail"])
6867
mark = pytest.mark.xfail(reason="xfail", strict=xfail["strict"])
6968
request.node.add_marker(mark)
7069
fit = OsipiBase(algorithm=algorithm, **kwargs)
7170
signal, ratio = signal_helper(data["data"])
71+
7272
tolerances = tolerances_helper(tolerances, ratio, data["noise"])
7373
[f_fit, Dp_fit, D_fit] = fit.osipi_fit(signal, bvals)
74-
75-
# Log the results and tolerances
76-
logger.info(tolerances["rtol"])
77-
logger.info(tolerances["atol"])
78-
7974
def to_list_if_needed(value):
8075
return value.tolist() if isinstance(value, np.ndarray) else value
81-
8276
test_result = {
8377
"name": name,
8478
"algorithm": algorithm,
8579
"f_fit": to_list_if_needed(f_fit),
8680
"Dp_fit": to_list_if_needed(Dp_fit),
8781
"D_fit": to_list_if_needed(D_fit),
88-
82+
"f": to_list_if_needed(data['f']),
83+
"Dp": to_list_if_needed(data['Dp']),
84+
"D": to_list_if_needed(data['D']),
8985
"status": "PASSED"
9086
}
87+
if xfail["xfail"]:
88+
test_result['status'] = "XFAILED"
9189

92-
try:
93-
npt.assert_allclose(data['f'], f_fit, rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
94-
npt.assert_allclose(data['D'], D_fit, rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
95-
npt.assert_allclose(data['Dp'], Dp_fit, rtol=tolerances["rtol"]["Dp"], atol=tolerances["atol"]["Dp"])
96-
except AssertionError as e:
97-
test_result["status"] = "FAILED"
98-
test_result["error"] = str(e)
99-
test_result["actual_values"] = {
100-
"f": to_list_if_needed(f_fit),
101-
"D": to_list_if_needed(D_fit),
102-
"Dp": to_list_if_needed(Dp_fit)
103-
}
104-
test_result["desired_values"] = {
105-
"f": data['f'],
106-
"D": data['D'],
107-
"Dp": data['Dp']
108-
}
109-
logger.error(f"Test failed for {name} with algorithm {algorithm}: {e}")
110-
111-
# Append the result to the test_results list
11290
test_results.append(test_result)
11391
with open('plots_data.json', 'w') as f:
11492
json.dump({"results": test_results, "rtol": tolerances["rtol"],
11593
"atol": tolerances["atol"], }, f, indent=4)
116-
logger.info(test_results)
94+
npt.assert_allclose(data['f'], f_fit, rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
95+
npt.assert_allclose(data['D'], D_fit, rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
96+
npt.assert_allclose(data['Dp'], Dp_fit, rtol=tolerances["rtol"]["Dp"], atol=tolerances["atol"]["Dp"])

0 commit comments

Comments
 (0)