Skip to content

Commit 66fbbae

Browse files
Merge pull request #91 from openclimatefix/issue/forecast-horizon
Issue/forecast horizon
2 parents 60259fa + cad1487 commit 66fbbae

File tree

3 files changed

+44
-94
lines changed

3 files changed

+44
-94
lines changed

nowcasting_datamodel/read/read.py

Lines changed: 14 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88
from datetime import datetime, timedelta, timezone
99
from typing import List, Optional
1010

11-
from sqlalchemy import desc
11+
from sqlalchemy import desc, text
1212
from sqlalchemy.orm import contains_eager, joinedload
1313
from sqlalchemy.orm.session import Session
1414

@@ -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,9 @@ 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.
273262
274-
return: List of forecasts objects from database
263+
:return: List of forecasts objects from database
275264
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:
283265
"""
284266
order_by_cols = []
285267

@@ -307,17 +289,6 @@ def get_latest_forecast_for_gsps(
307289
query=query, start_target_time=start_target_time, historic=historic
308290
)
309291

310-
from sqlalchemy import text
311-
312-
if forecast_horizon_minutes is not None:
313-
assert historic is False, Exception(
314-
"Loading a forecast horizon only works on non latest data."
315-
)
316-
query = query.join(ForecastValueSQL).filter(
317-
ForecastValueSQL.target_time - ForecastValueSQL.created_utc
318-
>= text(f"interval '{forecast_horizon_minutes} minute'")
319-
)
320-
321292
query = query.join(LocationSQL)
322293

323294
# option to preload values, makes querying quicker
@@ -374,6 +345,7 @@ def get_forecast_values(
374345
session: Session,
375346
gsp_id: Optional[int] = None,
376347
start_datetime: Optional[datetime] = None,
348+
forecast_horizon_minutes: Optional[int] = None,
377349
only_return_latest: Optional[bool] = False,
378350
) -> List[ForecastValueSQL]:
379351
"""
@@ -386,6 +358,9 @@ def get_forecast_values(
386358
If None is given then all are returned.
387359
:param only_return_latest: Optional to only return the latest forecast, not all of them.
388360
Default is False
361+
:param forecast_horizon_minutes: Optional filter on forecast horizon. For example
362+
forecast_horizon_minutes=120, means load the forecast than was made 2 hours before the
363+
target time. Note this only works for non-historic data.
389364
390365
return: List of forecasts values objects from database
391366
@@ -405,6 +380,14 @@ def get_forecast_values(
405380
query = query.filter(ForecastValueSQL.created_utc >= created_utc_filter)
406381
query = query.filter(ForecastSQL.created_utc >= created_utc_filter)
407382

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

tests/read/test_read_forecast.py

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

0 commit comments

Comments
 (0)