Skip to content

swap actual and desired in test #105

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
__pycache__/
*.nii.gz
*.nii
*.bval
*.bvec
*.dcm
*.mat
*.raw
Expand Down
23 changes: 15 additions & 8 deletions src/wrappers/OsipiBase.py
Original file line number Diff line number Diff line change
Expand Up @@ -268,7 +268,7 @@ def osipi_author():
"""Author identification"""
return ''

def osipi_simple_bias_and_RMSE_test(self, SNR, bvalues, f, Dstar, D, noise_realizations=100):
def osipi_simple_bias_and_RMSE_test(self, SNR, bvalues, f, Dstar, D, noise_realizations=100, print_results=True):
# Generate signal
bvalues = np.asarray(bvalues)
signals = f*np.exp(-bvalues*Dstar) + (1-f)*np.exp(-bvalues*D)
Expand All @@ -282,7 +282,10 @@ def osipi_simple_bias_and_RMSE_test(self, SNR, bvalues, f, Dstar, D, noise_reali
noised_signal = np.array([norm.rvs(signal, sigma) for signal in signals])

# Perform fit with the noised signal
f_estimates[i], Dstar_estimates[i], D_estimates[i] = self.ivim_fit(noised_signal, bvalues)
fit = self.osipi_fit(noised_signal, bvalues)
f_estimates[i] = fit['f']
Dstar_estimates[i] = fit['Dp']
D_estimates[i] = fit['D']

# Calculate bias
f_bias = np.mean(f_estimates) - f
Expand All @@ -293,9 +296,13 @@ def osipi_simple_bias_and_RMSE_test(self, SNR, bvalues, f, Dstar, D, noise_reali
f_RMSE = np.sqrt(np.var(f_estimates) + f_bias**2)
Dstar_RMSE = np.sqrt(np.var(Dstar_estimates) + Dstar_bias**2)
D_RMSE = np.sqrt(np.var(D_estimates) + D_bias**2)

print(f"f bias:\t{f_bias}\nf RMSE:\t{f_RMSE}")
print(f"Dstar bias:\t{Dstar_bias}\nDstar RMSE:\t{Dstar_RMSE}")
print(f"D bias:\t{D_bias}\nD RMSE:\t{D_RMSE}")



if print_results:
print()
print(f"\tBias\t\tRMSE")
print(f"f\t{f_bias:.3}\t\t{f_RMSE:.3}")
print(f"D*\t{Dstar_bias:.3}\t\t{Dstar_RMSE:.3}")
print(f"D\t{D_bias:.3}\t{D_RMSE:.3}")
print()
else:
return {'f':f_bias,'Dstar':Dstar_bias,'D':D_bias}, {'f':f_RMSE,'Dstar':Dstar_RMSE,'D':D_RMSE}
6 changes: 3 additions & 3 deletions tests/IVIMmodels/unit_tests/test_ivim_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,11 +85,11 @@ def to_list_if_needed(value):
"atol": tolerances["atol"]
}
record_property('test_data', test_result)
npt.assert_allclose(fit_result['f'],data['f'], rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
npt.assert_allclose(data['f'], fit_result['f'], rtol=tolerances["rtol"]["f"], atol=tolerances["atol"]["f"])
if data['f']<0.80: # we need some signal for D to be detected
npt.assert_allclose(fit_result['D'],data['D'], rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
npt.assert_allclose(data['D'], fit_result['D'], rtol=tolerances["rtol"]["D"], atol=tolerances["atol"]["D"])
if data['f']>0.03: #we need some f for D* to be interpretable
npt.assert_allclose(fit_result['Dp'],data['Dp'], rtol=tolerances["rtol"]["Dp"], atol=tolerances["atol"]["Dp"])
npt.assert_allclose(data['Dp'], fit_result['Dp'], rtol=tolerances["rtol"]["Dp"], atol=tolerances["atol"]["Dp"])
#assert fit_result['D'] < fit_result['Dp'], f"D {fit_result['D']} is larger than D* {fit_result['Dp']} for {name}"
if not skiptime:
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
Expand Down
20 changes: 12 additions & 8 deletions utilities/data_simulation/Download_data.py
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is fine for me; but all of the osipi code is expecting the download to be in the defined folder destination. For what purpose do you want to put it elsewhere?

Copy link
Collaborator Author

@oscarjalnefjord oscarjalnefjord Jun 10, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Indeed. I just wanted to add some flexibility in case you want to place the data somewhere else and use for other purposes.

Original file line number Diff line number Diff line change
Expand Up @@ -12,16 +12,20 @@ def unzip_file(zip_file_path, extracted_folder_path):
zip_ref.extract(file_info, extracted_folder_path)


def download_data(force=False):
def download_data(force=False,folder=None):
# Check if the folder exists, and create it if not
curdir=os.getcwd()
base_folder = os.path.abspath(os.path.dirname(__file__))
base_folder = os.path.split(os.path.split(base_folder)[0])[0]
if not os.path.exists(os.path.join(base_folder,'download')):
os.makedirs(os.path.join(base_folder,'download'))
print(f"Folder '{'download'}' created.")
# Change to the specified folder
os.chdir(os.path.join(base_folder,'download'))
if folder is None:
base_folder = os.path.abspath(os.path.dirname(__file__))
base_folder = os.path.split(os.path.split(base_folder)[0])[0]
if not os.path.exists(os.path.join(base_folder,'download')):
os.makedirs(os.path.join(base_folder,'download'))
print(f"Folder '{'download'}' created.")
download_folder = os.path.join(base_folder,'download')
else:
download_folder = folder
# Change to the specified folder
os.chdir(download_folder)
subprocess.check_call(["zenodo_get", 'https://zenodo.org/records/14605039'])
# Open the zip file
if force or not os.path.exists('Data'):
Expand Down