diff --git a/src/velofy/forecasts/methods/arima.py b/src/velofy/forecasts/methods/arima.py index da5a7da..7a59247 100644 --- a/src/velofy/forecasts/methods/arima.py +++ b/src/velofy/forecasts/methods/arima.py @@ -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 diff --git a/src/velofy/forecasts/methods/decomposition.py b/src/velofy/forecasts/methods/decomposition.py index ec71fec..cb932e8 100644 --- a/src/velofy/forecasts/methods/decomposition.py +++ b/src/velofy/forecasts/methods/decomposition.py @@ -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 diff --git a/src/velofy/forecasts/methods/ets.py b/src/velofy/forecasts/methods/ets.py index 021786e..6ecc193 100644 --- a/src/velofy/forecasts/methods/ets.py +++ b/src/velofy/forecasts/methods/ets.py @@ -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 diff --git a/src/velofy/forecasts/models/base.py b/src/velofy/forecasts/models/base.py index 0c490a8..8a6e14b 100644 --- a/src/velofy/forecasts/models/base.py +++ b/src/velofy/forecasts/models/base.py @@ -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 diff --git a/src/velofy/forecasts/models/tactical/horizontal.py b/src/velofy/forecasts/models/tactical/horizontal.py index fa11e35..bf934fc 100644 --- a/src/velofy/forecasts/models/tactical/horizontal.py +++ b/src/velofy/forecasts/models/tactical/horizontal.py @@ -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, diff --git a/src/velofy/forecasts/models/tactical/realtime.py b/src/velofy/forecasts/models/tactical/realtime.py index 81dad8d..369605c 100644 --- a/src/velofy/forecasts/models/tactical/realtime.py +++ b/src/velofy/forecasts/models/tactical/realtime.py @@ -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, diff --git a/src/velofy/forecasts/models/tactical/vertical.py b/src/velofy/forecasts/models/tactical/vertical.py index ae972d2..4d2528a 100644 --- a/src/velofy/forecasts/models/tactical/vertical.py +++ b/src/velofy/forecasts/models/tactical/vertical.py @@ -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, diff --git a/src/velofy/forecasts/timify.py b/src/velofy/forecasts/timify.py index 61d09ba..09727b2 100644 --- a/src/velofy/forecasts/timify.py +++ b/src/velofy/forecasts/timify.py @@ -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)