Skip to content

Commit 469c99c

Browse files
authored
Merge pull request #2848 from yarikoptic/bf-2847
FIX: Make positional arguments to LaplacianThickness require previous argument
2 parents 0d61bfb + 2ff466e commit 469c99c

File tree

4 files changed

+87
-14
lines changed

4 files changed

+87
-14
lines changed

nipype/interfaces/ants/segmentation.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -208,25 +208,29 @@ class LaplacianThicknessInputSpec(ANTSCommandInputSpec):
208208
keep_extension=True,
209209
hash_files=False)
210210
smooth_param = traits.Float(
211-
argstr='%f',
211+
argstr='%s',
212212
desc='Sigma of the Laplacian Recursive Image Filter (defaults to 1)',
213213
position=4)
214214
prior_thickness = traits.Float(
215-
argstr='%f',
215+
argstr='%s',
216216
desc='Prior thickness (defaults to 500)',
217+
requires=['smooth_param'],
217218
position=5)
218219
dT = traits.Float(
219-
argstr='%f',
220+
argstr='%s',
220221
desc='Time delta used during integration (defaults to 0.01)',
222+
requires=['prior_thickness'],
221223
position=6)
222224
sulcus_prior = traits.Float(
223-
argstr='%f',
225+
argstr='%s',
224226
desc='Positive floating point number for sulcus prior. '
225227
'Authors said that 0.15 might be a reasonable value',
228+
requires=['dT'],
226229
position=7)
227230
tolerance = traits.Float(
228-
argstr='%f',
231+
argstr='%s',
229232
desc='Tolerance to reach during optimization (defaults to 0.001)',
233+
requires=['sulcus_prior'],
230234
position=8)
231235

232236

nipype/interfaces/ants/tests/test_auto_LaplacianThickness.py

+9-5
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,9 @@ def test_LaplacianThickness_inputs():
77
input_map = dict(
88
args=dict(argstr='%s', ),
99
dT=dict(
10-
argstr='%f',
10+
argstr='%s',
1111
position=6,
12+
requires=['prior_thickness'],
1213
),
1314
environ=dict(
1415
nohash=True,
@@ -39,20 +40,23 @@ def test_LaplacianThickness_inputs():
3940
position=3,
4041
),
4142
prior_thickness=dict(
42-
argstr='%f',
43+
argstr='%s',
4344
position=5,
45+
requires=['smooth_param'],
4446
),
4547
smooth_param=dict(
46-
argstr='%f',
48+
argstr='%s',
4749
position=4,
4850
),
4951
sulcus_prior=dict(
50-
argstr='%f',
52+
argstr='%s',
5153
position=7,
54+
requires=['dT'],
5255
),
5356
tolerance=dict(
54-
argstr='%f',
57+
argstr='%s',
5558
position=8,
59+
requires=['sulcus_prior'],
5660
),
5761
)
5862
inputs = LaplacianThickness.input_spec()
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,59 @@
1+
# emacs: -*- mode: python; py-indent-offset: 4; indent-tabs-mode: nil -*-
2+
# vi: set ft=python sts=4 ts=4 sw=4 et:
3+
4+
from ..segmentation import LaplacianThickness
5+
from .test_resampling import change_dir
6+
7+
import os
8+
import pytest
9+
10+
11+
@pytest.fixture()
12+
def change_dir(request):
13+
orig_dir = os.getcwd()
14+
filepath = os.path.dirname(os.path.realpath(__file__))
15+
datadir = os.path.realpath(os.path.join(filepath, '../../../testing/data'))
16+
os.chdir(datadir)
17+
18+
def move2orig():
19+
os.chdir(orig_dir)
20+
21+
request.addfinalizer(move2orig)
22+
23+
24+
@pytest.fixture()
25+
def create_lt():
26+
lt = LaplacianThickness()
27+
# we do not run, so I stick some not really proper files as input
28+
lt.inputs.input_gm = 'diffusion_weighted.nii'
29+
lt.inputs.input_wm = 'functional.nii'
30+
return lt
31+
32+
33+
def test_LaplacianThickness_defaults(change_dir, create_lt):
34+
lt = create_lt
35+
base_cmd = 'LaplacianThickness functional.nii diffusion_weighted.nii functional_thickness.nii'
36+
assert lt.cmdline == base_cmd
37+
lt.inputs.smooth_param = 4.5
38+
assert lt.cmdline == base_cmd + " 4.5"
39+
lt.inputs.prior_thickness = 5.9
40+
assert lt.cmdline == base_cmd + " 4.5 5.9"
41+
42+
43+
def test_LaplacianThickness_wrongargs(change_dir, create_lt):
44+
lt = create_lt
45+
lt.inputs.tolerance = 0.001
46+
with pytest.raises(ValueError, match=r".* requires a value for input 'sulcus_prior' .*"):
47+
lt.cmdline
48+
lt.inputs.sulcus_prior = 0.15
49+
with pytest.raises(ValueError, match=r".* requires a value for input 'dT' .*"):
50+
lt.cmdline
51+
lt.inputs.dT = 0.01
52+
with pytest.raises(ValueError, match=r".* requires a value for input 'prior_thickness' .*"):
53+
lt.cmdline
54+
lt.inputs.prior_thickness = 5.9
55+
with pytest.raises(ValueError, match=r".* requires a value for input 'smooth_param' .*"):
56+
lt.cmdline
57+
lt.inputs.smooth_param = 4.5
58+
assert lt.cmdline == 'LaplacianThickness functional.nii diffusion_weighted.nii ' \
59+
'functional_thickness.nii 4.5 5.9 0.01 0.15 0.001'

nipype/interfaces/base/core.py

+10-4
Original file line numberDiff line numberDiff line change
@@ -202,10 +202,16 @@ def _check_requires(self, spec, name, value):
202202
for field in spec.requires
203203
]
204204
if any(values) and isdefined(value):
205-
msg = ("%s requires a value for input '%s' because one of %s "
206-
"is set. For a list of required inputs, see %s.help()" %
207-
(self.__class__.__name__, name,
208-
', '.join(spec.requires), self.__class__.__name__))
205+
if len(values) > 1:
206+
fmt = ("%s requires values for inputs %s because '%s' is set. "
207+
"For a list of required inputs, see %s.help()")
208+
else:
209+
fmt = ("%s requires a value for input %s because '%s' is set. "
210+
"For a list of required inputs, see %s.help()")
211+
msg = fmt % (self.__class__.__name__,
212+
', '.join("'%s'" % req for req in spec.requires),
213+
name,
214+
self.__class__.__name__)
209215
raise ValueError(msg)
210216

211217
def _check_xor(self, spec, name, value):

0 commit comments

Comments
 (0)