From 48826ecfaa43b2a84af1d9b368db760302c35d37 Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Wed, 23 Jul 2025 16:56:18 -0400 Subject: [PATCH 1/7] Fixing freesurfer-abcd brain masking --- CPAC/anat_preproc/anat_preproc.py | 35 +++++-------------------------- CPAC/anat_preproc/utils.py | 4 ++-- 2 files changed, 7 insertions(+), 32 deletions(-) diff --git a/CPAC/anat_preproc/anat_preproc.py b/CPAC/anat_preproc/anat_preproc.py index 51f2b2810..9bf2900e2 100644 --- a/CPAC/anat_preproc/anat_preproc.py +++ b/CPAC/anat_preproc/anat_preproc.py @@ -1128,37 +1128,12 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): name=f"wmparc_to_nifti_{pipe_num}", ) - # Register wmparc file if ingressing FreeSurfer data - if strat_pool.check_rpool("pipeline-fs_xfm"): - wmparc_to_native = pe.Node( - Function( - input_names=["source_file", "target_file", "xfm", "out_file"], - output_names=["transformed_file"], - function=normalize_wmparc, - ), - name=f"wmparc_to_native_{pipe_num}", - ) - - wmparc_to_native.inputs.out_file = "wmparc_warped.mgz" - - node, out = strat_pool.get_data("pipeline-fs_wmparc") - wf.connect(node, out, wmparc_to_native, "source_file") - - node, out = strat_pool.get_data("pipeline-fs_raw-average") - wf.connect(node, out, wmparc_to_native, "target_file") - - node, out = strat_pool.get_data("pipeline-fs_xfm") - wf.connect(node, out, wmparc_to_native, "xfm") - - wf.connect(wmparc_to_native, "transformed_file", wmparc_to_nifti, "in_file") - - else: - node, out = strat_pool.get_data("pipeline-fs_wmparc") - wf.connect(node, out, wmparc_to_nifti, "in_file") + node, out = strat_pool.get_data("pipeline-fs_wmparc") + wf.connect(node, out, wmparc_to_nifti, "in_file") wmparc_to_nifti.inputs.args = "-rt nearest" - node, out = strat_pool.get_data("desc-preproc_T1w") + node, out = strat_pool.get_data("desc-restore_T1w") wf.connect(node, out, wmparc_to_nifti, "reslice_like") binary_mask = pe.Node( @@ -1194,7 +1169,7 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): wf.connect(binary_filled_mask, "out_file", brain_mask_to_t1_restore, "in_file") - node, out = strat_pool.get_data("desc-preproc_T1w") + node, out = strat_pool.get_data("desc-restore_T1w") wf.connect(node, out, brain_mask_to_t1_restore, "ref_file") outputs = {"space-T1w_desc-brain_mask": (brain_mask_to_t1_restore, "out_file")} @@ -2028,7 +2003,7 @@ def brain_mask_acpc_freesurfer(wf, cfg, strat_pool, pipe_num, opt=None): option_key=["anatomical_preproc", "brain_extraction", "using"], option_val="FreeSurfer-ABCD", inputs=[ - "desc-preproc_T1w", + "desc-restore_T1w", "pipeline-fs_wmparc", "pipeline-fs_raw-average", "pipeline-fs_xfm", diff --git a/CPAC/anat_preproc/utils.py b/CPAC/anat_preproc/utils.py index a494ebced..2cff1e914 100644 --- a/CPAC/anat_preproc/utils.py +++ b/CPAC/anat_preproc/utils.py @@ -487,7 +487,7 @@ def mri_convert(in_file, reslice_like=None, out_file=None, args=None): import os if out_file is None: - out_file = in_file.replace(".mgz", ".nii.gz") + out_file = os.path.join(os.getcwd(), os.path.basename(in_file).replace(".mgz", ".nii.gz")) cmd = "mri_convert %s %s" % (in_file, out_file) @@ -525,7 +525,7 @@ def mri_convert_reorient(in_file, orientation, out_file=None): import os if out_file is None: - out_file = in_file.split(".")[0] + "_reoriented.mgz" + out_file = os.path.join(os.getcwd(), os.path.basename(in_file).split(".")[0] + "_reoriented.mgz") cmd = "mri_convert %s %s --out_orientation %s" % (in_file, out_file, orientation) From 55b14e2f581c43d4270ad7ffe12c74d6bf94a5aa Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Wed, 23 Jul 2025 17:06:49 -0400 Subject: [PATCH 2/7] removing pipeline-fs_xfm --- CPAC/anat_preproc/anat_preproc.py | 2 -- CPAC/pipeline/engine.py | 1 - 2 files changed, 3 deletions(-) diff --git a/CPAC/anat_preproc/anat_preproc.py b/CPAC/anat_preproc/anat_preproc.py index 9bf2900e2..908eddf45 100644 --- a/CPAC/anat_preproc/anat_preproc.py +++ b/CPAC/anat_preproc/anat_preproc.py @@ -2006,7 +2006,6 @@ def brain_mask_acpc_freesurfer(wf, cfg, strat_pool, pipe_num, opt=None): "desc-restore_T1w", "pipeline-fs_wmparc", "pipeline-fs_raw-average", - "pipeline-fs_xfm", "freesurfer-subject-dir", ], outputs=["space-T1w_desc-brain_mask"], @@ -2053,7 +2052,6 @@ def brain_mask_freesurfer_fsl_tight(wf, cfg, strat_pool, pipe_num, opt=None): "desc-preproc_T1w", "pipeline-fs_wmparc", "pipeline-fs_raw-average", - "pipeline-fs_xfm", "freesurfer-subject-dir", ], outputs=["space-T1w_desc-acpcbrain_mask"], diff --git a/CPAC/pipeline/engine.py b/CPAC/pipeline/engine.py index ac6e0cfe4..93e5cd798 100644 --- a/CPAC/pipeline/engine.py +++ b/CPAC/pipeline/engine.py @@ -2062,7 +2062,6 @@ def ingress_freesurfer(wf, rpool, cfg, data_paths, unique_id, part_id, ses_id): "pipeline-fs_hemi-R_desc-surfaceMap_volume": "surf/rh.volume", "pipeline-fs_hemi-L_desc-surfaceMesh_white": "surf/lh.white", "pipeline-fs_hemi-R_desc-surfaceMesh_white": "surf/rh.white", - "pipeline-fs_xfm": "mri/transforms/talairach.lta", } for key, outfile in recon_outs.items(): From 16650a1278d6f11269076d1cb9d90de8eed78e09 Mon Sep 17 00:00:00 2001 From: "pre-commit-ci[bot]" <66853113+pre-commit-ci[bot]@users.noreply.github.com> Date: Wed, 23 Jul 2025 21:07:22 +0000 Subject: [PATCH 3/7] [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --- CPAC/anat_preproc/utils.py | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/CPAC/anat_preproc/utils.py b/CPAC/anat_preproc/utils.py index 2cff1e914..63a20a488 100644 --- a/CPAC/anat_preproc/utils.py +++ b/CPAC/anat_preproc/utils.py @@ -487,7 +487,9 @@ def mri_convert(in_file, reslice_like=None, out_file=None, args=None): import os if out_file is None: - out_file = os.path.join(os.getcwd(), os.path.basename(in_file).replace(".mgz", ".nii.gz")) + out_file = os.path.join( + os.getcwd(), os.path.basename(in_file).replace(".mgz", ".nii.gz") + ) cmd = "mri_convert %s %s" % (in_file, out_file) @@ -525,7 +527,9 @@ def mri_convert_reorient(in_file, orientation, out_file=None): import os if out_file is None: - out_file = os.path.join(os.getcwd(), os.path.basename(in_file).split(".")[0] + "_reoriented.mgz") + out_file = os.path.join( + os.getcwd(), os.path.basename(in_file).split(".")[0] + "_reoriented.mgz" + ) cmd = "mri_convert %s %s --out_orientation %s" % (in_file, out_file, orientation) From c171017e2c1af396e6e4f7aa81b8d84fd4639964 Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Thu, 24 Jul 2025 12:54:11 -0400 Subject: [PATCH 4/7] adding to change log --- CHANGELOG.md | 1 + 1 file changed, 1 insertion(+) diff --git a/CHANGELOG.md b/CHANGELOG.md index a3a965b2c..83d43e770 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -69,6 +69,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Lingering calls to `cpac_outputs.csv` (was changed to `cpac_outputs.tsv` in v1.8.1). - A bug in the `freesurfer_abcd_preproc` nodeblock where the `Template` image was incorrectly used as `reference` during the `inverse_warp` step. Replacing it with the subject-specific `T1w` image resolved the issue of the `desc-restoreBrain_T1w` being chipped off. - A bug in `ideal_bandpass` where the frequency mask was incorrectly applied, which caused filter to fail in certain cases. +- `Freesufer-ABCD` brain masking strategy to create mask as per the original DCAN script. ### Upgraded dependencies From 8adf7f52c26c2ca5f44e9804cca3bb4119bf4753 Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Thu, 24 Jul 2025 14:25:16 -0400 Subject: [PATCH 5/7] adding fallback option desc-preproc_T1w --- CPAC/anat_preproc/anat_preproc.py | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/CPAC/anat_preproc/anat_preproc.py b/CPAC/anat_preproc/anat_preproc.py index 908eddf45..6b3892709 100644 --- a/CPAC/anat_preproc/anat_preproc.py +++ b/CPAC/anat_preproc/anat_preproc.py @@ -1133,7 +1133,10 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): wmparc_to_nifti.inputs.args = "-rt nearest" - node, out = strat_pool.get_data("desc-restore_T1w") + if strat_pool.check_rpool("desc-restore_T1w"): + node, out = strat_pool.get_data("desc-restore_T1w") + else: + node, out = strat_pool.get_data("desc-preproc_T1w") wf.connect(node, out, wmparc_to_nifti, "reslice_like") binary_mask = pe.Node( @@ -1169,7 +1172,10 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): wf.connect(binary_filled_mask, "out_file", brain_mask_to_t1_restore, "in_file") - node, out = strat_pool.get_data("desc-restore_T1w") + if strat_pool.check_rpool("desc-restore_T1w"): + node, out = strat_pool.get_data("desc-restore_T1w") + else: + node, out = strat_pool.get_data("desc-preproc_T1w") wf.connect(node, out, brain_mask_to_t1_restore, "ref_file") outputs = {"space-T1w_desc-brain_mask": (brain_mask_to_t1_restore, "out_file")} @@ -2003,7 +2009,7 @@ def brain_mask_acpc_freesurfer(wf, cfg, strat_pool, pipe_num, opt=None): option_key=["anatomical_preproc", "brain_extraction", "using"], option_val="FreeSurfer-ABCD", inputs=[ - "desc-restore_T1w", + ["desc-restore_T1w", "desc-preproc_T1w"], "pipeline-fs_wmparc", "pipeline-fs_raw-average", "freesurfer-subject-dir", @@ -2049,7 +2055,7 @@ def brain_mask_freesurfer_fsl_tight(wf, cfg, strat_pool, pipe_num, opt=None): option_key=["anatomical_preproc", "brain_extraction", "using"], option_val="FreeSurfer-ABCD", inputs=[ - "desc-preproc_T1w", + ["desc-restore_T1w", "desc-preproc_T1w"], "pipeline-fs_wmparc", "pipeline-fs_raw-average", "freesurfer-subject-dir", From 568eb0865c43877ab3fa7b8c8dad154dc502dce9 Mon Sep 17 00:00:00 2001 From: birajstha <111654544+birajstha@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:22:54 -0400 Subject: [PATCH 6/7] Update CPAC/anat_preproc/anat_preproc.py Co-authored-by: Jon Cluce --- CPAC/anat_preproc/anat_preproc.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CPAC/anat_preproc/anat_preproc.py b/CPAC/anat_preproc/anat_preproc.py index 6b3892709..7faef7d65 100644 --- a/CPAC/anat_preproc/anat_preproc.py +++ b/CPAC/anat_preproc/anat_preproc.py @@ -1172,10 +1172,7 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): wf.connect(binary_filled_mask, "out_file", brain_mask_to_t1_restore, "in_file") - if strat_pool.check_rpool("desc-restore_T1w"): - node, out = strat_pool.get_data("desc-restore_T1w") - else: - node, out = strat_pool.get_data("desc-preproc_T1w") + node, out = strat_pool.get_data(["desc-restore_T1w", "desc-preproc_T1w"]) wf.connect(node, out, brain_mask_to_t1_restore, "ref_file") outputs = {"space-T1w_desc-brain_mask": (brain_mask_to_t1_restore, "out_file")} From 5cb7a57089f9632f5043690ffece9c7b53b431d8 Mon Sep 17 00:00:00 2001 From: birajstha <111654544+birajstha@users.noreply.github.com> Date: Fri, 25 Jul 2025 17:23:02 -0400 Subject: [PATCH 7/7] Update CPAC/anat_preproc/anat_preproc.py Co-authored-by: Jon Cluce --- CPAC/anat_preproc/anat_preproc.py | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/CPAC/anat_preproc/anat_preproc.py b/CPAC/anat_preproc/anat_preproc.py index 7faef7d65..c1d144504 100644 --- a/CPAC/anat_preproc/anat_preproc.py +++ b/CPAC/anat_preproc/anat_preproc.py @@ -1133,10 +1133,7 @@ def freesurfer_abcd_brain_connector(wf, cfg, strat_pool, pipe_num, opt): wmparc_to_nifti.inputs.args = "-rt nearest" - if strat_pool.check_rpool("desc-restore_T1w"): - node, out = strat_pool.get_data("desc-restore_T1w") - else: - node, out = strat_pool.get_data("desc-preproc_T1w") + node, out = strat_pool.get_data(["desc-restore_T1w", "desc-preproc_T1w"]) wf.connect(node, out, wmparc_to_nifti, "reslice_like") binary_mask = pe.Node(