-
-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathconftest.py
303 lines (242 loc) · 8.13 KB
/
conftest.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
import os
import glob
import tempfile
import pytest
import pandas as pd
import numpy as np
import xarray as xr
import torch
import hydra
from datetime import timedelta
import pvnet
from pvnet.data import DataModule, SiteDataModule
import pvnet.models.multimodal.encoders.encoders3d
import pvnet.models.multimodal.linear_networks.networks
import pvnet.models.multimodal.site_encoders.encoders
from pvnet.models.multimodal.multimodal import Model
xr.set_options(keep_attrs=True)
def time_before_present(dt: timedelta):
return pd.Timestamp.now(tz=None) - dt
@pytest.fixture
def nwp_data():
# Load dataset which only contains coordinates, but no data
ds = xr.open_zarr(
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/sample_data/nwp_shell.zarr"
)
# Last init time was at least 2 hours ago and hour to 3-hour interval
t0_datetime_utc = time_before_present(timedelta(hours=2)).floor(timedelta(hours=3))
ds.init_time.values[:] = pd.date_range(
t0_datetime_utc - timedelta(hours=3 * (len(ds.init_time) - 1)),
t0_datetime_utc,
freq=timedelta(hours=3),
)
# This is important to avoid saving errors
for v in list(ds.coords.keys()):
if ds.coords[v].dtype == object:
ds[v].encoding.clear()
for v in list(ds.variables.keys()):
if ds[v].dtype == object:
ds[v].encoding.clear()
# Add data to dataset
ds["UKV"] = xr.DataArray(
np.zeros([len(ds[c]) for c in ds.coords]),
coords=ds.coords,
)
# Add stored attributes to DataArray
ds.UKV.attrs = ds.attrs["_data_attrs"]
del ds.attrs["_data_attrs"]
return ds
@pytest.fixture()
def sat_data():
# Load dataset which only contains coordinates, but no data
ds = xr.open_zarr(
f"{os.path.dirname(os.path.abspath(__file__))}/test_data/sample_data/non_hrv_shell.zarr"
)
# Change times so they lead up to present. Delayed by at most 1 hour
t0_datetime_utc = time_before_present(timedelta(minutes=0)).floor(timedelta(minutes=30))
t0_datetime_utc = t0_datetime_utc - timedelta(minutes=30)
ds.time.values[:] = pd.date_range(
t0_datetime_utc - timedelta(minutes=5 * (len(ds.time) - 1)),
t0_datetime_utc,
freq=timedelta(minutes=5),
)
# Add data to dataset
ds["data"] = xr.DataArray(
np.zeros([len(ds[c]) for c in ds.coords]),
coords=ds.coords,
)
# Add stored attributes to DataArray
ds.data.attrs = ds.attrs["_data_attrs"]
del ds.attrs["_data_attrs"]
return ds
@pytest.fixture()
def sample_train_val_datamodule():
# duplicate the sample batcnes for more training/val data
n_duplicates = 10
with tempfile.TemporaryDirectory() as tmpdirname:
os.makedirs(f"{tmpdirname}/train")
os.makedirs(f"{tmpdirname}/val")
file_n = 0
for file_n, file in enumerate(
glob.glob("tests/test_data/presaved_samples_uk_regional/train/*.pt")
):
sample = torch.load(file)
for i in range(n_duplicates):
# Save fopr both train and val
torch.save(sample, f"{tmpdirname}/train/{file_n:06}.pt")
torch.save(sample, f"{tmpdirname}/val/{file_n:06}.pt")
dm = DataModule(
configuration=None,
sample_dir=f"{tmpdirname}",
batch_size=2,
num_workers=0,
prefetch_factor=None,
train_period=[None, None],
val_period=[None, None],
)
yield dm
@pytest.fixture()
def sample_datamodule():
dm = DataModule(
sample_dir="tests/test_data/presaved_samples_uk_regional",
configuration=None,
batch_size=2,
num_workers=0,
prefetch_factor=None,
train_period=[None, None],
val_period=[None, None],
)
return dm
@pytest.fixture()
def sample_batch(sample_datamodule):
batch = next(iter(sample_datamodule.train_dataloader()))
return batch
@pytest.fixture()
def sample_satellite_batch(sample_batch):
sat_image = sample_batch["satellite_actual"]
return torch.swapaxes(sat_image, 1, 2)
@pytest.fixture()
def sample_pv_batch():
# TODO: Once PV site inputs are available from ocf-data-sampler UK regional remove these
# old batches. For now we use the old batches to test the site encoder models
return torch.load("tests/test_data/presaved_batches/train/000000.pt")
@pytest.fixture()
def sample_site_batch():
dm = SiteDataModule(
configuration=None,
batch_size=2,
num_workers=0,
prefetch_factor=None,
train_period=[None, None],
val_period=[None, None],
sample_dir="tests/test_data/presaved_samples_site",
)
batch = next(iter(dm.train_dataloader()))
return batch
@pytest.fixture()
def model_minutes_kwargs():
kwargs = dict(
forecast_minutes=480,
history_minutes=120,
)
return kwargs
@pytest.fixture()
def encoder_model_kwargs():
# Used to test encoder model on satellite data
kwargs = dict(
sequence_length=7, # 30 minutes of 5 minutely satellite data = 7 time steps
image_size_pixels=24,
in_channels=11,
out_features=128,
)
return kwargs
@pytest.fixture()
def site_encoder_model_kwargs():
# Used to test site encoder model on PV data
kwargs = dict(
sequence_length=180 // 5 + 1,
num_sites=349,
out_features=128,
)
return kwargs
@pytest.fixture()
def site_encoder_model_kwargs_dsampler():
# Used to test site encoder model on PV data
kwargs = dict(
sequence_length=60 // 15 - 1, num_sites=1, out_features=128, target_key_to_use="site"
)
return kwargs
@pytest.fixture()
def site_encoder_sensor_model_kwargs():
# Used to test site encoder model on PV data
kwargs = dict(
sequence_length=180 // 5 + 1,
num_sites=26,
out_features=128,
num_channels=23,
target_key_to_use="wind",
input_key_to_use="sensor",
)
return kwargs
@pytest.fixture()
def raw_multimodal_model_kwargs(model_minutes_kwargs):
kwargs = dict(
sat_encoder=dict(
_target_=pvnet.models.multimodal.encoders.encoders3d.DefaultPVNet,
_partial_=True,
in_channels=11,
out_features=128,
number_of_conv3d_layers=6,
conv3d_channels=32,
image_size_pixels=24,
),
nwp_encoders_dict={
"ukv": dict(
_target_=pvnet.models.multimodal.encoders.encoders3d.DefaultPVNet,
_partial_=True,
in_channels=11,
out_features=128,
number_of_conv3d_layers=6,
conv3d_channels=32,
image_size_pixels=24,
),
},
add_image_embedding_channel=True,
# ocf-data-sampler doesn't supprt PV site inputs yet
pv_encoder=None,
output_network=dict(
_target_=pvnet.models.multimodal.linear_networks.networks.ResFCNet2,
_partial_=True,
fc_hidden_features=128,
n_res_blocks=6,
res_block_layers=2,
dropout_frac=0.0,
),
embedding_dim=16,
include_sun=True,
include_gsp_yield_history=True,
sat_history_minutes=30,
nwp_history_minutes={"ukv": 120},
nwp_forecast_minutes={"ukv": 480},
min_sat_delay_minutes=0,
)
kwargs.update(model_minutes_kwargs)
return kwargs
@pytest.fixture()
def multimodal_model_kwargs(raw_multimodal_model_kwargs):
return hydra.utils.instantiate(raw_multimodal_model_kwargs)
@pytest.fixture()
def multimodal_model(multimodal_model_kwargs):
model = Model(**multimodal_model_kwargs)
return model
@pytest.fixture()
def multimodal_quantile_model(multimodal_model_kwargs):
model = Model(output_quantiles=[0.1, 0.5, 0.9], **multimodal_model_kwargs)
return model
@pytest.fixture()
def multimodal_quantile_model_ignore_minutes(multimodal_model_kwargs):
"""Only forecsat second half of the 8 hours"""
model = Model(
output_quantiles=[0.1, 0.5, 0.9], **multimodal_model_kwargs, forecast_minutes_ignore=240
)
return model