Reword the comments in velofy.forecasts

Each comment is one sentence or two short ones
joined by "=>", without a final period.
This commit is contained in:
Alexander Hess 2026-12-03 10:43:35 +01:00
commit 9b8fe8b8fd
Signed by: alexander
GPG key ID: B46EDB4DFC26805D
8 changed files with 20 additions and 34 deletions

View file

@ -31,8 +31,7 @@ def predict(
Raises: Raises:
ValueError: if `training_ts` contains `NaN` values ValueError: if `training_ts` contains `NaN` values
""" """
# Initialize R only if it is actually used. # Import R lazily => Code and tests not using it do not need R
# For example, the nox session "ci-tests-fast" does not use it.
from velofy import init_r # noqa:F401,PLC0415 from velofy import init_r # noqa:F401,PLC0415
# Re-seed R every time it is used to ensure reproducibility # Re-seed R every time it is used to ensure reproducibility

View file

@ -163,8 +163,7 @@ def stl( # noqa:C901,PLR0912,PLR0913,PLR0915
raise ValueError(msg) raise ValueError(msg)
robust = no > 0 robust = no > 0
# Initialize R only if it is actually used. # Import R lazily => Code and tests not using it do not need R
# For example, the nox session "ci-tests-fast" does not use it.
from velofy import init_r # noqa:F401,PLC0415 from velofy import init_r # noqa:F401,PLC0415
# Re-seed R every time it is used to ensure reproducibility # Re-seed R every time it is used to ensure reproducibility

View file

@ -32,8 +32,7 @@ def predict(
Raises: Raises:
ValueError: if `training_ts` contains `NaN` values ValueError: if `training_ts` contains `NaN` values
""" """
# Initialize R only if it is actually used. # Import R lazily => Code and tests not using it do not need R
# For example, the nox session "ci-tests-fast" does not use it.
from velofy import init_r # noqa:F401,PLC0415 from velofy import init_r # noqa:F401,PLC0415
# Re-seed R every time it is used to ensure reproducibility # Re-seed R every time it is used to ensure reproducibility

View file

@ -92,9 +92,8 @@ class ForecastingModelABC(abc.ABC):
).one_or_none(): ).one_or_none():
return cached_forecast return cached_forecast
# Horizontal and real-time `*Model`s return a `pd.DataFrame` with one # Horizontal and real-time `*Model`s predict one time step,
# row corresponding to the time step starting at `predict_at` # vertical ones all time steps of the day
# whereas vertical models return several rows, covering all time steps of a day
predictions = self.predict(pixel, predict_at, train_horizon) predictions = self.predict(pixel, predict_at, train_horizon)
# Convert the `predictions` into a `list` of `Forecast` objects # Convert the `predictions` into a `list` of `Forecast` objects

View file

@ -118,9 +118,8 @@ class HorizontalSMAModel(base.ForecastingModelABC):
msg = "the hsma model can only predict one step into the future" msg = "the hsma model can only predict one step into the future"
raise RuntimeError(msg) raise RuntimeError(msg)
# The "prediction" is calculated as the `np.mean()`. # The `training_ts` covers only full weeks
# As the `training_ts` covers only full week horizons, # => Its `np.mean()` needs no adjustment for weekly seasonality
# no adjustment regarding the weekly seasonality is needed.
predictions = pd.DataFrame( predictions = pd.DataFrame(
data={ data={
"actual": actuals_ts, "actual": actuals_ts,

View file

@ -81,10 +81,8 @@ class RealtimeARIMAModel(base.ForecastingModelABC):
seasonal_fit=False, seasonal_fit=False,
) )
# The overall `predictions` are the sum of the separate predictions above. # The seasonal extrapolation has no confidence interval
# As the linear extrapolation of the seasonal component has no # => The ARIMA model's one is shifted by the seasonal predictions
# confidence interval, we put the one from the ARIMA model around
# the extrapolated seasonal component.
predictions = pd.DataFrame( predictions = pd.DataFrame(
data={ data={
"actual": actuals_ts, "actual": actuals_ts,

View file

@ -81,10 +81,8 @@ class VerticalARIMAModel(base.ForecastingModelABC):
seasonal_fit=False, seasonal_fit=False,
) )
# The overall `predictions` are the sum of the separate predictions above. # The seasonal extrapolation has no confidence interval
# As the linear extrapolation of the seasonal component has no # => The ARIMA model's one is shifted by the seasonal predictions
# confidence interval, we put the one from the ARIMA model around
# the extrapolated seasonal component.
predictions = pd.DataFrame( predictions = pd.DataFrame(
data={ data={
"actual": actuals_ts, "actual": actuals_ts,

View file

@ -66,8 +66,8 @@ class OrderHistory:
def aggregate_orders(self) -> pd.DataFrame: # pragma: no cover def aggregate_orders(self) -> pd.DataFrame: # pragma: no cover
"""Generate and load all order totals from the database.""" """Generate and load all order totals from the database."""
# `data` is probably missing "pixel_id"-"start_at" pairs. # Without any demand in a `time_step`, `data` lacks
# This happens when there is no demand in the `Pixel` in the given `time_step`. # the corresponding "pixel_id"-"start_at" pair
data = pd.read_sql_query( data = pd.read_sql_query(
sa.text( sa.text(
f""" f"""
@ -165,8 +165,7 @@ class OrderHistory:
end_day.day, end_day.day,
pkg_config.SERVICE_END, pkg_config.SERVICE_END,
) )
# ... and all possible `tuple`s of "pixel_id"-"start_at" combinations. # ... and all "pixel_id"-"start_at" pairs within the operating hours
# The "start_at" values must lie within the operating hours.
gen = ( gen = (
(pixel_id, start_at) (pixel_id, start_at)
for pixel_id in sorted(index.levels[0]) for pixel_id in sorted(index.levels[0])
@ -287,9 +286,8 @@ class OrderHistory:
msg = "Internal error: cannot predict beyond the given data" msg = "Internal error: cannot predict beyond the given data"
raise RuntimeError(msg) raise RuntimeError(msg)
# The first and last training day are just before the `predict_at` day # The training days end just before the `predict_at` day and span
# and span exactly `train_horizon` weeks covering only the times of the # `train_horizon` weeks, but only at the time of day of `predict_at`
# day equal to the hour/minute of `predict_at`
first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon) first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon)
first_start_at = dt.datetime( first_start_at = dt.datetime(
first_train_day.year, first_train_day.year,
@ -456,9 +454,8 @@ class OrderHistory:
msg = "Internal error: cannot predict beyond the given data" msg = "Internal error: cannot predict beyond the given data"
raise RuntimeError(msg) raise RuntimeError(msg)
# The first and last training day are just before the `predict_at` day # The training days span `train_horizon` weeks before the `predict_at` day
# and span exactly `train_horizon` weeks covering all times of the day, # plus the times on the `predict_at` day before `predict_at`
# including times on the `predict_at` day that are earlier than `predict_at`
first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon) first_train_day = predict_at.date() - dt.timedelta(weeks=train_horizon)
first_start_at = dt.datetime( first_start_at = dt.datetime(
first_train_day.year, first_train_day.year,
@ -467,10 +464,8 @@ class OrderHistory:
pkg_config.SERVICE_START, pkg_config.SERVICE_START,
0, 0,
) )
# Predicting the first time step on the `predict_at` day is a corner case. # Predicting the day's first time step is a corner case
# Then, the previous day is indeed the `last_train_day`. Predicting any # => Only then is the previous day the `last_train_day`
# 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.
if predict_at.hour == pkg_config.SERVICE_START: if predict_at.hour == pkg_config.SERVICE_START:
last_train_day = predict_at.date() - dt.timedelta(days=1) last_train_day = predict_at.date() - dt.timedelta(days=1)
last_train_time = dt.time(pkg_config.SERVICE_END, 0) last_train_time = dt.time(pkg_config.SERVICE_END, 0)