MCPcopy Create free account
hub / github.com/PolymathicAI/AstroCLIP / PlotsCallback

Class PlotsCallback

astroclip/callbacks.py:57–103  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

55
56
57class PlotsCallback(Callback):
58 # TODO: Update with latest code
59 def __init__(self) -> None:
60 super().__init__()
61
62 def plot_spectrum(self, batch, output):
63 sample_id = 3
64
65 bs = len(batch["spectrum"])
66 sp_rec = batch["target"][:, 1:, 2:99].reshape(bs, -1)[sample_id]
67 in_rec = batch["input"][:, 1:, 2:99].reshape(bs, -1)[sample_id]
68 out_rec = output[:, 1:, 2:99].reshape(bs, -1)[sample_id]
69
70 # plot the moving average of the spectrum
71 win = 20
72
73 sp_rec = [sp_rec[i : i + win].mean().item() for i in range(0, len(sp_rec), win)]
74 in_rec = [in_rec[i : i + win].mean().item() for i in range(0, len(in_rec), win)]
75 out_rec = [
76 out_rec[i : i + win].mean().item() for i in range(0, len(out_rec), win)
77 ]
78
79 fig = plt.figure()
80 plt.plot(sp_rec, label="original")
81 plt.plot(in_rec, label="dropped")
82 plt.plot(out_rec, label="reconstructed")
83 plt.legend()
84 return fig
85
86 def on_validation_batch_start(
87 self,
88 trainer: Trainer,
89 pl_module: LightningModule,
90 batch: Any,
91 batch_idx: int,
92 dataloader_idx: int = 0,
93 ) -> None:
94 if batch_idx == 0:
95 output = trainer.model(batch["input"])
96 fig = self.plot_spectrum(batch, output)
97 for logger in trainer.loggers:
98 # Check WandbLogger and Enabled
99 if issubclass(type(logger), WandbLogger) and not issubclass(
100 type(logger.experiment), wandb.sdk.lib.disabled.RunDisabled
101 ):
102 logger: WandbLogger = logger
103 logger.experiment.log({f"plot/{pl_module.current_epoch:03d}": fig})

Callers

nothing calls this directly

Calls

no outgoing calls

Tested by

no test coverage detected