|
| 1 | +.. _joinnode_and_itersource: |
| 2 | + |
| 3 | +==================================== |
| 4 | +JoinNode, synchronize and itersource |
| 5 | +==================================== |
| 6 | +The previous :doc:`mapnode_and_iterables` chapter described how to |
| 7 | +fork and join nodes using MapNode and iterables. In this chapter, we |
| 8 | +introduce features which build on these concepts to add workflow |
| 9 | +flexibility. |
| 10 | + |
| 11 | +JoinNode, joinsource and joinfield |
| 12 | +================================== |
| 13 | + |
| 14 | +A :class:`nipype.pipeline.engine.JoinNode` generalizes MapNode to |
| 15 | +operate in conjunction with an upstream iterable node to reassemble |
| 16 | +downstream results, e.g.: |
| 17 | + |
| 18 | +.. digraph:: joinnode_ex |
| 19 | + |
| 20 | + "A" -> "B1" -> "C1" -> "D"; |
| 21 | + "A" -> "B2" -> "C2" -> "D"; |
| 22 | + "A" -> "B3" -> "C3" -> "D"; |
| 23 | + |
| 24 | +The code to achieve this is as follows: |
| 25 | + |
| 26 | +:: |
| 27 | + |
| 28 | + import nipype.pipeline.engine as pe |
| 29 | + a = pe.Node(interface=A(), name="a") |
| 30 | + b = pe.Node(interface=B(), name="b") |
| 31 | + b.iterables = ("in_file", images) |
| 32 | + c = pe.Node(interface=C(), name="c") |
| 33 | + d = pe.JoinNode(interface=D(), joinsource="b", |
| 34 | + joinfield="in_files", name="d") |
| 35 | + |
| 36 | + my_workflow = pe.Workflow(name="my_workflow") |
| 37 | + my_workflow.connect([(a,b,[('subject','subject')]), |
| 38 | + (b,c,[('out_file','in_file')]) |
| 39 | + (c,d,[('out_file','in_files')]) |
| 40 | + ]) |
| 41 | + |
| 42 | +This example assumes that interface "A" has one output *subject*, |
| 43 | +interface "B" has two inputs *subject* and *in_file* and one output |
| 44 | +*out_file*, interface "C" has one input *in_file* and one output |
| 45 | +*out_file*, and interface D has one list input *in_files*. The |
| 46 | +*images* variable is a list of three input image file names. |
| 47 | + |
| 48 | +As with *iterables* and the MapNode *iterfield*, the *joinfield* |
| 49 | +can be a list of fields. Thus, the declaration in the previous example |
| 50 | +is equivalent to the following: |
| 51 | + |
| 52 | +:: |
| 53 | + |
| 54 | + d = pe.JoinNode(interface=D(), joinsource="b", |
| 55 | + joinfield=["in_files"], name="d") |
| 56 | + |
| 57 | +The *joinfield* defaults to all of the JoinNode input fields, so the |
| 58 | +declaration is also equivalent to the following: |
| 59 | + |
| 60 | +:: |
| 61 | + |
| 62 | + d = pe.JoinNode(interface=D(), joinsource="b", name="d") |
| 63 | + |
| 64 | +In this example, the node "c" *out_file* outputs are collected into |
| 65 | +the JoinNode "d" *in_files* input list. The *in_files* order is the |
| 66 | +same as the upstream "b" node iterables order. |
| 67 | + |
| 68 | +The JoinNode input can be filtered for unique values by specifying |
| 69 | +the *unique* flag, e.g.: |
| 70 | + |
| 71 | +:: |
| 72 | + |
| 73 | +d = pe.JoinNode(interface=D(), joinsource="b", unique=True, name="d") |
| 74 | + |
| 75 | +synchronize |
| 76 | +=========== |
| 77 | + |
| 78 | +The :class:`nipype.pipeline.engine.Node` *iterables* parameter can be |
| 79 | +be a single field or a list of fields. If it is a list, then execution |
| 80 | +is performed over all permutations of the list items. For example: |
| 81 | + |
| 82 | +:: |
| 83 | + |
| 84 | + b.iterables = [("m", [1, 2]), ("n", [3, 4])] |
| 85 | + |
| 86 | +results in the execution graph: |
| 87 | + |
| 88 | +.. digraph:: multiple_iterables_ex |
| 89 | + |
| 90 | + "A" -> "B13" -> "C"; |
| 91 | + "A" -> "B14" -> "C"; |
| 92 | + "A" -> "B23" -> "C"; |
| 93 | + "A" -> "B24" -> "C"; |
| 94 | + |
| 95 | +where "B13" has inputs *m* = 1, *n* = 3, "B14" has inputs *m* = 1, |
| 96 | +*n* = 4, etc. |
| 97 | + |
| 98 | +The *synchronize* parameter synchronizes the iterables lists, e.g.: |
| 99 | + |
| 100 | +:: |
| 101 | + |
| 102 | + b.iterables = [("m", [1, 2]), ("n", [3, 4])] |
| 103 | + b.synchronize = True |
| 104 | + |
| 105 | +results in the execution graph: |
| 106 | + |
| 107 | +.. digraph:: synchronize_ex |
| 108 | + |
| 109 | + "A" -> "B13" -> "C"; |
| 110 | + "A" -> "B24" -> "C"; |
| 111 | + |
| 112 | +where the iterable inputs are selected in lock-step by index, i.e.: |
| 113 | + |
| 114 | +(*m*, *n*) = (1, 3) and (2, 4) |
| 115 | + |
| 116 | +for "B13" and "B24", resp. |
| 117 | + |
| 118 | +itersource |
| 119 | +========== |
| 120 | + |
| 121 | +The *itersource* feature allows you to expand a downstream iterable |
| 122 | +based on a mapping of an upstream iterable. For example: |
| 123 | + |
| 124 | +:: |
| 125 | + |
| 126 | + a = pe.Node(interface=A(), name="a") |
| 127 | + b = pe.Node(interface=B(), name="b") |
| 128 | + b.iterables = ("m", [1, 2]) |
| 129 | + c = pe.Node(interface=C(), name="c") |
| 130 | + d = pe.Node(interface=D(), name="d") |
| 131 | + d.itersource = ("b", "m") |
| 132 | + d.iterables = [("n", {1:[3,4], 2:[5,6]})] |
| 133 | + my_workflow = pe.Workflow(name="my_workflow") |
| 134 | + my_workflow.connect([(a,b,[('out_file','in_file')]), |
| 135 | + (b,c,[('out_file','in_file')]) |
| 136 | + (c,d,[('out_file','in_file')]) |
| 137 | + ]) |
| 138 | + |
| 139 | +results in the execution graph: |
| 140 | + |
| 141 | +.. digraph:: itersource_ex |
| 142 | + |
| 143 | + "A" -> "B1" -> "C1" -> "D13"; |
| 144 | + "C1" -> "D14"; |
| 145 | + "A" -> "B2" -> "C2" -> "D25"; |
| 146 | + "C2" -> "D26"; |
| 147 | + |
| 148 | +In this example, all interfaces have input *in_file* and output |
| 149 | +*out_file*. In addition, interface "B" has input *m* and interface "D" |
| 150 | +has input *n*. A Python dictionary associates the "b" node input |
| 151 | +value with the downstream "d" node *n* iterable values. |
| 152 | + |
| 153 | +This example can be extended with a summary JoinNode: |
| 154 | + |
| 155 | +:: |
| 156 | + |
| 157 | + e = pe.JoinNode(interface=E(), joinsource="d", |
| 158 | + joinfield="in_files", name="e") |
| 159 | + my_workflow.connect(d, 'out_file', |
| 160 | + e, 'in_files') |
| 161 | + |
| 162 | +resulting in the graph: |
| 163 | + |
| 164 | +.. digraph:: itersource_with_join_ex |
| 165 | + |
| 166 | + "A" -> "B1" -> "C1" -> "D13" -> "E"; |
| 167 | + "C1" -> "D14" -> "E"; |
| 168 | + "A" -> "B2" -> "C2" -> "D25" -> "E"; |
| 169 | + "C2" -> "D26" -> "E"; |
| 170 | + |
| 171 | +The combination of iterables, MapNode, JoinNode, synchronize and |
| 172 | +itersource enables the creation of arbitrarily complex workflow graphs. |
| 173 | +The astute workflow builder will recognize that this flexibility is |
| 174 | +both a blessing and a curse. These advanced features are handy additions |
| 175 | +to the Nipype toolkit when used judiciously. |
0 commit comments