Skip to content

Commit

Permalink
Parametrized the algorithm names in the testing. Made the search for …
Browse files Browse the repository at this point in the history
…algorithm files a little more robust.
  • Loading branch information
IvanARashid committed Sep 13, 2023
1 parent b965d51 commit bb79fc3
Show file tree
Hide file tree
Showing 3 changed files with 71 additions and 50 deletions.
43 changes: 43 additions & 0 deletions tests/IVIMmodels/unit_tests/simple_test_run_of_algorithm.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import numpy as np
import os
from pathlib import Path
from src.standardized.ETP_SRI_LinearFitting import ETP_SRI_LinearFitting
from src.standardized.IAR_LU_biexp import IAR_LU_biexp

## Simple test code...
# Used to just do a test run of an algorithm during development
def test_run(model, **kwargs):
bvalues = np.array([0, 200, 500, 800])

def ivim_model(b, S0=1, f=0.1, Dstar=0.03, D=0.001):
return S0*(f*np.exp(-b*Dstar) + (1-f)*np.exp(-b*D))

signals = ivim_model(bvalues)

#model = ETP_SRI_LinearFitting(thresholds=[200])
if kwargs:
results = model.osipi_fit(signals, bvalues, **kwargs)
else:
results = model.osipi_fit(signals, bvalues)
print(results)
#test = model.osipi_simple_bias_and_RMSE_test(SNR=20, bvalues=bvalues, f=0.1, Dstar=0.03, D=0.001, noise_realizations=10)

model1 = ETP_SRI_LinearFitting(thresholds=[200])
model2 = IAR_LU_biexp()

test_run(model1, linear_fit_option=True)
test_run(model2)

###########################################

path = Path(__file__).resolve().parents[3] # Move up to the root folder
path_standardized_algortihms = path / "src" / "standardized" # Move to the folder containing the algorithms
algorithms = os.listdir(path_standardized_algortihms) # Get the contents of the folder

# Remove some crap
algorithms.remove("__init__.py")
algorithms.remove("__pycache__")
algorithms.remove("template.py")

# Remove the .py extensions from the algorithms names
algorithms = [algorithm.split(".")[0] for algorithm in algorithms]
54 changes: 28 additions & 26 deletions tests/IVIMmodels/unit_tests/test_ivim_fit_linear.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,11 +66,12 @@ def data_ivim_fit_saved():
algorithms = os.listdir(path_standardized_algortihms) # Get the contents of the folder

# Remove some crap in the folder
algorithms.remove("__init__.py")
algorithms.remove("__pycache__")
algorithms.remove("template.py")
#algorithms.remove("__init__.py")
#algorithms.remove("__pycache__")
#algorithms.remove("template.py")

# Remove the .py extensions from the algorithms names
algorithms = [algorithm for algorithm in algorithms if algorithm[0:2].isupper()]
algorithms = [algorithm.split(".")[0] for algorithm in algorithms]


Expand All @@ -81,30 +82,31 @@ def data_ivim_fit_saved():
bvals = all_data.pop('config')
bvals = bvals['bvalues']
for name, data in all_data.items():
yield name, bvals, data, algorithms
for algorithm in algorithms:
yield name, bvals, data, algorithm


@pytest.mark.parametrize("name, bvals, data, algorithms", data_ivim_fit_saved())
def test_ivim_fit_saved(name, bvals, data, algorithms):
@pytest.mark.parametrize("name, bvals, data, algorithm", data_ivim_fit_saved())
def test_ivim_fit_saved(name, bvals, data, algorithm):
#fit = LinearFit()
#fit = ETP_SRI_LinearFitting()
for algorithm in algorithms:
fit = OsipiBase(algorithm=algorithm, thresholds=[500])
signal = np.asarray(data['data'])
has_negatives = np.any(signal<0)
signal = np.abs(signal)
ratio = 1 / signal[0]
signal /= signal[0]
tolerance = 1e-2 + 25 * data['noise'] * ratio # totally empirical
#if has_negatives: # this fitting doesn't do well with negatives
# tolerance += 1
#if data['f'] == 1.0:
#linear_fit = fit.ivim_fit(np.log(signal), bvals, linear_fit_option=True)
#npt.assert_allclose([data['f'], data['Dp']], linear_fit, atol=tolerance)
#else:
#[f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals)
#npt.assert_allclose([data['f'], data['D']], [f_fit, D_fit], atol=tolerance)
#npt.assert_allclose(data['Dp'], Dp_fit, atol=1e-1) # go easy on the perfusion as it's a linear fake
[f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals)
npt.assert_allclose([data['f'], data['D']], [f_fit, D_fit], atol=tolerance)
npt.assert_allclose(data['Dp'], Dp_fit, atol=1e-1) # go easy on the perfusion as it's a linear fake
#for algorithm in algorithms:
fit = OsipiBase(algorithm=algorithm, thresholds=[500])
signal = np.asarray(data['data'])
has_negatives = np.any(signal<0)
signal = np.abs(signal)
ratio = 1 / signal[0]
signal /= signal[0]
tolerance = 1e-2 + 25 * data['noise'] * ratio # totally empirical
#if has_negatives: # this fitting doesn't do well with negatives
# tolerance += 1
#if data['f'] == 1.0:
#linear_fit = fit.ivim_fit(np.log(signal), bvals, linear_fit_option=True)
#npt.assert_allclose([data['f'], data['Dp']], linear_fit, atol=tolerance)
#else:
#[f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals)
#npt.assert_allclose([data['f'], data['D']], [f_fit, D_fit], atol=tolerance)
#npt.assert_allclose(data['Dp'], Dp_fit, atol=1e-1) # go easy on the perfusion as it's a linear fake
[f_fit, Dp_fit, D_fit] = fit.ivim_fit(signal, bvals)
npt.assert_allclose([data['f'], data['D']], [f_fit, D_fit], atol=tolerance)
npt.assert_allclose(data['Dp'], Dp_fit, atol=1e-1) # go easy on the perfusion as it's a linear fake
24 changes: 0 additions & 24 deletions tests/simple_test_run_of_algorithm.py

This file was deleted.

0 comments on commit bb79fc3

Please sign in to comment.