Skip to content

Commit 455766d

Browse files
committed
Merge pull request #694 from chrisfilo/enh/custom_output_names
Added support for custom output names
2 parents d8f6c52 + 8f8c19d commit 455766d

File tree

3 files changed

+19
-21
lines changed

3 files changed

+19
-21
lines changed

doc/devel/cmd_interface_devel.rst

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -193,6 +193,10 @@ hash_files
193193

194194
name_template (optional)
195195
overrides the default ``_generated`` suffix
196+
197+
output_name (optional)
198+
name of the output (if this is not set same name as the input will be
199+
assumed)
196200

197201
keep_extension (optional - not used)
198202
if you want the extension from the input to be kept

nipype/interfaces/base.py

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1466,7 +1466,10 @@ def _filename_from_source(self, name):
14661466
break
14671467
else:
14681468
name_source = trait_spec.name_source
1469-
_, base, _ = split_filename(getattr(self.inputs, name_source))
1469+
source = getattr(self.inputs, name_source)
1470+
while isinstance(source, list):
1471+
source = source[0]
1472+
_, base, _ = split_filename(source)
14701473
retval = name_template % base
14711474
_, _, ext = split_filename(retval)
14721475
if trait_spec.keep_extension and ext:
@@ -1482,11 +1485,14 @@ def _overload_extension(self, value):
14821485

14831486
def _list_outputs(self):
14841487
metadata = dict(name_source=lambda t: t is not None)
1485-
out_names = self.inputs.traits(**metadata).keys()
1486-
if out_names:
1488+
traits = self.inputs.traits(**metadata)
1489+
if traits:
14871490
outputs = self.output_spec().get()
1488-
for name in out_names:
1489-
outputs[name] = \
1491+
for name, trait_spec in traits.iteritems():
1492+
out_name = name
1493+
if trait_spec.output_name != None:
1494+
out_name = trait_spec.output_name
1495+
outputs[out_name] = \
14901496
os.path.abspath(self._filename_from_source(name))
14911497
return outputs
14921498

nipype/interfaces/mrtrix/preprocess.py

Lines changed: 4 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -84,8 +84,8 @@ def _gen_outfilename(self):
8484
class DWI2TensorInputSpec(CommandLineInputSpec):
8585
in_file = InputMultiPath(File(exists=True), argstr='%s', mandatory=True,
8686
position=-2, desc='Diffusion-weighted images')
87-
out_filename = File(genfile=True, argstr='%s', position=-1,
88-
desc='Output tensor filename')
87+
out_filename = File(name_template="%s_tensor.mif", name_source="in_file", output_name="tensor",
88+
argstr='%s', posidesc='Output tensor filename', position=-1)
8989
encoding_file = File(argstr='-grad %s', position= 2,
9090
desc=('Encoding file supplied as a 4xN text file with '
9191
'each line is in the format [ X Y Z b ], where '
@@ -114,27 +114,15 @@ class DWI2Tensor(CommandLine):
114114
>>> dwi2tensor = mrt.DWI2Tensor()
115115
>>> dwi2tensor.inputs.in_file = 'dwi.mif'
116116
>>> dwi2tensor.inputs.encoding_file = 'encoding.txt'
117+
>>> dwi2tensor.cmdline
118+
'dwi2tensor -grad encoding.txt dwi.mif dwi_tensor.mif'
117119
>>> dwi2tensor.run() # doctest: +SKIP
118120
"""
119121

120122
_cmd = 'dwi2tensor'
121123
input_spec=DWI2TensorInputSpec
122124
output_spec=DWI2TensorOutputSpec
123125

124-
def _list_outputs(self):
125-
outputs = self.output_spec().get()
126-
outputs['tensor'] = op.abspath(self._gen_outfilename())
127-
return outputs
128-
129-
def _gen_filename(self, name):
130-
if name is 'out_filename':
131-
return self._gen_outfilename()
132-
else:
133-
return None
134-
def _gen_outfilename(self):
135-
_, name , _ = split_filename(self.inputs.in_file[0])
136-
return name + '_tensor.mif'
137-
138126
class Tensor2VectorInputSpec(CommandLineInputSpec):
139127
in_file = File(exists=True, argstr='%s', mandatory=True, position=-2,
140128
desc='Diffusion tensor image')

0 commit comments

Comments
 (0)