Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Issue/remove blend #262

Merged
merged 9 commits into from
Aug 15, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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.4
nowcasting_datamodel==1.5.8
nowcasting_dataset==3.7.12
sqlalchemy
psycopg2-binary
Expand Down
35 changes: 15 additions & 20 deletions src/database.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
APIRequestSQL,
Forecast,
ForecastValue,
ForecastValueLatestSQL,
ForecastValueSevenDaysSQL,
ForecastValueSQL,
GSPYield,
Expand Down Expand Up @@ -170,21 +171,9 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database(
start_datetime = get_start_datetime()

if forecast_horizon_minutes is None:
if gsp_id != 0:
forecast_values = get_forecast_values_latest(
session=session, gsp_id=gsp_id, start_datetime=start_datetime
)
else:
# get blend of forecast values from CNN and Nationa_xg
# this returns a list of ForecastValue objects
forecast_values = get_blend_forecast_values_latest(
session=session,
gsp_id=0,
start_datetime=start_datetime,
properties_model="National_xg",
weights=weights,
model_names=["cnn", "National_xg", "pvnet_v2"],
)
forecast_values = get_forecast_values_latest(
session=session, gsp_id=gsp_id, start_datetime=start_datetime, model_name="blend"
)

else:
forecast_values = get_blend_forecast_values_latest(
Expand All @@ -197,8 +186,10 @@ def get_latest_forecast_values_for_a_specific_gsp_from_database(
)

# convert to pydantic objects
if isinstance(forecast_values[0], ForecastValueSevenDaysSQL) or isinstance(
forecast_values[0], ForecastValueSQL
if (
devsjc marked this conversation as resolved.
Show resolved Hide resolved
isinstance(forecast_values[0], ForecastValueSevenDaysSQL)
or isinstance(forecast_values[0], ForecastValueSQL)
or isinstance(forecast_values[0], ForecastValueLatestSQL)
):
forecast_values = [ForecastValue.from_orm(f) for f in forecast_values]

Expand Down Expand Up @@ -253,12 +244,16 @@ def get_truth_values_for_a_specific_gsp_from_database(


def get_truth_values_for_all_gsps_from_database(
session: Session, n_gsp: Optional[int] = N_GSP, regime: Optional[str] = "in-day"
session: Session,
start_gsp: Optional[int] = 1,
end_gsp: Optional[int] = N_GSP + 1,
regime: Optional[str] = "in-day",
) -> List[LocationWithGSPYields]:
"""Get the truth value for all gsps for yesterday and today

:param session: sql session
:param n_gsp: the number of gsps we should load.
:param start_gsp: the start number of gsps we should load.
:param end_gsp: the end number of gsps we should load.
:param regime: option for "in-day" or "day-after"
:return: list of gsp yields
"""
Expand All @@ -267,7 +262,7 @@ def get_truth_values_for_all_gsps_from_database(

locations = get_gsp_yield_by_location(
session=session,
gsp_ids=list(range(1, n_gsp + 1)),
gsp_ids=list(range(start_gsp, end_gsp)),
start_datetime_utc=start_datetime,
regime=regime,
)
Expand Down
8 changes: 6 additions & 2 deletions src/tests/test_gsp.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,9 @@
def test_read_latest_one_gsp(db_session, api_client):
"""Check main solar/GB/gsp/{gsp_id}/forecast route works"""

forecasts = make_fake_forecasts(gsp_ids=list(range(0, 2)), session=db_session, add_latest=True)
forecasts = make_fake_forecasts(
gsp_ids=list(range(0, 2)), session=db_session, add_latest=True, model_name="blend"
)
db_session.add_all(forecasts)
db_session.commit()

Expand Down Expand Up @@ -72,7 +74,9 @@ def test_read_latest_gsp_id_greater_than_total(db_session, api_client):
def test_read_latest_gsp_id_equal_to_total(db_session, api_client):
"""Check that request with gsp_id<318 returns 200"""

forecasts = make_fake_forecasts(gsp_ids=[317], session=db_session, add_latest=True)
forecasts = make_fake_forecasts(
gsp_ids=[317], session=db_session, add_latest=True, model_name="blend"
)
db_session.add_all(forecasts)

app.dependency_overrides[get_session] = lambda: db_session
Expand Down
21 changes: 17 additions & 4 deletions src/tests/test_merged_routes.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from freezegun import freeze_time
from nowcasting_datamodel.fake import make_fake_forecast
from nowcasting_datamodel.models import ForecastValue, ForecastValueLatestSQL
from nowcasting_datamodel.read.read import get_model

from database import get_session
from main import app
Expand All @@ -13,27 +14,39 @@
def test_read_forecast_values_gsp(db_session, api_client):
"""Check main solar/GB/gsp/{gsp_id}/forecast route works"""

model = get_model(session=db_session, name="blend", version="0.0.1")

forecast_value_1_sql = ForecastValueLatestSQL(
target_time=datetime(2023, 6, 2), expected_power_generation_megawatts=1, gsp_id=1
target_time=datetime(2023, 6, 2),
expected_power_generation_megawatts=1,
gsp_id=1,
model_id=model.id,
)

forecast_value_2_sql = ForecastValueLatestSQL(
target_time=datetime(2023, 6, 1, 1), expected_power_generation_megawatts=2, gsp_id=1
target_time=datetime(2023, 6, 1, 1),
expected_power_generation_megawatts=2,
gsp_id=1,
model_id=model.id,
)

forecast_value_3_sql = ForecastValueLatestSQL(
target_time=datetime(2023, 6, 1), expected_power_generation_megawatts=3, gsp_id=1
target_time=datetime(2023, 6, 1),
expected_power_generation_megawatts=3,
gsp_id=1,
model_id=model.id,
)

forecast = make_fake_forecast(
gsp_id=1, session=db_session, t0_datetime_utc=datetime(2023, 1, 1)
gsp_id=1, session=db_session, t0_datetime_utc=datetime(2023, 1, 1), model_name="blend"
)
forecast.forecast_values_latest.append(forecast_value_1_sql)
forecast.forecast_values_latest.append(forecast_value_2_sql)
forecast.forecast_values_latest.append(forecast_value_3_sql)

# add to database
db_session.add_all([forecast])
db_session.commit()

app.dependency_overrides[get_session] = lambda: db_session

Expand Down
10 changes: 5 additions & 5 deletions src/tests/test_national.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
def test_read_latest_national_values(db_session, api_client):
"""Check main solar/GB/national/forecast route works"""

model = get_model(db_session, name="National_xg", version="0.0.1")
model = get_model(db_session, name="blend", version="0.0.1")

forecast = make_fake_national_forecast(
session=db_session, t0_datetime_utc=datetime.now(tz=timezone.utc)
Expand All @@ -26,7 +26,7 @@ def test_read_latest_national_values(db_session, api_client):
assert forecast.forecast_values[0].properties is not None

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

app.dependency_overrides[get_session] = lambda: db_session

Expand All @@ -48,7 +48,7 @@ def test_read_latest_national_values_no_properties(db_session, api_client):
Check fake propreties are made
"""

model = get_model(db_session, name="cnn", version="0.0.1")
model = get_model(db_session, name="blend", version="0.0.1")

forecast = make_fake_national_forecast(
session=db_session, t0_datetime_utc=datetime.now(tz=timezone.utc)
Expand All @@ -59,7 +59,7 @@ def test_read_latest_national_values_no_properties(db_session, api_client):
f.properties = None

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

app.dependency_overrides[get_session] = lambda: db_session

Expand All @@ -70,7 +70,7 @@ def test_read_latest_national_values_no_properties(db_session, api_client):
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.9, 2
national_forecast_values[24].expected_power_generation_megawatts * 0.8, 2
)


Expand Down
Loading