|
19 | 19 | import os |
20 | 20 | import shutil |
21 | 21 | import time |
22 | | -from typing import Optional |
| 22 | +from typing import cast, Optional |
23 | 23 |
|
24 | 24 | from CPAC.pipeline.nodeblock import nodeblock |
25 | 25 |
|
@@ -455,8 +455,8 @@ def anat_longitudinal_wf(subject_id: str, sub_list: list[dict], config: Configur |
455 | 455 |
|
456 | 456 | # Loop over the sessions to create the input for the longitudinal |
457 | 457 | # algorithm |
458 | | - strats_dct: dict[str, list[tuple[pe.Node, str]]] = {"desc-brain_T1w": [], |
459 | | - "desc-head_T1w": []} |
| 458 | + strats_dct: dict[str, list[tuple[pe.Node, str] | str]] = {"desc-brain_T1w": [], |
| 459 | + "desc-head_T1w": []} |
460 | 460 | for i, session in enumerate(sub_list): |
461 | 461 |
|
462 | 462 | unique_id: str = session['unique_id'] |
@@ -489,13 +489,16 @@ def anat_longitudinal_wf(subject_id: str, sub_list: list[dict], config: Configur |
489 | 489 | session_wfs[unique_id] = rpool |
490 | 490 |
|
491 | 491 | rpool.gather_pipes(workflow, config) |
492 | | - for key in strats_dct.keys(): |
493 | | - _resource: tuple[pe.Node, str] = rpool.get_data(key) |
494 | | - clone = _resource[0].clone(f"{_resource[0].name}_{session_id_list[i]}") |
495 | | - workflow.copy_input_connections(_resource[0], clone) |
496 | | - strats_dct[key].append((clone, _resource[1])) |
| 492 | + if dry_run: # build tbe graphs with connections that may be in other graphs |
| 493 | + for key in strats_dct.keys(): |
| 494 | + _resource = cast(tuple[pe.Node, str], rpool.get_data(key)) |
| 495 | + clone = _resource[0].clone(f"{_resource[0].name}_{session_id_list[i]}") |
| 496 | + workflow.copy_input_connections(_resource[0], clone) |
| 497 | + strats_dct[key].append((clone, _resource[1])) |
497 | 498 | if not dry_run: |
498 | 499 | workflow.run() |
| 500 | + for key in strats_dct.keys(): # get the outputs from run-nodes |
| 501 | + strats_dct[key].append(workflow.get_output_path(key, rpool)) |
499 | 502 |
|
500 | 503 | wf = initialize_nipype_wf(config, sub_list[0], |
501 | 504 | # just grab the first one for the name |
@@ -533,8 +536,8 @@ def anat_longitudinal_wf(subject_id: str, sub_list: list[dict], config: Configur |
533 | 536 | merge_skulls = pe.Node(Merge(num_sessions), name="merge_skulls") |
534 | 537 |
|
535 | 538 | for i in list(range(0, num_sessions)): |
536 | | - wf.connect(*strats_dct["desc-brain_T1w"][i], merge_brains, f"in{i + 1}") |
537 | | - wf.connect(*strats_dct["desc-head_T1w"][i], merge_skulls, f"in{i + 1}") |
| 539 | + _connect_node_or_path(wf, merge_brains, strats_dct, "desc-brain_T1w", i) |
| 540 | + _connect_node_or_path(wf, merge_skulls, strats_dct, "desc-head_T1w", i) |
538 | 541 | wf.connect(merge_brains, "out", template_node, "input_brain_list") |
539 | 542 | wf.connect(merge_skulls, "out", template_node, "input_skull_list") |
540 | 543 |
|
@@ -1198,3 +1201,11 @@ def func_longitudinal_template_wf(subject_id, strat_list, config): |
1198 | 1201 | workflow.run() |
1199 | 1202 |
|
1200 | 1203 | return |
| 1204 | + |
| 1205 | +def _connect_node_or_path(wf: pe.Workflow, node: pe.Node, strats_dct: dict[str, list[tuple[pe.Node, str] | str]], key: str, index: int) -> None: |
| 1206 | + """Set input appropriately for either a Node or a path string.""" |
| 1207 | + input: str = f"in{index + 1}" |
| 1208 | + if isinstance(strats_dct[key][index], str): |
| 1209 | + setattr(node.inputs, input, strats_dct[key][index]) |
| 1210 | + else: |
| 1211 | + wf.connect(*strats_dct[key][index], node, input) |
0 commit comments