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
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
from types import NoneType
from typing import Any, List, Mapping

import lightning.pytorch as pl
import matplotlib.pyplot as plt
import numpy as np
import plotly.express as plotly_express
import plotly.graph_objects as go
import torch
import wandb
from functorch.dim import tree_map
from lightning.pytorch.callbacks import Callback

import pdeinvbench.utils.pytorch_utils as ptu
from pdeinvbench.utils.logging_utils import (
    collect_loss_dicts,
)
from pdeinvbench.utils.types import PDE, TypeBatch1D, TypeBatch2D


class PDEParamErrorPlottingCallback(Callback):
    """
    Logs a set of errors stratified based on PDE parameter value.
    """

    def __init__(self):
        self.validation_step_loss_dicts = []
        """
        Loss dicts for validation and autoregressive validation. Each element is a tuple of (losses, pde_params).
        PDE_params comes directly from the dataloader.
        Loss: Dict[str, torch.Tensor] with keys (shape) 
        'data_loss' (B), 'residual_loss' (B), 'loss' (), 'data_loss_per_batch_element' (B), 'residual_per_batch_element' (B).
        """
        self.pde = None  # type: ignore
        self.params_to_predict = []

    def on_validation_epoch_start(
        self, trainer: pl.Trainer, pl_module: pl.LightningModule
    ):
        # Clear memory of loss dicts
        self.validation_step_loss_dicts = []
        self.pde = pl_module.pde
        if self.pde == PDE.DarcyFlow2D:
            self.params_to_predict = ["index"]
        else:
            self.params_to_predict = pl_module.params_to_predict

    def on_validation_batch_end(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        outputs: Mapping[str, torch.Tensor],
        batch: TypeBatch1D | TypeBatch2D,
        batch_idx: int,
        dataloader_idx: int = 0,
    ):
        # Convert to numpy arrays
        collect_loss_dicts(
            outputs,
            batch,
            "residual_per_batch_element",
            self.validation_step_loss_dicts,
        )

    def on_test_batch_end(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        outputs: Mapping[str, torch.Tensor],
        batch: TypeBatch1D | TypeBatch2D,
        batch_idx: int,
        dataloader_idx: int = 0,
    ):
        self.on_validation_batch_end(
            trainer, pl_module, outputs, batch, batch_idx)

    def generate_pde_parameter_histogram(self, loss_tuples):
        """
        Generates a histogram of PDE parameter values vs loss
        """
        if len(loss_tuples) == 0:
            return None
        if self.pde == PDE.DarcyFlow2D:
            parameter_keys = ["index"]
        else:
            parameter_keys = loss_tuples[0][1].keys()
        plots = {}
        for parameter in parameter_keys:
            # Num batches x Batch size
            parameter_values = [
                ptu.torch_to_numpy(loss_tuple[1][parameter].ravel())
                for loss_tuple in loss_tuples
            ]
            parameter_values = np.concatenate(parameter_values, axis=0)

            residuals = [
                ptu.torch_to_numpy(loss_tuple[0]["residual_per_batch_element"])
                for loss_tuple in loss_tuples
            ]
            residuals = np.concatenate(residuals, axis=0)

            key_name = "param_loss_per_batch_element"

            data_or_param_loss = [loss_tuple[0][key_name]
                                  for loss_tuple in loss_tuples]

            if len(data_or_param_loss[0].shape) != 0:
                data_or_param_loss = np.concatenate(data_or_param_loss, axis=0)

            residual_fig = plotly_express.density_heatmap(
                x=parameter_values,
                y=residuals,
                nbinsx=20,
                nbinsy=20,
                title=f"Residual vs. {parameter}",
            )
            residual_fig.update_layout(
                xaxis_title=f"{parameter} Values",
                yaxis_title="Residual",
                title_x=0.5,
                margin_t=40,
            )
            title = "Parameter"
            data_or_param_loss_fig = plotly_express.density_heatmap(
                x=parameter_values,
                y=data_or_param_loss,
                nbinsx=20,
                nbinsy=20,
                title=f"{title} Loss vs. {parameter}",
            )
            data_or_param_loss_fig.update_layout(
                xaxis_title=f"{title} Loss (MSE)",
                yaxis_title=f"{parameter} Values",
                title_x=0.5,
                margin_t=40,
            )
            plots[parameter] = [residual_fig, data_or_param_loss_fig]
        return plots

    def on_test_epoch_start(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        self.on_validation_epoch_start(trainer, pl_module)

    def log_plots(self, prefix: str):
        plots = self.generate_pde_parameter_histogram(
            self.validation_step_loss_dicts)
        if plots is not None:
            for parameter, plots in plots.items():
                residual_fig, data_loss_fig = plots
                wandb.log(
                    {
                        f"{prefix}/residual_vs_{parameter}": residual_fig,
                        f"{prefix}/data_loss_vs_{parameter}": data_loss_fig,
                    }
                )
        plt.close()

    def log_parameter_predictions_table(self, loss_tuples, prefix: str = "test"):
        if len(loss_tuples) == 0:
            return None
        if self.pde == PDE.DarcyFlow2D:
            parameter_keys = ["index"]
        else:
            parameter_keys = loss_tuples[0][1].keys()
        plots = {}
        columns = ["ic_index", "true_parameters",
                   "predicted_parameters", "param_loss"]
        for parameter in parameter_keys:
            # Num batches x Batch size
            true_parameters = [
                ptu.torch_to_numpy(loss_tuple[1][parameter].ravel())
                for loss_tuple in loss_tuples
            ]
            true_parameters = np.concatenate(true_parameters, axis=0)

            residuals = [
                ptu.torch_to_numpy(loss_tuple[0]["residual_per_batch_element"])
                for loss_tuple in loss_tuples
            ]
            residuals = np.concatenate(residuals, axis=0)

            key_name = "param_loss_per_batch_element"

            data_or_param_loss = [
                ptu.torch_to_numpy(loss_tuple[0][key_name])
                for loss_tuple in loss_tuples
            ]

            if len(data_or_param_loss[0].shape) != 0:
                data_or_param_loss = np.concatenate(data_or_param_loss, axis=0)

            ic_index = [
                ptu.torch_to_numpy(loss_tuple[2]).ravel() for loss_tuple in loss_tuples
            ]
            timestamps = [
                ptu.torch_to_numpy(loss_tuple[3]).ravel() for loss_tuple in loss_tuples
            ]

            ic_index = np.concatenate(ic_index, axis=0)
            timestamps = np.concatenate(timestamps, axis=0)

            predicted_parameters = [
                ptu.torch_to_numpy(
                    loss_tuple[0]["predictions"][parameter]).ravel()
                for loss_tuple in loss_tuples
            ]

            predicted_parameters = np.concatenate(predicted_parameters, axis=0)

            data = [
                [
                    ic_index[i],
                    timestamps[i],
                    true_parameters[i],
                    predicted_parameters[i],
                    data_or_param_loss[i],
                ]
                for i in range(len(ic_index))
            ]
            table = wandb.Table(
                data=data,
                columns=[
                    "ic_index",
                    "timestamps",
                    "true_parameters",
                    "predicted_parameters",
                    "param_loss",
                ],
            )
            wandb.log(
                {f"{prefix}/parameter_predictions_table_{parameter}": table})

    def on_validation_epoch_end(
        self, trainer: pl.Trainer, pl_module: pl.LightningModule
    ):
        # Plot error stratified by PDE parameter value
        self.log_plots("validation")
        # Clear caches
        self.validation_step_loss_dicts = []

    def on_test_epoch_start(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        self.on_validation_epoch_start(trainer, pl_module)

    def on_test_epoch_end(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        self.log_plots("test")
        self.log_parameter_predictions_table(
            self.validation_step_loss_dicts, "test")
        # Clear caches
        self.validation_step_loss_dicts = []


class PDEParamErrorTestTimeTailoringCallback(PDEParamErrorPlottingCallback):
    """
    Logs errors before and after tailoring, stratified by PDE parameter value.
    """

    def __init__(self):
        super().__init__()
        """
        Loss dicts for test time tailoring. Each element is a tuple of (losses, pde_params).
        PDE_params comes directly from the dataloader.
        Loss: Dict[str, torch.Tensor] with keys (shape) 
        'data_loss' (B), 'residual_loss' (B), 'loss' (), 'data_loss_per_batch_element' (B), 'residual_per_batch_element' (B).
        """
        self.pre_tailored_loss_dicts = []
        self.post_tailored_loss_dicts = []
        self.params_to_predict = []
        self.pde = None  # type: ignore

    def on_test_epoch_start(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        self.pde = pl_module.pde
        if self.pde == PDE.DarcyFlow2D:
            # 'Coeff' is a 2D parameter field, index corresponds to the filename of the parameter in the 2D field
            self.params_to_predict = ["index"]
        else:
            self.params_to_predict = pl_module.params_to_predict

    def on_test_batch_end(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        outputs: Mapping[str, torch.Tensor],
        batch: TypeBatch1D | TypeBatch2D,
        batch_idx: int,
        dataloader_idx: int = 0,
    ):
        if (
            not hasattr(pl_module, "num_tailoring_steps")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return
        if "pre_tailored_metrics" in outputs:

            collect_loss_dicts(
                outputs["pre_tailored_metrics"],
                batch,
                "param_loss_per_batch_element",
                self.pre_tailored_loss_dicts,
            )
        collect_loss_dicts(
            {key: v for key, v in outputs.items() if key != "pre_tailored_metrics"},
            batch,
            "param_loss_per_batch_element",
            self.post_tailored_loss_dicts,
        )

    def log_tables(self):
        # take the param_loss_per_batch_element for the pre and post tailored metrics

        pre_tailored_data_loss = [
            loss_tuple[0]["param_loss_per_batch_element"]
            for loss_tuple in self.pre_tailored_loss_dicts
        ]
        post_tailored_data_loss = [
            loss_tuple[0]["param_loss_per_batch_element"]
            for loss_tuple in self.post_tailored_loss_dicts
        ]

        parameter_values = [
            ptu.torch_to_numpy(
                loss_tuple[1][self.params_to_predict[0]].ravel())
            for loss_tuple in self.pre_tailored_loss_dicts
        ]

        pre_tailored_parameter_values = [
            ptu.torch_to_numpy(
                loss_tuple[0]["predictions"][self.params_to_predict[0]].ravel()
            )
            for loss_tuple in self.pre_tailored_loss_dicts
        ]
        post_tailored_parameter_values = [
            ptu.torch_to_numpy(
                loss_tuple[0]["predictions"][self.params_to_predict[0]].ravel()
            )
            for loss_tuple in self.post_tailored_loss_dicts
        ]

        parameter_values = np.concatenate(parameter_values, axis=0)
        pre_tailored_parameter_values = np.concatenate(
            pre_tailored_parameter_values, axis=0
        )
        post_tailored_parameter_values = np.concatenate(
            post_tailored_parameter_values, axis=0
        )

        pre_tailored_data_loss = np.concatenate(pre_tailored_data_loss, axis=0)
        post_tailored_data_loss = np.concatenate(
            post_tailored_data_loss, axis=0)

        # log table containing paramter value, pre tailored data loss, post tailored data loss
        data = [
            [
                parameter_values[i],
                pre_tailored_data_loss[i],
                post_tailored_data_loss[i],
                pre_tailored_parameter_values[i],
                post_tailored_parameter_values[i],
            ]
            for i in range(len(parameter_values))
        ]
        table = wandb.Table(
            data=data,
            columns=[
                "parameter_value",
                "pre_tailored_data_loss",
                "post_tailored_data_loss",
                "pre_tailored_parameter_value",
                "post_tailored_parameter_value",
            ],
        )
        wandb.log({"tailoring_data_loss_table": table})

    def on_test_epoch_end(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        if (
            not hasattr(pl_module, "num_tailoring_steps")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return
        self.log_tables()
        self.pre_tailored_loss_dicts = []
        self.post_tailored_loss_dicts = []


class TailoringTimingMetricsCallback(Callback):
    """
    Logs the timing metrics for the tailoring step.
    """

    def __init__(self):
        self.tailoring_timing_metrics = {}

    def on_test_batch_end(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        outputs: Mapping[str, torch.Tensor],
        batch: TypeBatch1D | TypeBatch2D,
        batch_idx: int,
        dataloader_idx: int = 0,
    ):
        if (
            not hasattr(pl_module, "tailoring_optimizer")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return

        if "tailoring_timing_metrics" in outputs:
            self.tailoring_timing_metrics[dataloader_idx] = outputs[
                "tailoring_timing_metrics"
            ]

    def on_test_epoch_end(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        for dataloader_idx in self.tailoring_timing_metrics:
            wandb.log(
                {
                    f"tailoring_timing_metrics_dataloader_{dataloader_idx}": self.tailoring_timing_metrics[
                        dataloader_idx
                    ]
                }
            )


class InverseErrorByTailoringStepCallback(Callback):
    """
    Helper callback that plots the error by tailoring step. On the Y-axis is the metric and the X-axis is the tailoring step.
    Uses plotly to generate the plot and plots to W&B.
    This is specifically for PINO and tailoring.
    """

    def __init__(self):
        # Required class variables - reset on val epoch start
        self.errors_by_tailor_step = {}

    def on_test_epoch_start(
        self, trainer: pl.Trainer, pl_module: pl.LightningModule
    ) -> None:
        if (
            not hasattr(pl_module, "tailoring_optimizer")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return
        self.errors_by_tailor_step = {}

    def generate_plots(self, pl_module, loader_idx=0):
        """
        Generates the plots for the data and residual error by tailoring step.
        """
        num_tailoring_steps = pl_module.num_tailoring_steps
        metric_plots = {}

        for error_metric in self.errors_by_tailor_step[loader_idx]:
            metric_data_by_tailor_step = np.asarray(
                self.errors_by_tailor_step[loader_idx][error_metric]
            )

            # Calculate mean across tailoring steps
            mean_metric_data_by_tailor_step = np.mean(
                metric_data_by_tailor_step, axis=0
            )

            # Calculate y-axis bounds with some padding (e.g., 5% of the range)
            y_min = np.min(metric_data_by_tailor_step)
            y_max = np.max(metric_data_by_tailor_step)
            y_range = y_max - y_min
            y_axis_min = y_min - 0.05 * y_range  # Add 5% padding below min
            y_axis_max = y_max + 0.05 * y_range  # Add 5% padding above max

            # Create data for the table
            data = [
                [x, y]
                for (x, y) in zip(
                    list(range(num_tailoring_steps)
                         ), mean_metric_data_by_tailor_step
                )
            ]
            table = wandb.Table(
                data=data, columns=["tailor_steps", f"mean_{error_metric}"]
            )

            # Create a Plotly figure for custom y-axis bounds
            fig = go.Figure()
            fig.add_trace(
                go.Scatter(
                    x=list(range(num_tailoring_steps)),
                    y=mean_metric_data_by_tailor_step,
                    mode="lines",
                    name=f"Mean {error_metric}",
                )
            )
            fig.update_layout(
                title=f"Tailoring Steps vs Mean {error_metric}",
                xaxis_title="Tailoring Steps",
                yaxis_title=f"Mean {error_metric}",
                # Set y-axis bounds
                yaxis=dict(range=[y_axis_min, y_axis_max]),
            )

            # Log the Plotly figure to WandB
            metric_plots[error_metric] = wandb.Plotly(fig)
        return metric_plots

    def on_test_batch_end(
        self,
        trainer: pl.Trainer,
        pl_module: pl.LightningModule,
        outputs: Mapping[str, torch.Tensor],
        batch: TypeBatch1D | TypeBatch2D,
        batch_idx: int,
        dataloader_idx: int = 0,
    ):
        """
        After each batch, we accumulate the metric for each tailoring step.
        """
        if (
            not hasattr(pl_module, "tailoring_optimizer")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return

        tailoring_metrics = outputs["tailoring_metrics"]
        if dataloader_idx not in self.errors_by_tailor_step:
            self.errors_by_tailor_step[dataloader_idx] = {}
        current_dataloader = trainer.test_dataloaders[dataloader_idx]

        for metric, metric_data in tailoring_metrics.items():
            if metric not in self.errors_by_tailor_step[dataloader_idx]:
                self.errors_by_tailor_step[dataloader_idx][metric] = []
            if "per_batch_element" in metric and pl_module.tailor_per_batch:
                current_batch_size = metric_data[0].shape[0]
                elements_to_add = pl_module.batch_size - current_batch_size
                if current_batch_size != pl_module.batch_size:
                    for tailoring_step in range(pl_module.num_tailoring_steps):
                        step_ouput = metric_data[tailoring_step]

                        # Get the last element of the tensor
                        last_element = step_ouput[-1]

                        # Create a tensor with repeated last elements
                        repeated_elements = (
                            last_element.repeat(elements_to_add, 1)
                            if len(step_ouput.shape) > 1
                            else last_element.repeat(elements_to_add)
                        )

                        # Concatenate the original tensor with the repeated elements
                        metric_data[tailoring_step] = torch.cat(
                            [step_ouput, repeated_elements], dim=0
                        )

            self.errors_by_tailor_step[dataloader_idx][metric].append(
                metric_data)

    def on_test_epoch_end(self, trainer: pl.Trainer, pl_module: pl.LightningModule):
        if (
            not hasattr(pl_module, "tailoring_optimizer")
            or pl_module.tailoring_optimizer is None
            or pl_module.num_tailoring_steps == 0
        ):
            return

        """
        After each epoch, we plot the metric by tailoring step.
        """

        for dataloader_idx in self.errors_by_tailor_step:
            tailoring_figures = self.generate_plots(pl_module, dataloader_idx)
            to_log = {}
            for tailoring_metric, err_fig in tailoring_figures.items():
                to_log[
                    f"tailoring_step_plots_dataloader_{dataloader_idx}/{tailoring_metric}"
                ] = err_fig

            wandb.log(to_log)
            plt.close()