Skip to content

Commit 4b587d6

Browse files
authored
Merge pull request #2139 from djarecka/mapnode_range
fix #2115: changing range into list in MultiPath class
2 parents 3164602 + 41233f8 commit 4b587d6

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

nipype/interfaces/base.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
import simplejson as json
3333
from dateutil.parser import parse as parseutc
3434
from packaging.version import Version
35+
import collections
3536

3637
from .. import config, logging, LooseVersion, __version__
3738
from ..utils.provenance import write_provenance
@@ -2058,9 +2059,15 @@ class MultiPath(traits.List):
20582059
"""
20592060

20602061
def validate(self, object, name, value):
2062+
2063+
# want to treat range and other sequences (except str) as list
2064+
if not isinstance(value, (str, bytes)) and isinstance(value, collections.Sequence):
2065+
value = list(value)
2066+
20612067
if not isdefined(value) or \
20622068
(isinstance(value, list) and len(value) == 0):
20632069
return Undefined
2070+
20642071
newvalue = value
20652072

20662073
if not isinstance(value, list) \

nipype/pipeline/engine/tests/test_engine.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -456,6 +456,24 @@ def test_mapnode_iterfield_check():
456456
with pytest.raises(ValueError): mod1._check_iterfield()
457457

458458

459+
@pytest.mark.parametrize("x_inp, f_exp", [
460+
(3, [6]), ([2, 3], [4, 6]), ((2, 3), [4, 6]),
461+
(range(3), [0, 2, 4]),
462+
("Str", ["StrStr"]), (["Str1", "Str2"], ["Str1Str1", "Str2Str2"])
463+
])
464+
def test_mapnode_iterfield_type(x_inp, f_exp):
465+
from nipype import MapNode, Function
466+
def double_func(x):
467+
return 2 * x
468+
double = Function(["x"], ["f_x"], double_func)
469+
470+
double_node = MapNode(double, name="double", iterfield=["x"])
471+
double_node.inputs.x = x_inp
472+
473+
res = double_node.run()
474+
assert res.outputs.f_x == f_exp
475+
476+
459477
def test_mapnode_nested(tmpdir):
460478
os.chdir(str(tmpdir))
461479
from nipype import MapNode, Function

0 commit comments

Comments
 (0)