Skip to content

Commit baa6ec0

Browse files
committed
FIX: Remove trace by default
1 parent cf4e25e commit baa6ec0

File tree

2 files changed

+27
-4
lines changed

2 files changed

+27
-4
lines changed

bin/parrec2nii

Lines changed: 21 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ def get_opt_parser():
103103
store the corresponding settings in the NIfTI header, unless
104104
non-uniform scaling is used, in which case the data is
105105
stored in the file in scaled form. Default: 'dv'""")))
106+
p.add_option(
107+
Option('--keep-trace', action="store_true", dest='keep_trace',
108+
default=False,
109+
help=one_line("""Do not discard the diagnostic Philips DTI
110+
trace volume, if it exists in the data.""")))
106111
p.add_option(
107112
Option("--overwrite", action="store_true", dest="overwrite",
108113
default=False,
@@ -171,12 +176,26 @@ def proc_file(infile, opts):
171176
ornt = io_orientation(np.diag([-1, 1, 1, 1]).dot(affine))
172177
if np.all(ornt == [[0, 1],
173178
[1, 1],
174-
[2, 1]]): # already in LAS+
179+
[2, 1]]): # already in LAS+
175180
t_aff = np.eye(4)
176-
else: # Not in LAS+
181+
else: # Not in LAS+
177182
t_aff = inv_ornt_aff(ornt, pr_img.shape)
178183
affine = np.dot(affine, t_aff)
179184
in_data = apply_orientation(in_data, ornt)
185+
186+
bvals, bvecs = pr_hdr.get_bvals_bvecs()
187+
if not opts.keep_trace: # discard Philips DTI trace if present
188+
if bvals is not None:
189+
bad_mask = np.logical_and(bvals != 0, (bvecs == 0).all(axis=1))
190+
if bad_mask.sum() > 0:
191+
pl = 's' if bad_mask.sum() != 1 else ''
192+
verbose('Removing %s DTI trace volume%s'
193+
% (bad_mask.sum(), pl))
194+
good_mask = ~bad_mask
195+
in_data = in_data[..., good_mask]
196+
bvals = bvals[good_mask]
197+
bvecs = bvecs[good_mask]
198+
180199
# Make corresponding NIfTI image
181200
nimg = nifti1.Nifti1Image(in_data, affine, pr_hdr)
182201
nhdr = nimg.header
@@ -208,7 +227,6 @@ def proc_file(infile, opts):
208227

209228
# write out bvals/bvecs if requested
210229
if opts.bvs:
211-
bvals, bvecs = pr_hdr.get_bvals_bvecs()
212230
if bvals is None and bvecs is None:
213231
verbose('No DTI volumes detected, bvals and bvecs not written')
214232
else:

nibabel/tests/test_scripts.py

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,12 +221,17 @@ def test_parrec2nii_with_data():
221221
assert_true(exists('DTI.nii'))
222222
assert_false(exists('DTI.bvals'))
223223
assert_false(exists('DTI.bvecs'))
224+
# ensure trace was removed
225+
img = load('DTI.nii')
226+
assert_equal(img.shape[-1], len(DTI_PAR_BVALS) - 1)
227+
del img
224228
# Does not overwrite unless option given
225229
code, stdout, stderr = run_command(['parrec2nii', dti_par],
226230
check_code=False)
227231
assert_equal(code, 1)
228232
# Writes bvals, bvecs files if asked
229-
run_command(['parrec2nii', '--overwrite', '--bvs', dti_par])
233+
run_command(['parrec2nii', '--overwrite', '--keep-trace',
234+
'--bvs', dti_par])
230235
assert_almost_equal(np.loadtxt('DTI.bvals'), DTI_PAR_BVALS)
231236
# Bvecs in header, transposed from PSL to LPS
232237
bvecs_LPS = DTI_PAR_BVECS[:, [2, 0, 1]]

0 commit comments

Comments
 (0)