Skip to content

Commit 87375ea

Browse files
committed
Merge pull request #426 from grlee77/parrec_v40
MRG: properly handle V4 .PAR files with diffusion info This PR fixes a bug in `get_bvals_bvecs` for V4 .PAR files (the bug is only present for V4, not the more recent V4.1 or V4.2). In V4, the `diffusion` field does not exist and the `bvecs` cannot be determined.
2 parents 7160d77 + 7d845e2 commit 87375ea

File tree

4 files changed

+203
-3
lines changed

4 files changed

+203
-3
lines changed

bin/parrec2nii

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -185,7 +185,7 @@ def proc_file(infile, opts):
185185

186186
bvals, bvecs = pr_hdr.get_bvals_bvecs()
187187
if not opts.keep_trace: # discard Philips DTI trace if present
188-
if bvals is not None:
188+
if bvecs is not None:
189189
bad_mask = np.logical_and(bvals != 0, (bvecs == 0).all(axis=1))
190190
if bad_mask.sum() > 0:
191191
pl = 's' if bad_mask.sum() != 1 else ''
@@ -229,6 +229,14 @@ def proc_file(infile, opts):
229229
if opts.bvs:
230230
if bvals is None and bvecs is None:
231231
verbose('No DTI volumes detected, bvals and bvecs not written')
232+
elif bvecs is None:
233+
verbose('DTI volumes detected, but no diffusion direction info was'
234+
'found. Writing .bvals file only.')
235+
with open(basefilename + '.bvals', 'w') as fid:
236+
# np.savetxt could do this, but it's just a loop anyway
237+
for val in bvals:
238+
fid.write('%s ' % val)
239+
fid.write('\n')
232240
else:
233241
verbose('Writing .bvals and .bvecs files')
234242
# Transform bvecs with reorientation affine

nibabel/parrec.py

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -703,7 +703,7 @@ def get_q_vectors(self):
703703
acquisition.
704704
"""
705705
bvals, bvecs = self.get_bvals_bvecs()
706-
if bvals is None and bvecs is None:
706+
if bvals is None or bvecs is None:
707707
return None
708708
return bvecs * bvals[:, np.newaxis]
709709

@@ -728,6 +728,8 @@ def get_bvals_bvecs(self):
728728
# All bvals within volume should be the same
729729
assert not np.any(np.diff(bvals, axis=0))
730730
bvals = bvals[0]
731+
if 'diffusion' not in self.image_defs.dtype.names:
732+
return bvals, None
731733
bvecs = self.image_defs['diffusion'][reorder].reshape(
732734
(n_slices, n_vols, 3), order='F')
733735
# All 3 values of bvecs should be same within volume

0 commit comments

Comments
 (0)