Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -155,3 +155,5 @@ main[1-9]*.py
*.jsonl
docs/node_modules/
tinkering

examples/raw_02_example_reproduction.py
3 changes: 1 addition & 2 deletions epftoolbox2/models/_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,6 @@ def _fit_one_numpy(hour: int, hz: int, day_in_test: int) -> Dict:
run_date = datetime.date.fromisoformat(cfg["test_start"]) + datetime.timedelta(days=day_in_test)
run_date_str = run_date.isoformat()
target_date_str = (run_date + datetime.timedelta(days=hz)).isoformat()
run_weekday = run_date.weekday()

scaler = StandardScaler()
train_x, train_y, test_x = scaler.fit_transform(
Expand All @@ -56,7 +55,7 @@ def _fit_one_numpy(hour: int, hz: int, day_in_test: int) -> Dict:
return {
"run_date": run_date_str,
"target_date": target_date_str,
"run_weekday": run_weekday,
"run_weekday": run_date.weekday(),
"hour": hour,
"horizon": hz,
"day_in_test": day_in_test,
Expand Down
21 changes: 13 additions & 8 deletions epftoolbox2/models/base.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from abc import ABC, abstractmethod
from concurrent.futures import ProcessPoolExecutor, as_completed
from typing import List, Dict, Callable, Union, Tuple, Optional
from datetime import date

import gc
import inspect
import multiprocessing
Expand Down Expand Up @@ -34,7 +34,6 @@ def __init__(
self._hour_days: Dict[int, np.ndarray] = {}
self._offset: int = 0
self._target: str = ""
self._run_date: str = ""

@property
def _model_kwargs(self) -> Dict:
Expand Down Expand Up @@ -69,7 +68,6 @@ def run(
test_end = resolve_date(test_end)

self._target = target
self._run_date = date.today().isoformat()

self._data = self._preprocess(data, horizon, target)

Expand Down Expand Up @@ -126,7 +124,6 @@ def run(

in_memory = None if save_to else []
mp_context = multiprocessing.get_context("fork" if sys.platform != "win32" else "spawn")

with ProcessPoolExecutor(
max_workers=n_processes,
mp_context=mp_context,
Expand Down Expand Up @@ -192,8 +189,16 @@ def _cleanup(self) -> None:

def _preprocess(self, data: pd.DataFrame, horizon: int, target: str) -> pd.DataFrame:
data = data.drop(columns=["hour", "day"], errors="ignore")
new_cols = {f"{target}_d+{h}": data[target].shift(-24 * h) for h in range(1, horizon + 1)}
new_cols["day"] = pd.Series(np.arange(len(data)) // 24, index=data.index)

data = data[~data.index.duplicated(keep="first")]
new_cols = {}
for h in range(1, horizon + 1):
future_idx = data.index + pd.Timedelta(hours=24 * h)
aligned = data[target].reindex(future_idx)
aligned.index = data.index
new_cols[f"{target}_d+{h}"] = aligned
midnight = data.index.normalize()
new_cols["day"] = pd.Series((midnight - midnight[0]).days, index=data.index)
new_cols["hour"] = pd.Series(data.index.hour, index=data.index)
return pd.concat([data, pd.DataFrame(new_cols)], axis=1)

Expand Down Expand Up @@ -222,10 +227,10 @@ def _precompute_scalable_masks(
for hour in range(24):
days = self._hour_days[hour]
day = self._offset
day_min = day - self.training_window - 1
mask = (days >= day_min) & (days <= day)
hour_df = self._hour_data[hour]
for hz in range(1, horizon + 1):
day_min = day - self.training_window - hz
mask = (days >= day_min) & (days <= day)
preds = self._expand_predictors(hz, hour=hour)
sample_x = (
hour_df[mask][preds]
Expand Down
11 changes: 5 additions & 6 deletions examples/02_model_pipeline_only.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@
from epftoolbox2.exporters import ExcelExporter, TerminalExporter

if __name__ == "__main__":
df = pd.read_csv("data_output.csv", index_col=0, parse_dates=True)
df.index = pd.to_datetime(df.index, utc=True).tz_convert("Europe/Warsaw").tz_localize(None)
df = pd.read_csv("data_output.csv", index_col=0)
df.index = pd.to_datetime(df.index, utc=True).tz_convert("Europe/Warsaw")
print(f"Loaded {len(df)} rows")

seasonal_indicators = [
Expand All @@ -38,6 +38,8 @@
*seasonal_indicators,
"load_actual_d-1",
"load_actual_d-7",
"price_d-1",
"price_d-7",
"warsaw_temperature_2m_d+{horizon}",
]

Expand All @@ -59,7 +61,7 @@
)
)
.add_evaluator(MAEEvaluator())
.add_exporter(TerminalExporter())
.add_exporter(TerminalExporter(['horizon']))
.add_exporter(ExcelExporter("model_results.xlsx"))
)

Expand All @@ -77,6 +79,3 @@

print("\n=== Results by Hour ===")
print(report.by_hour())

print("\n=== Results by Horizon ===")
print(report.by_horizon())
17 changes: 6 additions & 11 deletions examples/03_full_workflow.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@

ENTSOE_API_KEY = os.environ.get("ENTSOE_API_KEY", "YOUR_API_KEY_HERE")

DATA_START = "2023-05-01"
DATA_END = "2024-08-01"
DATA_START = "2023-01-01"
DATA_END = "2024-04-01"

TEST_START = "2024-06-01"
TEST_END = "2024-07-01"
TEST_START = "2024-02-01"
TEST_END = "2024-03-01"
if __name__ == "__main__":

df = (
Expand All @@ -40,8 +40,6 @@
.add_validator(NullCheckValidator(columns=["load_actual", "price"]))
.run(start=DATA_START, end=DATA_END, cache=True)
)


seasonal_indicators = [
"is_monday_d+{horizon}",
"is_tuesday_d+{horizon}",
Expand All @@ -58,9 +56,9 @@
"load_actual",
*seasonal_indicators,
"load_actual_d-1",
"load_actual_d-2",
"load_actual_d-7",
"price_d-1",
"price_d-2",
"price_d-7",
lambda h: f"warsaw_temperature_2m_d+{h}",
]

Expand All @@ -81,6 +79,3 @@
horizon=7,
save_dir="results",
)

print(report.summary())
print(report.by_horizon())
Loading