Skip to content

Commit 84bfbb9

Browse files
committed
move forecast horizon to read forecast values
1 parent 78bbdd3 commit 84bfbb9

File tree

3 files changed

+49
-97
lines changed

3 files changed

+49
-97
lines changed

nowcasting_datamodel/read/read.py

Lines changed: 15 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,6 @@ def get_latest_forecast(
121121
gsp_id: Optional[int] = None,
122122
historic: bool = False,
123123
start_target_time: Optional[datetime] = None,
124-
forecast_horizon_minutes: Optional[int] = None,
125124
) -> ForecastSQL:
126125
"""
127126
Read forecasts
@@ -149,7 +148,6 @@ def get_latest_forecast(
149148
start_target_time=start_target_time,
150149
historic=historic,
151150
gsp_ids=gsp_ids,
152-
forecast_horizon_minutes=forecast_horizon_minutes,
153151
)
154152

155153
if forecasts is None:
@@ -217,7 +215,6 @@ def get_all_gsp_ids_latest_forecast(
217215
start_target_time: Optional[datetime] = None,
218216
preload_children: Optional[bool] = False,
219217
historic: bool = False,
220-
forecast_horizon_minutes: Optional[int] = None,
221218
) -> List[ForecastSQL]:
222219
"""
223220
Read forecasts
@@ -228,9 +225,6 @@ def get_all_gsp_ids_latest_forecast(
228225
Filter: forecast values target time should be larger than this datetime
229226
:param preload_children: Option to preload children. This is a speed up, if we need them.
230227
:param historic: Option to load historic values or not
231-
:param forecast_horizon_minutes: Optional filter on forecast horizon. For example
232-
forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
233-
target time. Note this only works for non-historic data.
234228
235229
return: List of forecasts objects from database
236230
"""
@@ -244,7 +238,6 @@ def get_all_gsp_ids_latest_forecast(
244238
preload_children=preload_children,
245239
historic=historic,
246240
gsp_ids=list(range(0, N_GSP + 1)),
247-
forecast_horizon_minutes=forecast_horizon_minutes,
248241
)
249242

250243

@@ -254,7 +247,6 @@ def get_latest_forecast_for_gsps(
254247
start_target_time: Optional[datetime] = None,
255248
preload_children: Optional[bool] = False,
256249
historic: bool = False,
257-
forecast_horizon_minutes: Optional[int] = None,
258250
gsp_ids: List[int] = None,
259251
):
260252
"""
@@ -267,19 +259,8 @@ def get_latest_forecast_for_gsps(
267259
:param preload_children: Option to preload children. This is a speed up, if we need them.
268260
:param historic: Option to load historic values or not
269261
:param gsp_ids: Option to filter on gsps. If None, then only the lastest forecast is loaded.
270-
:param forecast_horizon_minutes: Optional filter on forecast horizon. For example
271-
forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
272-
target time. Note this only works for non-historic data.
262+
List of forecasts objects from database
273263
274-
return: List of forecasts objects from database
275-
276-
:param session:
277-
:param start_created_utc:
278-
:param start_target_time:
279-
:param preload_children:
280-
:param historic:
281-
:param gsp_ids:
282-
:return:
283264
"""
284265
order_by_cols = []
285266

@@ -307,20 +288,6 @@ def get_latest_forecast_for_gsps(
307288
query=query, start_target_time=start_target_time, historic=historic
308289
)
309290

310-
if forecast_horizon_minutes is not None:
311-
assert historic is False, Exception(
312-
"Loading a forecast horizon only works on non latest data."
313-
)
314-
315-
# need to join the ForecastValueSQL table
316-
if start_target_time is None:
317-
query = query.join(ForecastValueSQL)
318-
319-
query = query.filter(
320-
ForecastValueSQL.target_time - ForecastValueSQL.created_utc
321-
>= text(f"interval '{forecast_horizon_minutes} minute'")
322-
)
323-
324291
query = query.join(LocationSQL)
325292

326293
# option to preload values, makes querying quicker
@@ -377,6 +344,7 @@ def get_forecast_values(
377344
session: Session,
378345
gsp_id: Optional[int] = None,
379346
start_datetime: Optional[datetime] = None,
347+
forecast_horizon_minutes: Optional[int] = None,
380348
only_return_latest: Optional[bool] = False,
381349
) -> List[ForecastValueSQL]:
382350
"""
@@ -389,6 +357,11 @@ def get_forecast_values(
389357
If None is given then all are returned.
390358
:param only_return_latest: Optional to only return the latest forecast, not all of them.
391359
Default is False
360+
:param forecast_horizon_minutes: Optional filter on forecast horizon. For example
361+
forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
362+
target time. Note this only works for non-historic data.
363+
364+
return:
392365
393366
return: List of forecasts values objects from database
394367
@@ -408,6 +381,14 @@ def get_forecast_values(
408381
query = query.filter(ForecastValueSQL.created_utc >= created_utc_filter)
409382
query = query.filter(ForecastSQL.created_utc >= created_utc_filter)
410383

384+
if forecast_horizon_minutes is not None:
385+
386+
# this seems to only work for postgres
387+
query = query.filter(
388+
ForecastValueSQL.target_time - ForecastValueSQL.created_utc
389+
>= text(f"interval '{forecast_horizon_minutes} minute'")
390+
)
391+
411392
# filter on gsp_id
412393
if gsp_id is not None:
413394
query = query.join(ForecastSQL)

tests/read/test_read_forecast.py

Lines changed: 0 additions & 63 deletions
This file was deleted.
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
import logging
2+
from datetime import datetime, timezone
3+
4+
from nowcasting_datamodel.fake import (
5+
make_fake_forecast,
6+
)
7+
from nowcasting_datamodel.read.read import (
8+
get_forecast_values,
9+
)
10+
11+
logger = logging.getLogger(__name__)
12+
13+
14+
def test_get_latest_forecast_created_utc_gsp(db_session):
15+
t0_datetime_utc = datetime(2022, 1, 1, 12, tzinfo=timezone.utc)
16+
17+
f1 = make_fake_forecast(gsp_id=1, session=db_session, t0_datetime_utc=t0_datetime_utc)
18+
f2 = make_fake_forecast(gsp_id=1, session=db_session, t0_datetime_utc=t0_datetime_utc)
19+
20+
f1.forecast_values[0].created_utc = datetime(2022, 1, 1, 10, tzinfo=timezone.utc)
21+
f1.forecast_values[1].created_utc = datetime(2022, 1, 1, 10, tzinfo=timezone.utc)
22+
23+
f2.forecast_values[0].created_utc = datetime(2022, 1, 1, 12, tzinfo=timezone.utc)
24+
f2.forecast_values[1].created_utc = datetime(2022, 1, 1, 12, tzinfo=timezone.utc)
25+
26+
db_session.add_all([f1, f2])
27+
db_session.commit()
28+
29+
forecast_values = get_forecast_values(
30+
session=db_session, forecast_horizon_minutes=120, gsp_id=1
31+
)
32+
assert len(forecast_values) == 2
33+
assert forecast_values[0].created_utc == datetime(2022, 1, 1, 10, tzinfo=timezone.utc)
34+
assert forecast_values[0].target_time == datetime(2022, 1, 1, 12, tzinfo=timezone.utc)

0 commit comments

Comments
 (0)