Skip to content

Commit f12762a

Browse files
committed
Symplifying interfaces using name_source/name_template when possible
1 parent 4171b19 commit f12762a

File tree

7 files changed

+43
-66
lines changed

7 files changed

+43
-66
lines changed

nipype/interfaces/niftyreg/reg.py

Lines changed: 17 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -120,11 +120,13 @@ class RegAladinInputSpec(CommandLineInputSpec):
120120
argstr='-omp %i')
121121

122122
# Affine output transformation matrix file
123-
aff_file = File(genfile=True,
123+
aff_file = File(name_source=['flo_file'],
124+
name_template='%s_aff.txt',
124125
desc='The output affine matrix file',
125126
argstr='-aff %s')
126127
# Result warped image file
127-
res_file = File(genfile=True,
128+
res_file = File(name_source=['flo_file'],
129+
name_template='%s_res.nii.gz',
128130
desc='The affine transformed floating image',
129131
argstr='-res %s')
130132

@@ -155,35 +157,17 @@ class RegAladin(NiftyRegCommand):
155157
>>> node.inputs.flo_file = 'im2.nii'
156158
>>> node.inputs.rmask_file = 'mask.nii'
157159
>>> node.inputs.omp_core_val = 4
158-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
159-
'reg_aladin -aff .../im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \
160-
-res .../im2_res.nii.gz -rmask mask.nii'
160+
>>> node.cmdline # doctest: +ALLOW_UNICODE
161+
'reg_aladin -aff im2_aff.txt -flo im2.nii -omp 4 -ref im1.nii \
162+
-res im2_res.nii.gz -rmask mask.nii'
161163
162164
"""
163165
_cmd = get_custom_path('reg_aladin')
164166
input_spec = RegAladinInputSpec
165167
output_spec = RegAladinOutputSpec
166168

167-
def _gen_filename(self, name):
168-
if name == 'aff_file':
169-
return self._gen_fname(self.inputs.flo_file,
170-
suffix='_aff', ext='.txt')
171-
if name == 'res_file':
172-
return self._gen_fname(self.inputs.flo_file,
173-
suffix='_res', ext='.nii.gz')
174-
return None
175-
176169
def _list_outputs(self):
177-
outputs = self.output_spec().get()
178-
179-
if isdefined(self.inputs.aff_file):
180-
outputs['aff_file'] = self.inputs.aff_file
181-
else:
182-
outputs['aff_file'] = self._gen_filename('aff_file')
183-
if isdefined(self.inputs.res_file):
184-
outputs['res_file'] = self.inputs.aff_file
185-
else:
186-
outputs['res_file'] = self._gen_filename('res_file')
170+
outputs = super(RegAladin, self)._list_outputs()
187171

188172
# Make a list of the linear transformation file and the input image
189173
aff = os.path.abspath(outputs['aff_file'])
@@ -349,11 +333,13 @@ class RegF3DInputSpec(CommandLineInputSpec):
349333
argstr='-omp %i')
350334

351335
# Output CPP image file
352-
cpp_file = File(genfile=True,
336+
cpp_file = File(name_source=['flo_file'],
337+
name_template='%s_cpp.nii.gz',
353338
desc='The output CPP file',
354339
argstr='-cpp %s')
355340
# Output warped image file
356-
res_file = File(genfile=True,
341+
res_file = File(name_source=['flo_file'],
342+
name_template='%s_res.nii.gz',
357343
desc='The output resampled image',
358344
argstr='-res %s')
359345

@@ -385,9 +371,9 @@ class RegF3D(NiftyRegCommand):
385371
>>> node.inputs.flo_file = 'im2.nii'
386372
>>> node.inputs.rmask_file = 'mask.nii'
387373
>>> node.inputs.omp_core_val = 4
388-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
389-
'reg_f3d -cpp .../im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \
390-
-res .../im2_res.nii.gz -rmask mask.nii'
374+
>>> node.cmdline # doctest: +ALLOW_UNICODE
375+
'reg_f3d -cpp im2_cpp.nii.gz -flo im2.nii -omp 4 -ref im1.nii \
376+
-res im2_res.nii.gz -rmask mask.nii'
391377
392378
"""
393379
_cmd = get_custom_path('reg_f3d')
@@ -399,26 +385,8 @@ def _remove_extension(in_file):
399385
dn, bn, _ = split_filename(in_file)
400386
return os.path.join(dn, bn)
401387

402-
def _gen_filename(self, name):
403-
if name == 'res_file':
404-
return self._gen_fname(self.inputs.flo_file,
405-
suffix='_res', ext='.nii.gz')
406-
if name == 'cpp_file':
407-
return self._gen_fname(self.inputs.flo_file,
408-
suffix='_cpp', ext='.nii.gz')
409-
410388
def _list_outputs(self):
411-
outputs = self.output_spec().get()
412-
413-
if isdefined(self.inputs.res_file):
414-
outputs['res_file'] = self.inputs.res_file
415-
else:
416-
outputs['res_file'] = self._gen_filename('res_file')
417-
418-
if isdefined(self.inputs.cpp_file):
419-
outputs['cpp_file'] = self.inputs.cpp_file
420-
else:
421-
outputs['cpp_file'] = self._gen_filename('cpp_file')
389+
outputs = super(RegF3D, self)._list_outputs()
422390

423391
if self.inputs.vel_flag is True:
424392
res_name = self._remove_extension(outputs['res_file'])
@@ -436,4 +404,5 @@ def _list_outputs(self):
436404
cpp_file = os.path.abspath(outputs['cpp_file'])
437405
flo_file = os.path.abspath(self.inputs.flo_file)
438406
outputs['avg_output'] = '%s %s' % (cpp_file, flo_file)
407+
439408
return outputs

nipype/interfaces/niftyreg/regutils.py

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,8 @@ class RegToolsInputSpec(CommandLineInputSpec):
206206
mandatory=True)
207207

208208
# Output file path
209-
out_file = File(genfile=True,
209+
out_file = File(name_source=['in_file'],
210+
name_template='%s_tools.nii.gz',
210211
desc='The output file name',
211212
argstr='-out %s')
212213

@@ -301,8 +302,8 @@ class RegTools(NiftyRegCommand):
301302
>>> node.inputs.in_file = 'im1.nii'
302303
>>> node.inputs.mul_val = 4
303304
>>> node.inputs.omp_core_val = 4
304-
>>> node.cmdline # doctest: +ELLIPSIS +ALLOW_UNICODE
305-
'reg_tools -in im1.nii -mul 4.0 -omp 4 -out .../im1_tools.nii.gz'
305+
>>> node.cmdline # doctest: +ALLOW_UNICODE
306+
'reg_tools -in im1.nii -mul 4.0 -omp 4 -out im1_tools.nii.gz'
306307
307308
"""
308309
_cmd = get_custom_path('reg_tools')

nipype/interfaces/niftyreg/tests/test_auto_RegAladin.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ def test_RegAladin_inputs():
77
input_map = dict(aff_direct_flag=dict(argstr='-affDirect',
88
),
99
aff_file=dict(argstr='-aff %s',
10-
genfile=True,
10+
name_source=['flo_file'],
11+
name_template='%s_aff.txt',
1112
),
1213
args=dict(argstr='%s',
1314
),
@@ -56,7 +57,8 @@ def test_RegAladin_inputs():
5657
ref_up_val=dict(argstr='-refUpThr %f',
5758
),
5859
res_file=dict(argstr='-res %s',
59-
genfile=True,
60+
name_source=['flo_file'],
61+
name_template='%s_res.nii.gz',
6062
),
6163
rig_only_flag=dict(argstr='-rigOnly',
6264
),

nipype/interfaces/niftyreg/tests/test_auto_RegF3D.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,8 @@ def test_RegF3D_inputs():
1313
be_val=dict(argstr='-be %f',
1414
),
1515
cpp_file=dict(argstr='-cpp %s',
16-
genfile=True,
16+
name_source=['flo_file'],
17+
name_template='%s_cpp.nii.gz',
1718
),
1819
environ=dict(nohash=True,
1920
usedefault=True,
@@ -90,7 +91,8 @@ def test_RegF3D_inputs():
9091
ref_smooth_val=dict(argstr='-smooR %f',
9192
),
9293
res_file=dict(argstr='-res %s',
93-
genfile=True,
94+
name_source=['flo_file'],
95+
name_template='%s_res.nii.gz',
9496
),
9597
rlwth2_thr_val=dict(argstr='-rLwTh %d %f',
9698
),

nipype/interfaces/niftyreg/tests/test_auto_RegTools.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,8 @@ def test_RegTools_inputs():
3636
omp_core_val=dict(argstr='-omp %i',
3737
),
3838
out_file=dict(argstr='-out %s',
39-
genfile=True,
39+
name_source=['in_file'],
40+
name_template='%s_tools.nii.gz',
4041
),
4142
rms_val=dict(argstr='-rms %s',
4243
),

nipype/interfaces/niftyreg/tests/test_reg.py

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,11 +2,11 @@
22
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
33
# vi: set ft=python sts=4 ts=4 sw=4 et:
44

5+
import pytest
6+
57
from nipype.interfaces.niftyreg import (no_niftyreg, get_custom_path,
68
RegAladin, RegF3D)
79
from nipype.testing import example_data
8-
import os
9-
import pytest
1010

1111

1212
@pytest.mark.skipif(
@@ -37,11 +37,12 @@ def test_reg_aladin():
3737
-rmask {rmask}'
3838
expected_cmd = cmd_tmp.format(
3939
cmd=get_custom_path('reg_aladin'),
40-
aff=os.path.join(os.getcwd(), 'im2_aff.txt'),
40+
aff='im2_aff.txt',
4141
flo=flo_file,
4242
ref=ref_file,
43-
res=os.path.join(os.getcwd(), 'im2_res.nii.gz'),
44-
rmask=rmask_file)
43+
res='im2_res.nii.gz',
44+
rmask=rmask_file,
45+
)
4546

4647
assert nr_aladin.cmdline == expected_cmd
4748

@@ -77,10 +78,11 @@ def test_reg_f3d():
7778
-ref {ref} -res {res} -rmask {rmask} -vel'
7879
expected_cmd = cmd_tmp.format(
7980
cmd=get_custom_path('reg_f3d'),
80-
cpp=os.path.join(os.getcwd(), 'im2_cpp.nii.gz'),
81+
cpp='im2_cpp.nii.gz',
8182
flo=flo_file,
8283
ref=ref_file,
83-
res=os.path.join(os.getcwd(), 'im2_res.nii.gz'),
84-
rmask=rmask_file)
84+
res='im2_res.nii.gz',
85+
rmask=rmask_file,
86+
)
8587

8688
assert nr_f3d.cmdline == expected_cmd

nipype/interfaces/niftyreg/tests/test_regutils.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ def test_reg_tools_mul():
156156
expected_cmd = cmd_tmp.format(
157157
cmd=get_custom_path('reg_tools'),
158158
in_file=in_file,
159-
out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz'))
159+
out_file='im1_tools.nii.gz')
160160

161161
assert nr_tools.cmdline == expected_cmd
162162

@@ -169,7 +169,7 @@ def test_reg_tools_mul():
169169
expected_cmd = cmd_tmp.format(
170170
cmd=get_custom_path('reg_tools'),
171171
in_file=in_file,
172-
out_file=os.path.join(os.getcwd(), 'im1_tools.nii.gz'))
172+
out_file='im1_tools.nii.gz')
173173

174174
assert nr_tools_2.cmdline == expected_cmd
175175

0 commit comments

Comments
 (0)