-
Notifications
You must be signed in to change notification settings - Fork 0
/
visualizer_functions.py
executable file
·388 lines (315 loc) · 13.5 KB
/
visualizer_functions.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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Wed Sep 2 13:53:05 2020
@author: vganapa1
"""
import numpy as np
from helper_functions import create_alpha_mat
try:
import matplotlib.pyplot as plt
plt_flag = True
except ImportError:
plt_flag = False
def analyze_multiopt(training_opts_vec, dataset_type, worst_case = False):
results = []
for training_opt in training_opts_vec:
merit_vec = np.load(training_opt + '/MSE_final_' + dataset_type + '.npy')
if worst_case:
merit_vec = np.sort(merit_vec)
result = np.mean(merit_vec[-1000:])
else:
result = np.mean(merit_vec)
results.append(result)
return results
def optical_element_transform(optical_element, LitCoord2):
LED_mat = np.zeros(LitCoord2.shape)
LED_mat[np.nonzero(LitCoord2)]=optical_element
return LED_mat
def show_fig_alpha(save_path,
alpha,
LitCoord,
oe_ind = 0):
LED_mat = optical_element_transform(alpha.numpy()[:,oe_ind], LitCoord)
if plt_flag:
plt.figure()
plt.title('LED mat ' + str(oe_ind))
LED_mat[np.nonzero(LED_mat==0)] = np.nan
plt.imshow(LED_mat)
plt.colorbar()
plt.savefig(save_path + '/LED_mat_' + str(oe_ind) + '.png')
def show_fig_alpha2(alpha,
LitCoord,
):
LED_mat = optical_element_transform(alpha.numpy(), LitCoord)
if plt_flag:
plt.figure()
plt.title('LED mat')
LED_mat[np.nonzero(LED_mat==0)] = np.nan
plt.imshow(LED_mat)
plt.colorbar()
def make_box(pixels_x,
pixels_y,
pixel_x_start, # patch inclusive of start value
pixel_x_stop, # patch exclusive of stop value
pixel_y_start,
pixel_y_stop,
line_thickness = 2):
box = np.zeros([pixels_x,pixels_y,4])
box[pixel_x_start-line_thickness:pixel_x_stop+line_thickness,\
pixel_y_start-line_thickness:pixel_y_start, 3] = 1
box[pixel_x_start-line_thickness:pixel_x_stop+line_thickness,\
pixel_y_stop:pixel_y_stop+line_thickness, 3] = 1
box[pixel_x_start-line_thickness:pixel_x_start,\
pixel_y_start-line_thickness:pixel_y_stop+line_thickness, 3] = 1
box[pixel_x_stop:pixel_x_stop+line_thickness,\
pixel_y_start-line_thickness:pixel_y_stop+line_thickness, 3] = 1
return box
def show_figs_pixel_map(save_path,
im_stack,
LitCoord2,
pixel_x_start, # patch inclusive of start value
pixel_x_stop, # patch exclusive of stop value
pixel_y_start,
pixel_y_stop,
name_tag,
data_folder,
example_num,
iter_ind,
img_ind,
batch_ind = 0):
pixels_x = im_stack.shape[1]
pixels_y = im_stack.shape[2]
box = make_box(pixels_x,
pixels_y,
pixel_x_start, # patch inclusive of start value
pixel_x_stop, # patch exclusive of stop value
pixel_y_start,
pixel_y_stop)
if plt_flag:
plt.figure()
plt.title('Pixel Patch')
plt.imshow(np.sum(im_stack[batch_ind,:,:,:], axis=-1))
plt.colorbar()
plt.imshow(box)
plt.savefig(save_path + '/pixel_patch_' + name_tag + '_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
im_stack_patch = \
im_stack[batch_ind, \
pixel_x_start:pixel_x_stop, \
pixel_y_start:pixel_y_stop, \
:]
im_stack_patch = np.squeeze(im_stack_patch)
im_stack_patch = np.mean(im_stack_patch, axis = 0)
im_stack_patch = np.mean(im_stack_patch, axis = 0)
pixel_mat = optical_element_transform(im_stack_patch, LitCoord2)
pixel_mat[np.nonzero(pixel_mat==0)] = np.nan
if plt_flag:
plt.figure()
plt.title('Pixel patch average value')
plt.imshow(pixel_mat)
plt.colorbar()
plt.savefig(save_path + '/pixel_mat_' + name_tag + '_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
def show_figs2(save_path,
iter_vec,
train_loss_vec,
save_tag = 'train_loss_vec'
):
# train_loss_vec = np.array(train_loss_vec)[:,-1]
# val_loss_vec = np.array(val_loss_vec)[:,-1]
if plt_flag:
plt.figure()
plt.title(save_tag)
plt.plot(train_loss_vec)
plt.savefig(save_path + '/' + save_tag + '.png')
def show_fig_alpha_loc_probs(save_path,
alpha_loc_probs_vec,
example_num,
pattern_ind,
data_folder,
batch_ind=0):
plt.figure()
plt.title('alpha_loc_probs')
for ii in range(alpha_loc_probs_vec.shape[0]):
plt.plot(alpha_loc_probs_vec[ii,batch_ind,pattern_ind,:],label=ii)
plt.legend()
plt.savefig(save_path + '/alpha_loc_probs' + \
data_folder + '_example_' + str(example_num) + '_pattern_' + str(pattern_ind) + '.png')
def show_figs_a(save_path,
a,
batch_size_per_gpu,
data_folder,
example_num,
iter_ind,
pattern_ind,
num_leds,
num_patterns,
LitCoord2,
batch_ind=0):
a_mat = \
create_alpha_mat(a,
batch_size_per_gpu,
num_leds,
num_patterns,
LitCoord2)
a_mat = a_mat[batch_ind,:,:,pattern_ind]
a_mat = a_mat.numpy()
a_mat[np.nonzero(a_mat==0)] = np.nan
if plt_flag:
plt.figure()
plt.title('a_mat')
plt.imshow(a_mat)
plt.colorbar()
plt.savefig(save_path + '/a_mat' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_pattern_' + str(pattern_ind) + '.png')
def show_alpha_scatter(led_position_xy, alpha, im_stack_multiplexed):
plt.figure()
plt.title('LED Illumination Pattern')
plt.scatter(led_position_xy[:,0], led_position_xy[:,1],c=alpha, s=100, cmap='Greens', edgecolors= "black",
vmin=np.min(alpha), vmax=1.5*np.max(alpha))
plt.xlim((-50, 50))
plt.ylim((-50, 50))
plt.axis('square')
# plt.axis('off')
if im_stack_multiplexed is not None:
plt.figure()
plt.title('im_stack_multiplexed')
plt.imshow(im_stack_multiplexed)
plt.colorbar()
def show_figs_alpha(save_path,
alpha_sample,
batch_size_per_gpu,
im_stack_multiplexed,
data_folder,
example_num,
iter_ind,
pattern_ind,
num_leds,
num_patterns,
LitCoord2,
batch_ind=0):
alpha_mat = \
create_alpha_mat(alpha_sample,
batch_size_per_gpu,
num_leds,
num_patterns,
LitCoord2)
alpha_mat = alpha_mat[batch_ind,:,:,pattern_ind]
alpha_mat = alpha_mat.numpy()
alpha_mat[np.nonzero(alpha_mat==0)] = np.nan
im_stack_multiplexed = im_stack_multiplexed[batch_ind,:,:,pattern_ind]
if plt_flag:
plt.figure()
plt.title('alpha_mat')
plt.imshow(alpha_mat)
plt.colorbar()
plt.savefig(save_path + '/alpha_mat' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_pattern_' + str(pattern_ind) + '.png')
plt.figure()
plt.title('im_stack_multiplexed')
plt.imshow(im_stack_multiplexed)
plt.colorbar()
plt.savefig(save_path + '/im_stack_multiplexed_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_pattern_' + str(pattern_ind) + '.png')
def show_figs_input_output(save_path,
data_folder,
im_stack,
output,
batch_ind,
img_ind):
input_fig = im_stack[batch_ind,:,:,img_ind]
output_fig = output[batch_ind,:,:,img_ind]
vmin = np.min(input_fig)
vmax = np.max(input_fig)
if plt_flag:
plt.figure()
plt.title('input fig')
plt.imshow(input_fig, vmin=vmin, vmax=vmax)
plt.colorbar()
plt.savefig(save_path + '/input_fig_' + \
data_folder + '_img_ind_' + str(img_ind) + '_batch_ind_' + str(batch_ind) + '.png')
plt.figure()
plt.title('output fig')
plt.imshow(output_fig, vmin=vmin, vmax=vmax)
plt.colorbar()
plt.savefig(save_path + '/output_fig_' + \
data_folder + '_img_ind_' + str(img_ind) + '_batch_ind_' + str(batch_ind) + '.png')
def show_figs(save_path,
im_stack,
im_stack_mean,
im_stack_var,
data_folder,
example_num,
iter_ind,
img_ind,
batch_ind = 0):
if plt_flag:
x,y = im_stack_mean[batch_ind,:,:,0].shape
x = int(0.75*x)
y = int(0.75*y)
plt.figure()
plt.title('Pixel comparison 1')
plt.plot(im_stack_mean[batch_ind,x,y,:])
plt.plot(im_stack[batch_ind,x,y,:])
plt.savefig(save_path + '/line_compare0_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
x,y = im_stack_mean[batch_ind,:,:,0].shape
x = int(0.25*x)
y = int(0.25*y)
plt.figure()
plt.title('Pixel comparison 2')
plt.plot(im_stack_mean[batch_ind,x,y,:],label='computed')
plt.plot(im_stack[batch_ind, x,y,:], label = 'actual')
plt.legend()
plt.savefig(save_path + '/line_compare1_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
actual_guess = np.concatenate((im_stack[batch_ind,:,:,img_ind],im_stack_mean[batch_ind,:,:,img_ind]),axis=1)
plt.figure(figsize=[10,10])
plt.title('Actual and guess comparison')
plt.imshow(actual_guess, vmin=0, vmax=1) #vmax=np.max(im_stack[batch_ind,:,:,img_ind])
plt.savefig(save_path + '/actual_guess_comparison_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
plt.figure()
plt.title('im_stack actual')
plt.imshow(im_stack[batch_ind,:,:,img_ind])
plt.colorbar()
plt.savefig(save_path + '/im_stack_actual_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
plt.figure()
plt.title('im_stack_mean')
plt.imshow(im_stack_mean[batch_ind,:,:,img_ind])
plt.colorbar()
plt.savefig(save_path + '/guess_mean_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
plt.figure()
plt.title('im_stack_var')
plt.imshow(im_stack_var[batch_ind,:,:,img_ind])
plt.colorbar()
plt.savefig(save_path + '/guess_var_' + \
data_folder + '_example_' + str(example_num) + '_iter_' + str(iter_ind) + '_LED_' + str(img_ind) + '.png')
def make_cutout_fig(img, start_corner, size, vmin, vmax, folder_name, save_name, \
title = '', linewidth = 6, linewidth_zoom = 6, cmap='gray'):
# Create box
box = np.zeros(img.shape)
box[start_corner[0]:start_corner[0]+size[0],start_corner[1]:start_corner[1]+size[1]] = 1
start_corner2 = start_corner + linewidth
size2 = size - linewidth*2
box[start_corner2[0]:start_corner2[0]+size2[0],start_corner2[1]:start_corner2[1]+size2[1]] = 0
plt.figure()
# plt.title(title)
plt.imshow(img, vmin=vmin, vmax=vmax, cmap=cmap)
plt.axis('off')
plt.imshow(np.dstack([box, 1-box, 1-box, box]))
plt.savefig(folder_name+'/'+save_name+'_full.png',bbox_inches='tight', dpi = 300, pad_inches=0)
box2 = np.ones(size)
box2[linewidth_zoom:size[0]-linewidth_zoom,linewidth_zoom:size[1]-linewidth_zoom] = 0
img_zoomed = img[start_corner[0]:start_corner[0]+size[0],start_corner[1]:start_corner[1]+size[1]]
plt.figure()
# plt.title(title)
plt.imshow(img_zoomed, \
vmin=vmin, vmax=vmax, cmap=cmap)
plt.axis('off')
plt.imshow(np.dstack([box2, 1-box2, 1-box2, box2]))
plt.savefig(folder_name+'/'+save_name+'_zoom.png',bbox_inches='tight', dpi = 300, pad_inches=0)
return box2,img_zoomed