-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathpreprocess_totalsegmentor_hip.py
157 lines (124 loc) · 4.73 KB
/
preprocess_totalsegmentor_hip.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
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,
)
def process_subject(subject_id, ct_path, seg_path, config, output_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
)
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_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"]
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_basepath}/{subject_id}/{config["out_directories"]["derivatives"]}/{{output_type}}'
OUT_PATH_TEMPLATE = f'{subject_basepath}/{subject_id}/{config["out_directories"]["derivatives"]}/{{output_type}}/{{output_name}}'
create_directories(OUT_DIR_TEMPLATE, config)
process_subject(subject_id, ct_path, seg_path, config, OUT_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")