diff --git a/migrations/versions/rev_20210202_12_8bfb928a31f8_rename_training_horizon_into_train_horizon.py b/migrations/versions/rev_20210202_12_8bfb928a31f8_rename_training_horizon_into_train_horizon.py new file mode 100644 index 0000000..fb5fc93 --- /dev/null +++ b/migrations/versions/rev_20210202_12_8bfb928a31f8_rename_training_horizon_into_train_horizon.py @@ -0,0 +1,48 @@ +"""Rename `Forecast.training_horizon` into `.train_horizon`. + +Revision: #8bfb928a31f8 at 2021-02-02 12:55:09 +Revises: #c2af85bada01 +""" + +import os + +from alembic import op + +from urban_meal_delivery import configuration + + +revision = '8bfb928a31f8' +down_revision = 'c2af85bada01' +branch_labels = None +depends_on = None + + +config = configuration.make_config('testing' if os.getenv('TESTING') else 'production') + + +def upgrade(): + """Upgrade to revision 8bfb928a31f8.""" + op.execute( + f""" + ALTER TABLE + {config.CLEAN_SCHEMA}.forecasts + RENAME COLUMN + training_horizon + TO + train_horizon; + """, + ) # noqa:WPS355 + + +def downgrade(): + """Downgrade to revision c2af85bada01.""" + op.execute( + f""" + ALTER TABLE + {config.CLEAN_SCHEMA}.forecasts + RENAME COLUMN + train_horizon + TO + training_horizon; + """, + ) # noqa:WPS355 diff --git a/src/urban_meal_delivery/db/forecasts.py b/src/urban_meal_delivery/db/forecasts.py index 0937f97..a85fa74 100644 --- a/src/urban_meal_delivery/db/forecasts.py +++ b/src/urban_meal_delivery/db/forecasts.py @@ -27,7 +27,7 @@ class Forecast(meta.Base): pixel_id = sa.Column(sa.Integer, nullable=False, index=True) start_at = sa.Column(sa.DateTime, nullable=False) time_step = sa.Column(sa.SmallInteger, nullable=False) - training_horizon = sa.Column(sa.SmallInteger, nullable=False) + train_horizon = sa.Column(sa.SmallInteger, nullable=False) model = sa.Column(sa.Unicode(length=20), nullable=False) # We also store the actual order counts for convenient retrieval. # A `UniqueConstraint` below ensures that redundant values that @@ -71,7 +71,7 @@ class Forecast(meta.Base): ), sa.CheckConstraint('time_step > 0', name='time_step_must_be_positive'), sa.CheckConstraint( - 'training_horizon > 0', name='training_horizon_must_be_positive', + 'train_horizon > 0', name='training_horizon_must_be_positive', ), sa.CheckConstraint('actual >= 0', name='actuals_must_be_non_negative'), sa.CheckConstraint( @@ -124,7 +124,7 @@ class Forecast(meta.Base): ), # There can be only one prediction per forecasting setting. sa.UniqueConstraint( - 'pixel_id', 'start_at', 'time_step', 'training_horizon', 'model', + 'pixel_id', 'start_at', 'time_step', 'train_horizon', 'model', ), ) @@ -146,7 +146,7 @@ class Forecast(meta.Base): cls, pixel: db.Pixel, time_step: int, - training_horizon: int, + train_horizon: int, model: str, data: pd.Dataframe, ) -> List[db.Forecast]: @@ -166,7 +166,7 @@ class Forecast(meta.Base): Args: pixel: in which the forecast is made time_step: length of one time step in minutes - training_horizon: length of the training horizon in weeks + train_horizon: length of the training horizon in weeks model: name of the forecasting model data: a `pd.Dataframe` as described above (i.e., with the six columns holding `float`s) @@ -214,7 +214,7 @@ class Forecast(meta.Base): pixel=pixel, start_at=start_at, time_step=time_step, - training_horizon=training_horizon, + train_horizon=train_horizon, model=model, actual=actual, prediction=prediction, diff --git a/src/urban_meal_delivery/forecasts/models/base.py b/src/urban_meal_delivery/forecasts/models/base.py index 8be733a..9a9cd72 100644 --- a/src/urban_meal_delivery/forecasts/models/base.py +++ b/src/urban_meal_delivery/forecasts/models/base.py @@ -79,7 +79,7 @@ class ForecastingModelABC(abc.ABC): .filter_by(pixel=pixel) .filter_by(start_at=predict_at) .filter_by(time_step=self._order_history.time_step) - .filter_by(training_horizon=train_horizon) + .filter_by(train_horizon=train_horizon) .filter_by(model=self.name) .first() ) : @@ -94,7 +94,7 @@ class ForecastingModelABC(abc.ABC): forecasts = db.Forecast.from_dataframe( pixel=pixel, time_step=self._order_history.time_step, - training_horizon=train_horizon, + train_horizon=train_horizon, model=self.name, data=predictions, ) diff --git a/tests/db/test_forecasts.py b/tests/db/test_forecasts.py index a2cd1bb..ff37dda 100644 --- a/tests/db/test_forecasts.py +++ b/tests/db/test_forecasts.py @@ -28,7 +28,7 @@ def forecast(pixel): pixel=pixel, start_at=start_at, time_step=test_config.LONG_TIME_STEP, - training_horizon=test_config.LONG_TRAIN_HORIZON, + train_horizon=test_config.LONG_TRAIN_HORIZON, model=MODEL, actual=12, prediction=12.3, @@ -143,9 +143,9 @@ class TestConstraints: db_session.commit() @pytest.mark.parametrize('value', [-1, 0]) - def test_positive_training_horizon(self, db_session, forecast, value): + def test_positive_train_horizon(self, db_session, forecast, value): """Insert an instance with invalid data.""" - forecast.training_horizon = value + forecast.train_horizon = value db_session.add(forecast) with pytest.raises( @@ -418,7 +418,7 @@ class TestConstraints: pixel=forecast.pixel, start_at=forecast.start_at, time_step=forecast.time_step, - training_horizon=forecast.training_horizon, + train_horizon=forecast.train_horizon, model=forecast.model, actual=forecast.actual, prediction=2, @@ -479,7 +479,7 @@ class TestFromDataFrameConstructor: forecasts = db.Forecast.from_dataframe( pixel=pixel, time_step=test_config.LONG_TIME_STEP, - training_horizon=test_config.LONG_TRAIN_HORIZON, + train_horizon=test_config.LONG_TRAIN_HORIZON, model=MODEL, data=prediction_data, ) @@ -496,7 +496,7 @@ class TestFromDataFrameConstructor: forecasts = db.Forecast.from_dataframe( pixel=pixel, time_step=test_config.LONG_TIME_STEP, - training_horizon=test_config.LONG_TRAIN_HORIZON, + train_horizon=test_config.LONG_TRAIN_HORIZON, model=MODEL, data=prediction_data, ) diff --git a/tests/forecasts/test_models.py b/tests/forecasts/test_models.py index 19a21d2..ef24d3c 100644 --- a/tests/forecasts/test_models.py +++ b/tests/forecasts/test_models.py @@ -140,7 +140,7 @@ class TestGenericForecastingModelProperties: assert isinstance(result, db.Forecast) assert result.pixel == pixel assert result.start_at == predict_at - assert result.training_horizon == test_config.LONG_TRAIN_HORIZON + assert result.train_horizon == test_config.LONG_TRAIN_HORIZON @pytest.mark.db @pytest.mark.r