Skip to content

Commit

Permalink
Update dm 1.5.18 (#303)
Browse files Browse the repository at this point in the history
* nowcasting_datamodel==1.5.18

* add test

* add rounding

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* make sure cache is not used in tests

* lint

* [pre-commit.ci] auto fixes from pre-commit.com hooks

for more information, see https://pre-commit.ci

* lint

* make sure chace is not used

* tidy

* try to fix test

* move test

---------

Co-authored-by: pre-commit-ci[bot] <66853113+pre-commit-ci[bot]@users.noreply.github.com>
  • Loading branch information
peterdudfield and pre-commit-ci[bot] authored Sep 27, 2023
1 parent dd343f2 commit 378e3f0
Show file tree
Hide file tree
Showing 5 changed files with 39 additions and 18 deletions.
2 changes: 1 addition & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ uvicorn[standard]
pydantic
numpy
requests
nowcasting_datamodel==1.5.17
nowcasting_datamodel==1.5.18
nowcasting_dataset==3.7.12
sqlalchemy
psycopg2-binary
Expand Down
12 changes: 11 additions & 1 deletion src/pydantic_models.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@

from nowcasting_datamodel.models import Forecast, ForecastSQL, ForecastValue, Location, LocationSQL
from nowcasting_datamodel.models.utils import EnhancedBaseModel
from pydantic import Field
from pydantic import Field, validator

logger = logging.getLogger(__name__)

Expand All @@ -16,6 +16,11 @@ class GSPYield(EnhancedBaseModel):
datetime_utc: datetime = Field(..., description="The timestamp of the gsp yield")
solar_generation_kw: float = Field(..., description="The amount of solar generation")

@validator("solar_generation_kw")
def result_check(cls, v):
"""Round to 2 decimal places"""
return round(v, 2)


class LocationWithGSPYields(Location):
"""Location object with GSPYields"""
Expand Down Expand Up @@ -171,6 +176,11 @@ class NationalForecastValue(ForecastValue):
None, description="Dictionary to hold properties of the forecast, like p_levels. "
)

@validator("expected_power_generation_megawatts")
def result_check(cls, v):
"""Round to 2 decimal places"""
return round(v, 2)


class NationalForecast(Forecast):
"""One Forecast of generation at one timestamp"""
Expand Down
10 changes: 4 additions & 6 deletions src/tests/test_national.py
Original file line number Diff line number Diff line change
Expand Up @@ -187,7 +187,7 @@ def test_get_national_forecast_error(db_session, api_client):
assert response.status_code == 404


def test_read_latest_national_values_no_properties(db_session, api_client):
def test_read_latest_national_values_properties(db_session, api_client):
"""Check main solar/GB/national/forecast route works
Check fake propreties are made
Expand All @@ -200,22 +200,20 @@ def test_read_latest_national_values_no_properties(db_session, api_client):
)
forecast.model = model

for f in forecast.forecast_values:
f.properties = None

db_session.add(forecast)
update_all_forecast_latest(forecasts=[forecast], session=db_session)

app.dependency_overrides[get_session] = lambda: db_session

response = api_client.get("/v0/solar/GB/national/forecast")
# add test=test2 makes sure the cache is not used
response = api_client.get("/v0/solar/GB/national/forecast?test=test2")
assert response.status_code == 200

national_forecast_values = [NationalForecastValue(**f) for f in response.json()]
assert national_forecast_values[0].plevels is not None
# index 24 is the middle of the day
assert np.round(national_forecast_values[24].plevels["plevel_10"], 2) == np.round(
national_forecast_values[24].expected_power_generation_megawatts * 0.8, 2
national_forecast_values[24].expected_power_generation_megawatts * 0.9, 2
)


Expand Down
14 changes: 13 additions & 1 deletion src/tests/test_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

from freezegun import freeze_time

from utils import floor_30_minutes_dt, get_start_datetime, traces_sampler
from pydantic_models import NationalForecastValue
from utils import floor_30_minutes_dt, format_plevels, get_start_datetime, traces_sampler

LOWER_LIMIT_MINUTE = 0
UPPER_LIMIT_MINUTE = 60
Expand Down Expand Up @@ -101,3 +102,14 @@ def test_traces_sampler():
assert (
traces_sampler({"parent_sampled": False, "transaction_context": {"name": "error1"}}) == 1.0
)


def test_format_plevels():
"""Make sure dummy plevels are made correctly"""
fv = NationalForecastValue(
expected_power_generation_megawatts=1.0,
target_time=datetime(2021, 1, 1, 0, 0, 0, tzinfo=timezone.utc),
)

format_plevels(national_forecast_value=fv)
fv.plevels = {"10": 0.8, "90": 1.2}
19 changes: 10 additions & 9 deletions src/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,28 +146,29 @@ def format_plevels(national_forecast_value: NationalForecastValue):
:return:
"""
logger.debug(f"{national_forecast_value.plevels}")
power = national_forecast_value.expected_power_generation_megawatts
if (not isinstance(national_forecast_value.plevels, dict)) or (
national_forecast_value.plevels == {}
):
national_forecast_value.plevels = {
"plevel_10": national_forecast_value.expected_power_generation_megawatts * 0.8,
"plevel_90": national_forecast_value.expected_power_generation_megawatts * 1.2,
"plevel_10": round(power * 0.8, 2),
"plevel_90": round(power * 1.2, 2),
}

logger.info(f"plevels set to default: {national_forecast_value.plevels}")

# rename '10' and '90' to plevel_10 and plevel_90
for c in ["10", "90"]:
if c in national_forecast_value.plevels.keys():
national_forecast_value.plevels[f"plevel_{c}"] = national_forecast_value.plevels.pop(c)
national_forecast_value.plevels[f"plevel_{c}"] = round(
national_forecast_value.plevels.pop(c), 2
)

if national_forecast_value.plevels["plevel_10"] is None:
national_forecast_value.plevels["plevel_10"] = (
national_forecast_value.expected_power_generation_megawatts * 0.8
)
national_forecast_value.plevels["plevel_10"] = round(power * 0.8, 2)

if national_forecast_value.plevels["plevel_90"] is None:
national_forecast_value.plevels["plevel_90"] = (
national_forecast_value.expected_power_generation_megawatts * 1.2
)
national_forecast_value.plevels["plevel_90"] = round(power * 1.2, 2)


def filter_forecast_values(
Expand Down

0 comments on commit 378e3f0

Please sign in to comment.