Skip to content

Commit

Permalink
handle for read-only template files
Browse files Browse the repository at this point in the history
  • Loading branch information
birajstha committed Sep 18, 2024
1 parent 400e36a commit 4d2f933
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 69 deletions.
46 changes: 25 additions & 21 deletions CPAC/pipeline/engine.py
Original file line number Diff line number Diff line change
Expand Up @@ -2503,27 +2503,31 @@ def ingress_pipeconfig_paths(wf, cfg, rpool, unique_id, creds_path=None):
output = "outputspec.data"
node_name = f"{key}_config_ingress"

# check if the output is in desired orientation, if not reorient it
check_orient = pe.Node(
Function(
input_names=["input_file", "desired_orientation", "reorient"],
output_names=["output_file"],
function=check_orientation,
),
name=f"check_orient_{key}",
)
wf.connect(node, output, check_orient, "input_file")
check_orient.inputs.desired_orientation = desired_orientation
check_orient.inputs.reorient = True

rpool.set_data(
key,
check_orient,
"output_file",
json_info,
"",
f"check_orient-{node_name}-{key}",
)
if val.endswith(".nii.gz"):
# check if the output is in desired orientation, if not reorient it
check_orient = pe.Node(
Function(
input_names=["input_file", "desired_orientation", "reorient"],
output_names=["output_file"],
function=check_orientation,
imports=["from CPAC.pipeline.utils import reorient_image"],
),
name=f"check_orient_{key}",
)
wf.connect(node, output, check_orient, "input_file")
check_orient.inputs.desired_orientation = desired_orientation
check_orient.inputs.reorient = True

rpool.set_data(
key,
check_orient,
"output_file",
json_info,
"",
f"check_orient-{node_name}-{key}",
)
else:
rpool.set_data(key, node, output, json_info, "", node_name)
# templates, resampling from config
"""
template_keys = [
Expand Down
119 changes: 71 additions & 48 deletions CPAC/pipeline/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,80 +25,103 @@
MOVEMENT_FILTER_KEYS = motion_estimate_filter.outputs


def check_orientation(input_file, desired_orientation, reorient=False):
"""Find the orientation of the input file and reorient it if necessary.
def reorient_image(input_file, orientation):
"""Reorient the input image to the desired orientation. Replaces the original input_file with the reoriented image.
Parameters
----------
input_file : str
Input file path
desired_orientation : str
Desired orientation of the input file
reorient : bool
Reorient the input file to the desired orientation
Input image file path
orientation : str
Desired orientation of the input image
Returns
-------
orientation : str
Orientation of the input file
output_file : str
Reoriented image file path
"""
import os
import shutil
import subprocess

cmd_3dinfo = ["3dinfo", "-orient", input_file]

orientation = (
subprocess.run(cmd_3dinfo, capture_output=True, text=True, check=False)
.stdout.strip()
.upper()
output_file = os.path.join(
os.getcwd(),
f"reoriented_{os.path.basename(input_file)}",
)
if orientation != desired_orientation and reorient:
output_file = reorient_image(input_file, desired_orientation)
else:
output_file = input_file
# if output file exist delete it
if os.path.exists(output_file):
os.remove(output_file)

# make a copy of the input file as temp file so that the original file is not modified
temp_file = os.path.join(
os.getcwd(),
f"temp_{os.path.basename(input_file)}",
)
shutil.copy(input_file, temp_file)

cmd_3drefit = ["3drefit", "-deoblique", temp_file]
cmd_3dresample = [
"3dresample",
"-orient",
orientation,
"-prefix",
output_file,
"-inset",
temp_file,
]
subprocess.run(cmd_3drefit, check=True)
subprocess.run(cmd_3dresample, check=True)

# remove the temporary file
os.remove(temp_file)

return output_file


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_orientation(input_file, desired_orientation, reorient=True):
"""Find the orientation of the input file and reorient it if necessary. Does not modify the original input file.
Parameters
----------
input_file : str
Input image file path
orientation : str
Desired orientation of the input image
Input file path
desired_orientation : str
Desired orientation of the input file
reorient : bool
Reorient the input file to the desired orientation
Returns
-------
output_file : str
Reoriented image file path
"""
try:
import os
import subprocess
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_3dinfo = ["3dinfo", "-orient", input_file]

IFLOGGER.info(f"""+++\nReorienting : {input_file}\nto : {orientation}\n+++""")
subprocess.run(cmd_3drefit, check=True)
subprocess.run(cmd_3dresample, check=True)
return output_file
except Exception as e:
IFLOGGER.error(f"Reorienting failed for {input_file} with error: {e}")
return input_file
orientation = (
subprocess.run(cmd_3dinfo, capture_output=True, text=True, check=False)
.stdout.strip()
.upper()
)
if orientation != desired_orientation and reorient:
IFLOGGER.info(
f"+++ Reorienting {input_file} from {orientation} to {desired_orientation} +++"
)
try:
output_file = reorient_image(input_file, desired_orientation)
except Exception as e:
IFLOGGER.error(
f"Error in reorienting the image: {input_file}.\nCould not reorient the image to {desired_orientation}"
)
IFLOGGER.error(f"Error: {e}")
output_file = input_file # return the original file ?
else:
IFLOGGER.info(
f"+++ Orientation of {input_file} is already {desired_orientation} +++"
)
output_file = input_file
return output_file


def name_fork(resource_idx, cfg, json_info, out_dct):
Expand Down

0 comments on commit 4d2f933

Please sign in to comment.