Spaces:
Runtime error
Runtime error
File size: 20,764 Bytes
f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 f7b117b a6cfc29 1c6134e |
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 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 |
import pandas as pd
import plotly.express as px
import streamlit as st
from pandas.io.formats.style import Styler
from utils import get_leaderboard, get_model_ranks
def header(title: str) -> None:
st.title(title)
st.markdown(
"""
[EnFoBench](https://github.com/attila-balint-kul/energy-forecast-benchmark-toolkit)
is a community driven benchmarking framework for energy forecasting models.
"""
)
st.divider()
def logos() -> None:
left, right = st.columns(2)
with left:
st.image("./images/ku_leuven_logo.png")
with right:
st.image("./images/energyville_logo.png")
def links(current: str) -> None:
st.header("Sources")
st.link_button(
"GitHub Repository",
url="https://github.com/attila-balint-kul/energy-forecast-benchmark-toolkit",
use_container_width=True,
)
st.link_button(
"Documentation",
url="https://attila-balint-kul.github.io/energy-forecast-benchmark-toolkit/",
use_container_width=True,
)
st.link_button(
"Electricity Demand Dataset",
url="https://huggingface.co/datasets/EDS-lab/electricity-demand",
use_container_width=True,
)
st.link_button(
"HuggingFace Organization",
url="https://huggingface.co/EDS-lab",
use_container_width=True,
)
st.header("Other Dashboards")
if current != "ElectricityDemand":
st.link_button(
"Electricity Demand",
url="https://huggingface.co/spaces/EDS-lab/EnFoBench-ElectricityDemand",
use_container_width=True,
)
if current != "GasDemand":
st.link_button(
"Gas Demand",
url="https://huggingface.co/spaces/EDS-lab/EnFoBench-GasDemand",
use_container_width=True,
)
if current != "PVGeneration":
st.link_button(
"PVGeneration",
url="https://huggingface.co/spaces/EDS-lab/EnFoBench-PVGeneration",
use_container_width=True,
)
def model_selector(models: list[str], data: pd.DataFrame) -> set[str]:
# Group models by their prefix
model_groups: dict[str, list[str]] = {}
for model in models:
group, model_name = model.split(".", maxsplit=1)
if group not in model_groups:
model_groups[group] = []
model_groups[group].append(model_name)
models_to_plot = set()
st.header("Models to include")
left, middle, right = st.columns(3)
with left:
best_by_mae = st.button("Best by MAE", use_container_width=True)
if best_by_mae:
best_models_by_mae = get_model_ranks(data, "MAE.mean").head(10).model.tolist()
for model in models:
if model in best_models_by_mae:
st.session_state[model] = True
else:
st.session_state[model] = False
with middle:
best_by_rmse = st.button("Best by RMSE", use_container_width=True)
if best_by_rmse:
best_models_by_rmse = get_model_ranks(data, "RMSE.mean").head(10).model.tolist()
for model in models:
if model in best_models_by_rmse:
st.session_state[model] = True
else:
st.session_state[model] = False
with right:
best_by_rmae = st.button("Best by rMAE", use_container_width=True)
if best_by_rmae:
best_models_by_rmae = get_model_ranks(data, "rMAE.mean").head(10).model.tolist()
for model in models:
if model in best_models_by_rmae:
st.session_state[model] = True
else:
st.session_state[model] = False
left, right = st.columns(2)
with left:
select_none = st.button("Select None", use_container_width=True)
if select_none:
for model in models:
st.session_state[model] = False
with right:
select_all = st.button("Select All", use_container_width=True)
if select_all:
for model in models:
st.session_state[model] = True
for model_group, models in model_groups.items():
st.text(model_group)
for model_name in models:
to_plot = st.checkbox(
model_name, value=True, key=f"{model_group}.{model_name}"
)
if to_plot:
models_to_plot.add(f"{model_group}.{model_name}")
return models_to_plot
def overview_view(data: pd.DataFrame):
st.markdown("## Leaderboard")
leaderboard = get_leaderboard(data, ["MAE.mean", "RMSE.mean", "rMAE.mean"])
left, middle, right = st.columns(3)
with left:
best_models_mae = (
leaderboard.sort_values("MAE.mean", ascending=False)
.head(10)
.sort_values("MAE.mean")
)
fig = px.bar(best_models_mae, x="MAE.mean", y=best_models_mae.index)
fig.update_layout(
title="Top 10 models by MAE",
xaxis_title="",
yaxis_title="Model",
height=600,
)
st.plotly_chart(fig, use_container_width=True)
with middle:
best_models_mae = (
leaderboard.sort_values("RMSE.mean", ascending=False)
.head(10)
.sort_values("RMSE.mean")
)
fig = px.bar(best_models_mae, x="RMSE.mean", y=best_models_mae.index)
fig.update_layout(
title="Top 10 models by RMSE", xaxis_title="", yaxis_title="", height=600
)
st.plotly_chart(fig, use_container_width=True)
with right:
best_models_mae = (
leaderboard.sort_values("rMAE.mean", ascending=False)
.head(10)
.sort_values("rMAE.mean")
)
fig = px.bar(best_models_mae, x="rMAE.mean", y=best_models_mae.index)
fig.update_layout(
title="Top 10 models by rMAE", xaxis_title="", yaxis_title="", height=600
)
st.plotly_chart(fig, use_container_width=True)
st.dataframe(leaderboard, use_container_width=True)
def buildings_view(data: pd.DataFrame):
if 'metadata.cluster_size' not in data.columns:
data['metadata.cluster_size'] = 1
if 'metadata.building_class' not in data.columns:
data['metadata.building_class'] = "Unknown"
buildings = (
data[
[
"unique_id",
"metadata.cluster_size",
"metadata.building_class",
"metadata.location_id",
"metadata.timezone",
"dataset.available_history.days",
"dataset.available_history.observations",
"metadata.freq",
]
]
.groupby("unique_id")
.first()
.rename(
columns={
"metadata.cluster_size": "Cluster size",
"metadata.building_class": "Building class",
"metadata.location_id": "Location ID",
"metadata.timezone": "Timezone",
"dataset.available_history.days": "Available history (days)",
"dataset.available_history.observations": "Available history (#)",
"metadata.freq": "Frequency",
}
)
)
left, middle, right = st.columns(3)
with left:
st.metric("Number of buildings", data["unique_id"].nunique())
with middle:
st.metric(
"Residential",
data[data["metadata.building_class"] == "Residential"][
"unique_id"
].nunique(),
)
with right:
st.metric(
"Commercial",
data[data["metadata.building_class"] == "Commercial"][
"unique_id"
].nunique(),
)
st.divider()
left, middle, right = st.columns(3, gap="large")
with left:
st.markdown("#### Building classes")
fig = px.pie(
buildings.groupby("Building class").size().reset_index(),
values=0,
names="Building class",
)
fig.update_layout(
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
st.plotly_chart(fig, use_container_width=True)
with middle:
st.markdown("#### Timezones")
fig = px.pie(
buildings.groupby("Timezone").size().reset_index(),
values=0,
names="Timezone",
)
fig.update_layout(
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
st.plotly_chart(fig, use_container_width=True)
with right:
st.markdown("#### Frequencies")
fig = px.pie(
buildings.groupby("Frequency").size().reset_index(),
values=0,
names="Frequency",
)
fig.update_layout(
legend=dict(orientation="h", yanchor="bottom", y=1.02, xanchor="right", x=1)
)
st.plotly_chart(fig, use_container_width=True)
st.divider()
st.markdown("#### Buildings")
st.dataframe(
buildings.sort_values("Available history (days)"),
use_container_width=True,
column_config={
"Available history (days)": st.column_config.ProgressColumn(
"Available history (days)",
help="Available training data during the first prediction.",
format="%f",
min_value=0,
max_value=float(buildings["Available history (days)"].max()),
),
"Available history (#)": st.column_config.ProgressColumn(
"Available history (#)",
help="Available training data during the first prediction.",
format="%f",
min_value=0,
max_value=float(buildings["Available history (#)"].max()),
),
},
)
def models_view(data: pd.DataFrame):
models = (
data[
[
"model",
"cv_config.folds",
"cv_config.horizon",
"cv_config.step",
"cv_config.time",
"model_info.repository",
"model_info.tag",
"model_info.variate_type",
]
]
.groupby("model")
.first()
.rename(
columns={
"cv_config.folds": "CV Folds",
"cv_config.horizon": "CV Horizon",
"cv_config.step": "CV Step",
"cv_config.time": "CV Time",
"model_info.repository": "Image Repository",
"model_info.tag": "Image Tag",
"model_info.variate_type": "Variate type",
}
)
)
left, middle, right = st.columns(3)
with left:
st.metric("Models", len(models))
with middle:
st.metric(
"Univariate",
data[data["model_info.variate_type"] == "univariate"]["model"].nunique(),
)
with right:
st.metric(
"Univariate",
data[data["model_info.variate_type"] == "multivariate"]["model"].nunique(),
)
st.divider()
left, right = st.columns(2, gap="large")
with left:
st.markdown("#### Variate types")
fig = px.pie(
models.groupby("Variate type").size().reset_index(),
values=0,
names="Variate type",
)
st.plotly_chart(fig, use_container_width=True)
with right:
st.markdown("#### Frameworks")
_df = models.copy()
_df["Framework"] = _df.index.str.split(".").str[0]
fig = px.pie(
_df.groupby("Framework").size().reset_index(),
values=0,
names="Framework",
)
st.plotly_chart(fig, use_container_width=True)
st.divider()
st.markdown("### Models")
st.dataframe(models, use_container_width=True)
def accuracy_view(data: pd.DataFrame, models_to_plot: set[str]):
data_to_plot = data[data["model"].isin(models_to_plot)].sort_values(
by="model", ascending=True
)
left, right = st.columns(2, gap="small")
with left:
metric = st.selectbox("Metric", ["MAE", "RMSE", "MBE", "rMAE"], index=0)
with right:
aggregation = st.selectbox(
"Aggregation", ["min", "mean", "median", "max", "std"], index=1
)
st.markdown(f"#### {aggregation.capitalize()} {metric} per building")
if data_to_plot.empty:
st.warning("No data to display.")
else:
model_ranks = get_model_ranks(data_to_plot, f"{metric}.{aggregation}")
fig = px.box(
data_to_plot.merge(model_ranks, on="model").sort_values(by="rank"),
x=f"{metric}.{aggregation}",
y="model",
color="model",
points="all",
)
fig.update_layout(showlegend=False, height=50 * len(models_to_plot))
st.plotly_chart(fig, use_container_width=True)
st.divider()
left, right = st.columns(2, gap="large")
with left:
x_metric = st.selectbox(
"Metric", ["MAE", "RMSE", "MBE", "rMAE"], index=0, key="x_metric"
)
x_aggregation = st.selectbox(
"Aggregation",
["min", "mean", "median", "max", "std"],
index=1,
key="x_aggregation",
)
with right:
y_metric = st.selectbox(
"Aggregation", ["MAE", "RMSE", "MBE", "rMAE"], index=1, key="y_metric"
)
y_aggregation = st.selectbox(
"Aggregation",
["min", "mean", "median", "max", "std"],
index=1,
key="y_aggregation",
)
st.markdown(
f"#### {x_aggregation.capitalize()} {x_metric} vs {y_aggregation.capitalize()} {y_metric}"
)
if data_to_plot.empty:
st.warning("No data to display.")
else:
fig = px.scatter(
data_to_plot,
x=f"{x_metric}.{x_aggregation}",
y=f"{y_metric}.{y_aggregation}",
color="model",
)
fig.update_layout(height=600)
st.plotly_chart(fig, use_container_width=True)
st.divider()
left, right = st.columns(2, gap="small")
with left:
metric = st.selectbox(
"Metric", ["MAE", "RMSE", "MBE", "rMAE"], index=0, key="table_metric"
)
with right:
aggregation = st.selectbox(
"Aggregation across folds",
["min", "mean", "median", "max", "std"],
index=1,
key="table_aggregation",
)
metrics_table = data_to_plot.groupby(["model"]).agg(aggregation, numeric_only=True)[
[
f"{metric}.min",
f"{metric}.mean",
f"{metric}.median",
f"{metric}.max",
f"{metric}.std",
]
].sort_values(by=f"{metric}.mean")
def custom_table(styler):
styler.background_gradient(cmap="seismic", axis=0)
styler.format(precision=2)
# center text and increase font size
styler.map(lambda x: "text-align: center; font-size: 14px;")
return styler
st.markdown(f"#### {aggregation.capitalize()} {metric} stats per model")
styled_table = metrics_table.style.pipe(custom_table)
st.dataframe(styled_table, use_container_width=True)
metrics_per_building_table = (
data_to_plot.groupby(["model", "unique_id"])
.apply(aggregation, numeric_only=True)
.reset_index()
.pivot(index="model", columns="unique_id", values=f"{metric}.{aggregation}")
)
metrics_per_building_table.insert(
0, "mean", metrics_per_building_table.mean(axis=1)
)
metrics_per_building_table = metrics_per_building_table.sort_values(by="mean").drop(columns="mean")
def custom_table(styler: Styler):
styler.background_gradient(cmap="seismic", axis=None)
styler.format(precision=2)
# center text and increase font size
styler.map(lambda x: "text-align: center; font-size: 14px;")
return styler
st.markdown(f"#### {aggregation.capitalize()} {metric} stats per building")
styled_table = metrics_per_building_table.style.pipe(custom_table)
st.dataframe(styled_table, use_container_width=True)
def relative_performance_view(data: pd.DataFrame, models_to_plot: set[str]):
data_to_plot = data[data["model"].isin(models_to_plot)].sort_values(
by="model", ascending=True
)
st.markdown("#### Relative performance")
if data_to_plot.empty:
st.warning("No data to display.")
else:
baseline_choices = sorted(
data.filter(like="better_than")
.columns.str.removeprefix("better_than.")
.tolist()
)
if len(baseline_choices) > 1:
better_than_baseline = st.selectbox("Baseline model", options=baseline_choices)
else:
better_than_baseline = baseline_choices[0]
data_to_plot.loc[:, f"better_than.{better_than_baseline}.percentage"] = (
pd.json_normalize(data_to_plot[f"better_than.{better_than_baseline}"])[
"percentage"
].values
* 100
)
model_rank = get_model_ranks(data_to_plot, f"better_than.{better_than_baseline}.percentage")
fig = px.box(
data_to_plot.merge(model_rank).sort_values(by="rank"),
x=f"better_than.{better_than_baseline}.percentage",
y="model",
points="all",
)
fig.update_xaxes(range=[0, 100], title_text="Better than baseline (%)")
fig.update_layout(
showlegend=False,
height=50 * len(models_to_plot),
title=f"Better than {better_than_baseline} on % of days per building",
)
st.plotly_chart(fig, use_container_width=True)
def computation_view(data: pd.DataFrame, models_to_plot: set[str]):
data_to_plot = data[data["model"].isin(models_to_plot)].sort_values(
by="model", ascending=True
)
data_to_plot["resource_usage.CPU"] /= 3600
st.markdown("#### Computational Resources")
left, center, right = st.columns(3, gap="small")
with left:
metric = st.selectbox("Metric", ["MAE", "RMSE", "MBE", "rMAE"], index=0)
with center:
aggregation_per_building = st.selectbox(
"Aggregation per building", ["min", "mean", "median", "max", "std"], index=1
)
with right:
aggregation_per_model = st.selectbox(
"Aggregation per model", ["min", "mean", "median", "max", "std"], index=1
)
st.markdown(
f"#### {aggregation_per_model.capitalize()} {aggregation_per_building.capitalize()} {metric} vs CPU usage"
)
if data_to_plot.empty:
st.warning("No data to display.")
else:
aggregated_data = (
data_to_plot.groupby("model")
.agg(aggregation_per_building, numeric_only=True)
.reset_index()
)
fig = px.scatter(
aggregated_data,
x="resource_usage.CPU",
y=f"{metric}.{aggregation_per_model}",
color="model",
log_x=True,
)
fig.update_layout(height=600)
fig.update_xaxes(title_text="CPU usage (hours)")
fig.update_yaxes(
title_text=f"{metric} ({aggregation_per_building}, {aggregation_per_model})"
)
st.plotly_chart(fig, use_container_width=True)
st.divider()
st.markdown("#### Computational time vs historical data")
if data_to_plot.empty:
st.warning("No data to display.")
else:
fig = px.scatter(
data_to_plot,
x="dataset.available_history.observations",
y="resource_usage.CPU",
color="model",
trendline="ols",
hover_data=["model", "unique_id"],
)
fig.update_layout(height=600)
fig.update_xaxes(title_text="Available historical observations (#)")
fig.update_yaxes(title_text="CPU usage (hours)")
st.plotly_chart(fig, use_container_width=True)
st.divider()
cpu_per_building_table = (
data_to_plot.pivot(index="model", columns="unique_id", values="resource_usage.CPU")
)
def custom_table(styler: Styler):
styler.background_gradient(cmap="seismic", axis=None)
styler.format(precision=2)
# center text and increase font size
styler.map(lambda x: "text-align: center; font-size: 14px;")
return styler
st.markdown(f"#### Computational time per building")
styled_table = cpu_per_building_table.style.pipe(custom_table)
st.dataframe(styled_table, use_container_width=True)
|