-
Notifications
You must be signed in to change notification settings - Fork 1
/
preprocess_totalsegmentor_hip_angle_perturbation.py
211 lines (176 loc) · 6.25 KB
/
preprocess_totalsegmentor_hip_angle_perturbation.py
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
import os
from multiprocessing import Pool
from pathlib import Path
import numpy as np
from xrayto3d_preprocess import (
ProjectionType,
extract_bbox,
generate_xray,
get_logger,
get_orientation_code_itk,
get_stem,
read_config_and_load_components,
get_segmentation_labels,
read_image,
reorient_to,
write_image,
generate_perturbed_xray,
)
def process_subject(
subject_id,
ct_path,
seg_path,
config,
output_path_template,
output_perturbation_angle_path_template,
):
ct = read_image(ct_path)
seg = read_image(seg_path)
logger.debug(f"Image Size {ct.GetSize()} Spacing {np.around(ct.GetSpacing(),3)}")
# extract ROI and orient to particular orientation
roi_properties = config["ROI_properties"]
size = (roi_properties["size"],) * ct.GetDimension()
labels = get_segmentation_labels(seg)
# some scans may not have required anatomy labels
if 1 not in labels:
return
ct_roi = extract_bbox(
ct,
seg,
label_id=1,
physical_size=size,
padding_value=roi_properties["ct_padding"],
)
if get_orientation_code_itk(ct_roi) != roi_properties["axcode"]:
ct_roi = reorient_to(ct_roi, axcodes_to=roi_properties["axcode"])
out_ct_path = generate_path(
"ct_roi", "ct_roi", subject_id, output_path_template, config
)
write_image(ct_roi, out_ct_path)
seg_roi = extract_bbox(
seg,
seg,
label_id=1,
physical_size=size,
padding_value=roi_properties["seg_padding"],
)
if get_orientation_code_itk(seg_roi) != roi_properties["axcode"]:
seg_roi = reorient_to(seg_roi, axcodes_to=roi_properties["axcode"])
out_seg_path = generate_path(
"seg_roi", "seg_roi", subject_id, output_path_template, config
)
write_image(seg_roi, out_seg_path)
out_xray_ap_path = generate_path(
"xray_from_ct", "xray_ap", subject_id, output_path_template, config
)
generate_xray(
out_ct_path, ProjectionType.AP, seg_roi, config["xray_pose"], out_xray_ap_path
)
out_xray_lat_path = generate_path(
"xray_from_ct", "xray_lat", subject_id, output_path_template, config
)
generate_xray(
out_ct_path, ProjectionType.LAT, seg_roi, config["xray_pose"], out_xray_lat_path
)
for angle in config["xray_pose"]["perturbation_angle"]:
out_xray_lat_path = generate_perturbation_angle_path(
"xray_from_ct_angle_perturbation",
"xray_lat",
subject_id,
angle,
output_perturbation_angle_path_template,
config,
)
generate_perturbed_xray(
out_ct_path,
ProjectionType.LAT,
config["xray_pose"],
out_xray_lat_path,
angle,
)
def create_directories(out_path_template, config):
for key, out_dir in config["out_directories"].items():
Path(out_path_template.format(output_type=out_dir)).mkdir(
exist_ok=True, parents=True
)
def generate_perturbation_angle_path(
sub_dir: str,
name: str,
subject_id,
lat_view_perturbation_angle,
output_path_template,
config,
):
output_fileformat = config["filename_convention"]["output"]
out_dirs = config["out_directories"]
filename = output_fileformat[name].format(id=subject_id)
out_path = output_path_template.format(
output_type=out_dirs[sub_dir],
output_name=filename,
lat_view_perturbation_angle=lat_view_perturbation_angle,
)
logger.debug(out_path)
Path(out_path).parent.mkdir(parents=True, exist_ok=True)
return out_path
def generate_path(sub_dir: str, name: str, subject_id, output_path_template, config):
output_fileformat = config["filename_convention"]["output"]
out_dirs = config["out_directories"]
filename = output_fileformat[name].format(id=subject_id)
logger.debug(filename)
out_path = output_path_template.format(
output_type=out_dirs[sub_dir], output_name=filename
)
return out_path
def process_totalsegmentor_subject_helper(subject_id: str):
logger.debug(f"{subject_id}")
# define paths
input_fileformat = config["filename_convention"]["input"]
subject_basepath = config["subjects"]["subject_basepath"]
subject_outpath = config["subjects"]["subject_outpath"]
ct_path = Path(subject_basepath) / subject_id / input_fileformat["ct"]
seg_path = Path(subject_basepath) / subject_id / input_fileformat["seg"]
OUT_DIR_TEMPLATE = f'{subject_outpath}/{subject_id}/{config["out_directories"]["derivatives"]}/{{output_type}}'
OUT_PATH_TEMPLATE = f'{subject_outpath}/{subject_id}/{config["out_directories"]["derivatives"]}/{{output_type}}/{{output_name}}'
OUT_PERTURBATION_ANGLE_PATH_TEMPLATE = f'{subject_outpath}/{subject_id}/{config["out_directories"]["derivatives"]}/{{output_type}}/{{lat_view_perturbation_angle}}/{{output_name}}'
create_directories(OUT_DIR_TEMPLATE, config)
process_subject(
subject_id,
ct_path,
seg_path,
config,
OUT_PATH_TEMPLATE,
OUT_PERTURBATION_ANGLE_PATH_TEMPLATE,
)
if __name__ == "__main__":
import argparse
import pandas as pd
from tqdm import tqdm
parser = argparse.ArgumentParser()
parser.add_argument("config_file")
args = parser.parse_args()
config = read_config_and_load_components(args.config_file)
# create logger
dataset_name = get_stem(args.config_file)
logger = get_logger(dataset_name)
logger.debug(f"Generating dataset {dataset_name}")
logger.debug(f"Configuration {config}")
subject_list = (
pd.read_csv(config["subjects"]["subject_list"], header=None)
.to_numpy()
.flatten()
)
logger.debug(f"found {len(subject_list)} subjects")
logger.debug(subject_list)
num_workers = os.cpu_count()
# num_workers = 1
def initialize_config_for_all_workers():
global config
config = read_config_and_load_components(args.config_file)
with Pool(
processes=num_workers, initializer=initialize_config_for_all_workers
) as p:
results = tqdm(
p.map(process_totalsegmentor_subject_helper, sorted(subject_list)),
total=len(subject_list),
)
print("done")