从两个不同的节点获取两个输出并将它们作为一个列表提供给另一个节点作为一个单一输入

Taking two outputs from two different nodes and feeding them as a list to another node as one single input

是巴勃罗!

我正在尝试为 DTI 制作脚本。我已经有了函数和 nipype,但现在我正在尝试使用节点,但有一个节点需要两个输入的列表 -Merge()- 来合并两个图像。但是,它只接受一个输入,并且 connect() 不允许一次输入多个输入。 在这个函数中,它既不让我单独做,又尝试 util.merge 出现错误。

我想知道我是否可以分别从两个不同节点的输出中获取两个输入,然后将它们与另一个节点连接到一个列表中,然后将其馈送到将列表作为输入的节点中。 或者也许有另一种方法来定义允许这样做的节点?[​​=10=]

有人知道吗?

非常感谢您

如果我理解得很好,你只是想合并列表中的两个项目。

这就是合并界面的用途。您只需要在初始化时提供输入的数量,如 the doc 中所述。 no_flattenravel_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

的输出