Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

list as a single element of an input #160

Merged
merged 1 commit into from
Jan 3, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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