Repository: nipy/nipype

Issue:
ecc_pipeline discrepency in intensities
### Summary
ecc_pipeline change intensity range for non B0 scans

### Actual behavior
I have a raw diffusion image with B0 scan intensities around 1000 and other scans around 200.
After the ecc_pipeline, the B0 scan intensities have the same range while the other scans are between 0 and 1  

### Expected behavior
I expected the range of the scans to remain comparable to the raw image

### How to replicate the behavior
```Python
from nipype.workflows.dmri.fsl.artifacts import ecc_pipeline 
ecc = ecc_pipeline()
ecc.inputs.input_node.in_file = '4d_dwi.nii.gz'
ecc.inputs.input_node.in_bval = 'bval'
ecc.inputs.input_node.in_mask = 'brain_mask.nii.gz'
ecc.run()
```
### Platform details:
{'nibabel_version': '2.1.0', 'networkx_version': '1.11', 'numpy_version': '1.12.0', 'sys_platform': 'linux2', 'sys_version': '2.7.12 |Anaconda custom (64-bit)| (default, Jul  2 2016, 17:42:40) \n[GCC 4.4.7 20120313 (Red Hat 4.4.7-1)]', 'commit_source': 'installation', 'commit_hash': u'a4d1e07', 'pkg_path': '/home/salma/anaconda2/lib/python2.7/site-packages/nipype', 'sys_executable': '/home/salma/anaconda2/bin/python', 'traits_version': '4.6.0', 'scipy_version': '0.18.1'}



Discussion:
I am having exactly the same problem.

**Platform details:**
{'nibabel_version': '2.2.1', 'commit_hash': '%h', 'scipy_version': '1.0.0', 'sys_executable': '/Users/miguel/anaconda/bin/python', 'traits_version': '4.6.0', 'commit_source': 'archive substitution', 'networkx_version': '2.1', 'numpy_version': '1.14.0', 'nipype_version': '1.0.0', 'pkg_path': '/Users/miguel/anaconda/lib/python3.5/site-packages/nipype', 'sys_platform': 'darwin', 'sys_version': '3.5.4 | packaged by conda-forge | (default, Dec 18 2017, 06:40:48) \n[GCC 4.2.1 Compatible Apple LLVM 6.1.0 (clang-602.0.53)]'}
@salma1601 , @mmromero any chance you can point me to some data files I can use to reproduce the issue?
Hi @djarecka. I am just using a standard 4D volume. For what I saw, the problem comes from [dwi_flirt](https://github.com/nipy/nipype/blob/c28dbd458e5916d9d30b8c43bcac79d917934e40/nipype/workflows/dmri/fsl/artifacts.py#L568), where each volume (including the b0s) is equalized for FLIRT, then the original b0s are [merged](https://github.com/nipy/nipype/blob/c28dbd458e5916d9d30b8c43bcac79d917934e40/nipype/workflows/dmri/fsl/artifacts.py#L586) to the equalized dwi causing the issue. 
@djarecka From what I remember, the problem came from `exposure.equalize_adapthist` because I commented out this line https://github.com/nipy/nipype/blob/master/nipype/workflows/dmri/fsl/utils.py#L802
and the following one as a workaround for this error. I didn't go more in trying to debug

Resolving patch:
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
@@ -125,6 +125,14 @@ def dwi_flirt(name='DWICoregistration', excl_nodiff=False, flirt_param={}):
         fsl.FLIRT(**flirt_param),
         name='CoRegistration',
         iterfield=['in_file', 'in_matrix_file'])
+    apply_xfms = pe.MapNode(
+        fsl.ApplyXFM(
+            apply_xfm=True,
+            interp='spline',
+            bgvalue=0),
+        name='ApplyXFMs',
+        iterfield=['in_file', 'in_matrix_file']
+    )
     thres = pe.MapNode(
         fsl.Threshold(thresh=0.0),
         iterfield=['in_file'],
@@ -134,25 +142,30 @@ def dwi_flirt(name='DWICoregistration', excl_nodiff=False, flirt_param={}):
         niu.IdentityInterface(fields=['out_file', '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')])])
+    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')]),
+        (split, apply_xfms, [('out_files', 'in_file')]),
+        (dilate, enhdw, [('out_file', 'in_mask')]),
+        (enhb0, flirt, [('out_file', 'reference')]),
+        (enhb0, apply_xfms, [('out_file', 'reference')]),
+        (enhdw, flirt, [('out_file', 'in_file')]),
+        (initmat, flirt, [('init_xfms', 'in_matrix_file')]),
+        (flirt, apply_xfms, [('out_matrix_file', 'in_matrix_file')]),
+        (apply_xfms, 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
 
 


Test patch: