Repository: nipy/nipype

Issue:
dcmstack output file path
Hello, I am trying to specify an ouput file path for the `nipype.interfaces.dcmstack.DcmStack` interface, which should be in my data hierarchy and not in my script hierarchy.

I am doing this ~via:

```
from nipype.interfaces.dcmstack import DcmStack
stacker = DcmStack()
stacker.inputs.out_format = "/path/to/my/data"
```

stubbornly, dcmstack will export the resulting file to `/path/to/my/script/_path_to_my_data`

Can we fix this somehow? Right now I hav to export to my script directory and then move the file -.- which is a dodgy and slow way to fix this (different hard drives etc).



Discussion:
Right now, I guess that's not possible: https://github.com/nipy/nipype/blob/44f75665bf4776d7e6ff63395254de3111da8ce5/nipype/interfaces/dcmstack.py#L76

A nice solution to the problem could be adding a bool switch to NiftiGeneratorBaseInputSpec indicating if the path is absolute or relative. Then, modify the line I've pointed out to choose between op.join or op.abspath.

and how would that switch get triggered?


Resolving patch:
diff --git a/nipype/interfaces/dcmstack.py b/nipype/interfaces/dcmstack.py
--- a/nipype/interfaces/dcmstack.py
+++ b/nipype/interfaces/dcmstack.py
@@ -42,12 +42,15 @@ def sanitize_path_comp(path_comp):
             result.append(char)
     return ''.join(result)
 
+
 class NiftiGeneratorBaseInputSpec(TraitedSpec):
     out_format = traits.Str(desc="String which can be formatted with "
                             "meta data to create the output filename(s)")
-    out_ext = traits.Str('.nii.gz',
-                         usedefault=True,
+    out_ext = traits.Str('.nii.gz', usedefault=True,
                          desc="Determines output file type")
+    use_cwd = traits.Bool(True, usedefault=True,
+                          desc='use interface\'s current working directory')
+
 
 class NiftiGeneratorBase(BaseInterface):
     '''Base class for interfaces that produce Nifti files, potentially with
@@ -73,7 +76,12 @@ def _get_out_path(self, meta, idx=None):
             out_fmt = '-'.join(out_fmt)
         out_fn = (out_fmt % meta) + self.inputs.out_ext
         out_fn = sanitize_path_comp(out_fn)
-        return path.join(os.getcwd(), out_fn)
+
+        if self.inputs.use_cwd:
+            return path.join(os.getcwd(), out_fn)
+        else:
+            return path.abspath(out_fn)
+
 
 class DcmStackInputSpec(NiftiGeneratorBaseInputSpec):
     dicom_files = traits.Either(InputMultiPath(File(exists=True)),


Test patch:
diff --git a/nipype/interfaces/tests/test_auto_DcmStack.py b/nipype/interfaces/tests/test_auto_DcmStack.py
--- a/nipype/interfaces/tests/test_auto_DcmStack.py
+++ b/nipype/interfaces/tests/test_auto_DcmStack.py
@@ -13,6 +13,8 @@ def test_DcmStack_inputs():
     out_ext=dict(usedefault=True,
     ),
     out_format=dict(),
+    use_cwd=dict(usedefault=True,
+    ),
     )
     inputs = DcmStack.input_spec()
 
diff --git a/nipype/interfaces/tests/test_auto_GroupAndStack.py b/nipype/interfaces/tests/test_auto_GroupAndStack.py
--- a/nipype/interfaces/tests/test_auto_GroupAndStack.py
+++ b/nipype/interfaces/tests/test_auto_GroupAndStack.py
@@ -13,6 +13,8 @@ def test_GroupAndStack_inputs():
     out_ext=dict(usedefault=True,
     ),
     out_format=dict(),
+    use_cwd=dict(usedefault=True,
+    ),
     )
     inputs = GroupAndStack.input_spec()
 
diff --git a/nipype/interfaces/tests/test_auto_MergeNifti.py b/nipype/interfaces/tests/test_auto_MergeNifti.py
--- a/nipype/interfaces/tests/test_auto_MergeNifti.py
+++ b/nipype/interfaces/tests/test_auto_MergeNifti.py
@@ -10,6 +10,8 @@ def test_MergeNifti_inputs():
     ),
     out_format=dict(),
     sort_order=dict(),
+    use_cwd=dict(usedefault=True,
+    ),
     )
     inputs = MergeNifti.input_spec()
 
diff --git a/nipype/interfaces/tests/test_auto_SplitNifti.py b/nipype/interfaces/tests/test_auto_SplitNifti.py
--- a/nipype/interfaces/tests/test_auto_SplitNifti.py
+++ b/nipype/interfaces/tests/test_auto_SplitNifti.py
@@ -9,6 +9,8 @@ def test_SplitNifti_inputs():
     ),
     out_format=dict(),
     split_dim=dict(),
+    use_cwd=dict(usedefault=True,
+    ),
     )
     inputs = SplitNifti.input_spec()