Rename Forecast.training_horizon into .train_horizon

- we use that shorter name in `urban_meal_delivery.forecasts.*`
  and want to be consistent in the ORM layer as well
This commit is contained in:
Alexander Hess 2021-02-02 13:04:43 +01:00
parent 6fd16f2a6c
commit 3f5b4a50bb
Signed by: alexander
GPG key ID: 344EA5AB10D868E0
5 changed files with 63 additions and 15 deletions

View file

@ -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

View file

@ -27,7 +27,7 @@ class Forecast(meta.Base):
pixel_id = sa.Column(sa.Integer, nullable=False, index=True) pixel_id = sa.Column(sa.Integer, nullable=False, index=True)
start_at = sa.Column(sa.DateTime, nullable=False) start_at = sa.Column(sa.DateTime, nullable=False)
time_step = sa.Column(sa.SmallInteger, 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) model = sa.Column(sa.Unicode(length=20), nullable=False)
# We also store the actual order counts for convenient retrieval. # We also store the actual order counts for convenient retrieval.
# A `UniqueConstraint` below ensures that redundant values that # 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('time_step > 0', name='time_step_must_be_positive'),
sa.CheckConstraint( 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('actual >= 0', name='actuals_must_be_non_negative'),
sa.CheckConstraint( sa.CheckConstraint(
@ -124,7 +124,7 @@ class Forecast(meta.Base):
), ),
# There can be only one prediction per forecasting setting. # There can be only one prediction per forecasting setting.
sa.UniqueConstraint( 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, cls,
pixel: db.Pixel, pixel: db.Pixel,
time_step: int, time_step: int,
training_horizon: int, train_horizon: int,
model: str, model: str,
data: pd.Dataframe, data: pd.Dataframe,
) -> List[db.Forecast]: ) -> List[db.Forecast]:
@ -166,7 +166,7 @@ class Forecast(meta.Base):
Args: Args:
pixel: in which the forecast is made pixel: in which the forecast is made
time_step: length of one time step in minutes 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 model: name of the forecasting model
data: a `pd.Dataframe` as described above (i.e., data: a `pd.Dataframe` as described above (i.e.,
with the six columns holding `float`s) with the six columns holding `float`s)
@ -214,7 +214,7 @@ class Forecast(meta.Base):
pixel=pixel, pixel=pixel,
start_at=start_at, start_at=start_at,
time_step=time_step, time_step=time_step,
training_horizon=training_horizon, train_horizon=train_horizon,
model=model, model=model,
actual=actual, actual=actual,
prediction=prediction, prediction=prediction,

View file

@ -79,7 +79,7 @@ class ForecastingModelABC(abc.ABC):
.filter_by(pixel=pixel) .filter_by(pixel=pixel)
.filter_by(start_at=predict_at) .filter_by(start_at=predict_at)
.filter_by(time_step=self._order_history.time_step) .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) .filter_by(model=self.name)
.first() .first()
) : ) :
@ -94,7 +94,7 @@ class ForecastingModelABC(abc.ABC):
forecasts = db.Forecast.from_dataframe( forecasts = db.Forecast.from_dataframe(
pixel=pixel, pixel=pixel,
time_step=self._order_history.time_step, time_step=self._order_history.time_step,
training_horizon=train_horizon, train_horizon=train_horizon,
model=self.name, model=self.name,
data=predictions, data=predictions,
) )

View file

@ -28,7 +28,7 @@ def forecast(pixel):
pixel=pixel, pixel=pixel,
start_at=start_at, start_at=start_at,
time_step=test_config.LONG_TIME_STEP, time_step=test_config.LONG_TIME_STEP,
training_horizon=test_config.LONG_TRAIN_HORIZON, train_horizon=test_config.LONG_TRAIN_HORIZON,
model=MODEL, model=MODEL,
actual=12, actual=12,
prediction=12.3, prediction=12.3,
@ -143,9 +143,9 @@ class TestConstraints:
db_session.commit() db_session.commit()
@pytest.mark.parametrize('value', [-1, 0]) @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.""" """Insert an instance with invalid data."""
forecast.training_horizon = value forecast.train_horizon = value
db_session.add(forecast) db_session.add(forecast)
with pytest.raises( with pytest.raises(
@ -418,7 +418,7 @@ class TestConstraints:
pixel=forecast.pixel, pixel=forecast.pixel,
start_at=forecast.start_at, start_at=forecast.start_at,
time_step=forecast.time_step, time_step=forecast.time_step,
training_horizon=forecast.training_horizon, train_horizon=forecast.train_horizon,
model=forecast.model, model=forecast.model,
actual=forecast.actual, actual=forecast.actual,
prediction=2, prediction=2,
@ -479,7 +479,7 @@ class TestFromDataFrameConstructor:
forecasts = db.Forecast.from_dataframe( forecasts = db.Forecast.from_dataframe(
pixel=pixel, pixel=pixel,
time_step=test_config.LONG_TIME_STEP, time_step=test_config.LONG_TIME_STEP,
training_horizon=test_config.LONG_TRAIN_HORIZON, train_horizon=test_config.LONG_TRAIN_HORIZON,
model=MODEL, model=MODEL,
data=prediction_data, data=prediction_data,
) )
@ -496,7 +496,7 @@ class TestFromDataFrameConstructor:
forecasts = db.Forecast.from_dataframe( forecasts = db.Forecast.from_dataframe(
pixel=pixel, pixel=pixel,
time_step=test_config.LONG_TIME_STEP, time_step=test_config.LONG_TIME_STEP,
training_horizon=test_config.LONG_TRAIN_HORIZON, train_horizon=test_config.LONG_TRAIN_HORIZON,
model=MODEL, model=MODEL,
data=prediction_data, data=prediction_data,
) )

View file

@ -140,7 +140,7 @@ class TestGenericForecastingModelProperties:
assert isinstance(result, db.Forecast) assert isinstance(result, db.Forecast)
assert result.pixel == pixel assert result.pixel == pixel
assert result.start_at == predict_at 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.db
@pytest.mark.r @pytest.mark.r