Skip to content

Commit

Permalink
Merge pull request #20 from BrainLesion/clean_niftyreg
Browse files Browse the repository at this point in the history
Clean niftyreg
  • Loading branch information
neuronflow authored Nov 13, 2023
2 parents b87146f + 52316ae commit 1987c7a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 110 deletions.
1 change: 0 additions & 1 deletion brainles_preprocessing/registration/functional.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
from .niftyreg import niftyreg_caller
from .niftyreg import NiftyRegRegistrator


Expand Down
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)
10 changes: 5 additions & 5 deletions example_modality_centric.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,10 @@ def preprocess(inputDir):
flair_file = btk_raw_dir.files("*fla.nii.gz")

if len(t1_file) == len(t1c_file) == len(t2_file) == len(flair_file) == 1:
print(t1_file)
print(t1c_file)
print(t2_file)
print(flair_file)
# print(t1_file)
# print(t1c_file)
# print(t2_file)
# print(flair_file)

t1File = t1_file[0]
t1cFile = t1c_file[0]
Expand Down Expand Up @@ -107,5 +107,5 @@ def preprocess(inputDir):
exams = EXAMPLE_DATA_DIR.dirs()

for exam in tqdm(exams):
print(exam)
print("processing:", exam)
preprocess(exam)
18 changes: 12 additions & 6 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ enable = true

[tool.poetry]
name = "brainles_preprocessing"
version = "0.0.0" # will be overwritten by poetry-dynamic-versioning but needs to be set
version = "0.0.0" # will be overwritten by poetry-dynamic-versioning but needs to be set
description = "TODO."
license = "AGPL-3.0"

Expand All @@ -27,11 +27,17 @@ readme = "README.md"
repository = "https://github.com/BrainLesion/preprocessing"
#documentation = ""

keywords = ["brain tumor", "glioma", "BraTS", "skullstripping", "brain extraction"]
keywords = [
"brain tumor",
"glioma",
"BraTS",
"skullstripping",
"brain extraction",
]
classifiers = [
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Operating System :: OS Independent",
"Programming Language :: Python :: 3",
"License :: OSI Approved :: GNU Affero General Public License v3",
"Operating System :: OS Independent",
]


Expand All @@ -49,7 +55,7 @@ BrainLes-HD-BET = ">=0.0.5"

# utils
tqdm = "^4.64.1"
auxiliary = "^0.0.35"
auxiliary = "^0.0.37"
rich = "^13.6.0"

[tool.poetry.dev-dependencies]
Expand Down

0 comments on commit 1987c7a

Please sign in to comment.