-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathprepare_datasets.py
320 lines (266 loc) · 15.1 KB
/
prepare_datasets.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
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
import os
import numpy as np
import matplotlib.pyplot as plt
import pandas as pd
import scipy.ndimage as nd
import SimpleITK as sitk
import skimage.color as color
import torch
import torch.nn.functional as F
import deephistreg as dhr
import utils
import paths
def parse_dataset(csv_path, dataset_path, output_path, masks_path=None):
output_max_size = 4096
show = False
csv_file = pd.read_csv(csv_path)
for current_case in csv_file.iterrows():
current_id = current_case[1]['Unnamed: 0']
size = current_case[1]['Image size [pixels]']
diagonal = int(current_case[1]['Image diagonal [pixels]'])
y_size, x_size = int(size.split(",")[0][1:]), int(size.split(",")[1][:-1])
source_path = current_case[1]['Source image']
target_path = current_case[1]['Target image']
source_landmarks_path = current_case[1]['Source landmarks']
target_landmarks_path = current_case[1]['Target landmarks']
status = current_case[1]['status']
extension = source_path[-4:]
if masks_path is not None:
source_mask_path = os.path.join(masks_path, source_path.replace(extension, ".mha"))
target_mask_path = os.path.join(masks_path, target_path.replace(extension, ".mha"))
source_path = os.path.join(dataset_path, source_path.replace(extension, ".mha"))
target_path = os.path.join(dataset_path, target_path.replace(extension, ".mha"))
source_landmarks_path = os.path.join(dataset_path, source_landmarks_path)
target_landmarks_path = os.path.join(dataset_path, target_landmarks_path)
source_landmarks = utils.load_landmarks(source_landmarks_path)
if status == "training":
target_landmarks = utils.load_landmarks(target_landmarks_path)
source = color.rgb2gray(sitk.GetArrayFromImage(sitk.ReadImage(source_path)))
target = color.rgb2gray(sitk.GetArrayFromImage(sitk.ReadImage(target_path)))
if masks_path is not None:
source_mask = sitk.GetArrayFromImage(sitk.ReadImage(source_mask_path)).astype(np.ubyte)
target_mask = sitk.GetArrayFromImage(sitk.ReadImage(target_mask_path)).astype(np.ubyte)
source_mask = (source_mask / np.max(source_mask))
target_mask = (target_mask / np.max(target_mask))
source = 1 - utils.normalize(source)
target = 1 - utils.normalize(target)
padded_source, padded_target = utils.pad_images_np(source, target)
padded_source_landmarks = utils.pad_landmarks(source_landmarks, source.shape, padded_source.shape)
if status == "training":
padded_target_landmarks = utils.pad_landmarks(target_landmarks, target.shape, padded_target.shape)
if masks_path is not None:
padded_source_mask, padded_target_mask = utils.pad_images_np(source_mask, target_mask)
resample_factor = np.max(padded_source.shape) / output_max_size
gaussian_sigma = resample_factor / 1.25
smoothed_source = nd.gaussian_filter(padded_source, gaussian_sigma)
smoothed_target = nd.gaussian_filter(padded_target, gaussian_sigma)
resampled_source = utils.resample_image(smoothed_source, resample_factor)
resampled_target = utils.resample_image(smoothed_target, resample_factor)
resampled_source_landmarks = utils.resample_landmarks(padded_source_landmarks, resample_factor)
if status == "training":
resampled_target_landmarks = utils.resample_landmarks(padded_target_landmarks, resample_factor)
if masks_path is not None:
resampled_source_mask = (utils.resample_image(padded_source_mask, resample_factor) > 0.5).astype(np.ubyte)
resampled_target_mask = (utils.resample_image(padded_target_mask, resample_factor) > 0.5).astype(np.ubyte)
print("Current ID: ", current_id)
print("Y Size, X_size: ", y_size, x_size)
print("Source path: ", source_path)
print("Target path: ", target_path)
print("Source shape: ", source.shape)
print("Target shape: ", target.shape)
print("Padded source shape: ", padded_source.shape)
print("Padded target shape: ", padded_target.shape)
print("Resampled source shape: ", resampled_source.shape)
print("Resampled target shape: ", resampled_target.shape)
print("Source landmarks path: ", source_landmarks_path)
print("Target landmarks path: ", target_landmarks_path)
print("Source landmarks shape: ", source_landmarks.shape)
print("Status: ", status)
print("Resample factor: ", resample_factor)
if status == "training":
print("Target landmarks shape: ", target_landmarks.shape)
try:
print("Initial Median TRE: ", np.median(utils.calculate_tre(source_landmarks, target_landmarks)))
print("Resampled Median TRE: ", np.median(utils.calculate_tre(resampled_source_landmarks, resampled_target_landmarks)))
except:
print("Unequal number of landmarks.")
print()
to_save_source_mha = sitk.GetImageFromArray((utils.normalize(resampled_source).astype(np.float32)))
to_save_target_mha = sitk.GetImageFromArray((utils.normalize(resampled_target).astype(np.float32)))
to_save_source_jpg = sitk.GetImageFromArray(((utils.normalize(resampled_source).astype(np.float32))*255).astype(np.ubyte))
to_save_target_jpg = sitk.GetImageFromArray(((utils.normalize(resampled_target).astype(np.float32))*255).astype(np.ubyte))
to_save_source_landmarks = resampled_source_landmarks.astype(np.float32)
if status == "training":
to_save_target_landmarks = resampled_target_landmarks.astype(np.float32)
if masks_path is not None:
to_save_source_mask_mha = sitk.GetImageFromArray(resampled_source_mask.astype(np.ubyte))
to_save_target_mask_mha = sitk.GetImageFromArray(resampled_target_mask.astype(np.ubyte))
to_save_source_mask_jpg = sitk.GetImageFromArray((resampled_source_mask*255).astype(np.ubyte))
to_save_target_mask_jpg = sitk.GetImageFromArray((resampled_target_mask*255).astype(np.ubyte))
to_save_source_mha_path = os.path.join(output_path, str(current_id), "source.mha")
to_save_target_mha_path = os.path.join(output_path, str(current_id), "target.mha")
to_save_source_jpg_path = os.path.join(output_path, str(current_id), "source.jpg")
to_save_target_jpg_path = os.path.join(output_path, str(current_id), "target.jpg")
to_save_source_landmarks_path = os.path.join(output_path, str(current_id), "source_landmarks.csv")
if status == "training":
to_save_target_landmarks_path = os.path.join(output_path, str(current_id), "target_landmarks.csv")
if masks_path is not None:
to_save_source_mask_mha_path = os.path.join(output_path, str(current_id), "source_mask.mha")
to_save_target_mask_mha_path = os.path.join(output_path, str(current_id), "target_mask.mha")
to_save_source_mask_jpg_path = os.path.join(output_path, str(current_id), "source_mask.jpg")
to_save_target_mask_jpg_path = os.path.join(output_path, str(current_id), "target_mask.jpg")
if not os.path.isdir(os.path.dirname(to_save_source_mha_path)):
os.makedirs(os.path.dirname(to_save_source_mha_path))
sitk.WriteImage(to_save_source_mha, to_save_source_mha_path)
sitk.WriteImage(to_save_target_mha, to_save_target_mha_path)
sitk.WriteImage(to_save_source_jpg, to_save_source_jpg_path)
sitk.WriteImage(to_save_target_jpg, to_save_target_jpg_path)
utils.save_landmarks(to_save_source_landmarks, to_save_source_landmarks_path)
if status == "training":
utils.save_landmarks(to_save_target_landmarks, to_save_target_landmarks_path)
if masks_path is not None:
sitk.WriteImage(to_save_source_mask_mha, to_save_source_mask_mha_path)
sitk.WriteImage(to_save_target_mask_mha, to_save_target_mask_mha_path)
sitk.WriteImage(to_save_source_mask_jpg, to_save_source_mask_jpg_path)
sitk.WriteImage(to_save_target_mask_jpg, to_save_target_mask_jpg_path)
if show:
plt.figure()
no_rows = 1 if masks_path is None else 2
plt.subplot(no_rows, 2, 1)
plt.imshow(source, cmap='gray')
plt.plot(source_landmarks[:, 0], source_landmarks[:, 1], "r*")
plt.title("Source")
plt.subplot(no_rows, 2, 2)
plt.imshow(target, cmap='gray')
if status == "training":
plt.plot(target_landmarks[:, 0], target_landmarks[:, 1], "r*")
plt.title("Target")
if masks_path is not None:
plt.subplot(no_rows, 2, 3)
plt.imshow(source_mask, cmap='gray')
plt.title("Source Mask")
plt.subplot(no_rows, 2, 4)
plt.imshow(target_mask, cmap='gray')
plt.title("Target Mask")
plt.figure()
plt.subplot(no_rows, 2, 1)
plt.imshow(padded_source, cmap='gray')
plt.plot(padded_source_landmarks[:, 0], padded_source_landmarks[:, 1], "r*")
plt.title("Padded Source")
plt.subplot(no_rows, 2, 2)
plt.imshow(padded_target, cmap='gray')
if status == "training":
plt.plot(padded_target_landmarks[:, 0], padded_target_landmarks[:, 1], "r*")
plt.title("Padded Target")
if masks_path is not None:
plt.subplot(no_rows, 2, 3)
plt.imshow(padded_source_mask, cmap='gray')
plt.title("Padded Source Mask")
plt.subplot(no_rows, 2, 4)
plt.imshow(padded_target_mask, cmap='gray')
plt.title("Padded Target Mask")
plt.figure()
plt.subplot(1, 2, 1)
plt.imshow(smoothed_source, cmap='gray')
plt.plot(padded_source_landmarks[:, 0], padded_source_landmarks[:, 1], "r*")
plt.title("Smoothed Source")
plt.subplot(1, 2, 2)
plt.imshow(smoothed_target, cmap='gray')
if status == "training":
plt.plot(padded_target_landmarks[:, 0], padded_target_landmarks[:, 1], "r*")
plt.title("Smoothed Target")
plt.figure()
plt.subplot(no_rows, 2, 1)
plt.imshow(resampled_source, cmap='gray')
plt.plot(resampled_source_landmarks[:, 0], resampled_source_landmarks[:, 1], "r*")
plt.title("Resampled Source")
plt.subplot(no_rows, 2, 2)
plt.imshow(resampled_target, cmap='gray')
if status == "training":
plt.plot(resampled_target_landmarks[:, 0], resampled_target_landmarks[:, 1], "r*")
plt.title("Resampled Target")
if masks_path is not None:
plt.subplot(no_rows, 2, 3)
plt.imshow(resampled_source_mask, cmap='gray')
plt.title("Resampled Source Mask")
plt.subplot(no_rows, 2, 4)
plt.imshow(resampled_target_mask, cmap='gray')
plt.title("Resampled Target Mask")
plt.show()
def create_training_dataset(results_path, dataset_path, size, mode='min'):
show = False
ids = range(0, 481)
for current_id in ids:
current_id = str(current_id)
case_path = os.path.join(results_path, current_id)
transformed_target_path = os.path.join(case_path, "transformed_target.mha")
source_path = os.path.join(case_path, "source.mha")
transformed_target = sitk.GetArrayFromImage(sitk.ReadImage(transformed_target_path))
source = sitk.GetArrayFromImage(sitk.ReadImage(source_path))
t_source = torch.from_numpy(source)
t_target = torch.from_numpy(transformed_target)
if mode == 'min':
new_shape = utils.calculate_new_shape_min((t_source.size(0), t_source.size(1)), size)
if min(new_shape) == min(source.shape):
print("Resampling not required")
resampled_source = t_source
resampled_target = t_target
else:
resampled_source = utils.resample_tensor(t_source, new_shape)
resampled_target = utils.resample_tensor(t_target, new_shape)
elif mode == 'max':
new_shape = utils.calculate_new_shape_max((t_source.size(0), t_source.size(1)), size)
if max(new_shape) == max(source.shape):
print("Resampling not required")
resampled_source = t_source
resampled_target = t_target
else:
resampled_source = utils.resample_tensor(t_source, new_shape)
resampled_target = utils.resample_tensor(t_target, new_shape)
transformed_target_resampled = resampled_target.numpy()
source_resampled = resampled_source.numpy()
target_landmarks_path = os.path.join(case_path, "target_landmarks.csv")
try:
target_landmarks = utils.load_landmarks(target_landmarks_path)
status = "training"
except:
status = "evaluation"
print("Current ID: ", current_id)
print("Transformed target shape: ", transformed_target.shape)
print("Source shape: ", source.shape)
print("Resampled target shape: ", transformed_target_resampled.shape)
print("Resampled source shape: ", source_resampled.shape)
if show:
plt.figure()
plt.subplot(2, 2, 1)
plt.imshow(source, cmap='gray')
plt.axis('off')
plt.subplot(2, 2, 2)
plt.imshow(transformed_target, cmap='gray')
plt.axis('off')
plt.subplot(2, 2, 3)
plt.imshow(source_resampled, cmap='gray')
plt.axis('off')
plt.subplot(2, 2, 4)
plt.imshow(transformed_target_resampled, cmap='gray')
plt.axis('off')
plt.show()
to_save_source = sitk.GetImageFromArray(transformed_target_resampled)
to_save_target = sitk.GetImageFromArray(source_resampled)
output_path = os.path.join(dataset_path, status, current_id)
if not os.path.isdir(output_path):
os.makedirs(output_path)
source_output_path = os.path.join(output_path, "source.mha")
target_output_path = os.path.join(output_path, "target.mha")
sitk.WriteImage(to_save_source, source_output_path)
sitk.WriteImage(to_save_target, target_output_path)
if __name__ == "__main__":
csv_path = paths.csv_path
dataset_path = paths.original_data_path
output_path = paths.parsed_data_path
parse_dataset(csv_path, dataset_path, output_path, masks_path=None)
# The purpose of the code below is to create training dataset for the next registration step (e.g. from rotation alignment to affine or from affine to nonrigid)
# results_path = None
# dataset_path = None
# size = 1024 # Max shape in the training dataset (useful for e.g. decreasing the resolution for initial rotation search or affine registration)
# create_training_dataset(results_path, dataset_path, size, "min")