Skip to content

Commit

Permalink
reorienting all files before rpool initialization
Browse files Browse the repository at this point in the history
  • Loading branch information
birajstha committed Sep 9, 2024
1 parent c911fea commit c4e3364
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 8 deletions.
35 changes: 28 additions & 7 deletions CPAC/pipeline/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@
read_json,
write_output_json,
)

from nibabel.orientations import OrientationError

class ResourcePool:
def __init__(self, rpool=None, name=None, cfg=None, pipe_list=None):
Expand Down Expand Up @@ -2414,7 +2414,7 @@ def ingress_pipeconfig_paths(cfg, rpool, unique_id, creds_path=None):

import pandas as pd
import pkg_resources as p
from nibabel.orientations import OrientationError


template_csv = p.resource_filename("CPAC", "resources/cpac_templates.csv")
template_df = pd.read_csv(template_csv, keep_default_na=False)
Expand Down Expand Up @@ -2523,7 +2523,7 @@ def ingress_pipeconfig_paths(cfg, rpool, unique_id, creds_path=None):
if val.endswith(".nii.gz"):
templates.append([key, val])

table = check_all_orientations(templates, desired_orientation)
table = check_all_orientations(templates, desired_orientation, reorient=True)
df = pd.DataFrame(table, columns=["Resource", "Path", "Orientation"])

# check if any of the values in Orientation column are not RPI
Expand Down Expand Up @@ -2660,8 +2660,33 @@ def initiate_rpool(wf, cfg, data_paths=None, part_id=None):
creds_path = None

rpool = ResourcePool(name=unique_id, cfg=cfg)

desired_orientation = cfg.pipeline_setup["desired_orientation"]

if data_paths:
# check all data_paths and convert it to the desired_orientations
#Convert all anat to desired_orientation
if "anat" in data_paths:
anat = []
for key in data_paths["anat"]:
anat.append([key, data_paths["anat"][key]])
if anat:
try:
orientation = check_all_orientations(anat, desired_orientation, reorient=True)
except OrientationError as e:
raise e("Anatomical data is not in the desired orientation")

#Convert all func to desired_orientation
if "func" in data_paths:
func = []
for key in data_paths["func"]:
func.append([key, data_paths["func"][key]["scan"]])
if func:
try:
orientation = check_all_orientations(func, desired_orientation, reorient=True)
except :
raise OrientationError("Functional data is not in the desired orientation")

# ingress outdir
try:
if (
Expand Down Expand Up @@ -2691,11 +2716,7 @@ def initiate_rpool(wf, cfg, data_paths=None, part_id=None):
rpool = ingress_pipeconfig_paths(cfg, rpool, unique_id, creds_path)

# output files with 4 different scans
for x in rpool.get_entire_pool().keys():
print(x)
import sys

sys.exit()
return (wf, rpool)


Expand Down
44 changes: 43 additions & 1 deletion CPAC/pipeline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,48 @@ def find_orientation(input_file):
.upper()
)

def reorient_image(input_file, orientation):
"""Reorient the input image to the desired orientation. Replaces the original input_file with the reoriented image.
def check_all_orientations(input_images: list, desired_orientation: str = "RPI"):
Parameters
----------
input_file : str
Input image file path
orientation : str
Desired orientation of the input image
"""
import os
import subprocess

output_file = os.path.join(
os.path.dirname(input_file),
f"reoriented_{os.path.basename(input_file)}",
)
cmd_3drefit = ["3drefit", "-deoblique", input_file]
cmd_3dresample = [
"3dresample",
"-orient",
orientation,
"-prefix",
output_file,
"-inset",
input_file,
]
cmd_mv = ["mv", output_file, input_file]
print(f"""+++
Reorienting : {input_file}
to : {orientation}
+++""")
subprocess.run(cmd_3drefit, check=True)
subprocess.run(cmd_3dresample, check=True)
print(f"""+++Replacing {input_file} with reoriented image
""")
subprocess.run(cmd_mv, check=True)
return


def check_all_orientations(input_images: list, desired_orientation: str = "RPI", reorient=True):
"""Check the orientation of all input images.
Parameters
Expand Down Expand Up @@ -80,6 +120,8 @@ def check_all_orientations(input_images: list, desired_orientation: str = "RPI")
for key, image in input_images:
find_orient.inputs.input_file = image
orientation = find_orient.run().outputs.orientation
if reorient and orientation != desired_orientation:
reorient_image(image, desired_orientation)
orientations.append([key, image, orientation])
return orientations

Expand Down

0 comments on commit c4e3364

Please sign in to comment.