Repository: nipy/nipype

Issue:
artifacts.py hmc remove b0 images
I found a bug in the hcm pipeline. The pipeline removes the b0 images.

Fix for only one b0 image at the beginning: https://github.com/cni-md/nipype

(GetMovingDWs extract only images with diff weighting. If we have more than one b0 image or the b=0 is not at the beginning bval/bvec will have wrong values)

I found the procedure from epi.py fieldmap_correction more suitable to coregister the warped fieldmap with the EPIs to get the transformation matrix. Is there a reason to skip this in the BMapMag2B0 part of the fmc pipeline?

No I got stocked at Shiftmap2Warping. vsm2dfm give me a 4D image (one empty volume, one with the shift map?, one empty volume) whereas the ScaleField Node has still a resonable ShiftMap.

How should the result of vsm2dfm look like?

Joerg



Discussion:
@cni-md I'll look at it this evening. Thanks for posting!

> @cni-md How should the result of vsm2dfm look like?

A 4D volume with 3 timepoints, the displacements vector field corresponding to the warping. Two of them should be zero, and the timepoint corresponding with the phase-encoding direction will have the shifts, scaled to relative mm units. So it looks like it works ok in your case if the EPI is A >>> P or P >>> A.


Resolving patch:
diff --git a/nipype/workflows/dmri/fsl/artifacts.py b/nipype/workflows/dmri/fsl/artifacts.py
--- a/nipype/workflows/dmri/fsl/artifacts.py
+++ b/nipype/workflows/dmri/fsl/artifacts.py
@@ -67,34 +67,32 @@ def all_fmb_pipeline(name='hmc_sdc_ecc',
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   hmc,        [('in_file', 'inputnode.in_file'),
-                                    ('in_bvec', 'inputnode.in_bvec'),
-                                    ('in_bval', 'inputnode.in_bval')])
-        ,(inputnode,   avg_b0_0,   [('in_file', 'in_dwi'),
-                                    ('in_bval', 'in_bval')])
-        ,(avg_b0_0,    bet_dwi0,   [('out_file', 'in_file')])
-        ,(bet_dwi0,    hmc,        [('mask_file', 'inputnode.in_mask')])
-        ,(hmc,         sdc,        [('outputnode.out_file', 'inputnode.in_file')])
-        ,(bet_dwi0,    sdc,        [('mask_file', 'inputnode.in_mask')])
-        ,(inputnode,   sdc,        [('in_bval', 'inputnode.in_bval'),
-                                    ('bmap_pha', 'inputnode.bmap_pha'),
-                                    ('bmap_mag', 'inputnode.bmap_mag')])
-        ,(hmc,         ecc,        [('outputnode.out_xfms', 'inputnode.in_xfms')])
-        ,(inputnode,   ecc,        [('in_file', 'inputnode.in_file'),
-                                    ('in_bval', 'inputnode.in_bval')])
-        ,(bet_dwi0,    ecc,        [('mask_file', 'inputnode.in_mask')])
-        ,(ecc,         avg_b0_1,   [('outputnode.out_file', 'in_dwi')])
-        ,(inputnode,   avg_b0_1,   [('in_bval', 'in_bval')])
-        ,(avg_b0_1,    bet_dwi1,   [('out_file', 'in_file')])
-
-        ,(inputnode,   unwarp,     [('in_file', 'inputnode.in_dwi')])
-        ,(hmc,         unwarp,     [('outputnode.out_xfms', 'inputnode.in_hmc')])
-        ,(ecc,         unwarp,     [('outputnode.out_xfms', 'inputnode.in_ecc')])
-        ,(sdc,         unwarp,     [('outputnode.out_warp', 'inputnode.in_sdc')])
-
-        ,(hmc,         outputnode, [('outputnode.out_bvec', 'out_bvec')])
-        ,(unwarp,      outputnode, [('outputnode.out_file', 'out_file')])
-        ,(bet_dwi1,    outputnode, [('mask_file', 'out_mask')])
+        (inputnode, hmc,        [('in_file', 'inputnode.in_file'),
+                                 ('in_bvec', 'inputnode.in_bvec'),
+                                 ('in_bval', 'inputnode.in_bval')]),
+        (inputnode, avg_b0_0,   [('in_file', 'in_dwi'),
+                                 ('in_bval', 'in_bval')]),
+        (avg_b0_0,  bet_dwi0,   [('out_file', 'in_file')]),
+        (bet_dwi0,  hmc,        [('mask_file', 'inputnode.in_mask')]),
+        (hmc,       sdc,        [('outputnode.out_file', 'inputnode.in_file')]),
+        (bet_dwi0,  sdc,        [('mask_file', 'inputnode.in_mask')]),
+        (inputnode, sdc,        [('in_bval', 'inputnode.in_bval'),
+                                 ('bmap_pha', 'inputnode.bmap_pha'),
+                                 ('bmap_mag', 'inputnode.bmap_mag')]),
+        (hmc,       ecc,        [('outputnode.out_xfms', 'inputnode.in_xfms')]),
+        (inputnode, ecc,        [('in_file', 'inputnode.in_file'),
+                                 ('in_bval', 'inputnode.in_bval')]),
+        (bet_dwi0,  ecc,        [('mask_file', 'inputnode.in_mask')]),
+        (ecc,       avg_b0_1,   [('outputnode.out_file', 'in_dwi')]),
+        (inputnode, avg_b0_1,   [('in_bval', 'in_bval')]),
+        (avg_b0_1,  bet_dwi1,   [('out_file', 'in_file')]),
+        (inputnode, unwarp,     [('in_file', 'inputnode.in_dwi')]),
+        (hmc,       unwarp,     [('outputnode.out_xfms', 'inputnode.in_hmc')]),
+        (ecc,       unwarp,     [('outputnode.out_xfms', 'inputnode.in_ecc')]),
+        (sdc,       unwarp,     [('outputnode.out_warp', 'inputnode.in_sdc')]),
+        (hmc,       outputnode, [('outputnode.out_bvec', 'out_bvec')]),
+        (unwarp,    outputnode, [('outputnode.out_file', 'out_file')]),
+        (bet_dwi1,  outputnode, [('mask_file', 'out_mask')])
     ])
     return wf
 
@@ -154,34 +152,31 @@ def all_peb_pipeline(name='hmc_sdc_ecc',
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   hmc,        [('in_file', 'inputnode.in_file'),
-                                    ('in_bvec', 'inputnode.in_bvec'),
-                                    ('in_bval', 'inputnode.in_bval')])
-        ,(inputnode,   avg_b0_0,   [('in_file', 'in_dwi'),
-                                    ('in_bval', 'in_bval')])
-        ,(avg_b0_0,    bet_dwi0,   [('out_file', 'in_file')])
-        ,(bet_dwi0,    hmc,        [('mask_file', 'inputnode.in_mask')])
-        ,(hmc,         sdc,        [('outputnode.out_file', 'inputnode.in_file')])
-        ,(bet_dwi0,    sdc,        [('mask_file', 'inputnode.in_mask')])
-        ,(inputnode,   sdc,        [('in_bval', 'inputnode.in_bval'),
-                                    ('alt_file', 'inputnode.alt_file')])
-        ,(inputnode,   ecc,        [('in_file', 'inputnode.in_file'),
-                                    ('in_bval', 'inputnode.in_bval')])
-        ,(bet_dwi0,    ecc,        [('mask_file', 'inputnode.in_mask')])
-        ,(hmc,         ecc,        [('outputnode.out_xfms', 'inputnode.in_xfms')])
-        ,(ecc,         avg_b0_1,   [('outputnode.out_file', 'in_dwi')])
-        ,(inputnode,   avg_b0_1,   [('in_bval', 'in_bval')])
-        ,(avg_b0_1,    bet_dwi1,   [('out_file', 'in_file')])
-
-
-        ,(inputnode,   unwarp,     [('in_file', 'inputnode.in_dwi')])
-        ,(hmc,         unwarp,     [('outputnode.out_xfms', 'inputnode.in_hmc')])
-        ,(ecc,         unwarp,     [('outputnode.out_xfms', 'inputnode.in_ecc')])
-        ,(sdc,         unwarp,     [('outputnode.out_warp', 'inputnode.in_sdc')])
-
-        ,(hmc,         outputnode, [('outputnode.out_bvec', 'out_bvec')])
-        ,(unwarp,      outputnode, [('outputnode.out_file', 'out_file')])
-        ,(bet_dwi1,    outputnode, [('mask_file', 'out_mask')])
+        (inputnode, hmc,        [('in_file', 'inputnode.in_file'),
+                                 ('in_bvec', 'inputnode.in_bvec'),
+                                 ('in_bval', 'inputnode.in_bval')]),
+        (inputnode, avg_b0_0,   [('in_file', 'in_dwi'),
+                                 ('in_bval', 'in_bval')]),
+        (avg_b0_0,  bet_dwi0,   [('out_file', 'in_file')]),
+        (bet_dwi0,  hmc,        [('mask_file', 'inputnode.in_mask')]),
+        (hmc,       sdc,        [('outputnode.out_file', 'inputnode.in_file')]),
+        (bet_dwi0,  sdc,        [('mask_file', 'inputnode.in_mask')]),
+        (inputnode, sdc,        [('in_bval', 'inputnode.in_bval'),
+                                 ('alt_file', 'inputnode.alt_file')]),
+        (inputnode, ecc,        [('in_file', 'inputnode.in_file'),
+                                 ('in_bval', 'inputnode.in_bval')]),
+        (bet_dwi0,  ecc,        [('mask_file', 'inputnode.in_mask')]),
+        (hmc,       ecc,        [('outputnode.out_xfms', 'inputnode.in_xfms')]),
+        (ecc,       avg_b0_1,   [('outputnode.out_file', 'in_dwi')]),
+        (inputnode, avg_b0_1,   [('in_bval', 'in_bval')]),
+        (avg_b0_1,  bet_dwi1,   [('out_file', 'in_file')]),
+        (inputnode, unwarp,     [('in_file', 'inputnode.in_dwi')]),
+        (hmc,       unwarp,     [('outputnode.out_xfms', 'inputnode.in_hmc')]),
+        (ecc,       unwarp,     [('outputnode.out_xfms', 'inputnode.in_ecc')]),
+        (sdc,       unwarp,     [('outputnode.out_warp', 'inputnode.in_sdc')]),
+        (hmc,       outputnode, [('outputnode.out_bvec', 'out_bvec')]),
+        (unwarp,    outputnode, [('outputnode.out_file', 'out_file')]),
+        (bet_dwi1,  outputnode, [('mask_file', 'out_mask')])
     ])
     return wf
 
@@ -197,7 +192,8 @@ def all_fsl_pipeline(name='fsl_all_correct',
     Workflow that integrates FSL ``topup`` and ``eddy``.
 
 
-    .. warning:: this workflow rotates the gradients table (*b*-vectors) [Leemans09]_.
+    .. warning:: this workflow rotates the gradients table (*b*-vectors)
+      [Leemans09]_.
 
 
     .. warning:: this workflow does not perform jacobian modulation of each
@@ -251,30 +247,29 @@ def _gen_index(in_file):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   avg_b0_0,   [('in_file', 'in_dwi'),
-                                    ('in_bval', 'in_bval')])
-        ,(avg_b0_0,    bet_dwi0,   [('out_file','in_file')])
-        ,(bet_dwi0,    sdc,        [('mask_file', 'inputnode.in_mask')])
-        ,(inputnode,   sdc,        [('in_file', 'inputnode.in_file'),
-                                    ('alt_file', 'inputnode.alt_file'),
-                                    ('in_bval', 'inputnode.in_bval')])
-
-        ,(sdc,         ecc,        [('topup.out_enc_file', 'in_acqp'),
-                                    ('topup.out_fieldcoef', 'in_topup_fieldcoef'),
-                                    ('topup.out_movpar', 'in_topup_movpar')])
-        ,(bet_dwi0,    ecc,        [('mask_file', 'in_mask')])
-        ,(inputnode,   ecc,        [('in_file', 'in_file'),
-                                    (('in_file', _gen_index), 'in_index'),
-                                    ('in_bval', 'in_bval'),
-                                    ('in_bvec', 'in_bvec')])
-        ,(inputnode,   rot_bvec,   [('in_bvec', 'in_bvec')])
-        ,(ecc,         rot_bvec,   [('out_parameter', 'eddy_params')])
-        ,(ecc,         avg_b0_1,   [('out_corrected', 'in_dwi')])
-        ,(inputnode,   avg_b0_1,   [('in_bval', 'in_bval')])
-        ,(avg_b0_1,    bet_dwi1,   [('out_file','in_file')])
-        ,(ecc,         outputnode, [('out_corrected', 'out_file')])
-        ,(rot_bvec,    outputnode, [('out_file', 'out_bvec')])
-        ,(bet_dwi1,    outputnode, [('mask_file', 'out_mask')])
+        (inputnode,   avg_b0_0,   [('in_file', 'in_dwi'),
+                                   ('in_bval', 'in_bval')]),
+        (avg_b0_0,    bet_dwi0,   [('out_file', 'in_file')]),
+        (bet_dwi0,    sdc,        [('mask_file', 'inputnode.in_mask')]),
+        (inputnode,   sdc,        [('in_file', 'inputnode.in_file'),
+                                   ('alt_file', 'inputnode.alt_file'),
+                                   ('in_bval', 'inputnode.in_bval')]),
+        (sdc,         ecc,        [('topup.out_enc_file', 'in_acqp'),
+                                   ('topup.out_fieldcoef', 'in_topup_fieldcoef'),
+                                   ('topup.out_movpar', 'in_topup_movpar')]),
+        (bet_dwi0,    ecc,        [('mask_file', 'in_mask')]),
+        (inputnode,   ecc,        [('in_file', 'in_file'),
+                                   (('in_file', _gen_index), 'in_index'),
+                                   ('in_bval', 'in_bval'),
+                                   ('in_bvec', 'in_bvec')]),
+        (inputnode,   rot_bvec,   [('in_bvec', 'in_bvec')]),
+        (ecc,         rot_bvec,   [('out_parameter', 'eddy_params')]),
+        (ecc,         avg_b0_1,   [('out_corrected', 'in_dwi')]),
+        (inputnode,   avg_b0_1,   [('in_bval', 'in_bval')]),
+        (avg_b0_1,    bet_dwi1,   [('out_file', 'in_file')]),
+        (ecc,         outputnode, [('out_corrected', 'out_file')]),
+        (rot_bvec,    outputnode, [('out_file', 'out_bvec')]),
+        (bet_dwi1,    outputnode, [('mask_file', 'out_mask')])
     ])
     return wf
 
@@ -330,11 +325,12 @@ def hmc_pipeline(name='motion_correct'):
     Inputs::
 
         inputnode.in_file - input dwi file
-        inputnode.in_mask - weights mask of reference image (a file with data range \
-in [0.0, 1.0], indicating the weight of each voxel when computing the metric.
+        inputnode.in_mask - weights mask of reference image (a file with data \
+range in [0.0, 1.0], indicating the weight of each voxel when computing the \
+metric.
         inputnode.in_bvec - gradients file (b-vectors)
-        inputnode.ref_num (optional, default=0) index of the b0 volume that should be \
-taken as reference
+        inputnode.ref_num (optional, default=0) index of the b0 volume that \
+should be taken as reference
 
     Outputs::
 
@@ -346,17 +342,15 @@ def hmc_pipeline(name='motion_correct'):
     from nipype.workflows.data import get_flirt_schedule
 
     params = dict(dof=6, bgvalue=0, save_log=True, no_search=True,
-                  #cost='mutualinfo', cost_func='mutualinfo', bins=64,
+                  # cost='mutualinfo', cost_func='mutualinfo', bins=64,
                   schedule=get_flirt_schedule('hmc'))
 
     inputnode = pe.Node(niu.IdentityInterface(fields=['in_file', 'ref_num',
                         'in_bvec', 'in_bval', 'in_mask']), name='inputnode')
-    refid = pe.Node(niu.IdentityInterface(fields=['ref_num']),
-                    name='RefVolume')
-    pick_ref = pe.Node(fsl.ExtractROI(t_size=1), name='GetRef')
-    pick_mov = pe.Node(niu.Function(input_names=['in_file', 'in_bval', 'volid'],
-                       output_names=['out_file', 'out_bval'],
-                       function=remove_comp), name='GetMovingDWs')
+    split = pe.Node(niu.Function(function=hmc_split,
+                    input_names=['in_file', 'in_bval', 'ref_num'],
+                    output_names=['out_ref', 'out_mov', 'out_bval', 'volid']),
+                    name='SplitDWI')
     flirt = dwi_flirt(flirt_param=params)
     insmat = pe.Node(niu.Function(input_names=['inlist', 'volid'],
                      output_names=['out'], function=insert_mat),
@@ -370,24 +364,20 @@ def hmc_pipeline(name='motion_correct'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,  refid,      [(('ref_num', _checkrnum), 'ref_num')])
-        ,(inputnode,  pick_ref,   [('in_file', 'in_file')])
-        ,(inputnode,  pick_mov,   [('in_file', 'in_file'),
-                                   ('in_bval', 'in_bval')])
-        ,(inputnode,  flirt,      [('in_mask', 'inputnode.ref_mask')])
-        ,(refid,      pick_ref,   [('ref_num', 't_min')])
-        ,(refid,      pick_mov,   [('ref_num', 'volid')])
-        ,(pick_mov,   flirt,      [('out_file', 'inputnode.in_file'),
-                                   ('out_bval', 'inputnode.in_bval')])
-
-        ,(pick_ref,   flirt,      [('roi_file', 'inputnode.reference')])
-        ,(flirt,      insmat,     [('outputnode.out_xfms', 'inlist')])
-        ,(refid,      insmat,     [('ref_num', 'volid')])
-        ,(inputnode,  rot_bvec,   [('in_bvec', 'in_bvec')])
-        ,(insmat,     rot_bvec,   [('out', 'in_matrix')])
-        ,(rot_bvec,   outputnode, [('out_file', 'out_bvec')])
-        ,(flirt,      outputnode, [('outputnode.out_file', 'out_file')])
-        ,(insmat,     outputnode, [('out', 'out_xfms')])
+        (inputnode,     split,   [('in_file', 'in_file'),
+                                  ('in_bval', 'in_bval'),
+                                  ('ref_num', 'ref_num')]),
+        (inputnode,  flirt,      [('in_mask', 'inputnode.ref_mask')]),
+        (split,      flirt,      [('out_ref', 'inputnode.reference'),
+                                  ('out_mov', 'inputnode.in_file'),
+                                  ('out_bval', 'inputnode.in_bval')]),
+        (flirt,      insmat,     [('outputnode.out_xfms', 'inlist')]),
+        (split,      insmat,     [('volid', 'volid')]),
+        (inputnode,  rot_bvec,   [('in_bvec', 'in_bvec')]),
+        (insmat,     rot_bvec,   [('out', 'in_matrix')]),
+        (rot_bvec,   outputnode, [('out_file', 'out_bvec')]),
+        (flirt,      outputnode, [('outputnode.out_file', 'out_file')]),
+        (insmat,     outputnode, [('out', 'out_xfms')])
     ])
     return wf
 
@@ -396,32 +386,34 @@ def ecc_pipeline(name='eddy_correct'):
     """
     ECC stands for Eddy currents correction.
 
-    Creates a pipeline that corrects for artifacts induced by Eddy currents in dMRI
-    sequences.
+    Creates a pipeline that corrects for artifacts induced by Eddy currents in
+    dMRI sequences.
     It takes a series of diffusion weighted images and linearly co-registers
     them to one reference image (the average of all b0s in the dataset).
 
-    DWIs are also modulated by the determinant of the Jacobian as indicated by [Jones10]_ and
-    [Rohde04]_.
+    DWIs are also modulated by the determinant of the Jacobian as indicated by
+    [Jones10]_ and [Rohde04]_.
 
     A list of rigid transformation matrices can be provided, sourcing from a
-    :func:`.hmc_pipeline` workflow, to initialize registrations in a *motion free*
-    framework.
+    :func:`.hmc_pipeline` workflow, to initialize registrations in a *motion
+    free* framework.
 
-    A list of affine transformation matrices is available as output, so that transforms
-    can be chained (discussion
+    A list of affine transformation matrices is available as output, so that
+    transforms can be chained (discussion
     `here <https://github.com/nipy/nipype/pull/530#issuecomment-14505042>`_).
 
     .. admonition:: References
 
-      .. [Jones10] Jones DK, `The signal intensity must be modulated by the determinant of
-        the Jacobian when correcting for eddy currents in diffusion MRI
+      .. [Jones10] Jones DK, `The signal intensity must be modulated by the
+        determinant of the Jacobian when correcting for eddy currents in
+        diffusion MRI
         <http://cds.ismrm.org/protected/10MProceedings/files/1644_129.pdf>`_,
         Proc. ISMRM 18th Annual Meeting, (2010).
 
-      .. [Rohde04] Rohde et al., `Comprehensive Approach for Correction of Motion and Distortion
-        in Diffusion-Weighted MRI
-        <http://stbb.nichd.nih.gov/pdf/com_app_cor_mri04.pdf>`_, MRM 51:103-114 (2004).
+      .. [Rohde04] Rohde et al., `Comprehensive Approach for Correction of
+        Motion and Distortion in Diffusion-Weighted MRI
+        <http://stbb.nichd.nih.gov/pdf/com_app_cor_mri04.pdf>`_, MRM
+        51:103-114 (2004).
 
     Example
     -------
@@ -436,10 +428,12 @@ def ecc_pipeline(name='eddy_correct'):
     Inputs::
 
         inputnode.in_file - input dwi file
-        inputnode.in_mask - weights mask of reference image (a file with data range \
-sin [0.0, 1.0], indicating the weight of each voxel when computing the metric.
+        inputnode.in_mask - weights mask of reference image (a file with data \
+range sin [0.0, 1.0], indicating the weight of each voxel when computing the \
+metric.
         inputnode.in_bval - b-values table
-        inputnode.in_xfms - list of matrices to initialize registration (from head-motion correction)
+        inputnode.in_xfms - list of matrices to initialize registration (from \
+head-motion correction)
 
     Outputs::
 
@@ -481,27 +475,27 @@ def ecc_pipeline(name='eddy_correct'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,  avg_b0,     [('in_file', 'in_dwi'),
-                                   ('in_bval', 'in_bval')])
-        ,(inputnode,  pick_dws,   [('in_file', 'in_dwi'),
-                                   ('in_bval', 'in_bval')])
-        ,(inputnode,  merge,      [('in_file', 'in_dwi'),
-                                   ('in_bval', 'in_bval')])
-        ,(inputnode,  flirt,      [('in_mask', 'inputnode.ref_mask'),
-                                   ('in_xfms', 'inputnode.in_xfms'),
-                                   ('in_bval', 'inputnode.in_bval')])
-        ,(inputnode,  get_mat,    [('in_bval', 'in_bval')])
-        ,(avg_b0,     flirt,      [('out_file', 'inputnode.reference')])
-        ,(pick_dws,   flirt,      [('out_file', 'inputnode.in_file')])
-        ,(flirt,      get_mat,    [('outputnode.out_xfms', 'in_xfms')])
-        ,(flirt,      mult,       [(('outputnode.out_xfms',_xfm_jacobian),
-                                   'operand_value')])
-        ,(flirt,      split,      [('outputnode.out_file', 'in_file')])
-        ,(split,      mult,       [('out_files', 'in_file')])
-        ,(mult,       thres,      [('out_file', 'in_file')])
-        ,(thres,      merge,      [('out_file', 'in_corrected')])
-        ,(get_mat,    outputnode, [('out_files', 'out_xfms')])
-        ,(merge,      outputnode, [('out_file', 'out_file')])
+        (inputnode,  avg_b0,     [('in_file', 'in_dwi'),
+                                  ('in_bval', 'in_bval')]),
+        (inputnode,  pick_dws,   [('in_file', 'in_dwi'),
+                                  ('in_bval', 'in_bval')]),
+        (inputnode,  merge,      [('in_file', 'in_dwi'),
+                                  ('in_bval', 'in_bval')]),
+        (inputnode,  flirt,      [('in_mask', 'inputnode.ref_mask'),
+                                  ('in_xfms', 'inputnode.in_xfms'),
+                                  ('in_bval', 'inputnode.in_bval')]),
+        (inputnode,  get_mat,    [('in_bval', 'in_bval')]),
+        (avg_b0,     flirt,      [('out_file', 'inputnode.reference')]),
+        (pick_dws,   flirt,      [('out_file', 'inputnode.in_file')]),
+        (flirt,      get_mat,    [('outputnode.out_xfms', 'in_xfms')]),
+        (flirt,      mult,       [(('outputnode.out_xfms', _xfm_jacobian),
+                                  'operand_value')]),
+        (flirt,      split,      [('outputnode.out_file', 'in_file')]),
+        (split,      mult,       [('out_files', 'in_file')]),
+        (mult,       thres,      [('out_file', 'in_file')]),
+        (thres,      merge,      [('out_file', 'in_corrected')]),
+        (get_mat,    outputnode, [('out_files', 'out_xfms')]),
+        (merge,      outputnode, [('out_file', 'out_file')])
     ])
     return wf
 
@@ -513,14 +507,17 @@ def sdc_fmb(name='fmb_correction',
                             acc_factor=3,
                             enc_dir='y-')):
     """
-    SDC stands for susceptibility distortion correction. FMB stands for fieldmap-based.
-
-    The fieldmap based method (FMB) implements SDC by using a mapping of the B0 field
-    as proposed by [Jezzard95]_. This workflow uses the implementation of FSL
-    (`FUGUE <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE>`_). Phase unwrapping is performed
-    using `PRELUDE <http://fsl.fmrib.ox.ac.uk/fsl/fsl-4.1.9/fugue/prelude.html>`_
-    [Jenkinson03]_. Preparation of the fieldmap is performed reproducing the script in FSL
-    `fsl_prepare_fieldmap <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE/Guide#SIEMENS_data>`_.
+    SDC stands for susceptibility distortion correction. FMB stands for
+    fieldmap-based.
+
+    The fieldmap based method (FMB) implements SDC by using a mapping of the
+    B0 field as proposed by [Jezzard95]_. This workflow uses the implementation
+    of FSL (`FUGUE <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE>`_). Phase
+    unwrapping is performed using `PRELUDE
+    <http://fsl.fmrib.ox.ac.uk/fsl/fsl-4.1.9/fugue/prelude.html>`_
+    [Jenkinson03]_. Preparation of the fieldmap is performed reproducing the
+    script in FSL `fsl_prepare_fieldmap
+    <http://fsl.fmrib.ox.ac.uk/fsl/fslwiki/FUGUE/Guide#SIEMENS_data>`_.
 
     Example
     -------
@@ -538,12 +535,14 @@ def sdc_fmb(name='fmb_correction',
 
     .. admonition:: References
 
-      .. [Jezzard95] Jezzard P, and Balaban RS, `Correction for geometric distortion in echo
-        planar images from B0 field variations <http://dx.doi.org/10.1002/mrm.1910340111>`_,
+      .. [Jezzard95] Jezzard P, and Balaban RS, `Correction for geometric
+        distortion in echo planar images from B0 field variations
+        <http://dx.doi.org/10.1002/mrm.1910340111>`_,
         MRM 34(1):65-73. (1995). doi: 10.1002/mrm.1910340111.
 
-      .. [Jenkinson03] Jenkinson M., `Fast, automated, N-dimensional phase-unwrapping algorithm
-        <http://dx.doi.org/10.1002/mrm.10354>`_, MRM 49(1):193-197, 2003, doi: 10.1002/mrm.10354.
+      .. [Jenkinson03] Jenkinson M., `Fast, automated, N-dimensional
+        phase-unwrapping algorithm <http://dx.doi.org/10.1002/mrm.10354>`_,
+        MRM 49(1):193-197, 2003, doi: 10.1002/mrm.10354.
 
     """
     inputnode = pe.Node(niu.IdentityInterface(fields=['in_file', 'in_bval',
@@ -561,7 +560,7 @@ def sdc_fmb(name='fmb_correction',
     dilate = pe.Node(fsl.maths.MathsCommand(nan2zeros=True,
                      args='-kernel sphere 5 -dilM'), name='MskDilate')
     pha2rads = pe.Node(niu.Function(input_names=['in_file'], output_names=['out_file'],
-                      function=siemens2rads), name='PreparePhase')
+                       function=siemens2rads), name='PreparePhase')
     prelude = pe.Node(fsl.PRELUDE(process3d=True), name='PhaseUnwrap')
     rad2rsec = pe.Node(niu.Function(input_names=['in_file', 'delta_te'],
                        output_names=['out_file'], function=rads2radsec), name='ToRadSec')
@@ -572,7 +571,7 @@ def sdc_fmb(name='fmb_correction',
 
     flirt = pe.Node(fsl.FLIRT(interp='spline', cost='normmi', cost_func='normmi',
                     dof=6, bins=64, save_log=True, padding_size=10,
-                    searchr_x=[-4,4], searchr_y=[-4,4], searchr_z=[-4,4],
+                    searchr_x=[-4, 4], searchr_y=[-4, 4], searchr_z=[-4, 4],
                     fine_search=1, coarse_search=10),
                     name='BmapMag2B0')
     applyxfm = pe.Node(fsl.ApplyXfm(interp='spline', padding_size=10, apply_xfm=True),
@@ -606,43 +605,43 @@ def sdc_fmb(name='fmb_correction',
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   pha2rads,   [('bmap_pha', 'in_file')])
-        ,(inputnode,   firstmag,   [('bmap_mag', 'in_file')])
-        ,(inputnode,   avg_b0,     [('in_file', 'in_dwi'),
-                                    ('in_bval', 'in_bval')])
-        ,(firstmag,    n4,         [('roi_file', 'input_image')])
-        ,(n4,          bet,        [('output_image', 'in_file')])
-        ,(bet,         dilate,     [('mask_file', 'in_file')])
-        ,(pha2rads,    prelude,    [('out_file', 'phase_file')])
-        ,(n4,          prelude,    [('output_image', 'magnitude_file')])
-        ,(dilate,      prelude,    [('out_file', 'mask_file')])
-        ,(prelude,     rad2rsec,   [('unwrapped_phase_file', 'in_file')])
-        ,(avg_b0,      flirt,      [('out_file', 'reference')])
-        ,(inputnode,   flirt,      [('in_mask', 'ref_weight')])
-        ,(n4,          flirt,      [('output_image', 'in_file')])
-        ,(dilate,      flirt,      [('out_file', 'in_weight')])
-        ,(avg_b0,      applyxfm,   [('out_file', 'reference')])
-        ,(rad2rsec,    applyxfm,   [('out_file', 'in_file')])
-        ,(flirt,       applyxfm,   [('out_matrix_file', 'in_matrix_file')])
-        ,(applyxfm,    pre_fugue,  [('out_file', 'fmap_in_file')])
-        ,(inputnode,   pre_fugue,  [('in_mask', 'mask_file')])
-        ,(pre_fugue,   demean,     [('fmap_out_file', 'in_file')])
-        ,(inputnode,   demean,     [('in_mask', 'in_mask')])
-        ,(demean,      cleanup,    [('out_file', 'inputnode.in_file')])
-        ,(inputnode,   cleanup,    [('in_mask', 'inputnode.in_mask')])
-        ,(cleanup,     addvol,     [('outputnode.out_file', 'in_file')])
-        ,(inputnode,   vsm,        [('in_mask', 'mask_file')])
-        ,(addvol,      vsm,        [('out_file', 'fmap_in_file')])
-        ,(inputnode,   split,      [('in_file', 'in_file')])
-        ,(split,       unwarp,     [('out_files', 'in_file')])
-        ,(vsm,         unwarp,     [('shift_out_file', 'shift_in_file')])
-        ,(unwarp,      thres,      [('unwarped_file', 'in_file')])
-        ,(thres,       merge,      [('out_file', 'in_files')])
-        ,(merge,       vsm2dfm,    [('merged_file', 'inputnode.in_ref')])
-        ,(vsm,         vsm2dfm,    [('shift_out_file', 'inputnode.in_vsm')])
-        ,(merge,       outputnode, [('merged_file', 'out_file')])
-        ,(vsm,         outputnode, [('shift_out_file', 'out_vsm')])
-        ,(vsm2dfm,     outputnode, [('outputnode.out_warp', 'out_warp')])
+        (inputnode,   pha2rads,   [('bmap_pha', 'in_file')]),
+        (inputnode,   firstmag,   [('bmap_mag', 'in_file')]),
+        (inputnode,   avg_b0,     [('in_file', 'in_dwi'),
+                                   ('in_bval', 'in_bval')]),
+        (firstmag,    n4,         [('roi_file', 'input_image')]),
+        (n4,          bet,        [('output_image', 'in_file')]),
+        (bet,         dilate,     [('mask_file', 'in_file')]),
+        (pha2rads,    prelude,    [('out_file', 'phase_file')]),
+        (n4,          prelude,    [('output_image', 'magnitude_file')]),
+        (dilate,      prelude,    [('out_file', 'mask_file')]),
+        (prelude,     rad2rsec,   [('unwrapped_phase_file', 'in_file')]),
+        (avg_b0,      flirt,      [('out_file', 'reference')]),
+        (inputnode,   flirt,      [('in_mask', 'ref_weight')]),
+        (n4,          flirt,      [('output_image', 'in_file')]),
+        (dilate,      flirt,      [('out_file', 'in_weight')]),
+        (avg_b0,      applyxfm,   [('out_file', 'reference')]),
+        (rad2rsec,    applyxfm,   [('out_file', 'in_file')]),
+        (flirt,       applyxfm,   [('out_matrix_file', 'in_matrix_file')]),
+        (applyxfm,    pre_fugue,  [('out_file', 'fmap_in_file')]),
+        (inputnode,   pre_fugue,  [('in_mask', 'mask_file')]),
+        (pre_fugue,   demean,     [('fmap_out_file', 'in_file')]),
+        (inputnode,   demean,     [('in_mask', 'in_mask')]),
+        (demean,      cleanup,    [('out_file', 'inputnode.in_file')]),
+        (inputnode,   cleanup,    [('in_mask', 'inputnode.in_mask')]),
+        (cleanup,     addvol,     [('outputnode.out_file', 'in_file')]),
+        (inputnode,   vsm,        [('in_mask', 'mask_file')]),
+        (addvol,      vsm,        [('out_file', 'fmap_in_file')]),
+        (inputnode,   split,      [('in_file', 'in_file')]),
+        (split,       unwarp,     [('out_files', 'in_file')]),
+        (vsm,         unwarp,     [('shift_out_file', 'shift_in_file')]),
+        (unwarp,      thres,      [('unwarped_file', 'in_file')]),
+        (thres,       merge,      [('out_file', 'in_files')]),
+        (merge,       vsm2dfm,    [('merged_file', 'inputnode.in_ref')]),
+        (vsm,         vsm2dfm,    [('shift_out_file', 'inputnode.in_vsm')]),
+        (merge,       outputnode, [('merged_file', 'out_file')]),
+        (vsm,         outputnode, [('shift_out_file', 'out_vsm')]),
+        (vsm2dfm,     outputnode, [('outputnode.out_warp', 'out_warp')])
     ])
     return wf
 
@@ -719,51 +718,50 @@ def sdc_peb(name='peb_correction',
 
     unwarp = pe.Node(fsl.ApplyTOPUP(in_index=[1], method='jac'), name='unwarp')
 
-    #scaling = pe.Node(niu.Function(input_names=['in_file', 'enc_dir'],
-    #                  output_names=['factor'], function=_get_zoom),
-    #                  name='GetZoom')
-    #scaling.inputs.enc_dir = epi_params['enc_dir']
+    # scaling = pe.Node(niu.Function(input_names=['in_file', 'enc_dir'],
+    #                   output_names=['factor'], function=_get_zoom),
+    #                   name='GetZoom')
+    # scaling.inputs.enc_dir = epi_params['enc_dir']
     vsm2dfm = vsm2warp()
     vsm2dfm.inputs.inputnode.enc_dir = epi_params['enc_dir']
     vsm2dfm.inputs.inputnode.scaling = readout
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,  b0_ref,     [('in_file', 'in_file'),
-                                   (('ref_num', _checkrnum), 't_min')])
-        ,(inputnode,  b0_alt,     [('alt_file', 'in_file'),
-                                   (('ref_num', _checkrnum), 't_min')])
-        ,(b0_ref,     b0_comb,    [('roi_file', 'in1')])
-        ,(b0_alt,     b0_comb,    [('roi_file', 'in2')])
-        ,(b0_comb,    b0_merge,   [('out', 'in_files')])
-        ,(b0_merge,   topup,      [('merged_file', 'in_file')])
-        ,(topup,      unwarp,     [('out_fieldcoef', 'in_topup_fieldcoef'),
-                                   ('out_movpar', 'in_topup_movpar'),
-                                   ('out_enc_file', 'encoding_file')])
-        ,(inputnode,  unwarp,     [('in_file', 'in_files')])
-        ,(unwarp,     outputnode, [('out_corrected', 'out_file')])
-
-        #,(b0_ref,      scaling,    [('roi_file', 'in_file')])
-        #,(scaling,     vsm2dfm,    [('factor', 'inputnode.scaling')])
-        ,(b0_ref,      vsm2dfm,    [('roi_file', 'inputnode.in_ref')])
-        ,(topup,       vsm2dfm,    [('out_field', 'inputnode.in_vsm')])
-        ,(topup,       outputnode, [('out_field', 'out_vsm')])
-        ,(vsm2dfm,     outputnode, [('outputnode.out_warp', 'out_warp')])
+        (inputnode,  b0_ref,     [('in_file', 'in_file'),
+                                  (('ref_num', _checkrnum), 't_min')]),
+        (inputnode,  b0_alt,     [('alt_file', 'in_file'),
+                                  (('ref_num', _checkrnum), 't_min')]),
+        (b0_ref,     b0_comb,    [('roi_file', 'in1')]),
+        (b0_alt,     b0_comb,    [('roi_file', 'in2')]),
+        (b0_comb,    b0_merge,   [('out', 'in_files')]),
+        (b0_merge,   topup,      [('merged_file', 'in_file')]),
+        (topup,      unwarp,     [('out_fieldcoef', 'in_topup_fieldcoef'),
+                                  ('out_movpar', 'in_topup_movpar'),
+                                  ('out_enc_file', 'encoding_file')]),
+        (inputnode,  unwarp,     [('in_file', 'in_files')]),
+        (unwarp,     outputnode, [('out_corrected', 'out_file')]),
+        # (b0_ref,      scaling,    [('roi_file', 'in_file')]),
+        # (scaling,     vsm2dfm,    [('factor', 'inputnode.scaling')]),
+        (b0_ref,      vsm2dfm,    [('roi_file', 'inputnode.in_ref')]),
+        (topup,       vsm2dfm,    [('out_field', 'inputnode.in_vsm')]),
+        (topup,       outputnode, [('out_field', 'out_vsm')]),
+        (vsm2dfm,     outputnode, [('outputnode.out_warp', 'out_warp')])
     ])
     return wf
 
 
 def remove_bias(name='bias_correct'):
     """
-    This workflow estimates a single multiplicative bias field from the averaged *b0*
-    image, as suggested in [Jeurissen2014]_.
+    This workflow estimates a single multiplicative bias field from the
+    averaged *b0* image, as suggested in [Jeurissen2014]_.
 
     .. admonition:: References
 
-      .. [Jeurissen2014] Jeurissen B. et al., `Multi-tissue constrained spherical deconvolution
-        for improved analysis of multi-shell diffusion MRI data
-        <http://dx.doi.org/10.1016/j.neuroimage.2014.07.061>`_. NeuroImage (2014).
-        doi: 10.1016/j.neuroimage.2014.07.061
+      .. [Jeurissen2014] Jeurissen B. et al., `Multi-tissue constrained
+        spherical deconvolution for improved analysis of multi-shell diffusion
+        MRI data <http://dx.doi.org/10.1016/j.neuroimage.2014.07.061>`_.
+        NeuroImage (2014). doi: 10.1016/j.neuroimage.2014.07.061
 
 
     Example
@@ -797,16 +795,16 @@ def remove_bias(name='bias_correct'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,    avg_b0,         [('in_file', 'in_dwi'),
-                                         ('in_bval', 'in_bval')])
-        ,(avg_b0,       n4,             [('out_file', 'input_image')])
-        ,(inputnode,    n4,             [('in_mask', 'mask_image')])
-        ,(inputnode,    split,          [('in_file', 'in_file')])
-        ,(n4,           mult,           [('bias_image', 'operand_files')])
-        ,(split,        mult,           [('out_files', 'in_file')])
-        ,(mult,         thres,          [('out_file', 'in_file')])
-        ,(thres,        merge,          [('out_file', 'in_files')])
-        ,(merge,        outputnode,     [('merged_file', 'out_file')])
+        (inputnode,    avg_b0,         [('in_file', 'in_dwi'),
+                                        ('in_bval', 'in_bval')]),
+        (avg_b0,       n4,             [('out_file', 'input_image')]),
+        (inputnode,    n4,             [('in_mask', 'mask_image')]),
+        (inputnode,    split,          [('in_file', 'in_file')]),
+        (n4,           mult,           [('bias_image', 'operand_files')]),
+        (split,        mult,           [('out_files', 'in_file')]),
+        (mult,         thres,          [('out_file', 'in_file')]),
+        (thres,        merge,          [('out_file', 'in_files')]),
+        (merge,        outputnode,     [('merged_file', 'out_file')])
     ])
     return wf
 
diff --git a/nipype/workflows/dmri/fsl/utils.py b/nipype/workflows/dmri/fsl/utils.py
--- a/nipype/workflows/dmri/fsl/utils.py
+++ b/nipype/workflows/dmri/fsl/utils.py
@@ -31,18 +31,18 @@ def cleanup_edge_pipeline(name='Cleanup'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,     fugue,      [('in_file', 'fmap_in_file'),
-                                      ('in_mask', 'mask_file')])
-        ,(inputnode,     erode,      [('in_mask', 'in_file')])
-        ,(inputnode,     newmsk,     [('in_mask', 'in_file')])
-        ,(erode,         newmsk,     [('out_file', 'operand_files')])
-        ,(fugue,         applymsk,   [('fmap_out_file', 'in_file')])
-        ,(newmsk,        applymsk,   [('out_file', 'mask_file')])
-        ,(erode,         join,       [('out_file', 'in1')])
-        ,(applymsk,      join,       [('out_file', 'in2')])
-        ,(inputnode,     addedge,    [('in_file', 'in_file')])
-        ,(join,          addedge,    [('out', 'operand_files')])
-        ,(addedge,       outputnode, [('out_file', 'out_file')])
+        (inputnode,     fugue,      [('in_file', 'fmap_in_file'),
+                                     ('in_mask', 'mask_file')]),
+        (inputnode,     erode,      [('in_mask', 'in_file')]),
+        (inputnode,     newmsk,     [('in_mask', 'in_file')]),
+        (erode,         newmsk,     [('out_file', 'operand_files')]),
+        (fugue,         applymsk,   [('fmap_out_file', 'in_file')]),
+        (newmsk,        applymsk,   [('out_file', 'mask_file')]),
+        (erode,         join,       [('out_file', 'in1')]),
+        (applymsk,      join,       [('out_file', 'in2')]),
+        (inputnode,     addedge,    [('in_file', 'in_file')]),
+        (join,          addedge,    [('out', 'operand_files')]),
+        (addedge,       outputnode, [('out_file', 'out_file')])
     ])
     return wf
 
@@ -64,15 +64,14 @@ def vsm2warp(name='Shiftmap2Warping'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   fixhdr,      [('in_vsm', 'in_file'),
-                                     ('in_ref', 'in_file_hdr')])
-        ,(inputnode,   vsm,         [('scaling', 'operand_value')])
-        ,(fixhdr,      vsm,         [('out_file', 'in_file')])
-
-        ,(vsm,         vsm2dfm,     [('out_file', 'shift_in_file')])
-        ,(inputnode,   vsm2dfm,     [('in_ref', 'reference'),
-                                     ('enc_dir', 'shift_direction')])
-        ,(vsm2dfm,     outputnode,  [('out_file', 'out_warp')])
+        (inputnode,   fixhdr,      [('in_vsm', 'in_file'),
+                                    ('in_ref', 'in_file_hdr')]),
+        (inputnode,   vsm,         [('scaling', 'operand_value')]),
+        (fixhdr,      vsm,         [('out_file', 'in_file')]),
+        (vsm,         vsm2dfm,     [('out_file', 'shift_in_file')]),
+        (inputnode,   vsm2dfm,     [('in_ref', 'reference'),
+                                    ('enc_dir', 'shift_direction')]),
+        (vsm2dfm,     outputnode,  [('out_file', 'out_warp')])
     ])
     return wf
 
@@ -111,25 +110,25 @@ def dwi_flirt(name='DWICoregistration', excl_nodiff=False,
                          'out_xfms']), name='outputnode')
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,  split,      [('in_file', 'in_file')])
-        ,(inputnode,  dilate,     [('ref_mask', 'in_file')])
-        ,(inputnode,  enhb0,      [('ref_mask', 'in_mask')])
-        ,(inputnode,  initmat,    [('in_xfms', 'in_xfms'),
-                                   ('in_bval', 'in_bval')])
-        ,(inputnode,  n4,         [('reference', 'input_image'),
-                                   ('ref_mask', 'mask_image')])
-        ,(dilate,     flirt,      [('out_file', 'ref_weight'),
-                                   ('out_file', 'in_weight')])
-        ,(n4,         enhb0,      [('output_image', 'in_file')])
-        ,(split,      enhdw,      [('out_files', 'in_file')])
-        ,(dilate,     enhdw,      [('out_file', 'in_mask')])
-        ,(enhb0,      flirt,      [('out_file', 'reference')])
-        ,(enhdw,      flirt,      [('out_file', 'in_file')])
-        ,(initmat,    flirt,      [('init_xfms', 'in_matrix_file')])
-        ,(flirt,      thres,      [('out_file', 'in_file')])
-        ,(thres,      merge,      [('out_file', 'in_files')])
-        ,(merge,      outputnode, [('merged_file', 'out_file')])
-        ,(flirt,      outputnode, [('out_matrix_file', 'out_xfms')])
+        (inputnode,  split,      [('in_file', 'in_file')]),
+        (inputnode,  dilate,     [('ref_mask', 'in_file')]),
+        (inputnode,  enhb0,      [('ref_mask', 'in_mask')]),
+        (inputnode,  initmat,    [('in_xfms', 'in_xfms'),
+                                  ('in_bval', 'in_bval')]),
+        (inputnode,  n4,         [('reference', 'input_image'),
+                                  ('ref_mask', 'mask_image')]),
+        (dilate,     flirt,      [('out_file', 'ref_weight'),
+                                  ('out_file', 'in_weight')]),
+        (n4,         enhb0,      [('output_image', 'in_file')]),
+        (split,      enhdw,      [('out_files', 'in_file')]),
+        (dilate,     enhdw,      [('out_file', 'in_mask')]),
+        (enhb0,      flirt,      [('out_file', 'reference')]),
+        (enhdw,      flirt,      [('out_file', 'in_file')]),
+        (initmat,    flirt,      [('init_xfms', 'in_matrix_file')]),
+        (flirt,      thres,      [('out_file', 'in_file')]),
+        (thres,      merge,      [('out_file', 'in_files')]),
+        (merge,      outputnode, [('merged_file', 'out_file')]),
+        (flirt,      outputnode, [('out_matrix_file', 'out_xfms')])
     ])
     return wf
 
@@ -170,28 +169,27 @@ def apply_all_corrections(name='UnwarpArtifacts'):
 
     wf = pe.Workflow(name=name)
     wf.connect([
-         (inputnode,   warps,      [('in_sdc', 'warp1'),
-                                    ('in_hmc', 'premat'),
-                                    ('in_ecc', 'postmat'),
-                                    ('in_dwi', 'reference')])
-        ,(inputnode,   split,      [('in_dwi', 'in_file')])
-        ,(split,       selref,     [('out_files', 'inlist')])
-        ,(warps,       unwarp,     [('out_file', 'field_file')])
-        ,(split,       unwarp,     [('out_files', 'in_file')])
-        ,(selref,      unwarp,     [('out', 'ref_file')])
-        ,(selref,      coeffs,     [('out', 'reference')])
-        ,(warps,       coeffs,     [('out_file', 'in_file')])
-        ,(selref,      jacobian,   [('out', 'reference')])
-        ,(coeffs,      jacobian,   [('out_file', 'in_file')])
-        ,(unwarp,      jacmult,    [('out_file', 'in_file')])
-        ,(jacobian,    jacmult,    [('out_jacobian', 'operand_files')])
-        ,(jacmult,     thres,      [('out_file', 'in_file')])
-        ,(thres,       merge,      [('out_file', 'in_files')])
-
-        ,(warps,       outputnode, [('out_file', 'out_warp')])
-        ,(coeffs,      outputnode, [('out_file', 'out_coeff')])
-        ,(jacobian,    outputnode, [('out_jacobian', 'out_jacobian')])
-        ,(merge,       outputnode, [('merged_file', 'out_file')])
+        (inputnode,   warps,      [('in_sdc', 'warp1'),
+                                   ('in_hmc', 'premat'),
+                                   ('in_ecc', 'postmat'),
+                                   ('in_dwi', 'reference')]),
+        (inputnode,   split,      [('in_dwi', 'in_file')]),
+        (split,       selref,     [('out_files', 'inlist')]),
+        (warps,       unwarp,     [('out_file', 'field_file')]),
+        (split,       unwarp,     [('out_files', 'in_file')]),
+        (selref,      unwarp,     [('out', 'ref_file')]),
+        (selref,      coeffs,     [('out', 'reference')]),
+        (warps,       coeffs,     [('out_file', 'in_file')]),
+        (selref,      jacobian,   [('out', 'reference')]),
+        (coeffs,      jacobian,   [('out_file', 'in_file')]),
+        (unwarp,      jacmult,    [('out_file', 'in_file')]),
+        (jacobian,    jacmult,    [('out_jacobian', 'operand_files')]),
+        (jacmult,     thres,      [('out_file', 'in_file')]),
+        (thres,       merge,      [('out_file', 'in_files')]),
+        (warps,       outputnode, [('out_file', 'out_warp')]),
+        (coeffs,      outputnode, [('out_file', 'out_coeff')]),
+        (jacobian,    outputnode, [('out_jacobian', 'out_jacobian')]),
+        (merge,       outputnode, [('merged_file', 'out_file')])
     ])
     return wf
 
@@ -231,6 +229,52 @@ def extract_bval(in_dwi, in_bval, b=0, out_file=None):
     return out_file
 
 
+def hmc_split(in_file, in_bval, ref_num=0, lowbval=5.0):
+    """
+    Selects the reference and moving volumes from a dwi dataset
+    for the purpose of HMC.
+    """
+    import numpy as np
+    import nibabel as nb
+    import os.path as op
+    from nipype.interfaces.base import isdefined
+
+    im = nb.load(in_file)
+    data = im.get_data()
+    hdr = im.get_header().copy()
+    bval = np.loadtxt(in_bval)
+
+    lowbs = np.where(bval <= lowbval)[0]
+
+    volid = lowbs[0]
+    if (isdefined(ref_num) and (ref_num < len(lowbs))):
+        volid = [ref_num]
+
+    if volid == 0:
+        data = data[..., 1:]
+        bval = bval[1:]
+    elif volid == (data.shape[-1] - 1):
+        data = data[..., :-1]
+        bval = bval[:-1]
+    else:
+        data = np.concatenate((data[..., :volid], data[..., (volid + 1):]),
+                              axis=3)
+        bval = np.hstack((bval[:volid], bval[(volid + 1):]))
+
+    out_ref = op.abspath('hmc_ref.nii.gz')
+    out_mov = op.abspath('hmc_mov.nii.gz')
+    out_bval = op.abspath('bval_split.txt')
+
+    hdr.set_data_shape(refdata.shape)
+    refdata = data[..., volid]
+    nb.Nifti1Image(refdata, im.get_affine(), hdr).to_filename(out_ref)
+
+    hdr.set_data_shape(data.shape)
+    nb.Nifti1Image(data, im.get_affine(), hdr).to_filename(out_mov)
+    np.savetxt(out_bval, bval)
+    return [out_ref, out_mov, out_bval, volid]
+
+
 def remove_comp(in_file, in_bval, volid=0, out_file=None):
     """
     Removes the volume ``volid`` from the 4D nifti file
@@ -281,7 +325,8 @@ def insert_mat(inlist, volid=0):
 
 def recompose_dwi(in_dwi, in_bval, in_corrected, out_file=None):
     """
-    Recompose back the dMRI data accordingly the b-values table after EC correction
+    Recompose back the dMRI data accordingly the b-values table after EC
+    correction
     """
     import numpy as np
     import nibabel as nb
@@ -300,7 +345,8 @@ def recompose_dwi(in_dwi, in_bval, in_corrected, out_file=None):
     dwis = np.where(bvals != 0)[0].tolist()
 
     if len(dwis) != len(in_corrected):
-        raise RuntimeError('Length of DWIs in b-values table and after correction should match')
+        raise RuntimeError(('Length of DWIs in b-values table and after'
+                            'correction should match'))
 
     for bindex, dwi in zip(dwis, in_corrected):
         dwidata[..., bindex] = nb.load(dwi).get_data()
@@ -348,15 +394,16 @@ def b0_average(in_dwi, in_bval, out_file=None):
     import os.path as op
 
     if out_file is None:
-        fname,ext = op.splitext(op.basename(in_dwi))
+        fname, ext = op.splitext(op.basename(in_dwi))
         if ext == ".gz":
-            fname,ext2 = op.splitext(fname)
+            fname, ext2 = op.splitext(fname)
             ext = ext2 + ext
         out_file = op.abspath("%s_avg_b0%s" % (fname, ext))
 
     imgs = np.array(nb.four_to_three(nb.load(in_dwi)))
     bval = np.loadtxt(in_bval)
-    b0s = [im.get_data().astype(np.float32) for im in imgs[np.where(bval==0)]]
+    b0s = [im.get_data().astype(np.float32)
+           for im in imgs[np.where(bval == 0)]]
     b0 = np.average(np.array(b0s), axis=0)
 
     hdr = imgs[0].get_header().copy()
diff --git a/nipype/workflows/fmri/spm/preprocess.py b/nipype/workflows/fmri/spm/preprocess.py
--- a/nipype/workflows/fmri/spm/preprocess.py
+++ b/nipype/workflows/fmri/spm/preprocess.py
@@ -254,11 +254,11 @@ def create_DARTEL_template(name='dartel_template'):
                              iterfield=['channel_files'],
                              name='segment')
     workflow.connect(inputnode, 'structural_files', segment, 'channel_files')
-    
+
     version = spm.Info.version()
     if version:
         spm_path = version['path']
-        if version['name'] == 'SPM8':          
+        if version['name'] == 'SPM8':
             tissue1 = ((os.path.join(spm_path,'toolbox/Seg/TPM.nii'), 1), 2, (True,True), (False, False))
             tissue2 = ((os.path.join(spm_path,'toolbox/Seg/TPM.nii'), 2), 2, (True,True), (False, False))
             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'):
             tissue6 = ((os.path.join(spm_path,'tpm/TPM.nii'), 6), 2, (False,False), (False, False))
         else:
             logger.critical('Unsupported version of SPM')
-            
+
         segment.inputs.tissues = [tissue1, tissue2, tissue3, tissue4, tissue5, tissue6]
     else:
         logger.critical('SPM not found')
-        
+
     dartel = pe.Node(spm.DARTEL(), name='dartel')
 
     """Get the gray and white segmentation classes generated by NewSegment


Test patch: