-
Notifications
You must be signed in to change notification settings - Fork 0
/
9.1_NWP_Only.py
285 lines (248 loc) · 8.18 KB
/
9.1_NWP_Only.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
# %%
from copy import deepcopy
import numpy as np
import pandas as pd
import torch
import yaml
from yaml.loader import SafeLoader
from forecast.dataset import LoaderScaler
from forecast.get import load_data, load_loaders, load_model
from forecast.model import BayesianModelPredictor, get_metrics
from forecast.plot import (
plot_map_with_wind_predictions,
plot_talagrad_cumulative,
plot_talagrad_sharpness,
plot_timeseries_usage_example,
)
# %% read ymal file and create metrics json
with open("nwp_only_models.yaml", "r") as ymlfile:
model_dict = yaml.load(ymlfile, Loader=SafeLoader)
metrics = deepcopy(model_dict)
for dataset_type, _aux1 in model_dict.items():
for model_nn, _aux2 in _aux1.items():
for bayesian_framework, model_path in _aux2.items():
metrics[dataset_type][model_nn][bayesian_framework] = {
"rmse": 0.0,
"nll": 0.0,
"crps": 0.0,
}
# %% Calc all metrics
dataset_type = "A"
model_nn = "nwp"
bayesian_framework = "ensemble"
# Aqui aproveita pra carregar o dataset e os loaders só uma ves
fct_tensor, fct_dts, nwp_data = load_data("Dataset")
loader_path = f"Loader_Dataset{dataset_type}"
(
_loaders,
_dataset_train_val,
_,
_,
_dataset_test,
) = load_loaders(loader_path)
loaders_app, _, dataset_app, _, _ = load_loaders("Loader_App")
loaders = {
"train": _loaders["train_val"],
"test": _loaders["test"],
}
dataset_train = _dataset_train_val
dataset_test = _dataset_test
loader_scaler = LoaderScaler(dataset_train) # scaler usa o dataset de treino
# load models
models, loaders = load_model(model_path)
# Plots
prefix = f"{dataset_type}_{model_nn}_{bayesian_framework}"
image_directory = "../Figuras/results"
# plot_talagrad_cumulative(
# Y,
# means,
# stds,
# save_directory=f"{image_directory}/{prefix}_talagrad_cumulative.png",
# )
# plot_talagrad_sharpness(
# stds,
# save_directory=f"{image_directory}/{prefix}_talagrad_sharpness.png",
# )
sampler = BayesianModelPredictor(models, bayesian_framework, loader_scaler)
Y, means, stds = sampler.sample(loaders_app["test"])
plot_timeseries_usage_example(
sampler,
loaders_app["test"],
Y,
means,
stds,
save_directory=f"{image_directory}/{prefix}_timeseries_usage_example.png",
)
# plot_map_with_wind_predictions(
# sampler,
# loaders_app["test"],
# dataset_app,
# Y,
# means,
# stds,
# nwp_data,
# save_directory=f"{image_directory}/{prefix}_map_with_predictions.png",
# )
# %%
from copy import deepcopy
import numpy as np
import pandas as pd
import yaml
from yaml.loader import SafeLoader
from forecast.dataset import LoaderScaler
from forecast.get import load_data, load_loaders, load_model
from forecast.model import BayesianModelPredictor, get_metrics
from forecast.plot import (
plot_map_with_wind_predictions,
plot_talagrad_cumulative,
plot_talagrad_sharpness,
plot_timeseries_usage_example,
)
# %% read ymal file and create metrics json
with open("nwp_only_models.yaml", "r") as ymlfile:
model_dict = yaml.load(ymlfile, Loader=SafeLoader)
metrics = deepcopy(model_dict)
for dataset_type, _aux1 in model_dict.items():
for model_nn, _aux2 in _aux1.items():
for bayesian_framework, model_path in _aux2.items():
metrics[dataset_type][model_nn][bayesian_framework] = {
"rmse": 0.0,
"nll": 0.0,
"crps": 0.0,
}
# %%
results = pd.DataFrame(
columns=["Dataset", "Model", "Bayesian", "RMSE", "NLL", "CRPS"], data=[]
)
# %% Calc all metrics
for dataset_type, _aux1 in model_dict.items():
# Aqui aproveita pra carregar o dataset e os loaders só uma ves
fct_tensor, fct_dts, wtg_data = load_data("Dataset")
loader_path = f"Loader_Dataset{dataset_type}"
(
_loaders,
_dataset_train_val,
_,
_,
_dataset_test,
) = load_loaders(loader_path)
loaders_app, _, dataset_app, _, _ = load_loaders("Loader_App")
loaders = {
"train": _loaders["train_val"],
"test": _loaders["test"],
}
dataset_train = _dataset_train_val
dataset_test = _dataset_test
loader_scaler = LoaderScaler(
dataset_train
) # scaler usa o dataset de treino
for model_nn, _aux2 in _aux1.items():
for bayesian_framework, model_path in _aux2.items():
print(
f"Dataset {dataset_type} Modelo {model_nn} Bayesian {bayesian_framework} Path {model_path}"
)
# dataset_type = "C"
# model_nn = "lstm"
# bayesian_framework = "dropout"
# model_path = model_dict[dataset_type][model_nn][bayesian_framework]
if not model_path:
print("Skipping")
continue
# load models
models, loaders = load_model(model_path)
# Metrics
sampler = BayesianModelPredictor(
models, bayesian_framework, loader_scaler
)
Y, means, stds = sampler.sample(loaders["test"])
rmse, nll, crps = get_metrics(Y, means, stds)
metrics[dataset_type][model_nn][bayesian_framework] = {
"rmse": rmse,
"nll": nll,
"crps": crps,
}
results = results.append(
pd.DataFrame(
[
[
dataset_type,
model_nn,
bayesian_framework,
rmse,
nll,
crps,
]
],
columns=results.columns,
),
ignore_index=True,
)
# %% Apenas pra debug
results = pd.DataFrame(
columns=["Dataset", "Model", "Bayesian", "RMSE", "NLL", "CRPS"], data=[]
)
for dataset_type, _aux1 in model_dict.items():
for model_nn, _aux2 in _aux1.items():
for bayesian_framework, model_path in _aux2.items():
results = results.append(
pd.DataFrame(
[
[
dataset_type,
model_nn,
bayesian_framework,
metrics[dataset_type][model_nn][
bayesian_framework
]["rmse"],
metrics[dataset_type][model_nn][
bayesian_framework
]["nll"],
metrics[dataset_type][model_nn][
bayesian_framework
]["crps"],
]
],
columns=results.columns,
),
ignore_index=True,
)
# %% Para Cada Dataset
for dataset_type in ["A", "C"]:
aux = (
results[results["Dataset"] == dataset_type]
.pivot_table(
index=["Model", "Bayesian"],
values=["RMSE", "NLL", "CRPS"],
columns=[],
)
.reset_index()
)
aux["shift"] = aux["Model"].shift(1) == aux["Model"]
aux["Model"] = aux.apply(
lambda row: row["Model"] if not row["shift"] else "", axis=1
)
aux.drop(columns=["shift"], inplace=True)
aux2 = aux[aux["Bayesian"] == "dummy"][:1]
aux = aux[aux["Bayesian"] != "dummy"]
aux2["Model"] = "Dummy"
aux2["Bayesian"] = "---"
aux2["NLL"] = np.nan
aux2["CRPS"] = np.nan
aux = aux.append(aux2).reset_index(drop=True)
aux.columns = ["textbf{" + col + "}" for col in aux.columns]
message = aux.to_latex(float_format="%.4f", index=False).replace(
"NaN", "---"
)
message = (
message.replace("Bayesian", "Bayesian Framework")
.replace("dropout", "MC Dropout")
.replace("ensemble", "Deep Ensembles")
.replace("multiswag", "MultiSWAG")
.replace("swag", "SWAG")
.replace("nllbaseline", "NLL Baseline")
.replace("mlp", "MLP")
.replace("convlstm", "ConvLSTM")
.replace("lstm", "LSTM")
)
print(message.replace("textbf", "\\textbf"))
# %%