[6fe801]: / utils / elastix.py

Download this file

157 lines (128 with data), 6.0 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
import subprocess
import os
def excute_cmd(command):
'''
Execute a command and check for success.
Args:
command ('str'): Command to execute.
Returns:
result ('str'): Output of the command if successful.
'''
# excute the command
result = subprocess.run(command, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True, shell=True)
# Check the return code to see if the command was successful
if result.returncode == 0:
# print("Command executed successfully.")
# print("Output:")
return result.stdout
else:
print(f"Command failed with an error: {command}")
print(result.stderr)
return result.stderr
# Perform registration and label propagation
def register_elastix(fixed_path,
moving_path,
reg_params,
reg_params_key,
create_dir_callback,
excute_cmd_callback,
fMask = None,
mMask = None):
'''
Perform image registration using elastix.
Args:
fixed_path ('str'): Path to the fixed image.
moving_path ('str'): Path to the moving image.
reg_params ('str'): Registration parameters for elastix.
reg_params_key ('str'): Key to identify the registration parameters output folder.
create_dir_callback ('function'): Callback function to create directories.
excute_cmd_callback ('function'): Callback function to execute commands.
fMask ('str'): Optional path to a mask file.
mMask ('str'): Optional path to a mask file.
Returns:
None
'''
# Get the names of the fixed and moving images for the output directory, names without the file extensions
reg_fixed_name = fixed_path.replace("\\", "/").split("/")[-1].split(".")[0] # \\
reg_moving_name = moving_path.replace("\\", "/").split("/")[-1].split(".")[0]
# create output dir
output_dir = f'output/{reg_params_key}/images/output_{reg_fixed_name}/{reg_moving_name}'
create_dir_callback(output_dir)
# create elastix command line
if fMask and not mMask:
command_line = f'elastix -f "{fixed_path}" -m "{moving_path}" -fMask {fMask} {reg_params} -out "{output_dir}"'
elif mMask and not fMask:
command_line = f'elastix -f "{fixed_path}" -m "{moving_path}" -mMask {mMask} {reg_params} -out "{output_dir}"'
elif mMask and fMask:
command_line = f'elastix -f "{fixed_path}" -m "{moving_path}" -fMask {fMask} -mMask {mMask} {reg_params} -out "{output_dir}"'
else:
command_line = f'elastix -f "{fixed_path}" -m "{moving_path}" {reg_params} -out "{output_dir}"'
print("Excuting command: ", command_line)
# call elastix command
excute_cmd_callback(command_line)
def label_propagation_transformix(
fixed_path,
moving_path,
input_label,
transform_path,
replace_text_in_file_callback,
create_dir_callback,
excute_cmd_callback):
'''
Apply label propagation using transformix.
Args:
fixed_path ('str'): Path to the fixed image.
moving_path ('str'): Path to the moving image.
input_label ('str'): Path to the input label image.
transform_path ('str'): Path to the transformation parameters.
replace_text_in_file_callback ('function'): Callback function to replace text in a file.
create_dir_callback ('function'): Callback function to create directories.
excute_cmd_callback ('function'): Callback function to execute commands.
Returns:
None
'''
replace_text_in_file_callback(
transform_path,
search_text = '(FinalBSplineInterpolationOrder 3)',
replacement_text = '(FinalBSplineInterpolationOrder 0)')
# Get the names of the fixed and moving images for the output directory, names without the file extensions
reg_fixed_name = fixed_path.replace("\\", "/").split("/")[-1].split(".")[0]
reg_moving_name = os.path.join(moving_path.replace("\\", "/").split("/")[0], moving_path.replace("\\", "/").split("/")[-1].split(".")[0])
# create an output directory for the labels
output_dir = f'output/labels/output_{reg_fixed_name}/{reg_moving_name}' # rem _float64
# creates the output directory
create_dir_callback(output_dir)
# create transformix command line
command_line = f'transformix -in "{input_label}" -tp "{transform_path}" -out "{output_dir}"'
# run transformix on all combinations
excute_cmd_callback(command_line)
def control_points_transformix(
fixed_path,
moving_path,
reg_params_key,
input_points,
transform_path,
replace_text_in_file_callback,
create_dir_callback,
excute_cmd_callback
):
replace_text_in_file_callback(
transform_path,
search_text = '(FinalBSplineInterpolationOrder 3)',
replacement_text = '(FinalBSplineInterpolationOrder 0)')
replace_text_in_file_callback(
transform_path,
search_text = '(ResultImagePixelType "short")',
replacement_text = '(ResultImagePixelType "float")')
# Get the names of the fixed and moving images for the output directory, names without the file extensions
reg_fixed_name = fixed_path.replace("\\", "/").split("/")[-1].split(".")[0]
reg_moving_name = moving_path.replace("\\", "/").split("/")[-1].split(".")[0]
# create an output directory for the labels
output_dir = f'output/{reg_params_key}/points/output_{reg_fixed_name}/{reg_moving_name}' # rem _float64
# creates the output directory
create_dir_callback(output_dir)
# create transformix command line
command_line = f'transformix -def "{input_points}" -tp "{transform_path}" -out "{output_dir}"'
# run transformix on all combinations
excute_cmd_callback(command_line)
return output_dir