如果我理解得很好,您只想合并列表中的两个项目。
这就是合并接口的用途。您只需要在初始化时提供输入的数量,如文档中所述。no_flatten
和选项将ravel_input
帮助您输入是列表,这经常发生。
编辑:一个小例子:
from nipype import Node, Workflow
from nipype.interfaces.utility import Function, Merge
#create a workflow
example_wf = Workflow('merging_example')
#this function just injects stuff in the pipeline
def func_stuffSource(stuff):
return stuff
sourceA=Node(Function(input_names=["stuff"],
output_names=["returnedStuff"],
function=func_stuffSource),
name='sourceA_node')
sourceB=Node(Function(input_names=["stuff"],
output_names=["returnedStuff"],
function=func_stuffSource),
name='sourceB_node')
merger = Node(Merge(2), name='merger_node')
#we just give stuff to inject to the two nodes
sourceA.inputs.stuff=['a0', 'a1', 'a2']
sourceB.inputs.stuff=['b0', 'b1', 'b2', 'b3']
example_wf.connect([
( sourceA, merger, [('returnedStuff' , 'in1')]),
( sourceB, merger, [('returnedStuff' , 'in2')])
])
#simple function to print whatever the node gets
def printStuff(stuff):
from nipype import logging
logger = logging.getLogger("nipype.interface")
logger.info(str(stuff))
printer = Node(Function(input_names=['stuff'],
output_names=[],
function=printStuff),
name='print_node')
example_wf.connect([
(merger, printer, [('out', 'stuff')])
])
example_wf.run()
结果:
200306-08:32:22,463 nipype.workflow INFO:
Workflow merging_example settings: ['check', 'execution', 'logging', 'monitoring']
200306-08:32:22,475 nipype.workflow INFO:
Running serially.
200306-08:32:22,476 nipype.workflow INFO:
[Node] Setting-up "merging_example.sourceB_node" in "/tmp/tmpzfklhqwq/merging_example/sourceB_node".
200306-08:32:22,484 nipype.workflow INFO:
[Node] Running "sourceB_node" ("nipype.interfaces.utility.wrappers.Function")
200306-08:32:22,491 nipype.workflow INFO:
[Node] Finished "merging_example.sourceB_node".
200306-08:32:22,491 nipype.workflow INFO:
[Node] Setting-up "merging_example.sourceA_node" in "/tmp/tmppssiddpk/merging_example/sourceA_node".
200306-08:32:22,496 nipype.workflow INFO:
[Node] Running "sourceA_node" ("nipype.interfaces.utility.wrappers.Function")
200306-08:32:22,501 nipype.workflow INFO:
[Node] Finished "merging_example.sourceA_node".
200306-08:32:22,502 nipype.workflow INFO:
[Node] Setting-up "merging_example.merger_node" in "/tmp/tmp_h6t6h7o/merging_example/merger_node".
200306-08:32:22,505 nipype.workflow INFO:
[Node] Running "merger_node" ("nipype.interfaces.utility.base.Merge")
200306-08:32:22,511 nipype.workflow INFO:
[Node] Finished "merging_example.merger_node".
200306-08:32:22,511 nipype.workflow INFO:
[Node] Setting-up "merging_example.print_node" in "/tmp/tmpg4zggeq6/merging_example/print_node".
200306-08:32:22,517 nipype.workflow INFO:
[Node] Running "print_node" ("nipype.interfaces.utility.wrappers.Function")
200306-08:32:22,518 nipype.interface INFO:
['a0', 'a1', 'a2', 'b0', 'b1', 'b2', 'b3']
200306-08:32:22,522 nipype.workflow INFO:
[Node] Finished "merging_example.print_node".
请注意,我们的列表已正确合并。要自己检查,您可以创建到其他打印机节点,并将它们连接到 sourceA 和 sourceB 的输出