Skip to content

Commit

Permalink
Merge pull request #160 from djarecka/list_inp
Browse files Browse the repository at this point in the history
list as a single element of an input
  • Loading branch information
djarecka authored Jan 3, 2020
2 parents f9e72bf + a872608 commit 3f3f59e
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 2 deletions.
4 changes: 3 additions & 1 deletion pydra/engine/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
from .specs import Runtime, File


def ensure_list(obj):
def ensure_list(obj, tuple2list=False):
if obj is None:
return []
if isinstance(obj, list):
return obj
elif tuple2list and isinstance(obj, tuple):
return list(obj)
return [obj]


Expand Down
2 changes: 1 addition & 1 deletion pydra/engine/task.py
Original file line number Diff line number Diff line change
Expand Up @@ -240,7 +240,7 @@ def command_args(self):
if value is not True:
break
else:
cmd_add += ensure_list(value)
cmd_add += ensure_list(value, tuple2list=True)
if cmd_add is not None:
pos_args.append((pos, cmd_add))
# sorting all elements of the command
Expand Down
85 changes: 85 additions & 0 deletions pydra/engine/tests/test_node_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -642,6 +642,91 @@ def test_task_state_2(plugin):
assert nn.output_dir == []


@pytest.mark.parametrize("plugin", Plugins)
def test_task_state_3(plugin):
""" task with a tuple as an input, and a simple splitter """
nn = moment(name="NA", n=3, lst=[(2, 3, 4), (1, 2, 3)]).split(splitter="lst")
assert np.allclose(nn.inputs.n, 3)
assert np.allclose(nn.inputs.lst, [[2, 3, 4], [1, 2, 3]])
assert nn.state.splitter == "NA.lst"

with Submitter(plugin=plugin) as sub:
sub(nn)

# checking the results
results = nn.result()
for i, expected in enumerate([33, 12]):
assert results[i].output.out == expected
# checking the output_dir
assert nn.output_dir
for odir in nn.output_dir:
assert odir.exists()


@pytest.mark.parametrize("plugin", Plugins)
def test_task_state_4(plugin):
""" task with a tuple as an input, and the variable is part of the scalar splitter"""
nn = moment(name="NA", n=[1, 3], lst=[(2, 3, 4), (1, 2, 3)]).split(
splitter=("n", "lst")
)
assert np.allclose(nn.inputs.n, [1, 3])
assert np.allclose(nn.inputs.lst, [[2, 3, 4], [1, 2, 3]])
assert nn.state.splitter == ("NA.n", "NA.lst")

with Submitter(plugin=plugin) as sub:
sub(nn)

# checking the results
results = nn.result()
for i, expected in enumerate([3, 12]):
assert results[i].output.out == expected
# checking the output_dir
assert nn.output_dir
for odir in nn.output_dir:
assert odir.exists()


@pytest.mark.parametrize("plugin", Plugins)
def test_task_state_4_exception(plugin):
""" task with a tuple as an input, and the variable is part of the scalar splitter
the shapes are not matching, so exception should be raised
"""
nn = moment(name="NA", n=[1, 3, 3], lst=[(2, 3, 4), (1, 2, 3)]).split(
splitter=("n", "lst")
)
assert np.allclose(nn.inputs.n, [1, 3, 3])
assert np.allclose(nn.inputs.lst, [[2, 3, 4], [1, 2, 3]])
assert nn.state.splitter == ("NA.n", "NA.lst")

with pytest.raises(Exception) as excinfo:
with Submitter(plugin=plugin) as sub:
sub(nn)
assert "shape" in str(excinfo.value)


@pytest.mark.parametrize("plugin", Plugins)
def test_task_state_5(plugin):
""" ask with a tuple as an input, and the variable is part of the outer splitter """
nn = moment(name="NA", n=[1, 3], lst=[(2, 3, 4), (1, 2, 3)]).split(
splitter=["n", "lst"]
)
assert np.allclose(nn.inputs.n, [1, 3])
assert np.allclose(nn.inputs.lst, [[2, 3, 4], [1, 2, 3]])
assert nn.state.splitter == ["NA.n", "NA.lst"]

with Submitter(plugin=plugin) as sub:
sub(nn)

# checking the results
results = nn.result()
for i, expected in enumerate([3, 2, 33, 12]):
assert results[i].output.out == expected
# checking the output_dir
assert nn.output_dir
for odir in nn.output_dir:
assert odir.exists()


@pytest.mark.parametrize("plugin", Plugins)
def test_task_state_comb_1(plugin):
""" task with the simplest splitter and combiner"""
Expand Down

0 comments on commit 3f3f59e

Please sign in to comment.