Skip to content

Commit ef0a9c6

Browse files
committed
use multi_tensor instead of custom code in isotropic compartments
1 parent fc3f27c commit ef0a9c6

File tree

1 file changed

+20
-6
lines changed

1 file changed

+20
-6
lines changed

nipype/interfaces/dipy/simulate.py

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -174,8 +174,10 @@ def _run_interface(self, runtime):
174174
vfs = np.clip(vfs, 0., 1.)
175175

176176
fractions = np.concatenate((ffs, vfs), axis=3)
177+
177178
nb.Nifti1Image(fractions, aff, None).to_filename('fractions.nii.gz')
178-
nb.Nifti1Image(total_vf, aff, None).to_filename('total_vf.nii.gz')
179+
nb.Nifti1Image(np.sum(fractions, axis=3), aff, None).to_filename(
180+
'total_vf.nii.gz')
179181

180182
mhdr = hdr.copy()
181183
mhdr.set_data_dtype(np.uint8)
@@ -188,27 +190,39 @@ def _run_interface(self, runtime):
188190

189191
# Stack directions
190192
dirs = None
191-
for f in self.inputs.in_dirs:
192-
fd = nb.load(f).get_data()
193+
for i in range(nsticks):
194+
f = self.inputs.in_dirs[i]
195+
fd = np.nan_to_num(nb.load(f).get_data())
196+
w = np.linalg.norm(fd, axis=3)[..., np.newaxis]
197+
w[w < np.finfo(float).eps] = 1.0
198+
fd /= w
193199
if dirs is None:
194200
dirs = fd[msk > 0].copy()
195201
else:
196202
dirs = np.hstack((dirs, fd[msk > 0]))
197203

204+
# Add random directions for isotropic components
205+
for d in range(nballs):
206+
fd = np.random.randn(nvox, 3)
207+
w = np.linalg.norm(fd, axis=1)
208+
fd[w < np.finfo(float).eps, ...] = np.array([1., 0., 0.])
209+
w[w < np.finfo(float).eps] = 1.0
210+
fd /= w[..., np.newaxis]
211+
dirs = np.hstack((dirs, fd))
212+
198213
sf_evals = list(self.inputs.diff_sf)
199214
ba_evals = list(self.inputs.diff_iso)
200215

201216
mevals = [sf_evals] * nsticks + \
202217
[[ba_evals[d]] * 3 for d in range(nballs)]
203-
ba_sticks = [(1.0, 0.0, 0.0)] * nballs
204-
b0 = b0_im.get_data()[msk > 0]
205218

219+
b0 = b0_im.get_data()[msk > 0]
206220
args = []
207221
for i in range(nvox):
208222
args.append(
209223
{'fractions': fracs[i, ...].tolist(),
210224
'sticks': [tuple(dirs[i, j:j + 3])
211-
for j in range(nsticks)] + ba_sticks,
225+
for j in range(nsticks + nballs)],
212226
'gradients': gtab,
213227
'mevals': mevals,
214228
'S0': b0[i],

0 commit comments

Comments
 (0)