Skip to content

Commit 23dccd8

Browse files
committed
Using overload_extension instead of list_output/gen_filename
1 parent 84adfcd commit 23dccd8

12 files changed

+61
-70
lines changed

nipype/interfaces/niftyseg/label_fusion.py

Lines changed: 10 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@
1919
from ..base import (TraitedSpec, File, traits, isdefined, CommandLineInputSpec,
2020
NipypeInterfaceError)
2121
from .base import NiftySegCommand, get_custom_path
22-
from ...utils.filemanip import load_json, save_json
22+
from ...utils.filemanip import load_json, save_json, split_filename
2323

2424

2525
warn = warnings.warn
@@ -46,7 +46,8 @@ class LabelFusionInput(CommandLineInputSpec):
4646
desc='Filename of the ROI for label fusion')
4747

4848
out_file = File(argstr='-out %s',
49-
genfile=True,
49+
name_source=['in_file'],
50+
name_template='%s',
5051
desc='Output consensus segmentation')
5152

5253
prob_flag = traits.Bool(desc='Probabilistic/Fuzzy segmented image',
@@ -145,9 +146,9 @@ class LabelFusion(NiftySegCommand):
145146
>>> node.inputs.template_file = 'im3.nii'
146147
>>> node.inputs.template_num = 2
147148
>>> node.inputs.classifier_type = 'STEPS'
148-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
149+
>>> node.cmdline # doctest: +ALLOW_UNICODE
149150
'seg_LabFusion -in im1.nii -STEPS 2.000000 2 im2.nii im3.nii -out \
150-
.../im1_steps.nii'
151+
im1_steps.nii'
151152
152153
"""
153154
_cmd = get_custom_path('seg_LabFusion')
@@ -246,19 +247,11 @@ def get_staple_args(self, ranking):
246247
self.inputs.file_to_seg,
247248
self.inputs.template_file)
248249

249-
def _list_outputs(self):
250-
outputs = self.output_spec().get()
251-
if isdefined(self.inputs.out_file):
252-
outputs['out_file'] = self.inputs.out_file
253-
else:
254-
outputs['out_file'] = self._gen_filename('out_file')
255-
return outputs
256-
257-
def _gen_filename(self, name):
258-
_suffix = '_%s' % self.inputs.classifier_type.lower()
259-
if name == 'out_file':
260-
return self._gen_fname(self.inputs.in_file, suffix=_suffix)
261-
return None
250+
def _overload_extension(self, value, name=None):
251+
path, base, _ = split_filename(value)
252+
_, _, ext = split_filename(self.inputs.in_file)
253+
suffix = self.inputs.classifier_type.lower()
254+
return os.path.join(path, '{0}_{1}{2}'.format(base, suffix, ext))
262255

263256

264257
class CalcTopNCCInputSpec(CommandLineInputSpec):

nipype/interfaces/niftyseg/maths.py

Lines changed: 26 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@
1818
>>> os.chdir(datadir)
1919
"""
2020

21+
import os
22+
2123
from ..base import (TraitedSpec, File, traits, isdefined, CommandLineInputSpec,
2224
NipypeInterfaceError)
2325
from .base import NiftySegCommand, get_custom_path
26+
from ...utils.filemanip import split_filename
2427

2528

2629
class MathsInput(CommandLineInputSpec):
@@ -31,7 +34,8 @@ class MathsInput(CommandLineInputSpec):
3134
mandatory=True,
3235
desc='image to operate on')
3336

34-
out_file = File(genfile=True,
37+
out_file = File(name_source=['in_file'],
38+
name_template='%s',
3539
position=-2,
3640
argstr='%s',
3741
desc='image to write')
@@ -47,7 +51,7 @@ class MathsInput(CommandLineInputSpec):
4751

4852
class MathsOutput(TraitedSpec):
4953
"""Output Spec for seg_maths interfaces."""
50-
out_file = File(exists=True, desc='image written after calculations')
54+
out_file = File(desc='image written after calculations')
5155

5256

5357
class MathsCommand(NiftySegCommand):
@@ -72,24 +76,15 @@ class MathsCommand(NiftySegCommand):
7276
output_spec = MathsOutput
7377
_suffix = '_maths'
7478

75-
def _list_outputs(self):
76-
outputs = self.output_spec().get()
79+
def _overload_extension(self, value, name=None):
80+
path, base, _ = split_filename(value)
81+
_, _, ext = split_filename(self.inputs.in_file)
7782

7883
suffix = self._suffix
7984
if suffix != '_merged' and isdefined(self.inputs.operation):
8085
suffix = '_' + self.inputs.operation
8186

82-
if isdefined(self.inputs.out_file):
83-
outputs['out_file'] = self.inputs.out_file
84-
else:
85-
outputs['out_file'] = self._gen_fname(self.inputs.in_file,
86-
suffix=suffix)
87-
return outputs
88-
89-
def _gen_filename(self, name):
90-
if name == 'out_file':
91-
return self._list_outputs()['out_file']
92-
return None
87+
return os.path.join(path, '{0}{1}{2}'.format(base, suffix, ext))
9388

9489

9590
class UnaryMathsInput(MathsInput):
@@ -149,8 +144,8 @@ class UnaryMaths(MathsCommand):
149144
>>> node.inputs.in_file = 'im1.nii'
150145
>>> node.inputs.operation = 'sqrt'
151146
>>> node.inputs.output_datatype = 'float'
152-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
153-
'seg_maths im1.nii -sqrt -odt float .../im1_sqrt.nii'
147+
>>> node.cmdline # doctest: +ALLOW_UNICODE
148+
'seg_maths im1.nii -sqrt -odt float im1_sqrt.nii'
154149
155150
"""
156151
input_spec = UnaryMathsInput
@@ -237,8 +232,8 @@ class BinaryMaths(MathsCommand):
237232
>>> node.inputs.operation = 'sub'
238233
>>> node.inputs.operand_file = 'im2.nii'
239234
>>> node.inputs.output_datatype = 'float'
240-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
241-
'seg_maths im1.nii -sub im2.nii -odt float .../im1_sub.nii'
235+
>>> node.cmdline # doctest: +ALLOW_UNICODE
236+
'seg_maths im1.nii -sub im2.nii -odt float im1_sub.nii'
242237
243238
"""
244239
input_spec = BinaryMathsInput
@@ -255,14 +250,13 @@ def _format_arg(self, opt, spec, val):
255250

256251
return super(BinaryMaths, self)._format_arg(opt, spec, val)
257252

258-
def _list_outputs(self):
259-
outputs = super(BinaryMaths, self)._list_outputs()
260-
253+
def _overload_extension(self, value, name=None):
254+
path = super(BinaryMaths, self)._overload_extension(value, name)
261255
if self.inputs.operation == 'hdr_copy':
262-
outputs['out_file'] = self._gen_fname(
263-
self.inputs.operand_file,
264-
suffix='_{0}'.format(self.inputs.operation))
265-
return outputs
256+
_, base, ext = split_filename(self.inputs.operand_file)
257+
suffix = self.inputs.operation
258+
path = os.path.join(path, '{0}{1}{2}'.format(base, suffix, ext))
259+
return path
266260

267261

268262
class BinaryMathsInputInteger(MathsInput):
@@ -305,8 +299,8 @@ class BinaryMathsInteger(MathsCommand):
305299
>>> node.inputs.operation = 'dil'
306300
>>> node.inputs.operand_value = 2
307301
>>> node.inputs.output_datatype = 'float'
308-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
309-
'seg_maths im1.nii -dil 2 -odt float .../im1_dil.nii'
302+
>>> node.cmdline # doctest: +ALLOW_UNICODE
303+
'seg_maths im1.nii -dil 2 -odt float im1_dil.nii'
310304
311305
"""
312306
input_spec = BinaryMathsInputInteger
@@ -376,8 +370,8 @@ class TupleMaths(MathsCommand):
376370
>>> node.inputs.operand_file1 = 'im2.nii'
377371
>>> node.inputs.operand_value2 = 2.0
378372
>>> node.inputs.output_datatype = 'float'
379-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
380-
'seg_maths im1.nii -lncc im2.nii 2.00000000 -odt float .../im1_lncc.nii'
373+
>>> node.cmdline # doctest: +ALLOW_UNICODE
374+
'seg_maths im1.nii -lncc im2.nii 2.00000000 -odt float im1_lncc.nii'
381375
382376
"""
383377
input_spec = TupleMathsInput
@@ -417,9 +411,8 @@ class Merge(MathsCommand):
417411
>>> node.inputs.merge_files = files
418412
>>> node.inputs.dimension = 2
419413
>>> node.inputs.output_datatype = 'float'
420-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
421-
'seg_maths im1.nii -merge 2 2 im2.nii im3.nii -odt float \
422-
.../im1_merged.nii'
414+
>>> node.cmdline # doctest: +ALLOW_UNICODE
415+
'seg_maths im1.nii -merge 2 2 im2.nii im3.nii -odt float im1_merged.nii'
423416
424417
"""
425418
input_spec = MergeInput

nipype/interfaces/niftyseg/patchmatch.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@
1111
>>> datadir = os.path.realpath(os.path.join(filepath, '../../testing/data'))
1212
>>> os.chdir(datadir)
1313
"""
14-
import os
14+
1515
import warnings
1616

17-
from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec
17+
from ..base import TraitedSpec, File, traits, CommandLineInputSpec
1818
from .base import NiftySegCommand, get_custom_path
1919

2020

nipype/interfaces/niftyseg/tests/test_auto_BinaryMaths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def test_BinaryMaths_inputs():
3636
position=4,
3737
),
3838
out_file=dict(argstr='%s',
39-
genfile=True,
39+
name_source=['in_file'],
40+
name_template='%s',
4041
position=-2,
4142
),
4243
output_datatype=dict(argstr='-odt %s',

nipype/interfaces/niftyseg/tests/test_auto_BinaryMathsInteger.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ def test_BinaryMathsInteger_inputs():
2525
position=4,
2626
),
2727
out_file=dict(argstr='%s',
28-
genfile=True,
28+
name_source=['in_file'],
29+
name_template='%s',
2930
position=-2,
3031
),
3132
output_datatype=dict(argstr='-odt %s',

nipype/interfaces/niftyseg/tests/test_auto_LabelFusion.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,8 @@ def test_LabelFusion_inputs():
3333
mrf_value=dict(argstr='-MRF_beta %f',
3434
),
3535
out_file=dict(argstr='-out %s',
36-
genfile=True,
36+
name_source=['in_file'],
37+
name_template='%s',
3738
),
3839
prob_flag=dict(argstr='-outProb',
3940
),

nipype/interfaces/niftyseg/tests/test_auto_MathsCommand.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,8 @@ def test_MathsCommand_inputs():
1717
position=2,
1818
),
1919
out_file=dict(argstr='%s',
20-
genfile=True,
20+
name_source=['in_file'],
21+
name_template='%s',
2122
position=-2,
2223
),
2324
output_datatype=dict(argstr='-odt %s',

nipype/interfaces/niftyseg/tests/test_auto_Merge.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,8 @@ def test_Merge_inputs():
2323
position=4,
2424
),
2525
out_file=dict(argstr='%s',
26-
genfile=True,
26+
name_source=['in_file'],
27+
name_template='%s',
2728
position=-2,
2829
),
2930
output_datatype=dict(argstr='-odt %s',

nipype/interfaces/niftyseg/tests/test_auto_TupleMaths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,8 @@ def test_TupleMaths_inputs():
4141
position=4,
4242
),
4343
out_file=dict(argstr='%s',
44-
genfile=True,
44+
name_source=['in_file'],
45+
name_template='%s',
4546
position=-2,
4647
),
4748
output_datatype=dict(argstr='-odt %s',

nipype/interfaces/niftyseg/tests/test_auto_UnaryMaths.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def test_UnaryMaths_inputs():
2121
position=4,
2222
),
2323
out_file=dict(argstr='%s',
24-
genfile=True,
24+
name_source=['in_file'],
25+
name_template='%s',
2526
position=-2,
2627
),
2728
output_datatype=dict(argstr='-odt %s',

0 commit comments

Comments
 (0)