From 2f514ca0e11aa815a20f3680d27c3d04c475fd17 Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Mon, 24 Mar 2025 14:44:00 -0400 Subject: [PATCH 1/3] with precommit --- CPAC/anat_preproc/utils.py | 32 ++++++++++++++++++++++++++++++++ CPAC/pipeline/engine.py | 29 ++++++++++++++++++++++++++--- CPAC/utils/utils.py | 8 ++++++++ 3 files changed, 66 insertions(+), 3 deletions(-) diff --git a/CPAC/anat_preproc/utils.py b/CPAC/anat_preproc/utils.py index 39904bbb66..f848ce64b1 100644 --- a/CPAC/anat_preproc/utils.py +++ b/CPAC/anat_preproc/utils.py @@ -502,6 +502,38 @@ def mri_convert(in_file, reslice_like=None, out_file=None, args=None): return out_file +def mri_convert_reorient(in_file, orientation, out_file=None): + """ + Convert files from mgz to nifti format. + + Parameters + ---------- + in_file : string + A path of mgz input file + orientation : string + Orientation of the output file + out_file : string + A path of nifti output file + args : string + Arguments of mri_convert + + Returns + ------- + out_file : string + A path of nifti output file + """ + import os + + if out_file is None: + out_file = in_file.split(".")[0] + "_reoriented.mgz" + + cmd = "mri_convert %s %s --out_orientation %s" % (in_file, out_file, orientation) + + os.system(cmd) + + return out_file + + def wb_command(in_file): import os diff --git a/CPAC/pipeline/engine.py b/CPAC/pipeline/engine.py index 8749b1f787..f4b4662879 100644 --- a/CPAC/pipeline/engine.py +++ b/CPAC/pipeline/engine.py @@ -30,6 +30,7 @@ from nipype.interfaces import afni from nipype.interfaces.utility import Rename +from CPAC.anat_preproc.utils import mri_convert_reorient from CPAC.image_utils.spatial_smoothing import spatial_smoothing from CPAC.image_utils.statistical_transforms import ( fisher_z_score_standardize, @@ -66,6 +67,7 @@ from CPAC.utils.utils import ( check_prov_for_regtool, create_id_string, + flip_orientation_code, get_last_prov_entry, read_json, write_output_json, @@ -2043,9 +2045,30 @@ def ingress_freesurfer(wf, rpool, cfg, data_paths, unique_id, part_id, ses_id): creds_path=data_paths["creds_path"], dl_dir=cfg.pipeline_setup["working_directory"]["path"], ) - rpool.set_data( - key, fs_ingress, "outputspec.data", {}, "", f"fs_{key}_ingress" - ) + # if .mgz reorient to RPI + if outfile.endswith(".mgz"): + reorient_mgz = pe.Node( + Function( + input_names=["in_file", "orientation", "out_file"], + output_names=["out_file"], + function=mri_convert_reorient, + ), + name=f"reorient_mgz_{key}", + ) + # Flip orientation before reorient because mri_convert's orientation is opposite that of AFNI + reorient_mgz.inputs.orientation = flip_orientation_code( + cfg.pipeline_setup["desired_orientation"] + ) + reorient_mgz.inputs.out_file = None + wf.connect(fs_ingress, "outputspec.data", reorient_mgz, "in_file") + + rpool.set_data( + key, reorient_mgz, "out_file", {}, "", f"fs_{key}_ingress" + ) + else: + rpool.set_data( + key, fs_ingress, "outputspec.data", {}, "", f"fs_{key}_ingress" + ) else: warnings.warn( str(LookupError(f"\n[!] Path does not exist for {fullpath}.\n")) diff --git a/CPAC/utils/utils.py b/CPAC/utils/utils.py index b459262993..69b96be4ca 100644 --- a/CPAC/utils/utils.py +++ b/CPAC/utils/utils.py @@ -2631,3 +2631,11 @@ def _replace_in_value_list(current_value, replacement_tuple): for v in current_value if bool(v) and v not in {"None", "Off", ""} ] + + +def flip_orientation_code(code): + """ + Reverts an orientation code by flipping R↔L, A↔P, and I↔S. + """ + flip_dict = {"R": "L", "L": "R", "A": "P", "P": "A", "I": "S", "S": "I"} + return "".join(flip_dict[c] for c in code) From 7371e155da6c8a4c264b28dc819c73ffaf81053b Mon Sep 17 00:00:00 2001 From: "birajstha:construction_worker::penguin" Date: Mon, 24 Mar 2025 15:06:19 -0400 Subject: [PATCH 2/3] correcting the function doc-string --- CPAC/anat_preproc/utils.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/CPAC/anat_preproc/utils.py b/CPAC/anat_preproc/utils.py index f848ce64b1..a494ebceda 100644 --- a/CPAC/anat_preproc/utils.py +++ b/CPAC/anat_preproc/utils.py @@ -504,23 +504,23 @@ def mri_convert(in_file, reslice_like=None, out_file=None, args=None): def mri_convert_reorient(in_file, orientation, out_file=None): """ - Convert files from mgz to nifti format. + Reorient the mgz files using mri_orient. Parameters ---------- in_file : string - A path of mgz input file + A path of mgz input file. orientation : string - Orientation of the output file + Orientation of the output file. out_file : string - A path of nifti output file + A path of mgz output file. args : string - Arguments of mri_convert + Arguments of mri_convert. Returns ------- out_file : string - A path of nifti output file + A path of reoriented mgz output file. """ import os From 325deb5170ed239cd3ca54af78e96da73416bda0 Mon Sep 17 00:00:00 2001 From: birajstha <111654544+birajstha@users.noreply.github.com> Date: Mon, 24 Mar 2025 16:28:33 -0400 Subject: [PATCH 3/3] Update CPAC/pipeline/engine.py Co-authored-by: Jon Cluce --- CPAC/pipeline/engine.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CPAC/pipeline/engine.py b/CPAC/pipeline/engine.py index f4b4662879..11f5965e8e 100644 --- a/CPAC/pipeline/engine.py +++ b/CPAC/pipeline/engine.py @@ -2045,7 +2045,7 @@ def ingress_freesurfer(wf, rpool, cfg, data_paths, unique_id, part_id, ses_id): creds_path=data_paths["creds_path"], dl_dir=cfg.pipeline_setup["working_directory"]["path"], ) - # if .mgz reorient to RPI + # reorient *.mgz if outfile.endswith(".mgz"): reorient_mgz = pe.Node( Function(