File size: 17,546 Bytes
bc2cdff
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
import numpy as np
import os
import drjit as dr
import matplotlib.pyplot as plt
import matplotlib.gridspec as gridspec
from PDE2D.utils.sketch import *
from PDE2D import PATH
import matplotlib.animation as animation
dpi = 200
PER_ROW = 4
SAVEDATA = True
PNG = False
PDF = True

def create_path(path):
    if not os.path.exists(path):
        os.makedirs(path)

def iter_plot(wos, bbox, path, name, el_vals = None, el_std = None, electrode_nums = None, std = True,
              opt_param = "diffusion", resolution = [256, 256], wos_obj = None, compute_std = False, 
              max_range = [None, None], out_val : float = 1.0):
    path_reconstruction = os.path.join(path, "reconstruction")
    path_primal = os.path.join(path, "primal")
    path_grad = os.path.join(path, "grad")
    path_distance = os.path.join(path, "stepdistance")
    path_screening = os.path.join(path, "eff-screening")

    create_path(path_reconstruction)
    create_path(path_primal)
    create_path(path_grad)
    create_path(path_distance)
    create_path(path_screening)

    if SAVEDATA:
        create_path(os.path.join(path_reconstruction, "npy"))
        create_path(os.path.join(path_primal, "npy"))
        create_path(os.path.join(path_grad, "npy"))
        create_path(os.path.join(path_distance, "npy"))
        create_path(os.path.join(path_screening, "npy"))

    coeff = wos.input.get_coefficient(opt_param)
    coeff_obj = None
    if wos_obj is not None:
        coeff_obj = wos_obj.input.get_coefficient(opt_param)
    # Plot the coefficients
    plot_coeff(coeff, wos.input.shape, bbox, path_reconstruction, f"{opt_param}-{name}-scaled", resolution, coeff_obj, max_range = max_range, out_val=out_val)
    plot_coeff(coeff, wos.input.shape, bbox, path_reconstruction, f"{opt_param}-{name}", resolution, coeff_obj, out_val = out_val)
    
    if SAVEDATA:
        np.save(os.path.join(path_reconstruction, "npy", f"tensor-{name}.npy"), np.array(coeff.tensor))
        if wos_obj is not None:
            np.save(os.path.join(path_reconstruction, "npy", f"tensor-obj.npy"), np.array(coeff_obj.tensor))
    
    
    # Plot the gradient
    grad = dr.grad(coeff.tensor).numpy()
    fig1, ax1 = plt.subplots(1,1,figsize = (5,5))
    range_grad = max(np.max(grad), -np.min(grad))
    plot_image(grad, ax1, input_range = [-range_grad, range_grad], cmap = "coolwarm")

    fig2, ax2 = plt.subplots(1,1,figsize = (5,5))
    coeff_tensor = coeff.tensor.numpy().squeeze()
    plot_image(coeff_tensor, ax2)
    if PDF:
        fig1.savefig(os.path.join(path_grad, f"grad-{name}.pdf"), bbox_inches='tight', pad_inches=0.04, dpi=200)
        fig2.savefig(os.path.join(path_grad, f"tensor-{name}.pdf"), bbox_inches='tight', pad_inches=0.04, dpi=200)
    if PNG:
        fig1.savefig(os.path.join(path_grad, f"grad-{name}.png"), bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        fig1.savefig(os.path.join(path_grad, f"tensor-{name}.png"), bbox_inches='tight', pad_inches=0.04, dpi=dpi)
    plt.close(fig1)
    plt.close(fig2)
    if SAVEDATA:
        np.save(os.path.join(path_grad, "npy", f"grad-{name}.npy"), grad)
        np.save(os.path.join(path_grad, "npy", f"tensor-{name}.npy"), coeff_tensor)

    # Plot effective screening
        fig, ax = plt.subplots(1,1, figsize = (5,5))
        wos.input.eff_screening_tex.visualize(ax, bbox, resolution)
        if PDF:
            fig.savefig(os.path.join(path_screening, f"{name}.pdf"), bbox_inches='tight', pad_inches=0.04, dpi=200)
        if PNG:
            fig.savefig(os.path.join(path_screening, f"{name}.pdf"), bbox_inches='tight', pad_inches=0.04, dpi=200)
        plt.close(fig)

    if wos.use_accel:
        plot_coeff(wos.input.r_best_tex, wos.input.shape, bbox, path_distance, f"distance-{name}", resolution)
        plot_coeff(wos.input.σ_best_tex, wos.input.shape, bbox, path_distance, f"sigma-{name}", resolution)
        if SAVEDATA:
            np.save(os.path.join(path_distance, "npy", f"distance-{name}.npy"), wos.input.r_best_tex.tensor.numpy())
            np.save(os.path.join(path_distance, "npy", f"sigma-{name}.npy"), wos.input.σ_best_tex.tensor.numpy())
    
    if el_vals is not None:
        s = wos.input.shape.out_boundary
        plot_iter_result(el_vals, el_std, s.voltages, s.voltages_std, s.injections, 
                        electrode_nums, s.num_electrodes, path_primal, f"{name}", std = compute_std)


def plot_coeff(coeff, shape, bbox, path, name, resolution = [1024,1024], coeff_obj = None, max_range = [None, None], out_val : float = None):
    points = create_image_points(bbox, resolution, spp=1, centered = True)
    vals = coeff.get_value(points)
    if out_val is not None:
        mask = shape.inside_closed_surface_mask(points)
        vals = dr.select(mask, vals, out_val)
    image, _ = create_image_from_result(vals, resolution)
    if coeff_obj is None:
        fig, ax = plt.subplots(1,1,figsize = (5,5))
        plot_image(image[0], ax, input_range = max_range)
        shape.sketch(ax, bbox, resolution, sketch_center = True)
    else:
        vals_obj = coeff_obj.get_value(points)
        if out_val is not None:
            mask = shape.inside_closed_surface_mask(points)
            vals_obj = dr.select(mask, vals_obj, out_val)
        image_obj, _ = create_image_from_result(vals_obj, resolution)
        fig, (ax1, ax2) = plt.subplots(1,2, figsize = (10,5))
        plot_image(image_obj[0], ax1, input_range = max_range)
        plot_image(image[0], ax2, input_range = max_range)
        shape.sketch(ax1, bbox, resolution, sketch_in_boundaries = False)
        shape.sketch(ax2, bbox, resolution, sketch_center = True)
        ax1.set_title("Objective")
        ax2.set_title("Iteration")
    if PDF:
        fig.savefig(os.path.join(path, f"reconstruction-{name}.pdf"), bbox_inches='tight', pad_inches=0.04, dpi=200)
    if PNG:
        fig.savefig(os.path.join(path, f"reconstruction{name}.png"), bbox_inches='tight', pad_inches=0.04, dpi=dpi)
    plt.close(fig)
    return image


def plot_iter_result(signals, signals_std, objectives, objectives_std, injection_confs, electrode_nums, num_electrodes, 
                     path, name, selected_confs = None, std = True, name1 = "Iteration", name2 = "Objective"):
    num_confs = len(signals)
    selected_mask = np.zeros(num_confs, dtype = bool)
    if selected_confs is not None:
        selected_mask[selected_confs] = True
    
    if SAVEDATA:
        np.save(os.path.join(path, "npy", f"signal-{name}.npy"), np.array(signals))
        if std:
            np.save(os.path.join(path, "npy", f"signal_{name}-std.npy"), np.array(signals_std))
    per_row = PER_ROW
    num_rows = int(num_confs / per_row) + 1
    fig = plt.figure(figsize= (per_row * 8, num_rows * 4))
    
    g = gridspec.GridSpec(num_rows, per_row, figure = fig, wspace = 0.1, hspace=0.3)
    for i, (injection, signal, signal_std, objective, objective_std, el_nums) in enumerate(zip(injection_confs, signals, signals_std, objectives, 
                                                                    objectives_std, electrode_nums)):
        row = int(i / per_row)
        col = i % per_row
        ax = fig.add_subplot(g[row, col])
        
        #ax.set_axis_off()
        plot_primals(ax, signal, objective, el_nums, num_electrodes, std1=signal_std, std2=objective_std, fontsize = 5, label = False, name1 = name1, name2 = name2)
        
        if (selected_mask[i] == False):
            ax.set_title(f"Conf. {i}, E{injection[0]}-E{injection[1]}")
        else:
            ax.set_title(f"Conf. {i}, E{injection[0]}-E{injection[1]}", color = "red")
    
    path_iter_pdf = os.path.join(path, f"primal-{name}")
    path_iter_png = os.path.join(path, f"primal-{name}")
    if PDF:
        fig.savefig(f"{path_iter_pdf}.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
    if PNG:
        fig.savefig(f"{path_iter_png}.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
    plt.close(fig)

    if std:
        fig = plt.figure(figsize= (per_row * 8, num_rows * 4))

        g = gridspec.GridSpec(num_rows, per_row, figure = fig, wspace = 0.1, hspace=0.3)
        for i, (injection, signal, signal_std, objective, objective_std, el_nums) in enumerate(zip(injection_confs, signals, signals_std, objectives, 
                                                                        objectives_std, electrode_nums)):
            row = int(i / per_row)
            col = i % per_row
            ax = fig.add_subplot(g[row, col])
            
            #ax.set_axis_off()
            plot_diff_primal(ax, signal, objective, el_nums, num_electrodes, std1=signal_std, std2=objective_std, fontsize = 5)

            if (selected_mask[i] == False):
                ax.set_title(f"Conf. {i}, E{injection[0]}-E{injection[1]}")
            else:
                ax.set_title(f"Conf. {i}, E{injection[0]}-E{injection[1]}", color = "red")

        path_iter_pdf = os.path.join(path, f"primal-{name}-diff")
        path_iter_png = os.path.join(path, f"primal-{name}-diff")
        if PDF:
            fig.savefig(f"{path_iter_pdf}.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
        if PNG:
            fig.savefig(f"{path_iter_png}.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        plt.close(fig)



def plot_summary(loss_list, loss_reg_list, path, log = False, save_npy = SAVEDATA):
    losses = np.array(loss_list)
    losses_reg = np.array(loss_reg_list)
    if save_npy:
        np.save(os.path.join(path, "losses.npy"),losses)
        np.save(os.path.join(path, "losses_reg.npy"),losses_reg)

    fig, (ax1, ax2) = plt.subplots(1, 2, figsize = (16, 5))
    loss_all = losses.sum(axis = 1).squeeze()
    iters = np.arange(0, len(loss_all))
    ax1.plot(iters, loss_all)
    ax1.grid()
    ax1.set_title("Sum of All Setups")
    for i in range(losses.shape[1]):
        ax2.plot(iters, losses[:,i], label = f"Setup {i}")
    ax2.grid()
    #ax2.legend()
    if log:
        ax1.set_yscale("log")
        ax2.set_yscale("log")
    ax2.set_title("Across different setups")
    fig.suptitle("Evolution of the loss function")
    path_loss_pdf = os.path.join(path, "loss")
    path_loss_png = os.path.join(path, "loss")
    if log:
        if PDF:
            fig.savefig(f"{path_loss_pdf}-log-setups.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
        if PNG:
            fig.savefig(f"{path_loss_png}-log-setups.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
    else:
        if PDF:
            fig.savefig(f"{path_loss_pdf}-lin-setups.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
        if PNG:
            fig.savefig(f"{path_loss_png}-lin-setups.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
    plt.close(fig)
    
    if losses_reg.sum() > 0:
        # Now only plot the regularization loss.
        fig, ax = plt.subplots(1,1, figsize = (5,5))
        ax.plot(iters, losses_reg)
        ax.grid()
        ax.set_title("Regularization Loss")
        if log:
            ax.set_yscale("log")
            if PDF:
                fig.savefig(f"{path_loss_pdf}-log-reg.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
            if PNG:
                fig.savefig(f"{path_loss_png}-log-reg.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        else:
            if PDF:
                fig.savefig(f"{path_loss_pdf}-lin-reg.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
            if PNG:
                fig.savefig(f"{path_loss_png}-lin-reg.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        plt.close(fig)

        # Now only plot the regularization loss.
        fig, ax = plt.subplots(1,1, figsize = (5,5))
        ax.plot(iters, losses_reg + loss_all)
        ax.grid()
        ax.set_title("Loss")
        if log:
            ax.set_yscale("log")
            if PDF:
                fig.savefig(f"{path_loss_pdf}-log-all.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
            if PNG:
                fig.savefig(f"{path_loss_png}-log-all.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        else:
            if PDF:
                fig.savefig(f"{path_loss_pdf}-lin-all.pdf", bbox_inches='tight', pad_inches=0.04, dpi=300)
            if PNG:
                fig.savefig(f"{path_loss_png}-lin-all.png", bbox_inches='tight', pad_inches=0.04, dpi=dpi)
        plt.close(fig)

def create_animation(record, path, iternum, bbox, wos, max_range = None, wos_obj = None, resolution = [512, 512], 
                     opt_param = "diffusion.texture.tensor", fileset = None, out_val = None):

    name = "reconstruction" if max_range is None else "reconstruction-scaled"
    if (wos_obj is None) and (fileset is None):
        fig , (ax1, ax2) = plt.subplots(1,2, figsize = (10, 5))
    else:
        fig = plt.figure(figsize=(8, 6))
        g = gridspec.GridSpec(16, 20, figure = fig, wspace = 0.5, hspace=0.1)
        ax0 = fig.add_subplot(g[0:9,0:9])
        ax1 = fig.add_subplot(g[0:9,10:19])
        ax2 = fig.add_subplot(g[10:,:19])

    coeff_name = opt_param.split(".")[0]

    coeff = wos.input.get_coefficient(coeff_name)
    
    if fileset is not None:
        image_file = os.path.join(PATH ,f"eit-data/target_photos/fantom_{fileset}.jpg")
        image = plt.imread(image_file)
        ax0.imshow(image)
        ax0.set_axis_off()
        ax0.set_title("Objective")

    elif wos_obj is not None:
        coeff_obj = wos_obj.input.get_coefficient(coeff_name)

        points = create_image_points(bbox, resolution, spp=1, centered = True)
        vals = coeff_obj.get_value(points)
        if out_val is not None:
            mask = wos_obj.input.shape.inside_closed_surface_mask(points)
            vals = dr.select(mask, vals, out_val)
        obj_image, _ = create_image_from_result(vals, resolution)
        if max_range is None:
            plot_image(obj_image[0], ax0)
        else:
            plot_image(obj_image[0], ax0, input_range = max_range)
        ax0.set_title("Objective")
        wos_obj.input.shape.sketch(ax0, bbox, resolution, sketch_center = True)

    if max_range is None:
        maxval  = -np.inf
        minval = np.inf 
        for i in range(iternum):
            tensor = TensorXf(record[f"{opt_param}-{i}"]).numpy()
            maxval = max(maxval, np.max(tensor))
            minval = min(minval, np.min(tensor))
        max_range = [minval, maxval]
    coeff.tensor = TensorXf(record[f"{opt_param}-0"])
    coeff.update_texture()

    dirichlet_str = "dirichletpoints-0"
    dirichlet_points = None
    if dirichlet_str in record:
        dirichlet_points_ = record[f"dirichletpoints-0"]
        if dirichlet_points_.shape[0] > 0:
            dirichlet_points = Point2f(dirichlet_points_.T)
            dirichlet_points = point2sketch(dirichlet_points, bbox, resolution).numpy()
    
    im, s, line = start_animation(ax1, ax2, record, bbox, wos, dirichlet_points, coeff, resolution, max_range, out_val = out_val)
    update = lambda iteration : update_animation(wos, iteration, record, coeff, resolution, im, s, line, bbox, opt_param, out_val = out_val)

    ani = animation.FuncAnimation(fig=fig, func=update, frames=iternum+1, interval=30)
    writervideo = animation.FFMpegWriter(fps=25) 
    ani.save(filename=f"{path}/{name}.gif", writer="pillow")
    ani.save(f"{path}/{name}.mp4", writer=writervideo)

def start_animation(ax1, ax2, record, bbox, wos, in_boundary_points, coeff, resolution, max_range, out_val : float = None):
    points = create_image_points(bbox, resolution, spp = 4, centered = False)
    vals = coeff.get_value(points)
    if out_val is not None:
        mask = wos.input.shape.inside_closed_surface_mask(points)
        vals = dr.select(mask, vals, out_val)
    image, _ = create_image_from_result(vals, resolution)
    im = plot_image(image[0], ax1, input_range = max_range)
    wos.input.shape.sketch(ax1, bbox, resolution, sketch_center = True, sketch_in_boundaries = False)
    s = None
    if in_boundary_points is not None:
        s = ax1.scatter(in_boundary_points[0], in_boundary_points[1], s = 5, color = "red")
    
    ax1.set_title("Reconstruction")

    loss = record["loss"].sum(axis = 1).squeeze()
    loss_reg = record["loss-reg"]
    iters = np.arange(0, len(loss))
    ax2.plot(iters, loss + loss_reg, color = "grey", ls = "-.")
    line = ax2.plot(iters[0], loss[0], color = "red")[0]
    ax2.yaxis.tick_right()
    ax2.set_yscale("log")
    ax2.set_yscale("log")
    ax2.grid()
    ax2.set_ylabel("Loss")
    return im, s, line
    
def update_animation(wos, iteration, record, coeff, resolution, im, s, line, bbox, opt_param, out_val : float = None):
    coeff.tensor = TensorXf(record[f"{opt_param}-{iteration}"])
    coeff.update_texture()
    points = create_image_points(bbox, resolution, spp = 4, centered = False)
    vals = coeff.get_value(points)
    if out_val is not None:
        mask = wos.input.shape.inside_closed_surface_mask(points)
        vals = dr.select(mask, vals, out_val)
    image, _ = create_image_from_result(vals, resolution)
    im.set_data(image[0])
    if s is not None:
        dirichlet_points = record[f"dirichletpoints-{iteration}"]
        if dirichlet_points.shape[0] > 0:
            dirichlet_points = Point2f(dirichlet_points.T)
            dirichlet_points = point2sketch(dirichlet_points, bbox, resolution).numpy()
            s.set_offsets(dirichlet_points.T)

    loss = record["loss"].sum(axis = 1)[:iteration].squeeze()
    loss_reg = record["loss-reg"][:iteration]
    iters = np.arange(0, iteration)
    line.set_xdata(iters)
    line.set_ydata(loss + loss_reg)