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