-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathviz.py
More file actions
488 lines (401 loc) · 14.8 KB
/
viz.py
File metadata and controls
488 lines (401 loc) · 14.8 KB
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
"""
viz.py - Visualization utilities for the wind-turbine tensor decomposition project.
Assumptions:
- `run.py` has already been executed.
- It produced at least:
results/model_performance.csv
- Optionally:
results/decomposition_summary.csv
results/preds_<setting>_<model>.npz with arrays:
- y_test
- y_pred
"""
import os
from typing import List, Tuple, Optional
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
RESULTS_DIR = "results"
# ---------------------------------------------------------------------------
# Helpers
# ---------------------------------------------------------------------------
NICE_LABELS = {
# RAW
("raw", "persistence"): "Persistence (y(t-1))",
("raw", "ridge"): "Raw + Ridge",
("raw", "lstm"): "Raw + LSTM",
# PCA
("pca_k3", "ridge"): "PCA(k=3) + Ridge",
("pca_k3", "lstm"): "PCA(k=3) + LSTM",
("pca_k5", "ridge"): "PCA(k=5) + Ridge",
("pca_k5", "lstm"): "PCA(k=5) + LSTM",
# CP
("cp_latent_r12", "ridge"): "CP(r=12) + Ridge",
("cp_latent_r12", "lstm"): "CP(r=12) + LSTM",
# Tucker m3
("tucker_latent_m3_50_24_5", "ridge"): "Tucker m3 (50,24,5) + Ridge",
("tucker_latent_m3_50_24_5", "lstm"): "Tucker m3 (50,24,5) + LSTM",
("tucker_latent_m3_80_24_7", "ridge"): "Tucker m3 (80,24,7) + Ridge",
("tucker_latent_m3_80_24_7", "lstm"): "Tucker m3 (80,24,7) + LSTM",
# Tucker core
("tucker_latent_core_50_24_5", "ridge"): "Tucker core (50,24,5) + Ridge",
("tucker_latent_core_50_24_5", "lstm"): "Tucker core (50,24,5) + LSTM",
("tucker_latent_core_80_24_7", "ridge"): "Tucker core (80,24,7) + Ridge",
("tucker_latent_core_80_24_7", "lstm"): "Tucker core (80,24,7) + LSTM",
}
def pretty_label(setting: str, model: str) -> str:
key = (setting, model)
if key in NICE_LABELS:
return NICE_LABELS[key]
return f"{setting} + {model}"
def _check_file(path: str) -> None:
if not os.path.exists(path):
raise FileNotFoundError(f"Expected file not found: {path}")
# ---------------------------------------------------------------------------
# 1. Decomposition quality: CP vs Tucker reconstruction error
# ---------------------------------------------------------------------------
def plot_decomposition_errors(
path: str = os.path.join(RESULTS_DIR, "decomposition_summary.csv")
) -> None:
_check_file(path)
df = pd.read_csv(path)
df_cp = df[df["method"] == "cp"].copy()
df_tk = df[df["method"] == "tucker"].copy()
plt.figure()
if not df_cp.empty:
plt.plot(df_cp["rank"], df_cp["rel_error"], marker="o", label="CP (rank R)")
if not df_tk.empty:
plt.plot(df_tk["rank3"], df_tk["rel_error"], marker="s", label="Tucker (R_F)")
plt.xlabel("Rank (R for CP, R_F for Tucker)")
plt.ylabel("Relative reconstruction error")
plt.title("CP vs Tucker reconstruction error on daily tensor")
plt.legend()
plt.tight_layout()
plt.show()
# ---------------------------------------------------------------------------
# 2. Global model performance: bar charts of RMSE / MAE
# ---------------------------------------------------------------------------
def plot_model_performance(
path: str = os.path.join(RESULTS_DIR, "model_performance.csv"),
metric: str = "rmse",
model_filter: Optional[str] = None,
sort_ascending: bool = True,
) -> None:
"""
Bar chart of performance for all (setting, model) pairs.
metric : "rmse" or "mae"
model_filter : if given (e.g. "lstm" or "ridge"), only that model type is shown.
"""
_check_file(path)
df = pd.read_csv(path)
if metric not in {"rmse", "mae"}:
raise ValueError("metric must be 'rmse' or 'mae'")
if model_filter is not None:
df = df[df["model"] == model_filter].copy()
df["label"] = [pretty_label(s, m) for s, m in zip(df["setting"], df["model"])]
df = df.sort_values(metric, ascending=sort_ascending)
plt.figure(figsize=(10, 4))
plt.bar(df["label"], df[metric])
plt.ylabel(metric.upper())
plt.title(f"Test {metric.upper()} for all models"
+ ("" if model_filter is None else f" ({model_filter})"))
plt.xticks(rotation=30, ha="right")
if "persistence" in df["model"].values:
pers_vals = df[df["model"] == "persistence"][metric]
if not pers_vals.empty:
val = float(pers_vals.iloc[0])
plt.axhline(val, linestyle="--", label="Persistence")
plt.legend()
plt.tight_layout()
plt.show()
def plot_main_models_rmse(
path: str = os.path.join(RESULTS_DIR, "model_performance.csv")
) -> None:
"""
Focused RMSE comparison of the most important LSTM models:
- Persistence
- Raw + LSTM
- PCA(k=5) + LSTM
- CP(r=12) + LSTM
- Tucker core (50,24,5) + LSTM
- Tucker core (80,24,7) + LSTM
"""
_check_file(path)
df = pd.read_csv(path)
keep = [
("raw", "persistence"),
("raw", "lstm"),
("pca_k5", "lstm"),
("cp_latent_r12", "lstm"),
("tucker_latent_core_50_24_5", "lstm"),
("tucker_latent_core_80_24_7", "lstm"),
]
rows = []
for setting, model in keep:
row = df[(df["setting"] == setting) & (df["model"] == model)]
if not row.empty:
r = row.iloc[0]
rows.append({
"label": pretty_label(setting, model),
"rmse": r["rmse"],
})
plot_df = pd.DataFrame(rows)
plot_df = plot_df.sort_values("rmse", ascending=True)
plt.figure(figsize=(10, 4))
plt.bar(plot_df["label"], plot_df["rmse"])
plt.ylabel("RMSE")
plt.title("Forecast performance of main LSTM models (test RMSE)")
plt.xticks(rotation=25, ha="right")
pers = df[(df["setting"] == "raw") & (df["model"] == "persistence")]
if not pers.empty:
rmse_pers = pers["rmse"].iloc[0]
plt.axhline(rmse_pers, linestyle="--", label="Persistence")
plt.legend()
plt.tight_layout()
plt.show()
# ---------------------------------------------------------------------------
# 3. True vs predicted: time-series windows
# ---------------------------------------------------------------------------
def load_predictions(
setting: str,
model: str = "lstm",
results_dir: str = RESULTS_DIR,
) -> Tuple[np.ndarray, np.ndarray]:
"""
Load y_test and y_pred from results/preds_<setting>_<model>.npz.
Works for model in {"persistence", "ridge", "lstm"} provided
run.py saved files with this naming convention.
"""
fname = f"preds_{setting}_{model}.npz"
path = os.path.join(results_dir, fname)
_check_file(path)
data = np.load(path)
y_test = data["y_test"].ravel()
y_pred = data["y_pred"].ravel()
return y_test, y_pred
def plot_timeseries_window(
settings: List[str],
model: str = "lstm",
start: int = 0,
length: int = 400,
results_dir: str = RESULTS_DIR,
) -> None:
"""
Plot a window of the test series for several representations
using the same model type (ridge / lstm / persistence).
settings : list of setting names (e.g. ["raw", "tucker_latent_core_80_24_7"])
model : "lstm", "ridge", or "persistence"
"""
if not settings:
raise ValueError("settings list must not be empty")
y_test, y_pred_first = load_predictions(settings[0], model, results_dir)
end = min(start + length, len(y_test))
t = np.arange(end - start)
plt.figure(figsize=(10, 4))
plt.plot(t, y_test[start:end], label="True")
plt.plot(t, y_pred_first[start:end], label=pretty_label(settings[0], model))
for setting in settings[1:]:
_, y_pred = load_predictions(setting, model, results_dir)
plt.plot(t, y_pred[start:end], label=pretty_label(setting, model))
plt.xlabel("Time index (test, 10-min steps)")
plt.ylabel("Power output")
plt.title(f"Forecasts on test set (example window) – model={model}")
plt.legend()
plt.tight_layout()
plt.show()
def save_timeseries_per_model(
settings: List[str],
model: str = "lstm",
start: int = 0,
length: int = 400,
results_dir: str = RESULTS_DIR,
out_dir: str = "figures",
) -> None:
"""
For each setting, create a separate PNG with:
- true y_test
- predictions of (setting, model)
Example:
save_timeseries_per_model(
["raw", "tucker_latent_core_80_24_7"],
model="lstm",
start=200,
length=400,
)
"""
os.makedirs(out_dir, exist_ok=True)
for setting in settings:
y_test, y_pred = load_predictions(setting, model, results_dir)
end = min(start + length, len(y_test))
t = np.arange(end - start)
plt.figure(figsize=(10, 3))
plt.plot(t, y_test[start:end], label="True")
plt.plot(t, y_pred[start:end], label=pretty_label(setting, model))
plt.xlabel("Time index (test, 10-min steps)")
plt.ylabel("Power output")
plt.title(f"{pretty_label(setting, model)} – time series (test window)")
plt.legend()
plt.tight_layout()
fname = os.path.join(out_dir, f"ts_{setting}_{model}.png")
plt.savefig(fname, dpi=300, bbox_inches="tight")
plt.close()
# ---------------------------------------------------------------------------
# 4. True vs predicted: scatter plots (calibration)
# ---------------------------------------------------------------------------
def plot_scatter_true_vs_pred(
settings: List[str],
model: str = "lstm",
max_points: Optional[int] = 5000,
results_dir: str = RESULTS_DIR,
) -> None:
"""
Scatter: y_true vs y_pred for several representations using same model.
settings : list of setting names (e.g. ["raw", "tucker_latent_core_80_24_7"])
model : "lstm", "ridge", or "persistence"
"""
if not settings:
raise ValueError("settings list must not be empty")
y_test, _ = load_predictions(settings[0], model, results_dir)
n = len(y_test)
if max_points is not None and max_points < n:
rng = np.random.default_rng(0)
idx = rng.choice(n, size=max_points, replace=False)
else:
idx = np.arange(n)
plt.figure(figsize=(6, 6))
for setting in settings:
y_true, y_pred = load_predictions(setting, model, results_dir)
y_true_s = y_true[idx]
y_pred_s = y_pred[idx]
plt.scatter(y_true_s, y_pred_s, alpha=0.3, label=pretty_label(setting, model))
min_v = float(y_test.min())
max_v = float(y_test.max())
plt.plot([min_v, max_v], [min_v, max_v])
plt.xlabel("True power")
plt.ylabel("Predicted power")
plt.title(f"True vs predicted power on test set – model={model}")
plt.legend()
plt.tight_layout()
plt.show()
def save_scatter_per_model(
settings: List[str],
model: str = "lstm",
max_points: Optional[int] = 5000,
results_dir: str = RESULTS_DIR,
out_dir: str = "figures",
) -> None:
"""
For each setting, create a separate PNG:
x = true, y = predicted for (setting, model)
"""
os.makedirs(out_dir, exist_ok=True)
for setting in settings:
y_true, y_pred = load_predictions(setting, model, results_dir)
n = len(y_true)
if max_points is not None and max_points < n:
rng = np.random.default_rng(0)
idx = rng.choice(n, size=max_points, replace=False)
y_true_s = y_true[idx]
y_pred_s = y_pred[idx]
else:
y_true_s = y_true
y_pred_s = y_pred
plt.figure(figsize=(5, 5))
plt.scatter(y_true_s, y_pred_s, alpha=0.3)
min_v = float(min(y_true_s.min(), y_pred_s.min()))
max_v = float(max(y_true_s.max(), y_pred_s.max()))
plt.plot([min_v, max_v], [min_v, max_v])
plt.xlabel("True power")
plt.ylabel("Predicted power")
plt.title(f"{pretty_label(setting, model)} – true vs predicted")
plt.tight_layout()
fname = os.path.join(out_dir, f"scatter_{setting}_{model}.png")
plt.savefig(fname, dpi=300, bbox_inches="tight")
plt.close()
# ---------------------------------------------------------------------------
# 5. Convenience main (you can also call functions manually from a notebook)
# ---------------------------------------------------------------------------
def main():
# 1) Decomposition errors
try:
plot_decomposition_errors()
except FileNotFoundError:
print("decomposition_summary.csv not found; skipping decomposition plot.")
# 2) Global performance
try:
# all models
plot_model_performance(metric="rmse", model_filter=None)
# only LSTM
plot_model_performance(metric="rmse", model_filter="lstm")
# only Ridge
plot_model_performance(metric="rmse", model_filter="ridge")
# focused main LSTM models
plot_main_models_rmse()
except FileNotFoundError:
print("model_performance.csv not found; skipping performance plots.")
# 3) Save per-model time series (true vs each model), as images
try:
settings_important = [
"raw",
"pca_k5",
"cp_latent_r12",
"tucker_latent_core_50_24_5",
"tucker_latent_core_80_24_7",
]
# LSTM versions
save_timeseries_per_model(
settings_important,
model="lstm",
start=200,
length=400,
out_dir="figures_lstm_ts",
)
# Ridge versions
save_timeseries_per_model(
settings_important,
model="ridge",
start=200,
length=400,
out_dir="figures_ridge_ts",
)
# Persistence: only defined for 'raw'
save_timeseries_per_model(
["raw"],
model="persistence",
start=200,
length=400,
out_dir="figures_persistence_ts",
)
except FileNotFoundError:
print("Prediction .npz files not found; skipping per-model time-series export.")
# 4) Save per-model scatter plots (true vs predicted), as images
try:
settings_important = [
"raw",
"pca_k5",
"cp_latent_r12",
"tucker_latent_core_50_24_5",
"tucker_latent_core_80_24_7",
]
save_scatter_per_model(
settings_important,
model="lstm",
max_points=4000,
out_dir="figures_lstm_scatter",
)
save_scatter_per_model(
settings_important,
model="ridge",
max_points=4000,
out_dir="figures_ridge_scatter",
)
save_scatter_per_model(
["raw"],
model="persistence",
max_points=4000,
out_dir="figures_persistence_scatter",
)
except FileNotFoundError:
print("Prediction .npz files not found; skipping per-model scatter export.")
if __name__ == "__main__":
main()