Skip to content
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

code and data for simulation of brain phantom #9

Merged
merged 2 commits into from
Sep 1, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
7 changes: 7 additions & 0 deletions phantoms/brain/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
# Code for generating ivim phantoms of the brain

Two sets of ground truth data are used for simulation:
- Simulation in the diffusive regime (IVIM parameters D, f and D*): reference values from Ryghög et al. 2014 and b-values from Federau et al. 2012
- Simulation in the ballistic regime (IVIM parameters D, f and vd): reference values and sequence parameters from Ahlgren et al. 2016

The segmentation used by the simulations is the ICBM 2009a nonlinear symmetric 3T atlas (https://nist.mni.mcgill.ca/icbm-152-nonlinear-atlases-2009/), the same as in e.g. ASLDRO (https://asldro.readthedocs.io/en/stable/).
1 change: 1 addition & 0 deletions phantoms/brain/data/ballistic_snr200.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 10 20 30 40 50 60 70 80 90 100 120 140 160 180 200 10 20 30 40 50 60 70 80 90 100 120 140 160 180 200
1 change: 1 addition & 0 deletions phantoms/brain/data/ballistic_snr200.cval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.471 0.666 0.816 0.942 1.054 1.154 1.247 1.333 1.414 1.490 1.632 1.763 1.885 1.999 2.107
Binary file added phantoms/brain/data/ballistic_snr200.nii.gz
Binary file not shown.
1 change: 1 addition & 0 deletions phantoms/brain/data/diffusive_snr200.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 10 20 30 40 80 110 140 170 200 300 400 500 600 700 800 900
Binary file added phantoms/brain/data/diffusive_snr200.nii.gz
Binary file not shown.
1 change: 1 addition & 0 deletions phantoms/brain/ground_truth/ballistic.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 10 20 30 40 50 60 70 80 90 100 120 140 160 180 200 10 20 30 40 50 60 70 80 90 100 120 140 160 180 200
1 change: 1 addition & 0 deletions phantoms/brain/ground_truth/ballistic.cval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.000 0.471 0.666 0.816 0.942 1.054 1.154 1.247 1.333 1.414 1.490 1.632 1.763 1.885 1.999 2.107
15 changes: 15 additions & 0 deletions phantoms/brain/ground_truth/ballistic_calc_cval.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import os
import numpy as np

# Data from Ahlgren et al 2016 NMRinBiomed
Delta = 7.5e-3 # 7.5 ms
delta = 7.3e-3 # 7.3 ms

# For DDE sequence we have
# b = 2y^2G^2d^2(D-d/3), c = 2yGdD => c = sqrt(b 2D^2/(D-d/3))
bval_file = os.path.join(os.path.dirname(__file__),'ballistic.bval')
b = np.loadtxt(bval_file)
c = np.sqrt(b*2*Delta**2/(Delta-delta/3))
c[1:(c.size-1)//2+1] = 0 # flow compensated => c = 0
cval_file = bval_file.replace('bval','cval')
np.savetxt(cval_file,c,fmt='%.3f',newline=' ')
6 changes: 6 additions & 0 deletions phantoms/brain/ground_truth/ballistic_groundtruth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
{
"D":[1.2e-3,0.98e-3,3e-3],
"f":[0.0243,0.0164,0],
"vd":[1.71,1.73,0],
"Db":1.75e-3
}
1 change: 1 addition & 0 deletions phantoms/brain/ground_truth/diffusive.bval
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0 10 20 30 40 80 110 140 170 200 300 400 500 600 700 800 900
5 changes: 5 additions & 0 deletions phantoms/brain/ground_truth/diffusive_groundtruth.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
{
"D":[0.81e-3,0.86e-3,3e-3],
"f":[0.044,0.033,0],
"Dstar":[84e-3,76e-3,0]
}
Binary file not shown.
53 changes: 53 additions & 0 deletions phantoms/brain/sim_brain_phantom.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
import os
import shutil
import json
import numpy as np
import nibabel as nib
from scipy.ndimage import zoom

regime = 'diffusive'#'ballistic' #
Copy link
Contributor

@etpeterson etpeterson Aug 18, 2023

Choose a reason for hiding this comment

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

Maybe check if the regime is strictly either diffusive or ballistic. Right now a simple misspelling like balistic would actually generate the diffusive phantom.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Good point. I use predefined string variables now instead. Robust enough?

snr = 200
resolution = [3,3,3]

folder = os.path.dirname(__file__)

# Ground truth
nii = nib.load(os.path.join(folder,'ground_truth','hrgt_icbm_2009a_nls_3t.nii.gz'))
segmentation = np.squeeze(nii.get_fdata()[...,-1])

with open(os.path.join(folder,'ground_truth',regime+'_groundtruth.json'), 'r') as f:
ivim_pars = json.load(f)
S0 = 1

# Sequence parameters
bval_file = os.path.join(folder,'ground_truth',regime+'.bval')
b = np.loadtxt(bval_file)
if regime == 'ballistic':
cval_file = bval_file.replace('bval','cval')
c = np.loadtxt(cval_file)

# Calculate signal
S = np.zeros(list(np.shape(segmentation))+[b.size])

if regime == 'ballistic':
Db = ivim_pars["Db"]
for i,(D,f,vd) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["vd"])):
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Db-c**2*vd**2))
else:
for i,(D,f,Dstar) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["Dstar"])):
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Dstar))

# Resample to suitable resolution
im = zoom(S,np.append(np.diag(nii.affine)[:3]/np.array(resolution),1),order=1)
sz = im.shape

# Add noise
im_noise = np.abs(im + S0/snr*(np.random.randn(sz[0],sz[1],sz[2],sz[3])+1j*np.random.randn(sz[0],sz[1],sz[2],sz[3])))

# Save as image and sequence parameters
nii_out = nib.Nifti1Image(im_noise,np.eye(4))
base_name = os.path.join(folder,'data','{}_snr{}'.format(regime,snr))
nib.save(nii_out,base_name+'.nii.gz')
shutil.copyfile(bval_file,base_name+'.bval')
if regime == 'ballistic':
shutil.copyfile(cval_file,base_name+'.cval')