-
Notifications
You must be signed in to change notification settings - Fork 290
/
helpers.py
332 lines (273 loc) · 10.9 KB
/
helpers.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
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
import settings
import glob
import datetime
import os
import sys
import numpy
import cv2
from collections import defaultdict
from skimage.segmentation import clear_border
from skimage.measure import label, regionprops
from skimage.morphology import disk, dilation, binary_erosion, binary_closing
from skimage.filters import roberts, sobel
from scipy import ndimage as ndi
import math
import pandas
def compute_dice(label_img, pred_img, p_threshold=0.5):
p = pred_img.astype(numpy.float)
l = label_img.astype(numpy.float)
if p.max() > 127:
p /= 255.
if l.max() > 127:
l /= 255.
p = numpy.clip(p, 0, 1.0)
l = numpy.clip(l, 0, 1.0)
p[p > 0.5] = 1.0
p[p < 0.5] = 0.0
l[l > 0.5] = 1.0
l[l < 0.5] = 0.0
product = numpy.dot(l.flatten(), p.flatten())
dice_num = 2 * product + 1
pred_sum = p.sum()
label_sum = l.sum()
dice_den = pred_sum + label_sum + 1
dice_val = dice_num / dice_den
return dice_val
class Stopwatch(object):
def start(self):
self.start_time = Stopwatch.get_time()
def get_elapsed_time(self):
current_time = Stopwatch.get_time()
res = current_time - self.start_time
return res
def get_elapsed_seconds(self):
elapsed_time = self.get_elapsed_time()
res = elapsed_time.total_seconds()
return res
@staticmethod
def get_time():
res = datetime.datetime.now()
return res
@staticmethod
def start_new():
res = Stopwatch()
res.start()
return res
def load_patient_images(patient_id, base_dir=None, wildcard="*.*", exclude_wildcards=[]):
if base_dir == None:
base_dir = settings.LUNA_16_TRAIN_DIR
src_dir = base_dir + patient_id + "/"
src_img_paths = glob.glob(src_dir + wildcard)
for exclude_wildcard in exclude_wildcards:
exclude_img_paths = glob.glob(src_dir + exclude_wildcard)
src_img_paths = [im for im in src_img_paths if im not in exclude_img_paths]
src_img_paths.sort()
images = [cv2.imread(img_path, cv2.IMREAD_GRAYSCALE) for img_path in src_img_paths]
images = [im.reshape((1, ) + im.shape) for im in images]
res = numpy.vstack(images)
return res
def save_cube_img(target_path, cube_img, rows, cols):
assert rows * cols == cube_img.shape[0]
img_height = cube_img.shape[1]
img_width = cube_img.shape[1]
res_img = numpy.zeros((rows * img_height, cols * img_width), dtype=numpy.uint8)
for row in range(rows):
for col in range(cols):
target_y = row * img_height
target_x = col * img_width
res_img[target_y:target_y + img_height, target_x:target_x + img_width] = cube_img[row * cols + col]
cv2.imwrite(target_path, res_img)
def load_cube_img(src_path, rows, cols, size):
img = cv2.imread(src_path, cv2.IMREAD_GRAYSCALE)
# assert rows * size == cube_img.shape[0]
# assert cols * size == cube_img.shape[1]
res = numpy.zeros((rows * cols, size, size))
img_height = size
img_width = size
for row in range(rows):
for col in range(cols):
src_y = row * img_height
src_x = col * img_width
res[row * cols + col] = img[src_y:src_y + img_height, src_x:src_x + img_width]
return res
def get_normalized_img_unit8(img):
img = img.astype(numpy.float)
min = img.min()
max = img.max()
img -= min
img /= max - min
img *= 255
res = img.astype(numpy.uint8)
return res
def normalize_hu(image):
MIN_BOUND = -1000.0
MAX_BOUND = 400.0
image = (image - MIN_BOUND) / (MAX_BOUND - MIN_BOUND)
image[image > 1] = 1.
image[image < 0] = 0.
return image
def rescale_patient_images(images_zyx, org_spacing_xyz, target_voxel_mm, is_mask_image=False, verbose=False):
if verbose:
print("Spacing: ", org_spacing_xyz)
print("Shape: ", images_zyx.shape)
# print "Resizing dim z"
resize_x = 1.0
resize_y = float(org_spacing_xyz[2]) / float(target_voxel_mm)
interpolation = cv2.INTER_NEAREST if is_mask_image else cv2.INTER_LINEAR
res = cv2.resize(images_zyx, dsize=None, fx=resize_x, fy=resize_y, interpolation=interpolation) # opencv assumes y, x, channels umpy array, so y = z pfff
# print "Shape is now : ", res.shape
res = res.swapaxes(0, 2)
res = res.swapaxes(0, 1)
# print "Shape: ", res.shape
resize_x = float(org_spacing_xyz[0]) / float(target_voxel_mm)
resize_y = float(org_spacing_xyz[1]) / float(target_voxel_mm)
# cv2 can handle max 512 channels..
if res.shape[2] > 512:
res = res.swapaxes(0, 2)
res1 = res[:256]
res2 = res[256:]
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res1 = cv2.resize(res1, dsize=None, fx=resize_x, fy=resize_y, interpolation=interpolation)
res2 = cv2.resize(res2, dsize=None, fx=resize_x, fy=resize_y, interpolation=interpolation)
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res = numpy.vstack([res1, res2])
res = res.swapaxes(0, 2)
else:
res = cv2.resize(res, dsize=None, fx=resize_x, fy=resize_y, interpolation=interpolation)
# channels = cv2.split(res)
# resized_channels = []
# for channel in channels:
# channel = cv2.resize(channel, dsize=None, fx=resize_x, fy=resize_y)
# resized_channels.append(channel)
# res = cv2.merge(resized_channels)
# print "Shape after resize: ", res.shape
res = res.swapaxes(0, 2)
res = res.swapaxes(2, 1)
if verbose:
print("Shape after: ", res.shape)
return res
def rescale_patient_images2(images_zyx, target_shape, verbose=False):
if verbose:
print("Target: ", target_shape)
print("Shape: ", images_zyx.shape)
# print "Resizing dim z"
resize_x = 1.0
interpolation = cv2.INTER_NEAREST if False else cv2.INTER_LINEAR
res = cv2.resize(images_zyx, dsize=(target_shape[1], target_shape[0]), interpolation=interpolation) # opencv assumes y, x, channels umpy array, so y = z pfff
# print "Shape is now : ", res.shape
res = res.swapaxes(0, 2)
res = res.swapaxes(0, 1)
# cv2 can handle max 512 channels..
if res.shape[2] > 512:
res = res.swapaxes(0, 2)
res1 = res[:256]
res2 = res[256:]
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res1 = cv2.resize(res1, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res2 = cv2.resize(res2, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res1 = res1.swapaxes(0, 2)
res2 = res2.swapaxes(0, 2)
res = numpy.vstack([res1, res2])
res = res.swapaxes(0, 2)
else:
res = cv2.resize(res, dsize=(target_shape[2], target_shape[1]), interpolation=interpolation)
res = res.swapaxes(0, 2)
res = res.swapaxes(2, 1)
if verbose:
print("Shape after: ", res.shape)
return res
def print_global_vars(globs, names):
# globs = globals()
print("-- GLOBALS --")
for key in globs.keys():
if key in names:
print(key, ": ", globs[key])
print("")
PRINT_TAB_MAP = defaultdict(lambda: [])
def print_tabbed(value_list, justifications=None, map_id=None, show_map_idx=True):
map_entries = None
if map_id is not None:
map_entries = PRINT_TAB_MAP[map_id]
if map_entries is not None and show_map_idx:
idx = str(len(map_entries))
if idx == "0":
idx = "idx"
value_list.insert(0, idx)
if justifications is not None:
justifications.insert(0, 6)
value_list = [str(v) for v in value_list]
if justifications is not None:
new_list = []
assert(len(value_list) == len(justifications))
for idx, value in enumerate(value_list):
str_value = str(value)
just = justifications[idx]
if just > 0:
new_value = str_value.ljust(just)
else:
new_value = str_value.rjust(just)
new_list.append(new_value)
value_list = new_list
line = "\t".join(value_list)
if map_entries is not None:
map_entries.append(line)
print(line)
def get_segmented_lungs(im, plot=False):
# Step 1: Convert into a binary image.
binary = im < -400
# Step 2: Remove the blobs connected to the border of the image.
cleared = clear_border(binary)
# Step 3: Label the image.
label_image = label(cleared)
# Step 4: Keep the labels with 2 largest areas.
areas = [r.area for r in regionprops(label_image)]
areas.sort()
if len(areas) > 2:
for region in regionprops(label_image):
if region.area < areas[-2]:
for coordinates in region.coords:
label_image[coordinates[0], coordinates[1]] = 0
binary = label_image > 0
# Step 5: Erosion operation with a disk of radius 2. This operation is seperate the lung nodules attached to the blood vessels.
selem = disk(2)
binary = binary_erosion(binary, selem)
# Step 6: Closure operation with a disk of radius 10. This operation is to keep nodules attached to the lung wall.
selem = disk(10) # CHANGE BACK TO 10
binary = binary_closing(binary, selem)
# Step 7: Fill in the small holes inside the binary mask of lungs.
edges = roberts(binary)
binary = ndi.binary_fill_holes(edges)
# Step 8: Superimpose the binary mask on the input image.
get_high_vals = binary == 0
im[get_high_vals] = -2000
return im, binary
def prepare_image_for_net3D(img, mean_value=None):
img = img.astype(numpy.float32)
if mean_value is not None:
img -= mean_value
img /= 255.
img = img.reshape(1, img.shape[0], img.shape[1], img.shape[2], 1)
return img
def get_distance(df_row1, df_row2):
dist = math.sqrt(math.pow(df_row1["coord_x"] - df_row2["coord_x"], 2) + math.pow(df_row1["coord_y"] - df_row2["coord_y"], 2) + math.pow(df_row1["coord_y"] - df_row2["coord_y"], 2))
return dist
def percentage_to_pixels(x_perc, y_perc, z_perc, cube_image):
res_x = int(round(x_perc * cube_image.shape[2]))
res_y = int(round(y_perc * cube_image.shape[1]))
res_z = int(round(z_perc * cube_image.shape[0]))
return res_x, res_y, res_z
PATIENT_LIST = None
def get_patient_fold(patient_id, submission_set_neg=False):
global PATIENT_LIST
if PATIENT_LIST is None:
df = pandas.read_csv("resources/stage1_labels.csv")
PATIENT_LIST = df["id"].tolist()
if submission_set_neg:
if patient_id not in PATIENT_LIST:
return -1
res = PATIENT_LIST.index(patient_id)
res %= 6
return res