Skip to content

ENH: Make interfaces.utility.Merge(1) merge a list of lists #1390

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

Merged
merged 4 commits into from
Mar 22, 2017
Merged
Show file tree
Hide file tree
Changes from 3 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
36 changes: 24 additions & 12 deletions nipype/interfaces/utility/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -128,24 +128,36 @@ class Merge(IOBase):
def __init__(self, numinputs=0, **inputs):
super(Merge, self).__init__(**inputs)
self._numinputs = numinputs
add_traits(self.inputs, ['in%d' % (i + 1) for i in range(numinputs)])
if numinputs > 0:
input_names = ['in%d' % (i + 1) for i in range(numinputs)]
elif numinputs == 0:
input_names = ['in_lists']
else:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

so the only question here is whether this should be for Merge(1) instead of Merge(0)

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I can do Merge(1). The goal was to leave the interface unmodified for any existing uses, but I guess nobody would actually do Merge(1) as it stands.

If we do, I'd suggest making numinputs default to 1, so that Merge() can be useful.

And I can update the doctests to be demonstrative.

input_names = []
add_traits(self.inputs, input_names)

def _list_outputs(self):
outputs = self._outputs().get()
out = []

if self._numinputs == 0:
values = getattr(self.inputs, 'in_lists')
if not isdefined(values):
return outputs
else:
getval = lambda idx: getattr(self.inputs, 'in%d' % (idx + 1))
values = [getval(idx) for idx in range(self._numinputs)
if isdefined(getval(idx))]

if self.inputs.axis == 'vstack':
for idx in range(self._numinputs):
value = getattr(self.inputs, 'in%d' % (idx + 1))
if isdefined(value):
if isinstance(value, list) and not self.inputs.no_flatten:
out.extend(value)
else:
out.append(value)
for value in values:
if isinstance(value, list) and not self.inputs.no_flatten:
out.extend(value)
else:
out.append(value)
else:
for i in range(len(filename_to_list(self.inputs.in1))):
out.insert(i, [])
for j in range(self._numinputs):
out[i].append(filename_to_list(getattr(self.inputs, 'in%d' % (j + 1)))[i])
lists = [filename_to_list(val) for val in values]
out = [[val[i] for val in lists] for i in range(len(lists[0]))]
if out:
outputs['out'] = out
return outputs
Expand Down
35 changes: 35 additions & 0 deletions nipype/interfaces/utility/tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import pytest

from nipype.interfaces import utility
from nipype.interfaces.base import isdefined
import nipype.pipeline.engine as pe


Expand Down Expand Up @@ -49,3 +50,37 @@ def test_split(tmpdir, args, expected):
res = node.run()
assert res.outputs.out1 == expected[0]
assert res.outputs.out2 == expected[1]


@pytest.mark.parametrize("args, kwargs, in_lists, expected", [
([3], {}, [0, [1, 2], [3, 4, 5]], [0, 1, 2, 3, 4, 5]),
([], {}, None, None),
([], {}, [0, [1, 2], [3, 4, 5]], [0, 1, 2, 3, 4, 5]),
([3], {'axis': 'hstack'}, [[0], [1, 2], [3, 4, 5]], [[0, 1, 3]]),
([3], {'axis': 'hstack'}, [[0, 1], [2, 3], [4, 5]],
[[0, 2, 4], [1, 3, 5]]),
([3], {'axis': 'hstack'}, [[0, 1], [2, 3], [4, 5]],
[[0, 2, 4], [1, 3, 5]]),
# Note: Merge(0, axis='hstack') would error on run, prior to
# in_lists implementation
([0], {'axis': 'hstack'}, [[0], [1, 2], [3, 4, 5]], [[0, 1, 3]]),
([0], {'axis': 'hstack'}, [[0, 1], [2, 3], [4, 5]],
[[0, 2, 4], [1, 3, 5]]),
])
def test_merge(tmpdir, args, kwargs, in_lists, expected):
os.chdir(str(tmpdir))

node = pe.Node(utility.Merge(*args, **kwargs), name='merge')

numinputs = args[0] if args else 0
if numinputs == 0 and in_lists:
node.inputs.in_lists = in_lists
else:
for i in range(1, numinputs + 1):
setattr(node.inputs, 'in{:d}'.format(i), in_lists[i - 1])

res = node.run()
if numinputs == 0 and in_lists is None:
assert not isdefined(res.outputs.out)
else:
assert res.outputs.out == expected