Skip to content

Commit

Permalink
clean niftyreg
Browse files Browse the repository at this point in the history
Signed-off-by: neuronflow <[email protected]>
  • Loading branch information
neuronflow committed Nov 13, 2023
1 parent a758285 commit c2af840
Showing 1 changed file with 5 additions and 98 deletions.
103 changes: 5 additions & 98 deletions brainles_preprocessing/registration/niftyreg.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
import datetime
import os
import shlex
import subprocess

from auxiliary.runscript import ScriptRunner
from auxiliary.turbopath import turbopath
Expand Down Expand Up @@ -70,8 +67,6 @@ def register(
turbopath(__file__).parent + "/niftyreg_scripts/reg_aladin",
)

print("niftyreg_executable:", niftyreg_executable)

input_params = [
turbopath(niftyreg_executable),
turbopath(fixed_image),
Expand All @@ -80,10 +75,9 @@ def register(
turbopath(matrix),
]

print("Input Params:", input_params)

# Call the run method to execute the script and capture the output in the log file
success, error = runner.run(input_params)

# if success:
# print("Script executed successfully. Check the log file for details.")
# else:
Expand Down Expand Up @@ -116,105 +110,18 @@ def transform(
turbopath(__file__).parent + "/niftyreg_scripts/reg_resample",
)

# print("niftyreg_executable:", niftyreg_executable)

input_params = [
turbopath(niftyreg_executable),
turbopath(fixed_image),
turbopath(moving_image),
turbopath(transformed_image),
turbopath(matrix),
]
print("Input Params:", input_params)

# Call the run method to execute the script and capture the output in the log file
success, error = runner.run(input_params)


def run_bash_script_in_subprocess_and_log():
pass


# TODO consider removing this legacy function
def niftyreg_caller(
fixed_image,
moving_image,
transformed_image,
matrix,
log_file,
mode,
):
"""calls niftyreg for registration and transforms"""

the_shell = "/bin/bash"
registration_abspath = os.path.dirname(os.path.abspath(__file__))

if mode == "registration":
shell_script = os.path.join(
registration_abspath, "niftyreg_scripts", "rigid_reg.sh"
)
elif mode == "transformation":
shell_script = os.path.join(
registration_abspath, "niftyreg_scripts", "transform.sh"
)
else:
raise NotImplementedError("this mode is not implemented:", mode)

# let's try to call it
try:
starttime = str(datetime.datetime.now())
print("** starting: " + moving_image.name + " at: " + starttime)
t = Timer() # TicToc("name")
t.start()
# your code ...
# first we create the output dir
# os.makedirs(output_dir, exist_ok=True)

# generate subprocess call
readableCmd = (
the_shell,
shell_script,
fixed_image,
moving_image,
transformed_image,
matrix,
)
readableCmd = " ".join(readableCmd)
print(readableCmd)
command = shlex.split(readableCmd)
print(command)

# cwd = pathlib.Path(__file__).resolve().parent
cwd = registration_abspath
print("*** cwd:", cwd)

with open(log_file, "w") as outfile:
subprocess.run(
command,
stdout=outfile,
stderr=outfile,
cwd=cwd,
)

endtime = str(datetime.datetime.now().time())

elapsed = t.stop("call")
print(elapsed)

with open(log_file, "a") as file:
file.write("\n" + "************************************************" + "\n")
file.write("cwd: " + str(cwd) + "\n")
file.write("CALL: " + readableCmd + "\n")
file.write("************************************************" + "\n")
file.write("************************************************" + "\n")
file.write("start time: " + starttime + "\n")
file.write("end time: " + endtime + "\n")
file.write("time elapsed: " + str(int(elapsed) / 60) + " minutes" + "\n")
file.write("************************************************" + "\n")

except Exception as e:
print("error: " + str(e))
print("registration error for: " + moving_image.name)

endtime = str(datetime.datetime.now())
print("** finished: " + moving_image.name + " at: " + endtime)
# if success:
# print("Script executed successfully. Check the log file for details.")
# else:
# print("Script execution failed:", error)

0 comments on commit c2af840

Please sign in to comment.