Add urban_meal_delivery.forecasts.models sub-package
- `*Model`s use the `methods.*.predict()` functions to predict demand given an order time series generated by `timify.OrderHistory` - `models.base.ForecastingModelABC` unifies how all `*Model`s work and implements a caching strategy - implement three `*Model`s for tactical forecasting, based on the hets, varima, and rtarima models described in the first research paper - add overall documentation for `urban_meal_delivery.forecasts` package - move the fixtures in `tests.forecasts.timify.conftest` to `tests.forecasts.conftest` and adjust the horizon of the test horizon from two to three weeks
This commit is contained in:
parent
796fdc919c
commit
67cd58cf16
12 changed files with 747 additions and 71 deletions
|
|
@ -1,4 +1,4 @@
|
|||
"""Fixtures and globals for testing `urban_meal_delivery.forecasts`."""
|
||||
"""Fixtures for testing the `urban_meal_delivery.forecasts` sub-package."""
|
||||
|
||||
import datetime as dt
|
||||
|
||||
|
|
@ -7,6 +7,7 @@ import pytest
|
|||
|
||||
from tests import config as test_config
|
||||
from urban_meal_delivery import config
|
||||
from urban_meal_delivery.forecasts import timify
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
|
|
@ -28,7 +29,10 @@ def horizontal_datetime_index():
|
|||
index = pd.Index(gen)
|
||||
index.name = 'start_at'
|
||||
|
||||
assert len(index) == 15 # sanity check
|
||||
# Sanity check.
|
||||
# `+1` as both the `START` and `END` day are included.
|
||||
n_days = (test_config.END - test_config.START).days + 1
|
||||
assert len(index) == n_days
|
||||
|
||||
return index
|
||||
|
||||
|
|
@ -58,7 +62,10 @@ def vertical_datetime_index():
|
|||
index = pd.Index(gen)
|
||||
index.name = 'start_at'
|
||||
|
||||
assert len(index) == 15 * 12 # sanity check
|
||||
# Sanity check: n_days * n_number_of_opening_hours.
|
||||
# `+1` as both the `START` and `END` day are included.
|
||||
n_days = (test_config.END - test_config.START).days + 1
|
||||
assert len(index) == n_days * 12
|
||||
|
||||
return index
|
||||
|
||||
|
|
@ -67,3 +74,54 @@ def vertical_datetime_index():
|
|||
def vertical_no_demand(vertical_datetime_index):
|
||||
"""A vertical time series with order totals: no demand."""
|
||||
return pd.Series(0, index=vertical_datetime_index, name='n_orders')
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def good_pixel_id(pixel):
|
||||
"""A `pixel_id` that is on the `grid`."""
|
||||
return pixel.id # `== 1`
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def order_totals(good_pixel_id):
|
||||
"""A mock for `OrderHistory.totals`.
|
||||
|
||||
To be a bit more realistic, we sample two pixels on the `grid`.
|
||||
|
||||
Uses the LONG_TIME_STEP as the length of a time step.
|
||||
"""
|
||||
pixel_ids = [good_pixel_id, good_pixel_id + 1]
|
||||
|
||||
gen = (
|
||||
(pixel_id, start_at)
|
||||
for pixel_id in pixel_ids
|
||||
for start_at in pd.date_range(
|
||||
test_config.START, test_config.END, freq=f'{test_config.LONG_TIME_STEP}T',
|
||||
)
|
||||
if config.SERVICE_START <= start_at.hour < config.SERVICE_END
|
||||
)
|
||||
|
||||
# Re-index `data` filling in `0`s where there is no demand.
|
||||
index = pd.MultiIndex.from_tuples(gen)
|
||||
index.names = ['pixel_id', 'start_at']
|
||||
|
||||
df = pd.DataFrame(data={'n_orders': 1}, index=index)
|
||||
|
||||
# Sanity check: n_pixels * n_time_steps_per_day * n_days.
|
||||
# `+1` as both the `START` and `END` day are included.
|
||||
n_days = (test_config.END - test_config.START).days + 1
|
||||
assert len(df) == 2 * 12 * n_days
|
||||
|
||||
return df
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def order_history(order_totals, grid):
|
||||
"""An `OrderHistory` object that does not need the database.
|
||||
|
||||
Uses the LONG_TIME_STEP as the length of a time step.
|
||||
"""
|
||||
oh = timify.OrderHistory(grid=grid, time_step=test_config.LONG_TIME_STEP)
|
||||
oh._data = order_totals
|
||||
|
||||
return oh
|
||||
|
|
|
|||
181
tests/forecasts/test_models.py
Normal file
181
tests/forecasts/test_models.py
Normal file
|
|
@ -0,0 +1,181 @@
|
|||
"""Tests for the `urban_meal_delivery.forecasts.models` sub-package."""
|
||||
|
||||
import datetime as dt
|
||||
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from tests import config as test_config
|
||||
from urban_meal_delivery import db
|
||||
from urban_meal_delivery.forecasts import models
|
||||
|
||||
|
||||
MODELS = (
|
||||
models.HorizontalETSModel,
|
||||
models.RealtimeARIMAModel,
|
||||
models.VerticalARIMAModel,
|
||||
)
|
||||
|
||||
|
||||
@pytest.mark.parametrize('model_cls', MODELS)
|
||||
class TestGenericForecastingModelProperties:
|
||||
"""Test everything all concrete `*Model`s have in common.
|
||||
|
||||
The test cases here replace testing the `ForecastingModelABC` class on its own.
|
||||
|
||||
As uncertainty is in the nature of forecasting, we do not test the individual
|
||||
point forecasts or confidence intervals themselves. Instead, we confirm
|
||||
that all the `*Model`s adhere to the `ForecastingModelABC` generically.
|
||||
So, these test cases are more like integration tests conceptually.
|
||||
|
||||
Also, note that some `methods.*.predict()` functions use R behind the scenes.
|
||||
""" # noqa:RST215
|
||||
|
||||
def test_create_model(self, model_cls, order_history):
|
||||
"""Test instantiation of a new and concrete `*Model` object."""
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
assert model is not None
|
||||
|
||||
def test_model_has_a_name(self, model_cls, order_history):
|
||||
"""Access the `*Model.name` property."""
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
result = model.name
|
||||
|
||||
assert isinstance(result, str)
|
||||
|
||||
unique_model_names = set()
|
||||
|
||||
def test_each_model_has_a_unique_name(self, model_cls, order_history):
|
||||
"""The `*Model.name` values must be unique across all `*Model`s.
|
||||
|
||||
Important: this test case has a side effect that is visible
|
||||
across the different parametrized versions of this case!
|
||||
""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
assert model.name not in self.unique_model_names
|
||||
|
||||
self.unique_model_names.add(model.name)
|
||||
|
||||
@pytest.fixture
|
||||
def predict_at(self) -> dt.datetime:
|
||||
"""`NOON` on the day to be predicted."""
|
||||
return dt.datetime(
|
||||
test_config.END.year,
|
||||
test_config.END.month,
|
||||
test_config.END.day,
|
||||
test_config.NOON,
|
||||
)
|
||||
|
||||
@pytest.mark.r
|
||||
def test_make_prediction_structure(
|
||||
self, model_cls, order_history, pixel, predict_at,
|
||||
):
|
||||
"""`*Model.predict()` returns a `pd.DataFrame` ...
|
||||
|
||||
... with known columns.
|
||||
""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
result = model.predict(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
assert isinstance(result, pd.DataFrame)
|
||||
assert list(result.columns) == [
|
||||
'actual',
|
||||
'prediction',
|
||||
'low80',
|
||||
'high80',
|
||||
'low95',
|
||||
'high95',
|
||||
]
|
||||
|
||||
@pytest.mark.r
|
||||
def test_make_prediction_for_given_time_step(
|
||||
self, model_cls, order_history, pixel, predict_at,
|
||||
):
|
||||
"""`*Model.predict()` returns a row for ...
|
||||
|
||||
... the time step starting at `predict_at`.
|
||||
""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
result = model.predict(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
assert predict_at in result.index
|
||||
|
||||
@pytest.mark.r
|
||||
def test_make_prediction_contains_actual_values(
|
||||
self, model_cls, order_history, pixel, predict_at,
|
||||
):
|
||||
"""`*Model.predict()` returns a `pd.DataFrame` ...
|
||||
|
||||
... where the "actual" and "prediction" columns must not be empty.
|
||||
""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
result = model.predict(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
assert not result['actual'].isnull().any()
|
||||
assert not result['prediction'].isnull().any()
|
||||
|
||||
@pytest.mark.db
|
||||
@pytest.mark.r
|
||||
def test_make_forecast( # noqa:WPS211
|
||||
self, db_session, model_cls, order_history, pixel, predict_at,
|
||||
):
|
||||
"""`*Model.make_forecast()` returns a `Forecast` object.""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
result = model.make_forecast(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
assert isinstance(result, db.Forecast)
|
||||
assert result.pixel == pixel
|
||||
assert result.start_at == predict_at
|
||||
assert result.training_horizon == test_config.LONG_TRAIN_HORIZON
|
||||
|
||||
@pytest.mark.db
|
||||
@pytest.mark.r
|
||||
def test_make_forecast_is_cached( # noqa:WPS211
|
||||
self, db_session, model_cls, order_history, pixel, predict_at,
|
||||
):
|
||||
"""`*Model.make_forecast()` caches the `Forecast` object.""" # noqa:RST215
|
||||
model = model_cls(order_history=order_history)
|
||||
|
||||
assert db_session.query(db.Forecast).count() == 0
|
||||
|
||||
result1 = model.make_forecast(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
n_cached_forecasts = db_session.query(db.Forecast).count()
|
||||
assert n_cached_forecasts >= 1
|
||||
|
||||
result2 = model.make_forecast(
|
||||
pixel=pixel,
|
||||
predict_at=predict_at,
|
||||
train_horizon=test_config.LONG_TRAIN_HORIZON,
|
||||
)
|
||||
|
||||
assert n_cached_forecasts == db_session.query(db.Forecast).count()
|
||||
|
||||
assert result1 == result2
|
||||
|
|
@ -1,57 +0,0 @@
|
|||
"""Fixture for testing the `urban_meal_delivery.forecast.timify` module."""
|
||||
|
||||
import pandas as pd
|
||||
import pytest
|
||||
|
||||
from tests import config as test_config
|
||||
from urban_meal_delivery import config
|
||||
from urban_meal_delivery.forecasts import timify
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def good_pixel_id(pixel):
|
||||
"""A `pixel_id` that is on the `grid`."""
|
||||
return pixel.id # `== 1`
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def order_totals(good_pixel_id):
|
||||
"""A mock for `OrderHistory.totals`.
|
||||
|
||||
To be a bit more realistic, we sample two pixels on the `grid`.
|
||||
|
||||
Uses the LONG_TIME_STEP as the length of a time step.
|
||||
"""
|
||||
pixel_ids = [good_pixel_id, good_pixel_id + 1]
|
||||
|
||||
gen = (
|
||||
(pixel_id, start_at)
|
||||
for pixel_id in pixel_ids
|
||||
for start_at in pd.date_range(
|
||||
test_config.START, test_config.END, freq=f'{test_config.LONG_TIME_STEP}T',
|
||||
)
|
||||
if config.SERVICE_START <= start_at.hour < config.SERVICE_END
|
||||
)
|
||||
|
||||
# Re-index `data` filling in `0`s where there is no demand.
|
||||
index = pd.MultiIndex.from_tuples(gen)
|
||||
index.names = ['pixel_id', 'start_at']
|
||||
|
||||
df = pd.DataFrame(data={'n_orders': 1}, index=index)
|
||||
|
||||
# Sanity check: n_pixels * n_time_steps_per_day * n_weekdays * n_weeks.
|
||||
assert len(df) == 2 * 12 * (7 * 2 + 1)
|
||||
|
||||
return df
|
||||
|
||||
|
||||
@pytest.fixture
|
||||
def order_history(order_totals, grid):
|
||||
"""An `OrderHistory` object that does not need the database.
|
||||
|
||||
Uses the LONG_TIME_STEP as the length of a time step.
|
||||
"""
|
||||
oh = timify.OrderHistory(grid=grid, time_step=test_config.LONG_TIME_STEP)
|
||||
oh._data = order_totals
|
||||
|
||||
return oh
|
||||
|
|
@ -17,8 +17,8 @@ from urban_meal_delivery import config
|
|||
def good_predict_at():
|
||||
"""A `predict_at` within `START`-`END` and ...
|
||||
|
||||
... a long enough history so that either `train_horizon=1`
|
||||
or `train_horizon=2` works.
|
||||
... a long enough history so that either `SHORT_TRAIN_HORIZON`
|
||||
or `LONG_TRAIN_HORIZON` works.
|
||||
"""
|
||||
return datetime.datetime(
|
||||
test_config.END.year,
|
||||
|
|
@ -33,10 +33,10 @@ def good_predict_at():
|
|||
def bad_predict_at():
|
||||
"""A `predict_at` within `START`-`END` but ...
|
||||
|
||||
... not a long enough history so that both `train_horizon=1`
|
||||
and `train_horizon=2` do not work.
|
||||
... not a long enough history so that both `SHORT_TRAIN_HORIZON`
|
||||
and `LONG_TRAIN_HORIZON` do not work.
|
||||
"""
|
||||
predict_day = test_config.END - datetime.timedelta(weeks=1, days=1)
|
||||
predict_day = test_config.END - datetime.timedelta(weeks=2, days=1)
|
||||
return datetime.datetime(
|
||||
predict_day.year, predict_day.month, predict_day.day, test_config.NOON, 0,
|
||||
)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue