Skip to content

Commit 2ddd860

Browse files
Issue/no nan adjust mw (#195)
* TDD: add a test, to make sure adjust mw are zero * force adjust_mw to be 0.0 if me not found * fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix * fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci * fix * [pre-commit.ci] auto fixes from pre-commit.com hooks for more information, see https://pre-commit.ci --------- Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
1 parent 9763c58 commit 2ddd860

File tree

3 files changed

+41
-11
lines changed

3 files changed

+41
-11
lines changed

nowcasting_datamodel/read/read_metric.py

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -132,12 +132,13 @@ def read_latest_me_national(
132132
metric_values = query.all()
133133

134134
# add timezone, show be the same datetime interval for all
135-
datetime_interval = metric_values[0].datetime_interval
136-
datetime_interval.start_datetime_utc = datetime_interval.start_datetime_utc.replace(
137-
tzinfo=timezone.utc
138-
)
139-
datetime_interval.end_datetime_utc = datetime_interval.end_datetime_utc.replace(
140-
tzinfo=timezone.utc
141-
)
135+
if len(metric_values) > 0:
136+
datetime_interval = metric_values[0].datetime_interval
137+
datetime_interval.start_datetime_utc = datetime_interval.start_datetime_utc.replace(
138+
tzinfo=timezone.utc
139+
)
140+
datetime_interval.end_datetime_utc = datetime_interval.end_datetime_utc.replace(
141+
tzinfo=timezone.utc
142+
)
142143

143144
return metric_values

nowcasting_datamodel/save/adjust.py

Lines changed: 19 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
from datetime import datetime, timedelta
44
from typing import List, Union
55

6+
import numpy as np
67
import pandas as pd
78

89
from nowcasting_datamodel.models import Forecast, ForecastSQL, MetricValue, MetricValueSQL
@@ -56,8 +57,10 @@ def add_adjust_to_national_forecast(forecast: ForecastSQL, session):
5657

5758
# 1. read metric values
5859
latest_me = read_latest_me_national(session=session, model_name=model_name)
59-
assert len(latest_me) > 0
60-
logger.debug(f"Found {len(latest_me)} latest ME values")
60+
if len(latest_me) == 0:
61+
logger.warning(f"Found no ME values found for {model_name=}")
62+
else:
63+
logger.debug(f"Found {len(latest_me)} latest ME values")
6164

6265
# 2. filter value down to now onwards
6366
# get the number of hours to go ahead, we've added 1 to make sure we use the last one as well
@@ -78,9 +81,19 @@ def add_adjust_to_national_forecast(forecast: ForecastSQL, session):
7881

7982
# add value to ForecastValueSQL
8083
forecast_value.adjust_mw = value
84+
if np.isnan(value):
85+
logger.debug(
86+
f"Found ME value for {target_time} in {latest_me_df}, "
87+
f"but it was NaN, therefore adding adjust_mw as 0"
88+
)
89+
forecast_value.adjust_mw = 0.0
8190

8291
except Exception:
83-
logger.debug(f"Could not find ME value for {target_time} in {latest_me_df}")
92+
logger.debug(
93+
f"Could not find ME value for {target_time} in {latest_me_df}, "
94+
f"therefore adding adjust_mw as 0"
95+
)
96+
forecast_value.adjust_mw = 0.0
8497

8598

8699
def get_forecast_horizon_from_forecast(forecast: Union[ForecastSQL, Forecast]):
@@ -144,6 +157,9 @@ def reduce_metric_values_to_correct_forecast_horizon(
144157
)
145158

146159
latest_me_df = pd.DataFrame([MetricValue.from_orm(m).dict() for m in latest_me])
160+
if len(latest_me_df) == 0:
161+
# no latest ME values, so just making an empty dataframe
162+
latest_me_df = pd.DataFrame(columns=["forecast_horizon_minutes", "time_of_day", "value"])
147163

148164
# Let now big a dataframe of datetimes from now onwards. Lets say the time is 04.30, then
149165
# time forecast_horizon value
@@ -157,7 +173,6 @@ def reduce_metric_values_to_correct_forecast_horizon(
157173
start=datetime_now, end=datetime_now + timedelta(hours=hours_ahead), freq="30T"
158174
),
159175
)
160-
print(results_df)
161176
results_df["datetime"] = results_df.index
162177
results_df["time_of_day"] = results_df.index.time
163178
results_df["forecast_horizon_minutes"] = range(0, 30 * len(results_df), 30)

tests/save/test_adjust.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,20 @@ def test_add_adjust_to_forecasts(latest_me, db_session):
4949
assert forecasts[1].forecast_values[0].adjust_mw == 0.0
5050

5151

52+
@freeze_time("2023-01-09 16:25")
53+
def test_add_adjust_to_forecasts_no_me_values(db_session):
54+
"""Test to check, if there are no me values, make sure the adjust mw are 0 not NaN"""
55+
56+
datetime_now = datetime(2023, 1, 9, 16, 30, tzinfo=timezone.utc)
57+
forecasts = make_fake_forecasts(
58+
gsp_ids=list(range(0, 2)), session=db_session, t0_datetime_utc=datetime_now
59+
)
60+
61+
add_adjust_to_forecasts(session=db_session, forecasts_sql=forecasts)
62+
63+
assert forecasts[1].forecast_values[0].adjust_mw == 0.0
64+
65+
5266
def test_get_forecast_horizon_from_forecast(db_session):
5367
forecasts = make_fake_forecasts(gsp_ids=range(0, 1), session=db_session)
5468

0 commit comments

Comments
 (0)