Skip to content

Commit 8d72f9a

Browse files
committed
A new way to select b0 reference. Close #941
1 parent 725e580 commit 8d72f9a

File tree

3 files changed

+61
-20
lines changed

3 files changed

+61
-20
lines changed

nipype/workflows/dmri/fsl/artifacts.py

Lines changed: 11 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -342,17 +342,15 @@ def hmc_pipeline(name='motion_correct'):
342342
from nipype.workflows.data import get_flirt_schedule
343343

344344
params = dict(dof=6, bgvalue=0, save_log=True, no_search=True,
345-
#cost='mutualinfo', cost_func='mutualinfo', bins=64,
345+
# cost='mutualinfo', cost_func='mutualinfo', bins=64,
346346
schedule=get_flirt_schedule('hmc'))
347347

348348
inputnode = pe.Node(niu.IdentityInterface(fields=['in_file', 'ref_num',
349349
'in_bvec', 'in_bval', 'in_mask']), name='inputnode')
350-
refid = pe.Node(niu.IdentityInterface(fields=['ref_num']),
351-
name='RefVolume')
352-
pick_ref = pe.Node(fsl.ExtractROI(t_size=1), name='GetRef')
353-
pick_mov = pe.Node(niu.Function(input_names=['in_file', 'in_bval', 'volid'],
354-
output_names=['out_file', 'out_bval'],
355-
function=remove_comp), name='GetMovingDWs')
350+
split = pe.Node(niu.Function(function=hmc_split,
351+
input_names=['in_file', 'in_bval', 'ref_num'],
352+
output_names=['out_ref', 'out_mov', 'out_bval', 'volid']),
353+
name='SplitDWI')
356354
flirt = dwi_flirt(flirt_param=params)
357355
insmat = pe.Node(niu.Function(input_names=['inlist', 'volid'],
358356
output_names=['out'], function=insert_mat),
@@ -366,18 +364,15 @@ def hmc_pipeline(name='motion_correct'):
366364

367365
wf = pe.Workflow(name=name)
368366
wf.connect([
369-
(inputnode, refid, [(('ref_num', _checkrnum), 'ref_num')]),
370-
(inputnode, pick_ref, [('in_file', 'in_file')]),
371-
(inputnode, pick_mov, [('in_file', 'in_file'),
372-
('in_bval', 'in_bval')]),
367+
(inputnode, split, [('in_file', 'in_file'),
368+
('in_bval', 'in_bval'),
369+
('ref_num', 'ref_num')]),
373370
(inputnode, flirt, [('in_mask', 'inputnode.ref_mask')]),
374-
(refid, pick_ref, [('ref_num', 't_min')]),
375-
(refid, pick_mov, [('ref_num', 'volid')]),
376-
(pick_mov, flirt, [('out_file', 'inputnode.in_file'),
371+
(split, flirt, [('out_ref', 'inputnode.reference'),
372+
('out_mov', 'inputnode.in_file'),
377373
('out_bval', 'inputnode.in_bval')]),
378-
(pick_ref, flirt, [('roi_file', 'inputnode.reference')]),
379374
(flirt, insmat, [('outputnode.out_xfms', 'inlist')]),
380-
(refid, insmat, [('ref_num', 'volid')]),
375+
(split, insmat, [('volid', 'volid')]),
381376
(inputnode, rot_bvec, [('in_bvec', 'in_bvec')]),
382377
(insmat, rot_bvec, [('out', 'in_matrix')]),
383378
(rot_bvec, outputnode, [('out_file', 'out_bvec')]),

nipype/workflows/dmri/fsl/utils.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,52 @@ def extract_bval(in_dwi, in_bval, b=0, out_file=None):
229229
return out_file
230230

231231

232+
def hmc_split(in_file, in_bval, ref_num=0, lowbval=5.0):
233+
"""
234+
Selects the reference and moving volumes from a dwi dataset
235+
for the purpose of HMC.
236+
"""
237+
import numpy as np
238+
import nibabel as nb
239+
import os.path as op
240+
from nipype.interfaces.base import isdefined
241+
242+
im = nb.load(in_file)
243+
data = im.get_data()
244+
hdr = im.get_header().copy()
245+
bval = np.loadtxt(in_bval)
246+
247+
lowbs = np.where(bval <= lowbval)[0]
248+
249+
volid = lowbs[0]
250+
if (isdefined(ref_num) and (ref_num < len(lowbs))):
251+
volid = [ref_num]
252+
253+
if volid == 0:
254+
data = data[..., 1:]
255+
bval = bval[1:]
256+
elif volid == (data.shape[-1] - 1):
257+
data = data[..., :-1]
258+
bval = bval[:-1]
259+
else:
260+
data = np.concatenate((data[..., :volid], data[..., (volid + 1):]),
261+
axis=3)
262+
bval = np.hstack((bval[:volid], bval[(volid + 1):]))
263+
264+
out_ref = op.abspath('hmc_ref.nii.gz')
265+
out_mov = op.abspath('hmc_mov.nii.gz')
266+
out_bval = op.abspath('bval_split.txt')
267+
268+
hdr.set_data_shape(refdata.shape)
269+
refdata = data[..., volid]
270+
nb.Nifti1Image(refdata, im.get_affine(), hdr).to_filename(out_ref)
271+
272+
hdr.set_data_shape(data.shape)
273+
nb.Nifti1Image(data, im.get_affine(), hdr).to_filename(out_mov)
274+
np.savetxt(out_bval, bval)
275+
return [out_ref, out_mov, out_bval, volid]
276+
277+
232278
def remove_comp(in_file, in_bval, volid=0, out_file=None):
233279
"""
234280
Removes the volume ``volid`` from the 4D nifti file

nipype/workflows/fmri/spm/preprocess.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -254,11 +254,11 @@ def create_DARTEL_template(name='dartel_template'):
254254
iterfield=['channel_files'],
255255
name='segment')
256256
workflow.connect(inputnode, 'structural_files', segment, 'channel_files')
257-
257+
258258
version = spm.Info.version()
259259
if version:
260260
spm_path = version['path']
261-
if version['name'] == 'SPM8':
261+
if version['name'] == 'SPM8':
262262
tissue1 = ((os.path.join(spm_path,'toolbox/Seg/TPM.nii'), 1), 2, (True,True), (False, False))
263263
tissue2 = ((os.path.join(spm_path,'toolbox/Seg/TPM.nii'), 2), 2, (True,True), (False, False))
264264
tissue3 = ((os.path.join(spm_path,'toolbox/Seg/TPM.nii'), 3), 2, (True,False), (False, False))
@@ -275,11 +275,11 @@ def create_DARTEL_template(name='dartel_template'):
275275
tissue6 = ((os.path.join(spm_path,'tpm/TPM.nii'), 6), 2, (False,False), (False, False))
276276
else:
277277
logger.critical('Unsupported version of SPM')
278-
278+
279279
segment.inputs.tissues = [tissue1, tissue2, tissue3, tissue4, tissue5, tissue6]
280280
else:
281281
logger.critical('SPM not found')
282-
282+
283283
dartel = pe.Node(spm.DARTEL(), name='dartel')
284284

285285
"""Get the gray and white segmentation classes generated by NewSegment

0 commit comments

Comments
 (0)