2525
2626from CPAC .longitudinal .preproc import subject_specific_template
2727from CPAC .longitudinal .robust_template import mri_robust_template
28- from CPAC .longitudinal .wf .utils import check_creds_path , select_session_node
28+ from CPAC .longitudinal .wf .utils import (
29+ check_creds_path ,
30+ cross_graph_connections ,
31+ cross_pool_resources ,
32+ select_session_node ,
33+ )
2934from CPAC .pipeline import nipype_pipeline_engine as pe
3035from CPAC .pipeline .cpac_pipeline import (
3136 build_anat_preproc_stack ,
@@ -345,7 +350,7 @@ def anat_longitudinal_wf(
345350 for key in strats_dct .keys (): # get the outputs from run-nodes
346351 for index , data in enumerate (list (strats_dct [key ])):
347352 if isinstance (data , tuple ):
348- strats_dct [key ][index ] = workflow .get_output_path (* data )
353+ strats_dct [key ][index ] = workflow .get_output (* data )
349354
350355 wf = initialize_nipype_wf (
351356 config ,
@@ -460,24 +465,24 @@ def anat_longitudinal_wf(
460465 ],
461466 )
462467 wf = connect_pipeline (wf , config , rpool , pipeline_blocks )
463-
464468 if not dry_run :
465469 wf .run ()
466470
467471 # now, just write out a copy of the above to each session
468472 config .pipeline_setup ["pipeline_name" ] = orig_pipe_name
473+ longitudinal_rpool = rpool
474+ cpr = cross_pool_resources (f"longitudinal_{ subject_id } " )
469475 for session in sub_list :
470476 unique_id = session ["unique_id" ]
471477 input_creds_path = check_creds_path (session .get ("creds_path" ), subject_id )
472478
473- wf = initialize_nipype_wf (config , subject_id , unique_id )
474-
475- wf , rpool = initiate_rpool (wf , config , session )
479+ ses_wf = initialize_nipype_wf (config , subject_id , unique_id )
476480
481+ ses_wf , rpool = initiate_rpool (ses_wf , config , session )
477482 config .pipeline_setup ["pipeline_name" ] = f"longitudinal_{ orig_pipe_name } "
478483 if "derivatives_dir" in session :
479- rpool = ingress_output_dir (
480- wf ,
484+ ses_wf , rpool = ingress_output_dir (
485+ ses_wf ,
481486 config ,
482487 rpool ,
483488 long_id ,
@@ -491,32 +496,45 @@ def anat_longitudinal_wf(
491496
492497 match config ["longitudinal_template_generation" , "using" ]:
493498 case "C-PAC legacy" :
494- wf .connect (
495- brain_template_node ,
496- "output_brain_list" ,
497- select_sess ,
498- "output_brains" ,
499- )
500- wf .connect (brain_template_node , "warp_list" , select_sess , "warps" )
499+ for input_name , output_name in [
500+ ("output_brains" , "output_brain_list" ),
501+ ("warps" , "warp_list" ),
502+ ]:
503+ cross_graph_connections (
504+ wf ,
505+ ses_wf ,
506+ brain_template_node ,
507+ select_sess ,
508+ output_name ,
509+ input_name ,
510+ dry_run ,
511+ )
501512
502513 case "mri_robust_template" :
503- wf .connect (brain_template_node , "mapmov" , select_sess , "output_brains" )
504- wf .connect (
505- brain_template_node , "transform_outputs" , select_sess , "warps"
506- )
507514 head_select_sess = select_session_node (unique_id , "wholehead" )
508- wf .connect (
509- wholehead_template_node ,
510- "mapmov" ,
511- head_select_sess ,
512- "output_brains" ,
513- )
514- wf .connect (
515- wholehead_template_node ,
516- "transform_outputs" ,
517- head_select_sess ,
518- "warps" ,
519- )
515+ for input_name , output_name in [
516+ ("output_brains" , "mapmov" ),
517+ ("warps" , "transform_outputs" ),
518+ ]:
519+ cross_graph_connections (
520+ wf ,
521+ ses_wf ,
522+ brain_template_node ,
523+ select_sess ,
524+ output_name ,
525+ input_name ,
526+ dry_run ,
527+ )
528+ cross_graph_connections (
529+ wf ,
530+ ses_wf ,
531+ wholehead_template_node ,
532+ head_select_sess ,
533+ output_name ,
534+ input_name ,
535+ dry_run ,
536+ )
537+
520538 rpool .set_data (
521539 "space-longitudinal_desc-head_T1w" ,
522540 head_select_sess ,
@@ -553,29 +571,25 @@ def anat_longitudinal_wf(
553571
554572 config .pipeline_setup ["pipeline_name" ] = orig_pipe_name
555573 excl = ["space-template_desc-brain_T1w" , "space-T1w_desc-brain_mask" ]
556- rpool .gather_pipes (wf , config , add_excl = excl )
574+ rpool .gather_pipes (ses_wf , config , add_excl = excl )
575+ cross_pool_keys = ["from-longitudinal_to-template_mode-image_xfm" ]
576+ for key in cross_pool_keys :
577+ node , out = longitudinal_rpool .get_data (key )
578+ cross_graph_connections (wf , ses_wf , node , cpr , out , key , dry_run )
579+ rpool .set_data (key , cpr , key , {}, "" , cpr .name )
557580 if not dry_run :
558- wf .run ()
559-
560- # begin single-session stuff again
561- for session in sub_list :
562- unique_id = session ["unique_id" ]
563- input_creds_path = check_creds_path (session .get ("creds_path" ), subject_id )
564-
565- wf = initialize_nipype_wf (config , subject_id , unique_id )
566-
567- wf , rpool = initiate_rpool (wf , config , session )
581+ ses_wf .run ()
568582
569583 pipeline_blocks = [
570584 warp_longitudinal_T1w_to_template ,
571585 warp_longitudinal_seg_to_T1w ,
572586 ]
573587
574- wf = connect_pipeline (wf , config , rpool , pipeline_blocks )
588+ ses_wf = connect_pipeline (ses_wf , config , rpool , pipeline_blocks )
575589
576- rpool .gather_pipes (wf , config )
590+ rpool .gather_pipes (ses_wf , config )
577591
578592 # this is going to run multiple times!
579593 # once for every strategy!
580594 if not dry_run :
581- wf .run ()
595+ ses_wf .run ()
0 commit comments