Query with select() in the forecasting models
`Session.query()` is the legacy API in SQLAlchemy 2.x. `.one_or_none()` expresses that the unique constraint on the five filtered columns allows at most one cached `Forecast`.
This commit is contained in:
parent
2eb6b5769f
commit
185a37f7d4
1 changed files with 10 additions and 9 deletions
|
|
@ -5,6 +5,7 @@ import datetime as dt
|
||||||
import typing as t
|
import typing as t
|
||||||
|
|
||||||
import pandas as pd
|
import pandas as pd
|
||||||
|
import sqlalchemy as sa
|
||||||
|
|
||||||
from velofy import db
|
from velofy import db
|
||||||
from velofy.forecasts import timify
|
from velofy.forecasts import timify
|
||||||
|
|
@ -80,15 +81,15 @@ class ForecastingModelABC(abc.ABC):
|
||||||
Raises:
|
Raises:
|
||||||
RuntimeError: `*Model.predict()` misses the prediction for `predict_at`
|
RuntimeError: `*Model.predict()` misses the prediction for `predict_at`
|
||||||
"""
|
"""
|
||||||
if (
|
if cached_forecast := db.session.scalars(
|
||||||
cached_forecast := db.session.query(db.Forecast)
|
sa.select(db.Forecast).filter_by(
|
||||||
.filter_by(pixel=pixel)
|
pixel=pixel,
|
||||||
.filter_by(start_at=predict_at)
|
start_at=predict_at,
|
||||||
.filter_by(time_step=self._order_history.time_step)
|
time_step=self._order_history.time_step,
|
||||||
.filter_by(train_horizon=train_horizon)
|
train_horizon=train_horizon,
|
||||||
.filter_by(model=self.name)
|
model=self.name,
|
||||||
.first()
|
),
|
||||||
):
|
).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 return a `pd.DataFrame` with one
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue