1

是巴勃罗!

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

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

有人知道吗?

非常感谢你提前

4

1 回答 1

0

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

这就是合并接口的用途。您只需要在初始化时提供输入的数量,如文档中所述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 的输出

于 2020-03-05T16:29:19.977 回答