Clean up the comments and lint the notebooks #22
8 changed files with 20 additions and 34 deletions
Reword the comments in velofy.forecasts
Each comment is one sentence or two short ones joined by "=>", without a final period.
commit
9b8fe8b8fd
|
|
@ -31,8 +31,7 @@ def predict(
|
|||
Raises:
|
||||
ValueError: if `training_ts` contains `NaN` values
|
||||
"""
|
||||
# Initialize R only if it is actually used.
|
||||
# For example, the nox session "ci-tests-fast" does not use it.
|
||||
# Import R lazily => Code and tests not using it do not need R
|
||||
from velofy import init_r # noqa:F401,PLC0415
|
||||
|
||||
# Re-seed R every time it is used to ensure reproducibility
|
||||
|
|
|
|||
|
|
@ -163,8 +163,7 @@ def stl( # noqa:C901,PLR0912,PLR0913,PLR0915
|
|||
raise ValueError(msg)
|
||||
robust = no > 0
|
||||
|
||||
# Initialize R only if it is actually used.
|
||||
# For example, the nox session "ci-tests-fast" does not use it.
|
||||
# Import R lazily => Code and tests not using it do not need R
|
||||
from velofy import init_r # noqa:F401,PLC0415
|
||||
|
||||
# Re-seed R every time it is used to ensure reproducibility
|
||||
|
|
|
|||
|
|
@ -32,8 +32,7 @@ def predict(
|
|||
Raises:
|
||||
ValueError: if `training_ts` contains `NaN` values
|
||||
"""
|
||||
# Initialize R only if it is actually used.
|
||||
# For example, the nox session "ci-tests-fast" does not use it.
|
||||
# Import R lazily => Code and tests not using it do not need R
|
||||
from velofy import init_r # noqa:F401,PLC0415
|
||||
|
||||
# Re-seed R every time it is used to ensure reproducibility
|
||||
|
|
|
|||
|
|
@ -92,9 +92,8 @@ class ForecastingModelABC(abc.ABC):
|
|||
).one_or_none():
|
||||
return cached_forecast
|
||||
|
||||
# Horizontal and real-time `*Model`s return a `pd.DataFrame` with one
|
||||
# row corresponding to the time step starting at `predict_at`
|
||||
# whereas vertical models return several rows, covering all time steps of a day
|
||||
# Horizontal and real-time `*Model`s predict one time step,
|
||||
# vertical ones all time steps of the day
|
||||
predictions = self.predict(pixel, predict_at, train_horizon)
|
||||
|
||||
# Convert the `predictions` into a `list` of `Forecast` objects
|
||||
|
|
|
|||
|
|
@ -118,9 +118,8 @@ class HorizontalSMAModel(base.ForecastingModelABC):
|
|||
msg = "the hsma model can only predict one step into the future"
|
||||
raise RuntimeError(msg)
|
||||
|
||||
# The "prediction" is calculated as the `np.mean()`.
|
||||
# As the `training_ts` covers only full week horizons,
|
||||
# no adjustment regarding the weekly seasonality is needed.
|
||||
# The `training_ts` covers only full weeks
|
||||
# => Its `np.mean()` needs no adjustment for weekly seasonality
|
||||
predictions = pd.DataFrame(
|
||||
data={
|
||||
"actual": actuals_ts,
|
||||
|
|
|
|||
|
|
@ -81,10 +81,8 @@ class RealtimeARIMAModel(base.ForecastingModelABC):
|
|||
seasonal_fit=False,
|
||||
)
|
||||
|
||||
# The overall `predictions` are the sum of the separate predictions above.
|
||||
# As the linear extrapolation of the seasonal component has no
|
||||
# confidence interval, we put the one from the ARIMA model around
|
||||
# the extrapolated seasonal component.
|
||||
# The seasonal extrapolation has no confidence interval
|
||||
# => The ARIMA model's one is shifted by the seasonal predictions
|
||||
predictions = pd.DataFrame(
|
||||
data={
|
||||
"actual": actuals_ts,
|
||||
|
|
|
|||
|
|
@ -81,10 +81,8 @@ class VerticalARIMAModel(base.ForecastingModelABC):
|
|||
seasonal_fit=False,
|
||||
)
|
||||
|
||||
# The overall `predictions` are the sum of the separate predictions above.
|
||||
# As the linear extrapolation of the seasonal component has no
|
||||
# confidence interval, we put the one from the ARIMA model around
|
||||
# the extrapolated seasonal component.
|
||||
# The seasonal extrapolation has no confidence interval
|
||||
# => The ARIMA model's one is shifted by the seasonal predictions
|
||||
predictions = pd.DataFrame(
|
||||
data={
|
||||
"actual": actuals_ts,
|
||||
|
|
|
|||
|
|
@ -66,8 +66,8 @@ class OrderHistory:
|
|||
|
||||
def aggregate_orders(self) -> pd.DataFrame: # pragma: no cover
|
||||
"""Generate and load all order totals from the database."""
|
||||
# `data` is probably missing "pixel_id"-"start_at" pairs.
|
||||
# This happens when there is no demand in the `Pixel` in the given `time_step`.
|
||||
# Without any demand in a `time_step`, `data` lacks
|
||||
# the corresponding "pixel_id"-"start_at" pair
|
||||
data = pd.read_sql_query(
|
||||
sa.text(
|
||||
f"""
|
||||
|
|
@ -165,8 +165,7 @@ class OrderHistory:
|
|||
end_day.day,
|
||||
pkg_config.SERVICE_END,
|
||||
)
|
||||
# ... and all possible `tuple`s of "pixel_id"-"start_at" combinations.
|
||||
# The "start_at" values must lie within the operating hours.
|
||||
# ... and all "pixel_id"-"start_at" pairs within the operating hours
|
||||
gen = (
|
||||
(pixel_id, start_at)
|
||||
for pixel_id in sorted(index.levels[0])
|
||||
|
|
@ -287,9 +286,8 @@ class OrderHistory:
|
|||
msg = "Internal error: cannot predict beyond the given data"
|
||||
raise RuntimeError(msg)
|
||||
|
||||
# The first and last training day are just before the `predict_at` day
|
||||
# and span exactly `train_horizon` weeks covering only the times of the
|
||||
# day equal to the hour/minute of `predict_at`
|
||||
# The training days end just before the `predict_at` day and span
|
||||
# `train_horizon` weeks, but only at the time of day of `predict_at`
|
||||
first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon)
|
||||
first_start_at = dt.datetime(
|
||||
first_train_day.year,
|
||||
|
|
@ -456,9 +454,8 @@ class OrderHistory:
|
|||
msg = "Internal error: cannot predict beyond the given data"
|
||||
raise RuntimeError(msg)
|
||||
|
||||
# The first and last training day are just before the `predict_at` day
|
||||
# and span exactly `train_horizon` weeks covering all times of the day,
|
||||
# including times on the `predict_at` day that are earlier than `predict_at`
|
||||
# The training days span `train_horizon` weeks before the `predict_at` day
|
||||
# plus the times on the `predict_at` day before `predict_at`
|
||||
first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon)
|
||||
first_start_at = dt.datetime(
|
||||
first_train_day.year,
|
||||
|
|
@ -467,10 +464,8 @@ class OrderHistory:
|
|||
pkg_config.SERVICE_START,
|
||||
0,
|
||||
)
|
||||
# Predicting the first time step on the `predict_at` day is a corner case.
|
||||
# Then, the previous day is indeed the `last_train_day`. Predicting any
|
||||
# other time step implies that the `predict_at` day is the `last_train_day`.
|
||||
# `last_train_time` is the last "start_at" before the one being predicted.
|
||||
# Predicting the day's first time step is a corner case
|
||||
# => Only then is the previous day the `last_train_day`
|
||||
if predict_at.hour == pkg_config.SERVICE_START:
|
||||
last_train_day = predict_at.date() - dt.timedelta(days=1)
|
||||
last_train_time = dt.time(pkg_config.SERVICE_END, 0)
|
||||
|
|
|
|||
Loading…
Reference in a new issue