-
-
Notifications
You must be signed in to change notification settings - Fork 17
/
Copy pathutils.py
368 lines (287 loc) · 11.4 KB
/
utils.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
"""Utils"""
import logging
import warnings
from collections.abc import Sequence
from typing import Optional
import lightning.pytorch as pl
import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
import pylab
import rich.syntax
import rich.tree
import xarray as xr
from lightning.pytorch.loggers import Logger
from lightning.pytorch.utilities import rank_zero_only
from ocf_datapipes.batch import BatchKey
from ocf_datapipes.utils import Location
from ocf_datapipes.utils.geospatial import osgb_to_lon_lat
from omegaconf import DictConfig, OmegaConf
def get_logger(name=__name__, level=logging.INFO) -> logging.Logger:
"""Initializes multi-GPU-friendly python logger."""
logger = logging.getLogger(name)
logger.setLevel(level)
# this ensures all logging levels get marked with the rank zero decorator
# otherwise logs would get multiplied for each GPU process in multi-GPU setup
for level in (
"debug",
"info",
"warning",
"error",
"exception",
"fatal",
"critical",
):
setattr(logger, level, rank_zero_only(getattr(logger, level)))
return logger
class GSPLocationLookup:
"""Query object for GSP location from GSP ID"""
def __init__(self, x_osgb: xr.DataArray, y_osgb: xr.DataArray):
"""Query object for GSP location from GSP ID
Args:
x_osgb: DataArray of the OSGB x-coordinate for any given GSP ID
y_osgb: DataArray of the OSGB y-coordinate for any given GSP ID
"""
self.x_osgb = x_osgb
self.y_osgb = y_osgb
def __call__(self, gsp_id: int) -> Location:
"""Returns the locations for the input GSP IDs.
Args:
gsp_id: Integer ID of the GSP
"""
return Location(
x=self.x_osgb.sel(gsp_id=gsp_id).item(),
y=self.y_osgb.sel(gsp_id=gsp_id).item(),
id=gsp_id,
)
class SiteLocationLookup:
"""Query object for site location from site ID"""
def __init__(self, long: xr.DataArray, lat: xr.DataArray):
"""Query object for site location from site ID
Args:
long: DataArray of the longitude coordinates for any given site ID
lat: DataArray of the latitude coordinates for any given site ID
"""
self.longitude = long
self.latitude = lat
def __call__(self, site_id: int) -> Location:
"""Returns the locations for the input site IDs.
Args:
site_id: Integer ID of the site
"""
return Location(
coordinate_system="lon_lat",
x=self.longitude.sel(pv_system_id=site_id).item(),
y=self.latitude.sel(pv_system_id=site_id).item(),
id=site_id,
)
def extras(config: DictConfig) -> None:
"""A couple of optional utilities.
Controlled by main config file:
- disabling warnings
- easier access to debug mode
- forcing debug friendly configuration
Modifies DictConfig in place.
Args:
config (DictConfig): Configuration composed by Hydra.
"""
log = get_logger()
# enable adding new keys to config
OmegaConf.set_struct(config, False)
# disable python warnings if <config.ignore_warnings=True>
if config.get("ignore_warnings"):
log.info("Disabling python warnings! <config.ignore_warnings=True>")
warnings.filterwarnings("ignore")
# set <config.trainer.fast_dev_run=True> if <config.debug=True>
if config.get("debug"):
log.info("Running in debug mode! <config.debug=True>")
config.trainer.fast_dev_run = True
# force debugger friendly configuration if <config.trainer.fast_dev_run=True>
if config.trainer.get("fast_dev_run"):
log.info("Forcing debugger friendly configuration! <config.trainer.fast_dev_run=True>")
# Debuggers don't like GPUs or multiprocessing
if config.trainer.get("gpus"):
config.trainer.gpus = 0
if config.datamodule.get("pin_memory"):
config.datamodule.pin_memory = False
if config.datamodule.get("num_workers"):
config.datamodule.num_workers = 0
# disable adding new keys to config
OmegaConf.set_struct(config, True)
@rank_zero_only
def print_config(
config: DictConfig,
fields: Sequence[str] = (
"trainer",
"model",
"datamodule",
"callbacks",
"logger",
"seed",
),
resolve: bool = True,
) -> None:
"""Prints content of DictConfig using Rich library and its tree structure.
Args:
config (DictConfig): Configuration composed by Hydra.
fields (Sequence[str], optional): Determines which main fields from config will
be printed and in what order.
resolve (bool, optional): Whether to resolve reference fields of DictConfig.
"""
style = "dim"
tree = rich.tree.Tree("CONFIG", style=style, guide_style=style)
for field in fields:
branch = tree.add(field, style=style, guide_style=style)
config_section = config.get(field)
branch_content = str(config_section)
if isinstance(config_section, DictConfig):
branch_content = OmegaConf.to_yaml(config_section, resolve=resolve)
branch.add(rich.syntax.Syntax(branch_content, "yaml"))
rich.print(tree)
with open("config_tree.txt", "w") as fp:
rich.print(tree, file=fp)
def empty(*args, **kwargs):
"""Returns nothing"""
pass
@rank_zero_only
def log_hyperparameters(
config: DictConfig,
model: pl.LightningModule,
datamodule: pl.LightningDataModule,
trainer: pl.Trainer,
callbacks: list[pl.Callback],
logger: list[Logger],
) -> None:
"""This method controls which parameters from Hydra config are saved by Lightning loggers.
Additionaly saves:
- number of trainable model parameters
"""
hparams = {}
# choose which parts of hydra config will be saved to loggers
hparams["trainer"] = config["trainer"]
hparams["model"] = config["model"]
hparams["datamodule"] = config["datamodule"]
if "seed" in config:
hparams["seed"] = config["seed"]
if "callbacks" in config:
hparams["callbacks"] = config["callbacks"]
# save number of model parameters
hparams["model/params_total"] = sum(p.numel() for p in model.parameters())
hparams["model/params_trainable"] = sum(
p.numel() for p in model.parameters() if p.requires_grad
)
hparams["model/params_not_trainable"] = sum(
p.numel() for p in model.parameters() if not p.requires_grad
)
# send hparams to all loggers
trainer.logger.log_hyperparams(hparams)
# disable logging any more hyperparameters for all loggers
# this is just a trick to prevent trainer from logging hparams of model,
# since we already did that above
trainer.logger.log_hyperparams = empty
def finish(
config: DictConfig,
model: pl.LightningModule,
datamodule: pl.LightningDataModule,
trainer: pl.Trainer,
callbacks: list[pl.Callback],
loggers: list[Logger],
) -> None:
"""Makes sure everything closed properly."""
# without this sweeps with wandb logger might crash!
if any([isinstance(logger, pl.loggers.wandb.WandbLogger) for logger in loggers]):
import wandb
wandb.finish()
def plot_batch_forecasts(
batch,
y_hat,
batch_idx=None,
quantiles=None,
key_to_plot: str = "gsp",
timesteps_to_plot: Optional[list[int]] = None,
):
"""Plot a batch of data and the forecast from that batch"""
def _get_numpy(key):
return batch[key].cpu().numpy().squeeze()
y_key = BatchKey[f"{key_to_plot}"]
y_id_key = BatchKey[f"{key_to_plot}_id"]
BatchKey[f"{key_to_plot}_t0_idx"]
time_utc_key = BatchKey[f"{key_to_plot}_time_utc"]
y = batch[y_key][:, :, 0].cpu().numpy() # Select the one it is trained on
y_hat = y_hat.cpu().numpy()
# Select between the timesteps in timesteps to plot
plotting_name = key_to_plot.upper()
gsp_ids = batch[y_id_key].cpu().numpy().squeeze()
times_utc = batch[time_utc_key].cpu().numpy().squeeze().astype("datetime64[s]")
times_utc = [pd.to_datetime(t) for t in times_utc]
if timesteps_to_plot is not None:
y = y[:, timesteps_to_plot[0] : timesteps_to_plot[1]]
y_hat = y_hat[:, timesteps_to_plot[0] : timesteps_to_plot[1]]
times_utc = [t[timesteps_to_plot[0] : timesteps_to_plot[1]] for t in times_utc]
batch_size = y.shape[0]
fig, axes = plt.subplots(4, 4, figsize=(16, 16))
for i, ax in enumerate(axes.ravel()):
if i >= batch_size:
ax.axis("off")
continue
ax.plot(times_utc[i], y[i], marker=".", color="k", label=r"$y$")
if quantiles is None:
ax.plot(
times_utc[i][-len(y_hat[i]) :], y_hat[i], marker=".", color="r", label=r"$\hat{y}$"
)
else:
cm = pylab.get_cmap("twilight")
for nq, q in enumerate(quantiles):
ax.plot(
times_utc[i][-len(y_hat[i]) :],
y_hat[i, :, nq],
color=cm(q),
label=r"$\hat{y}$" + f"({q})",
alpha=0.7,
)
ax.set_title(f"ID: {gsp_ids[i]} | {times_utc[i][0].date()}", fontsize="small")
xticks = [t for t in times_utc[i] if t.minute == 0][::2]
ax.set_xticks(ticks=xticks, labels=[f"{t.hour:02}" for t in xticks], rotation=90)
ax.grid()
axes[0, 0].legend(loc="best")
for ax in axes[-1, :]:
ax.set_xlabel("Time (hour of day)")
if batch_idx is not None:
title = f"Normed {plotting_name} output : batch_idx={batch_idx}"
else:
title = f"Normed {plotting_name} output"
plt.suptitle(title)
plt.tight_layout()
return fig
def construct_ocf_ml_metrics_batch_df(batch, y, y_hat):
"""Helper function tot construct DataFrame for ocf_ml_metrics"""
def _repeat(x):
return np.repeat(x.squeeze(), n_times)
def _get_numpy(key):
return batch[key].cpu().numpy().squeeze()
t0_idx = batch[BatchKey.gsp_t0_idx]
times_utc = _get_numpy(BatchKey.gsp_time_utc)
n_times = len(times_utc[0]) - t0_idx - 1
y_osgb_centre = _get_numpy(BatchKey.gsp_y_osgb)
x_osgb_centre = _get_numpy(BatchKey.gsp_x_osgb)
longitude, latitude = osgb_to_lon_lat(x=x_osgb_centre, y=y_osgb_centre)
# Store df columns in dict
df_dict = {}
# Repeat these features for each forecast time
df_dict["latitude"] = _repeat(latitude)
df_dict["longitude"] = _repeat(longitude)
df_dict["id"] = _repeat(_get_numpy(BatchKey.gsp_id))
df_dict["t0_datetime_utc"] = _repeat(times_utc[:, t0_idx]).astype("datetime64[s]")
df_dict["capacity_mwp"] = _repeat(_get_numpy(BatchKey.gsp_capacity_megawatt_power))
# TODO: Some (10%) of these values are NaN -> 0 for time t0 for pvnet pipeline
# Better to search for last non-nan (non-zero)?
df_dict["t0_actual_pv_outturn_mw"] = _repeat(
(_get_numpy(BatchKey.gsp_capacity_megawatt_power)[:, None] * _get_numpy(BatchKey.gsp))[
:, t0_idx
]
)
# Flatten the forecasts times to 1D
df_dict["target_datetime_utc"] = times_utc[:, t0_idx + 1 :].flatten().astype("datetime64[s]")
df_dict["actual_pv_outturn_mw"] = y.cpu().numpy().flatten() * df_dict["capacity_mwp"]
df_dict["forecast_pv_outturn_mw"] = y_hat.cpu().numpy().flatten() * df_dict["capacity_mwp"]
return pd.DataFrame(df_dict)