Skip to content

Commit b2ed0be

Browse files
committed
Prevent phantom code from running during test
1 parent d8121c2 commit b2ed0be

File tree

1 file changed

+56
-54
lines changed

1 file changed

+56
-54
lines changed

phantoms/brain/sim_brain_phantom.py

Lines changed: 56 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -5,57 +5,59 @@
55
import nibabel as nib
66
from scipy.ndimage import zoom
77

8-
DIFFUSIVE_REGIME = 'diffusive'
9-
BALLISTIC_REGIME = 'ballistic'
10-
11-
folder = os.path.dirname(__file__)
12-
13-
###########################
14-
# Simulation parameters #
15-
regime = DIFFUSIVE_REGIME
16-
snr = 200
17-
resolution = [3,3,3]
18-
# #
19-
###########################
20-
21-
22-
# Ground truth
23-
nii = nib.load(os.path.join(folder,'ground_truth','hrgt_icbm_2009a_nls_3t.nii.gz'))
24-
segmentation = np.squeeze(nii.get_fdata()[...,-1])
25-
26-
with open(os.path.join(folder,'ground_truth',regime+'_groundtruth.json'), 'r') as f:
27-
ivim_pars = json.load(f)
28-
S0 = 1
29-
30-
# Sequence parameters
31-
bval_file = os.path.join(folder,'ground_truth',regime+'.bval')
32-
b = np.loadtxt(bval_file)
33-
if regime == BALLISTIC_REGIME:
34-
cval_file = bval_file.replace('bval','cval')
35-
c = np.loadtxt(cval_file)
36-
37-
# Calculate signal
38-
S = np.zeros(list(np.shape(segmentation))+[b.size])
39-
40-
if regime == BALLISTIC_REGIME:
41-
Db = ivim_pars["Db"]
42-
for i,(D,f,vd) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["vd"])):
43-
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Db-c**2*vd**2))
44-
else:
45-
for i,(D,f,Dstar) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["Dstar"])):
46-
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Dstar))
47-
48-
# Resample to suitable resolution
49-
im = zoom(S,np.append(np.diag(nii.affine)[:3]/np.array(resolution),1),order=1)
50-
sz = im.shape
51-
52-
# Add noise
53-
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])))
54-
55-
# Save as image and sequence parameters
56-
nii_out = nib.Nifti1Image(im_noise,np.eye(4))
57-
base_name = os.path.join(folder,'data','{}_snr{}'.format(regime,snr))
58-
nib.save(nii_out,base_name+'.nii.gz')
59-
shutil.copyfile(bval_file,base_name+'.bval')
60-
if regime == BALLISTIC_REGIME:
61-
shutil.copyfile(cval_file,base_name+'.cval')
8+
if __name__ == "__main__":
9+
10+
DIFFUSIVE_REGIME = 'diffusive'
11+
BALLISTIC_REGIME = 'ballistic'
12+
13+
folder = os.path.dirname(__file__)
14+
15+
###########################
16+
# Simulation parameters #
17+
regime = DIFFUSIVE_REGIME
18+
snr = 200
19+
resolution = [3,3,3]
20+
# #
21+
###########################
22+
23+
24+
# Ground truth
25+
nii = nib.load(os.path.join(folder,'ground_truth','hrgt_icbm_2009a_nls_3t.nii.gz'))
26+
segmentation = np.squeeze(nii.get_fdata()[...,-1])
27+
28+
with open(os.path.join(folder,'ground_truth',regime+'_groundtruth.json'), 'r') as f:
29+
ivim_pars = json.load(f)
30+
S0 = 1
31+
32+
# Sequence parameters
33+
bval_file = os.path.join(folder,'ground_truth',regime+'.bval')
34+
b = np.loadtxt(bval_file)
35+
if regime == BALLISTIC_REGIME:
36+
cval_file = bval_file.replace('bval','cval')
37+
c = np.loadtxt(cval_file)
38+
39+
# Calculate signal
40+
S = np.zeros(list(np.shape(segmentation))+[b.size])
41+
42+
if regime == BALLISTIC_REGIME:
43+
Db = ivim_pars["Db"]
44+
for i,(D,f,vd) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["vd"])):
45+
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Db-c**2*vd**2))
46+
else:
47+
for i,(D,f,Dstar) in enumerate(zip(ivim_pars["D"],ivim_pars["f"],ivim_pars["Dstar"])):
48+
S[segmentation==i+1,:] = S0*((1-f)*np.exp(-b*D)+f*np.exp(-b*Dstar))
49+
50+
# Resample to suitable resolution
51+
im = zoom(S,np.append(np.diag(nii.affine)[:3]/np.array(resolution),1),order=1)
52+
sz = im.shape
53+
54+
# Add noise
55+
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])))
56+
57+
# Save as image and sequence parameters
58+
nii_out = nib.Nifti1Image(im_noise,np.eye(4))
59+
base_name = os.path.join(folder,'data','{}_snr{}'.format(regime,snr))
60+
nib.save(nii_out,base_name+'.nii.gz')
61+
shutil.copyfile(bval_file,base_name+'.bval')
62+
if regime == BALLISTIC_REGIME:
63+
shutil.copyfile(cval_file,base_name+'.cval')

0 commit comments

Comments
 (0)