2021-02-01 11:32:10 +01:00
|
|
|
"""Test all the `*.predict()` functions in the `methods` sub-package."""
|
2021-01-11 20:17:00 +01:00
|
|
|
|
|
|
|
|
import datetime as dt
|
|
|
|
|
|
|
|
|
|
import pandas as pd
|
|
|
|
|
import pytest
|
|
|
|
|
|
|
|
|
|
from tests import config as test_config
|
2026-11-23 18:06:53 +01:00
|
|
|
from velofy import config
|
|
|
|
|
from velofy.forecasts.methods import arima
|
|
|
|
|
from velofy.forecasts.methods import ets
|
|
|
|
|
from velofy.forecasts.methods import extrapolate_season
|
2021-01-11 20:17:00 +01:00
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def forecast_interval():
|
|
|
|
|
"""A `pd.Index` with `DateTime` values ...
|
|
|
|
|
|
|
|
|
|
... that takes place one day after the `START`-`END` horizon and
|
|
|
|
|
resembles an entire day (`12` "start_at" values as we use `LONG_TIME_STEP`).
|
|
|
|
|
"""
|
|
|
|
|
future_day = test_config.END.date() + dt.timedelta(days=1)
|
|
|
|
|
first_start_at = dt.datetime(
|
2026-11-25 10:24:07 +01:00
|
|
|
future_day.year,
|
|
|
|
|
future_day.month,
|
|
|
|
|
future_day.day,
|
|
|
|
|
config.SERVICE_START,
|
|
|
|
|
0,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
end_of_day = dt.datetime(
|
2026-11-25 10:24:07 +01:00
|
|
|
future_day.year,
|
|
|
|
|
future_day.month,
|
|
|
|
|
future_day.day,
|
|
|
|
|
config.SERVICE_END,
|
|
|
|
|
0,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
2026-11-24 16:46:28 +01:00
|
|
|
start_ats = pd.date_range(
|
2026-11-25 10:24:07 +01:00
|
|
|
first_start_at,
|
|
|
|
|
end_of_day,
|
2026-11-28 03:08:55 +01:00
|
|
|
freq=f"{test_config.LONG_TIME_STEP}min",
|
2026-11-24 16:46:28 +01:00
|
|
|
)
|
2021-01-11 20:17:00 +01:00
|
|
|
gen = (
|
|
|
|
|
start_at
|
2026-11-24 16:46:28 +01:00
|
|
|
for start_at in start_ats
|
2021-01-11 20:17:00 +01:00
|
|
|
if config.SERVICE_START <= start_at.hour < config.SERVICE_END
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
index = pd.Index(gen)
|
2026-11-28 03:08:55 +01:00
|
|
|
index.name = "start_at"
|
2021-01-11 20:17:00 +01:00
|
|
|
|
|
|
|
|
return index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.fixture
|
|
|
|
|
def forecast_time_step():
|
|
|
|
|
"""A `pd.Index` with one `DateTime` value, resembling `NOON`."""
|
|
|
|
|
future_day = test_config.END.date() + dt.timedelta(days=1)
|
|
|
|
|
|
|
|
|
|
start_at = dt.datetime(
|
2026-11-25 10:24:07 +01:00
|
|
|
future_day.year,
|
|
|
|
|
future_day.month,
|
|
|
|
|
future_day.day,
|
|
|
|
|
test_config.NOON,
|
|
|
|
|
0,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
index = pd.Index([start_at])
|
2026-11-28 03:08:55 +01:00
|
|
|
index.name = "start_at"
|
2021-01-11 20:17:00 +01:00
|
|
|
|
|
|
|
|
return index
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.r
|
2021-02-01 11:32:10 +01:00
|
|
|
@pytest.mark.parametrize(
|
2026-11-28 03:08:55 +01:00
|
|
|
"func",
|
2026-11-25 10:24:07 +01:00
|
|
|
[arima.predict, ets.predict, extrapolate_season.predict],
|
2021-02-01 11:32:10 +01:00
|
|
|
)
|
2021-01-11 20:17:00 +01:00
|
|
|
class TestMakePredictions:
|
|
|
|
|
"""Make predictions with `arima.predict()` and `ets.predict()`."""
|
|
|
|
|
|
|
|
|
|
def test_training_data_contains_nan_values(
|
2026-11-25 10:24:07 +01:00
|
|
|
self,
|
|
|
|
|
func,
|
|
|
|
|
vertical_no_demand,
|
|
|
|
|
forecast_interval,
|
2021-01-11 20:17:00 +01:00
|
|
|
):
|
|
|
|
|
"""`training_ts` must not contain `NaN` values."""
|
|
|
|
|
vertical_no_demand.iloc[0] = pd.NA
|
|
|
|
|
|
2026-11-28 03:08:55 +01:00
|
|
|
with pytest.raises(ValueError, match="must not contain `NaN`"):
|
2021-01-11 20:17:00 +01:00
|
|
|
func(
|
|
|
|
|
training_ts=vertical_no_demand,
|
|
|
|
|
forecast_interval=forecast_interval,
|
2021-02-01 11:32:10 +01:00
|
|
|
frequency=test_config.VERTICAL_FREQUENCY_LONG,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
def test_structure_of_returned_dataframe(
|
2026-11-25 10:24:07 +01:00
|
|
|
self,
|
|
|
|
|
func,
|
|
|
|
|
vertical_no_demand,
|
|
|
|
|
forecast_interval,
|
2021-01-11 20:17:00 +01:00
|
|
|
):
|
|
|
|
|
"""Both `.predict()` return a `pd.DataFrame` with five columns."""
|
|
|
|
|
result = func(
|
|
|
|
|
training_ts=vertical_no_demand,
|
|
|
|
|
forecast_interval=forecast_interval,
|
2021-02-01 11:32:10 +01:00
|
|
|
frequency=test_config.VERTICAL_FREQUENCY_LONG,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
assert isinstance(result, pd.DataFrame)
|
|
|
|
|
assert list(result.columns) == [
|
2026-11-28 03:08:55 +01:00
|
|
|
"prediction",
|
|
|
|
|
"low80",
|
|
|
|
|
"high80",
|
|
|
|
|
"low95",
|
|
|
|
|
"high95",
|
2021-01-11 20:17:00 +01:00
|
|
|
]
|
|
|
|
|
|
|
|
|
|
def test_predict_horizontal_time_series_with_no_demand(
|
2026-11-25 10:24:07 +01:00
|
|
|
self,
|
|
|
|
|
func,
|
|
|
|
|
horizontal_no_demand,
|
|
|
|
|
forecast_time_step,
|
2021-01-11 20:17:00 +01:00
|
|
|
):
|
|
|
|
|
"""Predicting a horizontal time series with no demand ...
|
|
|
|
|
|
|
|
|
|
... returns a `pd.DataFrame` with five columns holding only `0.0` values.
|
|
|
|
|
"""
|
|
|
|
|
predictions = func(
|
|
|
|
|
training_ts=horizontal_no_demand,
|
|
|
|
|
forecast_interval=forecast_time_step,
|
|
|
|
|
frequency=7,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = predictions.sum().sum()
|
|
|
|
|
|
|
|
|
|
assert result == 0
|
|
|
|
|
|
|
|
|
|
def test_predict_vertical_time_series_with_no_demand(
|
2026-11-25 10:24:07 +01:00
|
|
|
self,
|
|
|
|
|
func,
|
|
|
|
|
vertical_no_demand,
|
|
|
|
|
forecast_interval,
|
2021-01-11 20:17:00 +01:00
|
|
|
):
|
|
|
|
|
"""Predicting a vertical time series with no demand ...
|
|
|
|
|
|
|
|
|
|
... returns a `pd.DataFrame` with five columns holding only `0.0` values.
|
|
|
|
|
"""
|
|
|
|
|
predictions = func(
|
|
|
|
|
training_ts=vertical_no_demand,
|
|
|
|
|
forecast_interval=forecast_interval,
|
2021-02-01 11:32:10 +01:00
|
|
|
frequency=test_config.VERTICAL_FREQUENCY_LONG,
|
2021-01-11 20:17:00 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
|
|
result = predictions.sum().sum()
|
|
|
|
|
|
|
|
|
|
assert result == 0
|