|
a |
|
b/utils_plots.py |
|
|
1 |
import matplotlib |
|
|
2 |
import utils |
|
|
3 |
|
|
|
4 |
if utils.hostname() != 'user': |
|
|
5 |
matplotlib.use('Agg') |
|
|
6 |
|
|
|
7 |
import matplotlib.pyplot as plt |
|
|
8 |
import warnings |
|
|
9 |
import numpy as np |
|
|
10 |
import matplotlib.animation as animation |
|
|
11 |
|
|
|
12 |
warnings.simplefilter('ignore') |
|
|
13 |
anim_running = True |
|
|
14 |
|
|
|
15 |
|
|
|
16 |
def plot_slice_3d_2(image3d, mask, axis, pid, img_dir=None, idx=None): |
|
|
17 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
18 |
fig.canvas.set_window_title(pid) |
|
|
19 |
masked_image = image3d * mask |
|
|
20 |
if idx is None: |
|
|
21 |
roi_idxs = np.where(mask == 1.) |
|
|
22 |
if len(roi_idxs[0]) > 0: |
|
|
23 |
idx = (np.mean(roi_idxs[0]), np.mean(roi_idxs[1]), np.mean(roi_idxs[2])) |
|
|
24 |
else: |
|
|
25 |
print 'No nodules' |
|
|
26 |
idx = np.array(image3d.shape) / 2 |
|
|
27 |
else: |
|
|
28 |
idx = idx.astype(int) |
|
|
29 |
if axis == 0: # sax |
|
|
30 |
ax[0, 0].imshow(image3d[idx[0], :, :], cmap=plt.cm.gray) |
|
|
31 |
ax[0, 1].imshow(mask[idx[0], :, :], cmap=plt.cm.gray) |
|
|
32 |
ax[1, 0].imshow(masked_image[idx[0], :, :], cmap=plt.cm.gray) |
|
|
33 |
if axis == 1: # 2 lungs |
|
|
34 |
ax[0, 0].imshow(image3d[:, idx[1], :], cmap=plt.cm.gray) |
|
|
35 |
ax[0, 1].imshow(mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
36 |
ax[1, 0].imshow(masked_image[:, idx[1], :], cmap=plt.cm.gray) |
|
|
37 |
if axis == 2: # side view |
|
|
38 |
ax[0, 0].imshow(image3d[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
39 |
ax[0, 1].imshow(mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
40 |
ax[1, 0].imshow(masked_image[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
41 |
|
|
|
42 |
if img_dir is not None: |
|
|
43 |
fig.savefig(img_dir + '/%s%s.png' % (pid, axis), bbox_inches='tight') |
|
|
44 |
else: |
|
|
45 |
plt.show() |
|
|
46 |
fig.clf() |
|
|
47 |
plt.close('all') |
|
|
48 |
|
|
|
49 |
|
|
|
50 |
def plot_slice_3d_3(input, mask, prediction, axis, pid, img_dir=None, idx=None): |
|
|
51 |
# to convert cuda arrays to numpy array |
|
|
52 |
input = np.asarray(input) |
|
|
53 |
mask = np.asarray(mask) |
|
|
54 |
prediction = np.asarray(prediction) |
|
|
55 |
|
|
|
56 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
57 |
fig.canvas.set_window_title(pid) |
|
|
58 |
if idx is None: |
|
|
59 |
roi_idxs = np.where(mask > 0) |
|
|
60 |
if len(roi_idxs[0]) > 0: |
|
|
61 |
idx = (int(np.mean(roi_idxs[0])), |
|
|
62 |
int(np.mean(roi_idxs[1])), |
|
|
63 |
int(np.mean(roi_idxs[2]))) |
|
|
64 |
else: |
|
|
65 |
print 'No nodules' |
|
|
66 |
idx = np.array(input.shape) / 2 |
|
|
67 |
else: |
|
|
68 |
idx = idx.astype(int) |
|
|
69 |
if axis == 0: # sax |
|
|
70 |
ax[0, 0].imshow(prediction[idx[0], :, :], cmap=plt.cm.gray) |
|
|
71 |
ax[1, 0].imshow(input[idx[0], :, :], cmap=plt.cm.gray) |
|
|
72 |
ax[0, 1].imshow(mask[idx[0], :, :], cmap=plt.cm.gray) |
|
|
73 |
if axis == 1: # 2 lungs |
|
|
74 |
ax[0, 0].imshow(prediction[:, idx[1], :], cmap=plt.cm.gray) |
|
|
75 |
ax[1, 0].imshow(input[:, idx[1], :], cmap=plt.cm.gray) |
|
|
76 |
ax[0, 1].imshow(mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
77 |
if axis == 2: # side view |
|
|
78 |
ax[0, 0].imshow(prediction[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
79 |
ax[1, 0].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
80 |
ax[0, 1].imshow(mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
81 |
if img_dir is not None: |
|
|
82 |
fig.savefig(img_dir + '/%s-%s.png' % (pid, axis), bbox_inches='tight') |
|
|
83 |
else: |
|
|
84 |
plt.show() |
|
|
85 |
fig.clf() |
|
|
86 |
plt.close('all') |
|
|
87 |
|
|
|
88 |
|
|
|
89 |
def plot_slice_3d_3axis(input, pid, img_dir=None, idx=None): |
|
|
90 |
# to convert cuda arrays to numpy array |
|
|
91 |
input = np.asarray(input) |
|
|
92 |
|
|
|
93 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
94 |
fig.canvas.set_window_title(pid) |
|
|
95 |
ax[0, 0].imshow(input[idx[0], :, :], cmap=plt.cm.gray) |
|
|
96 |
ax[1, 0].imshow(input[:, idx[1], :], cmap=plt.cm.gray) |
|
|
97 |
ax[0, 1].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
98 |
|
|
|
99 |
if img_dir is not None: |
|
|
100 |
fig.savefig(img_dir + '/%s.png' % (pid), bbox_inches='tight') |
|
|
101 |
else: |
|
|
102 |
plt.show() |
|
|
103 |
fig.clf() |
|
|
104 |
plt.close('all') |
|
|
105 |
|
|
|
106 |
|
|
|
107 |
def plot_slice_3d_4(input, mask, prediction, lung_mask, axis, pid, img_dir=None, idx=None): |
|
|
108 |
# to convert cuda arrays to numpy array |
|
|
109 |
input = np.asarray(input) |
|
|
110 |
mask = np.asarray(mask) |
|
|
111 |
prediction = np.asarray(prediction) |
|
|
112 |
|
|
|
113 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
114 |
fig.canvas.set_window_title(pid) |
|
|
115 |
if idx is None: |
|
|
116 |
roi_idxs = np.where(mask > 0) |
|
|
117 |
if len(roi_idxs[0]) > 0: |
|
|
118 |
idx = (int(np.mean(roi_idxs[0])), |
|
|
119 |
int(np.mean(roi_idxs[1])), |
|
|
120 |
int(np.mean(roi_idxs[2]))) |
|
|
121 |
else: |
|
|
122 |
print 'No nodules' |
|
|
123 |
idx = np.array(input.shape) / 2 |
|
|
124 |
else: |
|
|
125 |
idx = idx.astype(int) |
|
|
126 |
if axis == 0: # sax |
|
|
127 |
ax[0, 0].imshow(prediction[idx[0], :, :], cmap=plt.cm.gray) |
|
|
128 |
ax[1, 0].imshow(input[idx[0], :, :], cmap=plt.cm.gray) |
|
|
129 |
ax[0, 1].imshow(mask[idx[0], :, :], cmap=plt.cm.gray) |
|
|
130 |
ax[1, 1].imshow(lung_mask[idx[0], :, :], cmap=plt.cm.gray) |
|
|
131 |
if axis == 1: # 2 lungs |
|
|
132 |
ax[0, 0].imshow(prediction[:, idx[1], :], cmap=plt.cm.gray) |
|
|
133 |
ax[1, 0].imshow(input[:, idx[1], :], cmap=plt.cm.gray) |
|
|
134 |
ax[0, 1].imshow(mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
135 |
ax[1, 1].imshow(lung_mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
136 |
if axis == 2: # side view |
|
|
137 |
ax[0, 0].imshow(prediction[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
138 |
ax[1, 0].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
139 |
ax[0, 1].imshow(mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
140 |
ax[1, 1].imshow(lung_mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
141 |
if img_dir is not None: |
|
|
142 |
fig.savefig(img_dir + '/%s-%s.png' % (pid, axis), bbox_inches='tight') |
|
|
143 |
else: |
|
|
144 |
plt.show() |
|
|
145 |
fig.clf() |
|
|
146 |
plt.close('all') |
|
|
147 |
|
|
|
148 |
|
|
|
149 |
def plot_slice_3d_3_patch(input, mask, prediction, axis, pid, patch_size=64, img_dir=None, idx=None): |
|
|
150 |
# to convert cuda arrays to numpy array |
|
|
151 |
input = np.asarray(input) |
|
|
152 |
mask = np.asarray(mask) |
|
|
153 |
prediction = np.asarray(prediction) |
|
|
154 |
|
|
|
155 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
156 |
fig.canvas.set_window_title(pid) |
|
|
157 |
if idx is None: |
|
|
158 |
roi_idxs = np.where(mask > 0) |
|
|
159 |
if len(roi_idxs[0]) > 0: |
|
|
160 |
idx = (np.mean(roi_idxs[0]), np.mean(roi_idxs[1]), np.mean(roi_idxs[2])) |
|
|
161 |
else: |
|
|
162 |
print 'No nodules' |
|
|
163 |
idx = np.array(input.shape) / 2 |
|
|
164 |
if axis == 0: # sax |
|
|
165 |
sz, sy, sx = slice(idx[0], idx[0] + 1), slice(idx[1] - patch_size, idx[1] + patch_size), slice( |
|
|
166 |
idx[2] - patch_size, idx[2] + patch_size) |
|
|
167 |
ax[0, 0].imshow(prediction[sz, sy, sx], cmap=plt.cm.gray) |
|
|
168 |
ax[1, 0].imshow(input[sz, sy, sx], cmap=plt.cm.gray) |
|
|
169 |
ax[0, 1].imshow(mask[sz, sy, sx], cmap=plt.cm.gray) |
|
|
170 |
if axis == 1: # 2 lungs |
|
|
171 |
ax[0, 0].imshow(prediction[:, idx[1], :], cmap=plt.cm.gray) |
|
|
172 |
ax[1, 0].imshow(input[:, idx[1], :], cmap=plt.cm.gray) |
|
|
173 |
ax[0, 1].imshow(mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
174 |
if axis == 2: # side view |
|
|
175 |
ax[0, 0].imshow(prediction[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
176 |
ax[1, 0].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
177 |
ax[0, 1].imshow(mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
178 |
if img_dir is not None: |
|
|
179 |
fig.savefig(img_dir + '/%s.png' % pid, bbox_inches='tight') |
|
|
180 |
else: |
|
|
181 |
plt.show() |
|
|
182 |
fig.clf() |
|
|
183 |
plt.close('all') |
|
|
184 |
|
|
|
185 |
|
|
|
186 |
def plot_slice_3d_2_patch(ct_scan, mask, pid, img_dir=None, idx=None): |
|
|
187 |
# to convert cuda arrays to numpy array |
|
|
188 |
ct_scan = np.asarray(ct_scan) |
|
|
189 |
mask = np.asarray(mask) |
|
|
190 |
|
|
|
191 |
fig, ax = plt.subplots(2, 3, figsize=[8, 8]) |
|
|
192 |
fig.canvas.set_window_title(pid) |
|
|
193 |
|
|
|
194 |
if idx == None: |
|
|
195 |
#just plot in the middle of the cube |
|
|
196 |
in_sh = ct_scan.shape |
|
|
197 |
idx = [in_sh[0]/2,in_sh[1]/2,in_sh[2]/2] |
|
|
198 |
print np.amin(ct_scan), np.amax(ct_scan) |
|
|
199 |
print np.amin(mask), np.amax(mask) |
|
|
200 |
|
|
|
201 |
|
|
|
202 |
ax[0, 0].imshow(ct_scan[idx[0], :, :], cmap=plt.cm.gray) |
|
|
203 |
ax[0, 1].imshow(ct_scan[:, idx[1], :], cmap=plt.cm.gray) |
|
|
204 |
ax[0, 2].imshow(ct_scan[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
205 |
|
|
|
206 |
ax[1, 0].imshow(mask[idx[0], :, :], cmap=plt.cm.gray) |
|
|
207 |
ax[1, 1].imshow(mask[:, idx[1], :], cmap=plt.cm.gray) |
|
|
208 |
ax[1, 2].imshow(mask[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
209 |
|
|
|
210 |
if img_dir is not None: |
|
|
211 |
fig.savefig(img_dir + '/%s.png' % pid, bbox_inches='tight') |
|
|
212 |
else: |
|
|
213 |
plt.show() |
|
|
214 |
fig.clf() |
|
|
215 |
plt.close('all') |
|
|
216 |
|
|
|
217 |
|
|
|
218 |
def plot_2d(img, mask, pid, img_dir): |
|
|
219 |
# fig = plt.figure() |
|
|
220 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
221 |
fig.canvas.set_window_title(pid) |
|
|
222 |
ax[0, 0].imshow(img, cmap='gray') |
|
|
223 |
ax[0, 1].imshow(mask, cmap='gray') |
|
|
224 |
ax[1, 0].imshow(img * mask, cmap='gray') |
|
|
225 |
plt.show() |
|
|
226 |
fig.savefig(img_dir + '/%s.png' % pid, bbox_inches='tight') |
|
|
227 |
fig.clf() |
|
|
228 |
plt.close('all') |
|
|
229 |
|
|
|
230 |
|
|
|
231 |
def plot_2d_4(img, img_prev, img_next, mask, pid, img_dir): |
|
|
232 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
233 |
fig.canvas.set_window_title(pid) |
|
|
234 |
ax[0, 0].imshow(img, cmap='gray') |
|
|
235 |
ax[0, 1].imshow(img_prev, cmap='gray') |
|
|
236 |
ax[1, 0].imshow(img_next, cmap='gray') |
|
|
237 |
ax[1, 1].imshow(img * mask, cmap='gray') |
|
|
238 |
plt.show() |
|
|
239 |
fig.savefig(img_dir + '/%s.png' % pid, bbox_inches='tight') |
|
|
240 |
fig.clf() |
|
|
241 |
plt.close('all') |
|
|
242 |
|
|
|
243 |
|
|
|
244 |
def plot_2d_animation(input, mask, predictions): |
|
|
245 |
rgb_image = np.concatenate((input, input, input), axis=0) |
|
|
246 |
mask = np.concatenate((np.zeros_like(input), mask, predictions), axis=0) |
|
|
247 |
# green = targets |
|
|
248 |
# blue = predictions |
|
|
249 |
# red = overlap |
|
|
250 |
|
|
|
251 |
idxs = np.where(mask > 0.3) |
|
|
252 |
rgb_image[idxs] = mask[idxs] |
|
|
253 |
|
|
|
254 |
rgb_image = np.rollaxis(rgb_image, axis=0, start=4) |
|
|
255 |
print rgb_image.shape |
|
|
256 |
|
|
|
257 |
def get_data_step(step): |
|
|
258 |
return rgb_image[step, :, :, :] |
|
|
259 |
|
|
|
260 |
fig = plt.figure() |
|
|
261 |
im = fig.gca().imshow(get_data_step(0)) |
|
|
262 |
|
|
|
263 |
def init(): |
|
|
264 |
im.set_data(get_data_step(0)) |
|
|
265 |
return im, |
|
|
266 |
|
|
|
267 |
def animate(i): |
|
|
268 |
im.set_data(get_data_step(i)) |
|
|
269 |
return im, |
|
|
270 |
|
|
|
271 |
anim = animation.FuncAnimation(fig, animate, init_func=init, |
|
|
272 |
frames=rgb_image.shape[1], |
|
|
273 |
interval=20000 / rgb_image.shape[0], |
|
|
274 |
blit=True) |
|
|
275 |
|
|
|
276 |
def on_click(event): |
|
|
277 |
global anim_running |
|
|
278 |
if anim_running: |
|
|
279 |
anim.event_source.stop() |
|
|
280 |
anim_running = False |
|
|
281 |
else: |
|
|
282 |
anim.event_source.start() |
|
|
283 |
anim_running = True |
|
|
284 |
|
|
|
285 |
fig.canvas.mpl_connect('button_press_event', on_click) |
|
|
286 |
try: |
|
|
287 |
plt.show() |
|
|
288 |
except AttributeError: |
|
|
289 |
pass |
|
|
290 |
|
|
|
291 |
def plot_all_slices(input, pid, img_dir=None): |
|
|
292 |
# to convert cuda arrays to numpy array |
|
|
293 |
input = np.asarray(input) |
|
|
294 |
|
|
|
295 |
for idx in range(0, input.shape[0]-3, 4): |
|
|
296 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
297 |
fig.canvas.set_window_title(pid) |
|
|
298 |
ax[0, 0].imshow(input[idx, :, :], cmap=plt.cm.gray) |
|
|
299 |
ax[1, 0].imshow(input[idx+1, :, :], cmap=plt.cm.gray) |
|
|
300 |
ax[0, 1].imshow(input[idx+2, :, :], cmap=plt.cm.gray) |
|
|
301 |
ax[1, 1].imshow(input[idx+3, :, :], cmap=plt.cm.gray) |
|
|
302 |
|
|
|
303 |
if img_dir is not None: |
|
|
304 |
fig.savefig(img_dir + '_' + str(pid) + '_' + str(idx) + '.png' , bbox_inches='tight') |
|
|
305 |
else: |
|
|
306 |
plt.show() |
|
|
307 |
fig.clf() |
|
|
308 |
plt.close('all') |
|
|
309 |
|
|
|
310 |
|
|
|
311 |
def plot_all_slices(ct_scan, mask, pid, img_dir=None): |
|
|
312 |
# to convert cuda arrays to numpy array |
|
|
313 |
ct_scan = np.asarray(ct_scan) |
|
|
314 |
mask = np.asarray(mask) |
|
|
315 |
|
|
|
316 |
for idx in range(0, mask.shape[0]-3, 2): |
|
|
317 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
318 |
fig.canvas.set_window_title(pid) |
|
|
319 |
ax[0, 0].imshow(mask[idx, :, :], cmap=plt.cm.gray) |
|
|
320 |
ax[1, 0].imshow(ct_scan[idx+1, :, :], cmap=plt.cm.gray) |
|
|
321 |
ax[0, 1].imshow(mask[idx+2, :, :], cmap=plt.cm.gray) |
|
|
322 |
ax[1, 1].imshow(ct_scan[idx+3, :, :], cmap=plt.cm.gray) |
|
|
323 |
|
|
|
324 |
if img_dir is not None: |
|
|
325 |
fig.savefig(img_dir + '_' + str(pid) + '_' + str(idx) + '.png' , bbox_inches='tight') |
|
|
326 |
else: |
|
|
327 |
plt.show() |
|
|
328 |
fig.clf() |
|
|
329 |
plt.close('all') |
|
|
330 |
|
|
|
331 |
def plot_4_slices(input, pid, img_dir=None, idx=None): |
|
|
332 |
# to convert cuda arrays to numpy array |
|
|
333 |
input = np.asarray(input) |
|
|
334 |
|
|
|
335 |
fig, ax = plt.subplots(2, 2, figsize=[8, 8]) |
|
|
336 |
fig.canvas.set_window_title(pid) |
|
|
337 |
ax[0, 0].imshow(input[idx[0], :, :], cmap=plt.cm.gray) |
|
|
338 |
ax[1, 0].imshow(input[:, idx[1], :], cmap=plt.cm.gray) |
|
|
339 |
ax[0, 1].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
340 |
ax[1, 1].imshow(input[:, :, idx[2]], cmap=plt.cm.gray) |
|
|
341 |
|
|
|
342 |
if img_dir is not None: |
|
|
343 |
fig.savefig(img_dir + '/%s.png' % (pid), bbox_inches='tight') |
|
|
344 |
else: |
|
|
345 |
plt.show() |
|
|
346 |
fig.clf() |
|
|
347 |
plt.close('all') |
|
|
348 |
|
|
|
349 |
|
|
|
350 |
def plot_learning_curves(train_losses, valid_losses, expid, img_dir): |
|
|
351 |
fig = plt.figure() |
|
|
352 |
x_range = np.arange(len(train_losses)) + 1 |
|
|
353 |
|
|
|
354 |
plt.plot(x_range, train_losses) |
|
|
355 |
plt.plot(x_range, valid_losses) |
|
|
356 |
|
|
|
357 |
if img_dir is not None: |
|
|
358 |
fig.savefig(img_dir + '/%s.png' % expid, bbox_inches='tight') |
|
|
359 |
print 'Saved plot' |
|
|
360 |
else: |
|
|
361 |
plt.show() |
|
|
362 |
fig.clf() |
|
|
363 |
plt.close('all') |