Skip to content

Commit 1604b4f

Browse files
committed
♻️ 🎨 SSOT orientation_node
1 parent 6339798 commit 1604b4f

5 files changed

Lines changed: 47 additions & 73 deletions

File tree

CPAC/anat_preproc/anat_preproc.py

Lines changed: 5 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2012-2023 C-PAC Developers
2+
# Copyright (C) 2012-2025 C-PAC Developers
33

44
# This file is part of C-PAC.
55

@@ -1227,15 +1227,7 @@ def freesurfer_fsl_brain_connector(wf, cfg, strat_pool, pipe_num, opt):
12271227
wf.connect(node, out, convert_fs_T1_to_nifti, "in_file")
12281228

12291229
# 3dresample -orient RPI -inset brainmask.nii.gz -prefix brain_fs.nii.gz
1230-
reorient_fs_brainmask = pe.Node(
1231-
interface=afni.Resample(),
1232-
name=f"reorient_fs_brainmask_{node_id}",
1233-
mem_gb=0,
1234-
mem_x=(0.0115, "in_file", "t"),
1235-
)
1236-
reorient_fs_brainmask.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
1237-
reorient_fs_brainmask.inputs.outputtype = "NIFTI_GZ"
1238-
1230+
reorient_fs_brainmask = cfg.orientation_node(f"reorient_fs_brainmask_{node_id}")
12391231
wf.connect(
12401232
convert_fs_brainmask_to_nifti, "out_file", reorient_fs_brainmask, "in_file"
12411233
)
@@ -1249,15 +1241,7 @@ def freesurfer_fsl_brain_connector(wf, cfg, strat_pool, pipe_num, opt):
12491241
wf.connect(reorient_fs_brainmask, "out_file", binarize_fs_brain, "in_file")
12501242

12511243
# 3dresample -orient RPI -inset T1.nii.gz -prefix head_fs.nii.gz
1252-
reorient_fs_T1 = pe.Node(
1253-
interface=afni.Resample(),
1254-
name=f"reorient_fs_T1_{node_id}",
1255-
mem_gb=0,
1256-
mem_x=(0.0115, "in_file", "t"),
1257-
)
1258-
reorient_fs_T1.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
1259-
reorient_fs_T1.inputs.outputtype = "NIFTI_GZ"
1260-
1244+
reorient_fs_T1 = cfg.orientation_node(f"reorient_fs_T1_{node_id}")
12611245
wf.connect(convert_fs_T1_to_nifti, "out_file", reorient_fs_T1, "in_file")
12621246

12631247
# flirt -in head_fs.nii.gz -ref ${FSLDIR}/data/standard/MNI152_T1_1mm.nii.gz \
@@ -1454,15 +1438,7 @@ def anatomical_init(wf, cfg, strat_pool, pipe_num, opt=None) -> NODEBLOCK_RETURN
14541438
node, out = strat_pool.get_data("T1w")
14551439
wf.connect(node, out, anat_deoblique, "in_file")
14561440

1457-
anat_reorient = pe.Node(
1458-
interface=afni.Resample(),
1459-
name=f"anat_reorient_{pipe_num}",
1460-
mem_gb=0,
1461-
mem_x=(0.0115, "in_file", "t"),
1462-
)
1463-
anat_reorient.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
1464-
anat_reorient.inputs.outputtype = "NIFTI_GZ"
1465-
1441+
anat_reorient = cfg.orientation_node(f"anat_reorient_{pipe_num}")
14661442
wf.connect(anat_deoblique, "out_file", anat_reorient, "in_file")
14671443

14681444
outputs = {
@@ -2262,15 +2238,7 @@ def anatomical_init_T2(wf, cfg, strat_pool, pipe_num, opt=None):
22622238
node, out = strat_pool.get_data("T2w")
22632239
wf.connect(node, out, T2_deoblique, "in_file")
22642240

2265-
T2_reorient = pe.Node(
2266-
interface=afni.Resample(),
2267-
name=f"T2_reorient_{pipe_num}",
2268-
mem_gb=0,
2269-
mem_x=(0.0115, "in_file", "t"),
2270-
)
2271-
T2_reorient.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
2272-
T2_reorient.inputs.outputtype = "NIFTI_GZ"
2273-
2241+
T2_reorient = cfg.orientation_node(f"T2_reorient_{pipe_num}")
22742242
wf.connect(T2_deoblique, "out_file", T2_reorient, "in_file")
22752243

22762244
outputs = {

CPAC/anat_preproc/lesion_preproc.py

Lines changed: 5 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
# -*- coding: utf-8 -*-
2-
# Copyright (C) 2019-2023 C-PAC Developers
2+
# Copyright (C) 2019-2025 C-PAC Developers
33

44
# This file is part of C-PAC.
55

@@ -20,6 +20,7 @@
2020

2121
from CPAC.pipeline import nipype_pipeline_engine as pe
2222
from CPAC.utils.interfaces import Function
23+
from CPAC.utils.nifti_utils import orientation_node
2324

2425

2526
def inverse_lesion(lesion_path):
@@ -126,18 +127,10 @@ def create_lesion_preproc(cfg=None, wf_name="lesion_preproc"):
126127
preproc.connect(lesion_deoblique, "out_file", outputnode, "refit")
127128

128129
# Anatomical reorientation
129-
lesion_reorient = pe.Node(
130-
interface=afni.Resample(),
131-
name="lesion_reorient",
132-
mem_gb=0,
133-
mem_x=(0.0115, "in_file", "t"),
130+
node_name = "lesion_reorient"
131+
lesion_reorient = (
132+
cfg.orientation_node(node_name) if cfg else orientation_node(node_name, "RPI")
134133
)
135-
136-
lesion_reorient.inputs.orientation = (
137-
cfg.pipeline_setup["desired_orientation"] if cfg else "RPI"
138-
)
139-
lesion_reorient.inputs.outputtype = "NIFTI_GZ"
140-
141134
preproc.connect(lesion_deoblique, "out_file", lesion_reorient, "in_file")
142135
preproc.connect(lesion_reorient, "out_file", outputnode, "reorient")
143136

CPAC/func_preproc/func_preproc.py

Lines changed: 3 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2012-2023 C-PAC Developers
1+
# Copyright (C) 2012-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -521,16 +521,7 @@ def func_reorient(wf, cfg, strat_pool, pipe_num, opt=None):
521521
node, out = strat_pool.get_data("bold")
522522
wf.connect(node, out, func_deoblique, "in_file")
523523

524-
func_reorient = pe.Node(
525-
interface=afni_utils.Resample(),
526-
name=f"func_reorient_{pipe_num}",
527-
mem_gb=0,
528-
mem_x=(0.0115, "in_file", "t"),
529-
)
530-
531-
func_reorient.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
532-
func_reorient.inputs.outputtype = "NIFTI_GZ"
533-
524+
func_reorient = cfg.orientation_node(f"func_reorient_{pipe_num}")
534525
wf.connect(func_deoblique, "out_file", func_reorient, "in_file")
535526

536527
outputs = {
@@ -1283,16 +1274,7 @@ def bold_mask_anatomical_refined(wf, cfg, strat_pool, pipe_num, opt=None):
12831274
node, out = strat_pool.get_data("bold")
12841275
wf.connect(node, out, func_deoblique, "in_file")
12851276

1286-
func_reorient = pe.Node(
1287-
interface=afni_utils.Resample(),
1288-
name=f"raw_func_reorient_{pipe_num}",
1289-
mem_gb=0,
1290-
mem_x=(0.0115, "in_file", "t"),
1291-
)
1292-
1293-
func_reorient.inputs.orientation = cfg.pipeline_setup["desired_orientation"]
1294-
func_reorient.inputs.outputtype = "NIFTI_GZ"
1295-
1277+
func_reorient = cfg.orientation_node(f"raw_func_reorient_{pipe_num}")
12961278
wf.connect(func_deoblique, "out_file", func_reorient, "in_file")
12971279

12981280
wf.connect(func_reorient, "out_file", init_bold_mask, "inputspec.func")

CPAC/utils/configuration/configuration.py

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2022-2024 C-PAC Developers
1+
# Copyright (C) 2022-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -18,13 +18,14 @@
1818

1919
import os
2020
import re
21-
from typing import Any, Optional
21+
from typing import Any, cast, Literal, Optional
2222
from warnings import warn
2323

2424
from click import BadParameter
2525
import pkg_resources as p
2626
import yaml
2727

28+
from CPAC.pipeline.nipype_pipeline_engine import Node
2829
from .diff import dct_diff
2930

3031
CONFIG_KEY_TYPE = str | list[str]
@@ -639,6 +640,16 @@ def key_type_error(self, key):
639640
)
640641
)
641642

643+
def orientation_node(self, name: str) -> Node:
644+
"""Return a node configured to resample an input with AFNI 3dresample."""
645+
from CPAC.utils.nifti_utils import orientation_node
646+
647+
orientation = cast(
648+
Literal["RPI", "LPI", "RAI", "LAI", "RAS", "LAS", "RPS", "LPS"],
649+
self["pipeline_setup", "desired_orientation"],
650+
)
651+
return orientation_node(name=name, orientation=orientation)
652+
642653

643654
def check_pname(p_name: str, pipe_config: Configuration) -> str:
644655
"""Check / set `p_name`, the str representation of a pipeline for use in filetrees.

CPAC/utils/nifti_utils.py

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright (C) 2019-2024 C-PAC Developers
1+
# Copyright (C) 2019-2025 C-PAC Developers
22

33
# This file is part of C-PAC.
44

@@ -17,9 +17,13 @@
1717
"""Utlities for NIfTI images."""
1818

1919
import os
20+
from typing import Literal
2021

2122
import numpy as np
2223
import nibabel as nib
24+
from nipype.interfaces.afni import utils as afni_utils
25+
26+
from CPAC.pipeline import nipype_pipeline_engine as pe
2327

2428

2529
def nifti_image_input(
@@ -93,3 +97,19 @@ def inverse_nifti_values(image):
9397
out_data[zeros] = 0
9498

9599
return nib.nifti1.Nifti1Image(out_data, img.affine)
100+
101+
102+
def orientation_node(
103+
name: str,
104+
orientation: Literal["RPI", "LPI", "RAI", "LAI", "RAS", "LAS", "RPS", "LPS"],
105+
) -> pe.Node:
106+
"""Return a node configured to resample an input with AFNI 3dresample."""
107+
return pe.Node(
108+
interface=afni_utils.Resample(
109+
orientation=orientation,
110+
outputtype="NIFTI_GZ",
111+
),
112+
name=name,
113+
mem_gb=0,
114+
mem_x=(0.0115, "in_file", "t"),
115+
)

0 commit comments

Comments
 (0)