-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #29 from Fraunhofer-IIS/28-update-case-study
elaborate case study
- Loading branch information
Showing
17 changed files
with
810 additions
and
285 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
# %% | ||
import pandas as pd | ||
from pathlib import Path | ||
import matplotlib.pyplot as plt | ||
|
||
# %% | ||
dir = Path(__file__).parent | ||
areas = ["output_and_income", "consumption_and_orders", "prices"] | ||
df_collection = [] | ||
for area in areas: | ||
df = pd.read_csv(dir / f"overall_losses_{area}.csv", index_col=[0, 1, 2]) | ||
df.index.set_names(["var", "model", "rolling_origin"], inplace=True) | ||
df.columns = range(1, len(df.columns) + 1) | ||
df.columns.name = "Forecast Step" | ||
df_collection.append(df) | ||
|
||
# %% Overall results | ||
dfs = pd.concat(df_collection, keys=areas, axis=0) | ||
dfs.groupby(level=(2)).mean().round(3).to_latex(dir / f"overall_results.tex") | ||
|
||
# %% Results for first step over three variable groups | ||
forecast_step = 1 | ||
|
||
dfs = pd.concat([df[forecast_step] for df in df_collection], keys=areas, axis=1) | ||
mean_error_per_group = dfs.groupby(level=1).mean() | ||
|
||
# Plot | ||
ax = mean_error_per_group.rank().plot(kind='bar', figsize=(10, 4)) | ||
ax.spines[['right', 'top']].set_visible(False) | ||
plt.ylabel('Rank') | ||
plt.xlabel('Model') | ||
plt.legend(bbox_to_anchor=(1,1)) | ||
plt.tight_layout() | ||
plt.savefig(dir / 'model_ranking_in_groups.pdf') |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
from torch import nn | ||
import torch | ||
import pandas as pd | ||
|
||
|
||
class Evaluator: | ||
def __init__(self, model: nn.Module, forecast_horizon: int): | ||
self.losses = [] | ||
self.model = model | ||
self.forecast_horizon = forecast_horizon | ||
self.loss_metric = nn.functional.mse_loss | ||
|
||
def evaluate( | ||
self, dataset: torch.utils.data.Dataset, index: int = None | ||
) -> pd.DataFrame: | ||
self.model.eval() | ||
|
||
for features_past, target_past, target_future in dataset: | ||
features_past = features_past.unsqueeze(1) | ||
target_past = target_past.unsqueeze(1) | ||
|
||
with torch.no_grad(): | ||
input = self.model.get_input(features_past, target_past) | ||
output = self.model(*input) | ||
forecasts = self.model.extract_forecasts(output) | ||
forecasts = forecasts.squeeze(1) | ||
if forecasts.size(-1) > 1: | ||
forecasts = forecasts[..., [index]] | ||
|
||
assert forecasts.shape == target_future.shape | ||
|
||
self.losses.append( | ||
self.loss_metric( | ||
forecasts, target_future, reduction="none" | ||
).flatten().tolist() | ||
) | ||
return pd.DataFrame(self.losses) |
Oops, something went wrong.