-
Notifications
You must be signed in to change notification settings - Fork 0
/
track_upper_incisor.py
181 lines (146 loc) · 4.79 KB
/
track_upper_incisor.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
import argparse
import funcy
import numpy as np
import os
import pandas as pd
import yaml
from functools import partial
from glob import glob
from multiprocessing import Pool
from skimage.metrics import structural_similarity
from tqdm import tqdm
from helpers import sequences_from_dict
from track_incisors import *
def optimize(search_arr, mask, metric, how=max):
search_H, search_W = search_arr.shape
mask_H, mask_W = mask.shape
x_shifts = list(range(0, search_W - mask_W))
y_shifts = list(range(0, search_H - mask_H))
optim_data = []
for shift_x in x_shifts:
for shift_y in y_shifts:
x_start = shift_x
x_end = shift_x + mask_W
y_start = shift_y
y_end = shift_y + mask_H
region_arr = search_arr[y_start:y_end, x_start:x_end]
val = metric(region_arr, mask)
item = (shift_x, shift_y, val)
optim_data.append(item)
best_shift_x, best_shift_y, val = how(optim_data, key=lambda tup: tup[-1])
xc = best_shift_x + mask_W // 2
yc = best_shift_y + mask_H // 2
optim = {
"xc": xc,
"yc": yc,
"value": val
}
return optim
def process_frame(
filepath,
cropped_mask,
search_space,
metric
):
x0_search = search_space["x0"]
y0_search = search_space["y0"]
x1_search = search_space["x1"]
y1_search = search_space["y1"]
other_arr = load_input_image(filepath)
search_arr = other_arr[y0_search:y1_search, x0_search:x1_search]
optim = optimize(search_arr, cropped_mask, metric)
xc = x0_search + optim["xc"]
yc = y0_search + optim["yc"]
rel_filepath = filepath.replace(os.path.dirname(datadir), "").strip("/")
item = {
"subject": subject,
"sequence": sequence,
"frame": int(os.path.basename(filepath).split(".")[0]),
"filepath": rel_filepath,
"x0": xc,
"y0": yc
}
return item
def compute_references(
datadir,
database,
subject,
sequence,
search_space,
save_to=None,
img_dir="NPY_MR",
img_ext="npy",
):
ref_mask = get_sequence_reference_mask(
database,
datadir,
subject,
"upper-incisor",
img_dir=img_dir,
img_ext=img_ext,
)
metric = structural_similarity
process_other = partial(
process_frame,
cropped_mask=ref_mask,
search_space=search_space,
metric=metric
)
dcm_filepaths = sorted(glob(os.path.join(datadir, subject, sequence, img_dir, f"*.{img_ext}")))
progress_bar = tqdm(dcm_filepaths, desc=f"{subject} {sequence}")
with Pool(10) as pool:
data = pool.map(process_other, progress_bar)
x0s = funcy.lmap(lambda d: d["x0"], data)
avg_x0s = moving_average(x0s, window_size=10)
y0s = funcy.lmap(lambda d: d["y0"], data)
avg_y0s = moving_average(y0s, window_size=10)
[d.update({
"avg_x0": avg_x0s[i],
"avg_y0": avg_y0s[i],
}) for i, d in enumerate(data)]
df = pd.DataFrame(data)
if save_to is not None:
df.to_csv(save_to, index=False)
return df
if __name__ == "__main__":
parser = argparse.ArgumentParser()
parser.add_argument("--config", dest="config_filepath")
args = parser.parse_args()
with open(args.config_filepath) as f:
cfg = yaml.safe_load(f)
overwrite = cfg.get("overwrite", False)
datadir = cfg["datadir"]
database = cfg["database"]
save_to = cfg["save_to"]
search_space = cfg["search_space"]
control_params = cfg["control_params"]
img_dir = cfg["img_dir"]
img_ext = cfg["img_ext"]
sequences = sequences_from_dict(datadir, cfg["sequences"])
for subject, sequence in sequences:
if not os.path.exists(save_to):
os.makedirs(save_to)
csv_filepath = os.path.join(save_to, f"optimization_upper-incisor_{subject}-{sequence}.csv")
if os.path.isfile(csv_filepath) and not overwrite:
df = pd.read_csv(csv_filepath)
else:
df = compute_references(
datadir,
database,
subject,
sequence,
search_space,
csv_filepath,
img_dir,
img_ext,
)
for i, row in tqdm(df.iterrows(), desc=f"{subject} {sequence} Drawing upper incisor", total=len(df)):
frame = "%04d" % row["frame"]
x0 = row["avg_x0"]
y0 = row["avg_y0"]
upper_incisor = draw_incisor((x0, y0), control_params)
save_dir = os.path.join(save_to, subject, sequence, "inference_contours")
if not os.path.exists(save_dir):
os.makedirs(save_dir)
save_filepath = os.path.join(save_dir, f"{frame}_upper-incisor.npy")
np.save(save_filepath, upper_incisor)