Repository: nipy/nipype

Issue:
Error creating gradient_matrix.txt in DTIRecon (Diffusion Toolkit)
### Summary
When the gradient_matrix.txt file is created from bvecs, the lines are [read](https://github.com/nipy/nipype/blob/db267032616ba05b5fd465491da6134e46041276/nipype/interfaces/diffusion_toolkit/dti.py#L106) in order y, z, x; instead of x, y, z swapping all the directions in the resulting gradient_matrix.txt file 

### Actual behavior
The columns of the actual gradient_matrix.txt are y, z, x, b-value.

### Expected behavior
The columns of the expected gradient_matrix.txt must be x, y ,z, b-value.

### How to replicate the behavior
Just run the example in the documentation of nipype interface for Diffusion Toolkit. My data comes from a Philips Scanner.

### Platform details:
{'scipy_version': '1.0.0', 'sys_executable': '/Users/miguel/anaconda/bin/python', 'sys_platform': 'darwin', 'commit_hash': '%h', 'traits_version': '4.6.0', 'nipype_version': '1.0.0', 'pkg_path': '/Users/miguel/anaconda/lib/python3.5/site-packages/nipype', 'commit_source': 'archive substitution', 'nibabel_version': '2.2.1', 'numpy_version': '1.14.0', '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)]', 'networkx_version': '2.1'}
1.0.0


Discussion:
@mmromero thanks for letting us know.

@oesteban - I just want to double check with you, since I checked that you were changing the order in [your commit](https://github.com/nipy/nipype/commit/a16b7eb356b7f1560f1327eceb0e422ed12fd9b5). Was it just a mistake? 
@mmromero aren't they written in the right order [here](https://github.com/nipy/nipype/blob/db267032616ba05b5fd465491da6134e46041276/nipype/interfaces/diffusion_toolkit/dti.py#L121)?
@oesteban, I had different results when using dti_recon from the termnial and nipype DTIRecon with exactly the same configuration. So, after diving into the details I found that the gradient_matrix.txt files were different. 

I modified my local DTIRecon to read the bvecs file as follow, and the results from dti_recon and DTIRecon were the same.
```
    def _create_gradient_matrix(self, bvecs_file, bvals_file):
        _gradient_matrix_file = 'gradient_matrix.txt'
        with open(bvals_file) as fbvals:
            bvals = [val for val in re.split('\s+', fbvals.readline().strip())]
        with open(bvecs_file) as fbvecs:
            bvecs_x = [
                val for val in re.split('\s+',
                                        fbvecs.readline().strip())
            ]
            bvecs_y = [
                val for val in re.split('\s+',
                                        fbvecs.readline().strip())
            ]
            bvecs_z = [
                val for val in re.split('\s+',
                                        fbvecs.readline().strip())
            ] 
...
```


@oesteban the part where you read `bvecs_file` has different order than expected. I was checking that  [odf](https://github.com/nipy/nipype/blob/db267032616ba05b5fd465491da6134e46041276/nipype/interfaces/diffusion_toolkit/odf.py#L107) have the "traditional" x,y,z order.

I can reorder the reading part, but since I saw you changed the order from x,y,z to z,x,y some time ago, I wanted just check if you had anything specific in mind.
I can't recall why is that. Probably a bug :(
@oesteban ok, sorry for spamming, just didn't want to reverse your changes without checking.

@mmromero - thank you again for reporting, would you like to create a pull request with your changes?
Sure, I will.
Hi @mmromero we're hoping to release on Monday. If you have a few minutes to submit this fix, it would be good to get it in quickly.
@effigies, I don't have rights to push a branch into the repository. So I cannot create the pull request. Can you grant me access?
You need to fork a copy of the repository, and push to that.

See the [contributing guide](https://github.com/nipy/nipype/blob/master/CONTRIBUTING.md#making-a-change).

Resolving patch:
diff --git a/nipype/interfaces/diffusion_toolkit/dti.py b/nipype/interfaces/diffusion_toolkit/dti.py
--- a/nipype/interfaces/diffusion_toolkit/dti.py
+++ b/nipype/interfaces/diffusion_toolkit/dti.py
@@ -103,18 +103,9 @@ def _create_gradient_matrix(self, bvecs_file, bvals_file):
         with open(bvals_file) as fbvals:
             bvals = [val for val in re.split('\s+', fbvals.readline().strip())]
         with open(bvecs_file) as fbvecs:
-            bvecs_y = [
-                val for val in re.split('\s+',
-                                        fbvecs.readline().strip())
-            ]
-            bvecs_z = [
-                val for val in re.split('\s+',
-                                        fbvecs.readline().strip())
-            ]
-            bvecs_x = [
-                val for val in re.split('\s+',
-                                        fbvecs.readline().strip())
-            ]
+          bvecs_x = fbvecs.readline().split()
+          bvecs_y = fbvecs.readline().split()
+          bvecs_z = fbvecs.readline().split()
 
         with open(_gradient_matrix_file, 'w') as gradient_matrix_f:
             for i in range(len(bvals)):


Test patch: