Skip to content

Commit b84f217

Browse files
committed
Adding NiftyRegCommandInputSpec and using overload_extension
1 parent 1527855 commit b84f217

File tree

8 files changed

+64
-94
lines changed

8 files changed

+64
-94
lines changed

nipype/interfaces/niftyreg/base.py

Lines changed: 8 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@
2525
import subprocess
2626
from warnings import warn
2727

28-
from ..base import CommandLine, isdefined
28+
from ..base import CommandLine, isdefined, CommandLineInputSpec, traits
2929
from ...utils.filemanip import split_filename
3030

3131

@@ -46,6 +46,13 @@ def no_niftyreg(cmd='reg_f3d'):
4646
return True
4747

4848

49+
class NiftyRegCommandInputSpec(CommandLineInputSpec):
50+
"""Input Spec for niftyreg interfaces."""
51+
# Set the number of omp thread to use
52+
omp_core_val = traits.Int(desc='Number of openmp thread to use',
53+
argstr='-omp %i')
54+
55+
4956
class NiftyRegCommand(CommandLine):
5057
"""
5158
Base support interface for NiftyReg commands.
@@ -118,18 +125,3 @@ def _gen_fname(self, basename, out_dir=None, suffix=None, ext=None):
118125
if suffix is not None:
119126
final_bn = ''.join((final_bn, suffix))
120127
return os.path.abspath(os.path.join(out_dir, final_bn + final_ext))
121-
122-
def _gen_filename(self, name):
123-
if name == 'out_file':
124-
return self._gen_fname(self.inputs.in_file,
125-
suffix=self._suffix,
126-
ext='.nii.gz')
127-
return None
128-
129-
def _list_outputs(self):
130-
outputs = self.output_spec().get()
131-
if isdefined(self.inputs.out_file):
132-
outputs['out_file'] = self.inputs.out_file
133-
else:
134-
outputs['out_file'] = self._gen_filename('out_file')
135-
return outputs

nipype/interfaces/niftyreg/reg.py

Lines changed: 4 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,16 +21,16 @@
2121
import os
2222
import warnings
2323

24-
from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec
25-
from .base import get_custom_path, NiftyRegCommand
24+
from ..base import TraitedSpec, File, traits, isdefined
25+
from .base import get_custom_path, NiftyRegCommand, NiftyRegCommandInputSpec
2626
from ...utils.filemanip import split_filename
2727

2828

2929
warn = warnings.warn
3030
warnings.filterwarnings('always', category=UserWarning)
3131

3232

33-
class RegAladinInputSpec(CommandLineInputSpec):
33+
class RegAladinInputSpec(NiftyRegCommandInputSpec):
3434
""" Input Spec for RegAladin. """
3535
# Input reference file
3636
ref_file = File(exists=True,
@@ -116,9 +116,6 @@ class RegAladinInputSpec(CommandLineInputSpec):
116116
# Verbosity off
117117
verbosity_off_flag = traits.Bool(argstr='-voff',
118118
desc='Turn off verbose output')
119-
# Set the number of omp thread to use
120-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
121-
argstr='-omp %i')
122119

123120
# Affine output transformation matrix file
124121
aff_file = File(name_source=['flo_file'],
@@ -177,7 +174,7 @@ def _list_outputs(self):
177174
return outputs
178175

179176

180-
class RegF3DInputSpec(CommandLineInputSpec):
177+
class RegF3DInputSpec(NiftyRegCommandInputSpec):
181178
""" Input Spec for RegF3D. """
182179
# Input reference file
183180
ref_file = File(exists=True,
@@ -329,9 +326,6 @@ class RegF3DInputSpec(CommandLineInputSpec):
329326
# verbosity off
330327
verbosity_off_flag = traits.Bool(argstr='-voff',
331328
desc='Turn off verbose output')
332-
# Set the number of omp thread to use
333-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
334-
argstr='-omp %i')
335329

336330
# Output CPP image file
337331
cpp_file = File(name_source=['flo_file'],

nipype/interfaces/niftyreg/regutils.py

Lines changed: 37 additions & 58 deletions
Original file line numberDiff line numberDiff line change
@@ -20,16 +20,16 @@
2020
import warnings
2121
import os
2222

23-
from ..base import TraitedSpec, File, traits, isdefined, CommandLineInputSpec
24-
from .base import get_custom_path, NiftyRegCommand
23+
from ..base import TraitedSpec, File, traits, isdefined
24+
from .base import get_custom_path, NiftyRegCommand, NiftyRegCommandInputSpec
2525
from ...utils.filemanip import split_filename
2626

2727

2828
warn = warnings.warn
2929
warnings.filterwarnings('always', category=UserWarning)
3030

3131

32-
class RegResampleInputSpec(CommandLineInputSpec):
32+
class RegResampleInputSpec(NiftyRegCommandInputSpec):
3333
""" Input Spec for RegResample. """
3434
# Input reference file
3535
ref_file = File(exists=True,
@@ -53,7 +53,8 @@ class RegResampleInputSpec(CommandLineInputSpec):
5353
desc='Type of output')
5454

5555
# Output file name
56-
out_file = File(genfile=True,
56+
out_file = File(name_source=['flo_file'],
57+
name_template='%s',
5758
argstr='%s',
5859
position=-1,
5960
desc='The output filename of the transformed image')
@@ -80,10 +81,6 @@ class RegResampleInputSpec(CommandLineInputSpec):
8081
estimating the PSF [0]'
8182
psf_alg = traits.Enum(0, 1, argstr='-psf_alg %d', desc=desc)
8283

83-
# Set the number of omp thread to use
84-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
85-
argstr='-omp %d')
86-
8784

8885
class RegResampleOutputSpec(TraitedSpec):
8986
""" Output Spec for RegResample. """
@@ -109,9 +106,9 @@ class RegResample(NiftyRegCommand):
109106
>>> node.inputs.trans_file = 'warpfield.nii'
110107
>>> node.inputs.inter_val = 'LIN'
111108
>>> node.inputs.omp_core_val = 4
112-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
109+
>>> node.cmdline # doctest: +ALLOW_UNICODE
113110
'reg_resample -flo im2.nii -inter 1 -omp 4 -ref im1.nii -trans \
114-
warpfield.nii -res .../im2_res.nii.gz'
111+
warpfield.nii -res im2_res.nii.gz'
115112
116113
"""
117114
_cmd = get_custom_path('reg_resample')
@@ -126,15 +123,13 @@ def _format_arg(self, name, spec, value):
126123
else:
127124
return super(RegResample, self)._format_arg(name, spec, value)
128125

129-
def _gen_filename(self, name):
130-
if name == 'out_file':
131-
return self._gen_fname(self.inputs.flo_file,
132-
suffix='_%s' % self.inputs.type,
133-
ext='.nii.gz')
134-
return None
126+
def _overload_extension(self, value, name=None):
127+
path, base, _ = split_filename(value)
128+
suffix = self.inputs.type
129+
return os.path.join(path, '{0}_{1}.nii.gz'.format(base, suffix))
135130

136131

137-
class RegJacobianInputSpec(CommandLineInputSpec):
132+
class RegJacobianInputSpec(NiftyRegCommandInputSpec):
138133
""" Input Spec for RegJacobian. """
139134
# Reference file name
140135
desc = 'Reference/target file (required if specifying CPP transformations.'
@@ -151,13 +146,11 @@ class RegJacobianInputSpec(CommandLineInputSpec):
151146
argstr='-%s',
152147
position=-2,
153148
desc='Type of jacobian outcome')
154-
out_file = File(genfile=True,
149+
out_file = File(name_source=['trans_file'],
150+
name_template='%s',
155151
desc='The output jacobian determinant file name',
156152
argstr='%s',
157153
position=-1)
158-
# Set the number of omp thread to use
159-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
160-
argstr='-omp %i')
161154

162155

163156
class RegJacobianOutputSpec(TraitedSpec):
@@ -180,24 +173,22 @@ class RegJacobian(NiftyRegCommand):
180173
>>> node.inputs.ref_file = 'im1.nii'
181174
>>> node.inputs.trans_file = 'warpfield.nii'
182175
>>> node.inputs.omp_core_val = 4
183-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
176+
>>> node.cmdline # doctest: +ALLOW_UNICODE
184177
'reg_jacobian -omp 4 -ref im1.nii -trans warpfield.nii -jac \
185-
.../warpfield_jac.nii.gz'
178+
warpfield_jac.nii.gz'
186179
187180
"""
188181
_cmd = get_custom_path('reg_jacobian')
189182
input_spec = RegJacobianInputSpec
190183
output_spec = RegJacobianOutputSpec
191184

192-
def _gen_filename(self, name):
193-
if name == 'out_file':
194-
return self._gen_fname(self.inputs.trans_file,
195-
suffix='_%s' % self.inputs.type,
196-
ext='.nii.gz')
197-
return None
185+
def _overload_extension(self, value, name=None):
186+
path, base, _ = split_filename(value)
187+
suffix = self.inputs.type
188+
return os.path.join(path, '{0}_{1}.nii.gz'.format(base, suffix))
198189

199190

200-
class RegToolsInputSpec(CommandLineInputSpec):
191+
class RegToolsInputSpec(NiftyRegCommandInputSpec):
201192
""" Input Spec for RegTools. """
202193
# Input image file
203194
in_file = File(exists=True,
@@ -276,10 +267,6 @@ class RegToolsInputSpec(CommandLineInputSpec):
276267
desc=desc,
277268
argstr='-smoG %f %f %f')
278269

279-
# Set the number of omp thread to use
280-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
281-
argstr='-omp %i')
282-
283270

284271
class RegToolsOutputSpec(TraitedSpec):
285272
""" Output Spec for RegTools. """
@@ -312,7 +299,7 @@ class RegTools(NiftyRegCommand):
312299
_suffix = '_tools'
313300

314301

315-
class RegAverageInputSpec(CommandLineInputSpec):
302+
class RegAverageInputSpec(NiftyRegCommandInputSpec):
316303
""" Input Spec for RegAverage. """
317304
avg_files = traits.List(File(exist=True),
318305
position=1,
@@ -424,10 +411,11 @@ def _gen_filename(self, name):
424411
if isdefined(self.inputs.avg_lts_files):
425412
return self._gen_fname(self._suffix, ext='.txt')
426413
elif isdefined(self.inputs.avg_files):
427-
_, _, ext = split_filename(self.inputs.avg_files[0])
428-
if ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']:
429-
return self._gen_fname(self._suffix, ext=ext)
414+
_, _, _ext = split_filename(self.inputs.avg_files[0])
415+
if _ext not in ['.nii', '.nii.gz', '.hdr', '.img', '.img.gz']:
416+
return self._gen_fname(self._suffix, ext=_ext)
430417
return self._gen_fname(self._suffix, ext='.nii.gz')
418+
431419
return None
432420

433421
@property
@@ -440,7 +428,7 @@ def cmdline(self):
440428
return '%s --cmd_file %s' % (self.cmd, reg_average_cmd)
441429

442430

443-
class RegTransformInputSpec(CommandLineInputSpec):
431+
class RegTransformInputSpec(NiftyRegCommandInputSpec):
444432
""" Input Spec for RegTransform. """
445433
ref1_file = File(exists=True,
446434
desc='The input reference/target image',
@@ -584,15 +572,10 @@ class RegTransformInputSpec(CommandLineInputSpec):
584572
argstr='%s',
585573
desc='transformation file to write')
586574

587-
# Set the number of omp thread to use
588-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
589-
argstr='-omp %i')
590-
591575

592576
class RegTransformOutputSpec(TraitedSpec):
593577
""" Output Spec for RegTransform. """
594-
desc = 'Output File (transformation in any format)'
595-
out_file = File(exists=True, desc=desc)
578+
out_file = File(desc='Output File (transformation in any format)')
596579

597580

598581
class RegTransform(NiftyRegCommand):
@@ -679,7 +662,7 @@ def _list_outputs(self):
679662
return outputs
680663

681664

682-
class RegMeasureInputSpec(CommandLineInputSpec):
665+
class RegMeasureInputSpec(NiftyRegCommandInputSpec):
683666
""" Input Spec for RegMeasure. """
684667
# Input reference file
685668
ref_file = File(exists=True,
@@ -695,12 +678,10 @@ class RegMeasureInputSpec(CommandLineInputSpec):
695678
mandatory=True,
696679
argstr='-%s',
697680
desc='Measure of similarity to compute')
698-
out_file = File(genfile=True,
681+
out_file = File(name_source=['flo_file'],
682+
name_template='%s',
699683
argstr='-out %s',
700684
desc='The output text file containing the measure')
701-
# Set the number of omp thread to use
702-
omp_core_val = traits.Int(desc='Number of openmp thread to use',
703-
argstr='-omp %i')
704685

705686

706687
class RegMeasureOutputSpec(TraitedSpec):
@@ -723,17 +704,15 @@ class RegMeasure(NiftyRegCommand):
723704
>>> node.inputs.flo_file = 'im2.nii'
724705
>>> node.inputs.measure_type = 'lncc'
725706
>>> node.inputs.omp_core_val = 4
726-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
727-
'reg_measure -flo im2.nii -lncc -omp 4 -out .../im2_lncc.txt -ref im1.nii'
707+
>>> node.cmdline # doctest: +ALLOW_UNICODE
708+
'reg_measure -flo im2.nii -lncc -omp 4 -out im2_lncc.txt -ref im1.nii'
728709
729710
"""
730711
_cmd = get_custom_path('reg_measure')
731712
input_spec = RegMeasureInputSpec
732713
output_spec = RegMeasureOutputSpec
733714

734-
def _gen_filename(self, name):
735-
if name == 'out_file':
736-
return self._gen_fname(self.inputs.flo_file,
737-
suffix='_%s' % self.inputs.measure_type,
738-
ext='.txt')
739-
return None
715+
def _overload_extension(self, value, name=None):
716+
path, base, _ = split_filename(value)
717+
suffix = self.inputs.measure_type
718+
return os.path.join(path, '{0}_{1}.txt'.format(base, suffix))

nipype/interfaces/niftyreg/tests/test_auto_RegAverage.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,8 @@ def test_RegAverage_inputs():
4242
ignore_exception=dict(nohash=True,
4343
usedefault=True,
4444
),
45+
omp_core_val=dict(argstr='-omp %i',
46+
),
4547
out_file=dict(argstr='%s',
4648
genfile=True,
4749
position=0,

nipype/interfaces/niftyreg/tests/test_auto_RegJacobian.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,8 @@ def test_RegJacobian_inputs():
1515
omp_core_val=dict(argstr='-omp %i',
1616
),
1717
out_file=dict(argstr='%s',
18-
genfile=True,
18+
name_source=['trans_file'],
19+
name_template='%s',
1920
position=-1,
2021
),
2122
ref_file=dict(argstr='-ref %s',

nipype/interfaces/niftyreg/tests/test_auto_RegMeasure.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,8 @@ def test_RegMeasure_inputs():
2121
omp_core_val=dict(argstr='-omp %i',
2222
),
2323
out_file=dict(argstr='-out %s',
24-
genfile=True,
24+
name_source=['flo_file'],
25+
name_template='%s',
2526
),
2627
ref_file=dict(argstr='-ref %s',
2728
mandatory=True,

nipype/interfaces/niftyreg/tests/test_auto_RegResample.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,10 +17,11 @@ def test_RegResample_inputs():
1717
),
1818
inter_val=dict(argstr='-inter %d',
1919
),
20-
omp_core_val=dict(argstr='-omp %d',
20+
omp_core_val=dict(argstr='-omp %i',
2121
),
2222
out_file=dict(argstr='%s',
23-
genfile=True,
23+
name_source=['flo_file'],
24+
name_template='%s',
2425
position=-1,
2526
),
2627
pad_val=dict(argstr='-pad %f',

0 commit comments

Comments
 (0)