Upgrade black

This commit is contained in:
Alexander Hess 2026-11-25 10:24:07 +01:00
commit dbdce83067
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
8 changed files with 1208 additions and 914 deletions

View file

@ -50,7 +50,9 @@ def vertical_datetime_index():
which is `12` for `LONG_TIME_STEP` values.
"""
start_ats = pd.date_range(
test_config.START, test_config.END, freq=f'{test_config.LONG_TIME_STEP}T',
test_config.START,
test_config.END,
freq=f'{test_config.LONG_TIME_STEP}T',
)
gen = (
start_at
@ -103,7 +105,9 @@ def order_totals(good_pixel_id):
pixel_ids = [good_pixel_id, good_pixel_id + 1]
start_ats = pd.date_range(
test_config.START, test_config.END, freq=f'{test_config.LONG_TIME_STEP}T',
test_config.START,
test_config.END,
freq=f'{test_config.LONG_TIME_STEP}T',
)
gen = (
(pixel_id, start_at)

View file

@ -22,14 +22,18 @@ class TestInvalidArguments:
with pytest.raises(ValueError, match='`NaN` values'):
decomposition.stl(
time_series, frequency=test_config.VERTICAL_FREQUENCY_LONG, ns=NS,
time_series,
frequency=test_config.VERTICAL_FREQUENCY_LONG,
ns=NS,
)
def test_ns_not_odd(self, vertical_no_demand):
"""`ns` must be odd and `>= 7`."""
with pytest.raises(ValueError, match='`ns`'):
decomposition.stl(
vertical_no_demand, frequency=test_config.VERTICAL_FREQUENCY_LONG, ns=8,
vertical_no_demand,
frequency=test_config.VERTICAL_FREQUENCY_LONG,
ns=8,
)
@pytest.mark.parametrize('ns', [-99, -1, 1, 5])
@ -197,7 +201,9 @@ class TestValidArguments:
def test_structure_of_returned_dataframe(self, vertical_no_demand):
"""`stl()` returns a `pd.DataFrame` with three columns."""
result = decomposition.stl(
vertical_no_demand, frequency=test_config.VERTICAL_FREQUENCY_LONG, ns=NS,
vertical_no_demand,
frequency=test_config.VERTICAL_FREQUENCY_LONG,
ns=NS,
)
assert isinstance(result, pd.DataFrame)
@ -216,7 +222,18 @@ class TestValidArguments:
@pytest.mark.parametrize('ni', [2, 3])
@pytest.mark.parametrize('no', [0, 1])
def test_decompose_time_series_with_no_demand( # noqa:WPS211,WPS216
self, vertical_no_demand, nt, nl, ds, dt, dl, js, jt, jl, ni, no, # noqa:WPS110
self,
vertical_no_demand,
nt,
nl,
ds,
dt,
dl,
js,
jt,
jl,
ni,
no, # noqa:WPS110
):
"""Decomposing a time series with no demand ...

View file

@ -21,14 +21,24 @@ def forecast_interval():
"""
future_day = test_config.END.date() + dt.timedelta(days=1)
first_start_at = dt.datetime(
future_day.year, future_day.month, future_day.day, config.SERVICE_START, 0,
future_day.year,
future_day.month,
future_day.day,
config.SERVICE_START,
0,
)
end_of_day = dt.datetime(
future_day.year, future_day.month, future_day.day, config.SERVICE_END, 0,
future_day.year,
future_day.month,
future_day.day,
config.SERVICE_END,
0,
)
start_ats = pd.date_range(
first_start_at, end_of_day, freq=f'{test_config.LONG_TIME_STEP}T',
first_start_at,
end_of_day,
freq=f'{test_config.LONG_TIME_STEP}T',
)
gen = (
start_at
@ -48,7 +58,11 @@ def forecast_time_step():
future_day = test_config.END.date() + dt.timedelta(days=1)
start_at = dt.datetime(
future_day.year, future_day.month, future_day.day, test_config.NOON, 0,
future_day.year,
future_day.month,
future_day.day,
test_config.NOON,
0,
)
index = pd.Index([start_at])
@ -59,13 +73,17 @@ def forecast_time_step():
@pytest.mark.r
@pytest.mark.parametrize(
'func', [arima.predict, ets.predict, extrapolate_season.predict],
'func',
[arima.predict, ets.predict, extrapolate_season.predict],
)
class TestMakePredictions:
"""Make predictions with `arima.predict()` and `ets.predict()`."""
def test_training_data_contains_nan_values(
self, func, vertical_no_demand, forecast_interval,
self,
func,
vertical_no_demand,
forecast_interval,
):
"""`training_ts` must not contain `NaN` values."""
vertical_no_demand.iloc[0] = pd.NA
@ -78,7 +96,10 @@ class TestMakePredictions:
)
def test_structure_of_returned_dataframe(
self, func, vertical_no_demand, forecast_interval,
self,
func,
vertical_no_demand,
forecast_interval,
):
"""Both `.predict()` return a `pd.DataFrame` with five columns."""
result = func(
@ -97,7 +118,10 @@ class TestMakePredictions:
]
def test_predict_horizontal_time_series_with_no_demand(
self, func, horizontal_no_demand, forecast_time_step,
self,
func,
horizontal_no_demand,
forecast_time_step,
):
"""Predicting a horizontal time series with no demand ...
@ -114,7 +138,10 @@ class TestMakePredictions:
assert result == 0
def test_predict_vertical_time_series_with_no_demand(
self, func, vertical_no_demand, forecast_interval,
self,
func,
vertical_no_demand,
forecast_interval,
):
"""Predicting a vertical time series with no demand ...

View file

@ -1,6 +1,5 @@
"""Tests for the `velofy.forecasts.models` sub-package."""
import pandas as pd
import pytest
@ -62,7 +61,11 @@ class TestGenericForecastingModelProperties:
@pytest.mark.r
def test_make_prediction_structure(
self, model_cls, order_history, pixel, predict_at,
self,
model_cls,
order_history,
pixel,
predict_at,
):
"""`*Model.predict()` returns a `pd.DataFrame` ...
@ -88,7 +91,11 @@ class TestGenericForecastingModelProperties:
@pytest.mark.r
def test_make_prediction_for_given_time_step(
self, model_cls, order_history, pixel, predict_at,
self,
model_cls,
order_history,
pixel,
predict_at,
):
"""`*Model.predict()` returns a row for ...
@ -106,7 +113,11 @@ class TestGenericForecastingModelProperties:
@pytest.mark.r
def test_make_prediction_contains_actual_values(
self, model_cls, order_history, pixel, predict_at,
self,
model_cls,
order_history,
pixel,
predict_at,
):
"""`*Model.predict()` returns a `pd.DataFrame` ...
@ -126,7 +137,12 @@ class TestGenericForecastingModelProperties:
@pytest.mark.db
@pytest.mark.r
def test_make_forecast( # noqa:WPS211
self, db_session, model_cls, order_history, pixel, predict_at,
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)
@ -145,7 +161,12 @@ class TestGenericForecastingModelProperties:
@pytest.mark.db
@pytest.mark.r
def test_make_forecast_is_cached( # noqa:WPS211
self, db_session, model_cls, order_history, pixel, predict_at,
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)

View file

@ -57,7 +57,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 0 # noqa:WPS507 sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -65,7 +66,11 @@ class TestAggregateOrders:
assert len(result) == 0 # noqa:WPS507
def test_evenly_distributed_ad_hoc_orders(
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""12 ad-hoc orders, one per operating hour."""
# Create one order per hour and 12 orders in total.
@ -82,7 +87,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 12 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -94,7 +100,11 @@ class TestAggregateOrders:
assert result['n_orders'].sum() == 12
def test_evenly_distributed_ad_hoc_orders_with_no_demand_late( # noqa:WPS218
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""10 ad-hoc orders, one per hour, no orders after 21."""
# Create one order per hour and 10 orders in total.
@ -111,7 +121,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 10 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -125,7 +136,11 @@ class TestAggregateOrders:
assert result.iloc[10:]['n_orders'].sum() == 0
def test_one_ad_hoc_order_every_other_hour(
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""6 ad-hoc orders, one every other hour."""
# Create one order every other hour.
@ -142,7 +157,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 6 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -154,7 +170,11 @@ class TestAggregateOrders:
assert result['n_orders'].sum() == 6
def test_one_ad_hoc_and_one_pre_order(
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""1 ad-hoc and 1 scheduled order.
@ -180,7 +200,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 2 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -192,7 +213,11 @@ class TestAggregateOrders:
assert result['n_orders'].sum() == 1
def test_evenly_distributed_ad_hoc_orders_with_half_hour_time_steps( # noqa:WPS218
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""12 ad-hoc orders, one per hour, with 30 minute time windows.
@ -212,7 +237,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 12 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.SHORT_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.SHORT_TIME_STEP,
)
result = oh.aggregate_orders()
@ -226,7 +252,11 @@ class TestAggregateOrders:
assert result.iloc[1::2]['n_orders'].sum() == 0
def test_ad_hoc_orders_over_two_days(
self, db_session, one_pixel_grid, restaurant, make_order,
self,
db_session,
one_pixel_grid,
restaurant,
make_order,
):
"""First day 12 ad-hoc orders, one per operating hour ...
@ -262,7 +292,8 @@ class TestAggregateOrders:
assert len(restaurant.orders) == 18 # sanity check
oh = timify.OrderHistory(
grid=one_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=one_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()
@ -275,7 +306,12 @@ class TestAggregateOrders:
@pytest.fixture
def two_pixel_grid( # noqa:WPS211
self, db_session, city, make_address, make_restaurant, addresses_mock,
self,
db_session,
city,
make_address,
make_restaurant,
addresses_mock,
):
"""A persisted `Grid` with two `Pixel` objects."""
# One `Address` in the lower-left `Pixel`, ...
@ -301,7 +337,10 @@ class TestAggregateOrders:
return grid
def test_two_pixels_with_shifted_orders( # noqa:WPS218
self, db_session, two_pixel_grid, make_order,
self,
db_session,
two_pixel_grid,
make_order,
):
"""One restaurant with one order every other hour ...
@ -356,7 +395,8 @@ class TestAggregateOrders:
assert len(restaurant2.orders) == 24
oh = timify.OrderHistory(
grid=two_pixel_grid, time_step=test_config.LONG_TIME_STEP,
grid=two_pixel_grid,
time_step=test_config.LONG_TIME_STEP,
)
result = oh.aggregate_orders()

View file

@ -16,7 +16,10 @@ class TestAverageDailyDemand:
"""Tests for the `OrderHistory.avg_daily_demand()` method."""
def test_avg_daily_demand_with_constant_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""The average daily demand must be the number of time steps ...
@ -34,7 +37,10 @@ class TestAverageDailyDemand:
assert result == 12.0
def test_avg_daily_demand_with_no_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""Without demand, the average daily demand must be `0.0`."""
order_history._data.loc[:, 'n_orders'] = 0
@ -52,7 +58,10 @@ class TestChooseTacticalModel:
"""Tests for the `OrderHistory.choose_tactical_model()` method."""
def test_best_model_with_high_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""With high demand, the average daily demand is `.>= 25.0`."""
# With 12 time steps per day, the ADD becomes `36.0`.
@ -67,7 +76,10 @@ class TestChooseTacticalModel:
assert isinstance(result, models.HorizontalETSModel)
def test_best_model_with_medium_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""With medium demand, the average daily demand is `>= 10.0` and `< 25.0`."""
# With 12 time steps per day, the ADD becomes `24.0`.
@ -82,7 +94,10 @@ class TestChooseTacticalModel:
assert isinstance(result, models.HorizontalETSModel)
def test_best_model_with_low_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""With low demand, the average daily demand is `>= 2.5` and `< 10.0`."""
# With 12 time steps per day, the ADD becomes `12.0` ...
@ -115,7 +130,10 @@ class TestChooseTacticalModel:
assert isinstance(result, models.HorizontalSMAModel)
def test_best_model_with_no_demand(
self, order_history, good_pixel_id, predict_at,
self,
order_history,
good_pixel_id,
predict_at,
):
"""Without demand, the average daily demand is `< 2.5`."""
order_history._data.loc[:, 'n_orders'] = 0
@ -129,7 +147,10 @@ class TestChooseTacticalModel:
assert isinstance(result, models.TrivialModel)
def test_best_model_for_unknown_train_horizon(
self, order_history, good_pixel_id, predict_at, # noqa:RST215
self,
order_history,
good_pixel_id,
predict_at, # noqa:RST215
):
"""For `train_horizon` values not included in the rule-based system ...

View file

@ -38,7 +38,11 @@ def bad_predict_at():
"""
predict_day = test_config.END - datetime.timedelta(weeks=6, days=1)
return datetime.datetime(
predict_day.year, predict_day.month, predict_day.day, test_config.NOON, 0,
predict_day.year,
predict_day.month,
predict_day.day,
test_config.NOON,
0,
)
@ -57,7 +61,11 @@ class TestMakeHorizontalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_are_series(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The time series come as a `pd.Series`."""
result = order_history.make_horizontal_ts(
@ -75,7 +83,11 @@ class TestMakeHorizontalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_have_correct_length(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The length of a training time series must be a multiple of `7` ...
@ -94,7 +106,11 @@ class TestMakeHorizontalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_frequency_is_number_of_weekdays(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The `frequency` must be `7`."""
result = order_history.make_horizontal_ts(
@ -109,7 +125,11 @@ class TestMakeHorizontalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_no_long_enough_history1(
self, order_history, good_pixel_id, bad_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
bad_predict_at,
train_horizon,
):
"""If the `predict_at` day is too early in the `START`-`END` horizon ...
@ -123,7 +143,10 @@ class TestMakeHorizontalTimeSeries:
)
def test_no_long_enough_history2(
self, order_history, good_pixel_id, good_predict_at,
self,
order_history,
good_pixel_id,
good_predict_at,
):
"""If the `train_horizon` is longer than the `START`-`END` horizon ...
@ -131,7 +154,9 @@ class TestMakeHorizontalTimeSeries:
"""
with pytest.raises(RuntimeError):
order_history.make_horizontal_ts(
pixel_id=good_pixel_id, predict_at=good_predict_at, train_horizon=999,
pixel_id=good_pixel_id,
predict_at=good_predict_at,
train_horizon=999,
)
@ -150,7 +175,11 @@ class TestMakeVerticalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_are_series(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The time series come as `pd.Series`."""
result = order_history.make_vertical_ts(
@ -168,7 +197,11 @@ class TestMakeVerticalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_have_correct_length(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The length of a training time series is the product of the ...
@ -197,7 +230,11 @@ class TestMakeVerticalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_frequency_is_number_number_of_weekly_time_steps(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The `frequency` is the number of weekly time steps."""
result = order_history.make_vertical_ts(
@ -218,7 +255,11 @@ class TestMakeVerticalTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_no_long_enough_history1(
self, order_history, good_pixel_id, bad_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
bad_predict_at,
train_horizon,
):
"""If the `predict_at` day is too early in the `START`-`END` horizon ...
@ -232,7 +273,10 @@ class TestMakeVerticalTimeSeries:
)
def test_no_long_enough_history2(
self, order_history, good_pixel_id, good_predict_at,
self,
order_history,
good_pixel_id,
good_predict_at,
):
"""If the `train_horizon` is longer than the `START`-`END` horizon ...
@ -261,7 +305,11 @@ class TestMakeRealTimeTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_are_series(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The time series come as `pd.Series`."""
result = order_history.make_realtime_ts(
@ -279,7 +327,11 @@ class TestMakeRealTimeTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_have_correct_length1(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The length of a training time series is the product of the ...
@ -297,7 +349,9 @@ class TestMakeRealTimeTimeSeries:
0,
)
result = order_history.make_realtime_ts(
pixel_id=good_pixel_id, predict_at=predict_at, train_horizon=train_horizon,
pixel_id=good_pixel_id,
predict_at=predict_at,
train_horizon=train_horizon,
)
training_ts, _, actuals_ts = result
@ -313,7 +367,11 @@ class TestMakeRealTimeTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_time_series_have_correct_length2(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The length of a training time series is the product of the ...
@ -352,7 +410,11 @@ class TestMakeRealTimeTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_frequency_is_number_number_of_weekly_time_steps(
self, order_history, good_pixel_id, good_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
good_predict_at,
train_horizon,
):
"""The `frequency` is the number of weekly time steps."""
result = order_history.make_realtime_ts(
@ -373,7 +435,11 @@ class TestMakeRealTimeTimeSeries:
@pytest.mark.parametrize('train_horizon', test_config.TRAIN_HORIZONS)
def test_no_long_enough_history1(
self, order_history, good_pixel_id, bad_predict_at, train_horizon,
self,
order_history,
good_pixel_id,
bad_predict_at,
train_horizon,
):
"""If the `predict_at` day is too early in the `START`-`END` horizon ...
@ -387,7 +453,10 @@ class TestMakeRealTimeTimeSeries:
)
def test_no_long_enough_history2(
self, order_history, good_pixel_id, good_predict_at,
self,
order_history,
good_pixel_id,
good_predict_at,
):
"""If the `train_horizon` is longer than the `START`-`END` horizon ...
@ -395,5 +464,7 @@ class TestMakeRealTimeTimeSeries:
"""
with pytest.raises(RuntimeError):
order_history.make_realtime_ts(
pixel_id=good_pixel_id, predict_at=good_predict_at, train_horizon=999,
pixel_id=good_pixel_id,
predict_at=good_predict_at,
train_horizon=999,
)

View file

@ -73,7 +73,8 @@ class TestMethods:
def test_first_order_at_non_existing_pixel(self, order_history, good_pixel_id):
"""Test `OrderHistory.first_order_at()` with bad input."""
with pytest.raises(
LookupError, match='`pixel_id` is not in the `grid`',
LookupError,
match='`pixel_id` is not in the `grid`',
):
order_history.first_order_at(-1)
@ -87,6 +88,7 @@ class TestMethods:
def test_last_order_at_non_existing_pixel(self, order_history, good_pixel_id):
"""Test `OrderHistory.last_order_at()` with bad input."""
with pytest.raises(
LookupError, match='`pixel_id` is not in the `grid`',
LookupError,
match='`pixel_id` is not in the `grid`',
):
order_history.last_order_at(-1)