From 2ba40d194e99ee70550a4c9ea2d6fc2871da6c3b Mon Sep 17 00:00:00 2001 From: James Fulton Date: Fri, 20 Oct 2023 14:16:41 +0000 Subject: [PATCH 1/5] add optional sum-of-GSP saving --- pvnet_app/app.py | 36 ++++++++++++++++++++++++++++++++++++ tests/test_app.py | 11 ++++++----- 2 files changed, 42 insertions(+), 5 deletions(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 4e6bd0a..a97061a 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -77,6 +77,12 @@ model_name_ocf_db = "pvnet_v2" use_adjuster = os.getenv("USE_ADJUSTER", "True").lower() == "true" +# If environmental variable is true, the sum-of-GSPs will be computed and saved under a different +# model name. This can be useful to compare against the summation model and therefore monitor its +# performance in production +save_gsp_sum = os.getenv("SAVE_GSP_SUM", "False").lower() == "true" +gsp_sum_model_name_ocf_db = "pvnet_gsp_sum" + # --------------------------------------------------------------------------- # LOGGER formatter = logging.Formatter( @@ -491,6 +497,18 @@ def app( logger.info( f"National forecast is {da_abs.sel(gsp_id=0, output_label='forecast_mw').values}" ) + + if save_gsp_sum: + # Compute the sum if we are logging the sume of GSPs independently + logger.info("Summing across GSPs to for independent sum-of-GSP saving") + da_abs_sum_gsps = ( + da_abs.sum(dim="gsp_id") + # Only select the central forecast for the GSP sum. The sums of different p-levels + # are not a meaningful qauntities + .sel(output_label=["forecast_mw"]) + .expand_dims(dim="gsp_id", axis=0) + .assign_coords(gsp_id=[0]) + ) # --------------------------------------------------------------------------- # Escape clause for making predictions locally @@ -514,6 +532,24 @@ def app( update_gsp=True, apply_adjuster=apply_adjuster, ) + + if save_gsp_sum: + # Save the sum of GSPs independently - mainly for summation model monitoring + sql_forecasts = convert_dataarray_to_forecasts( + da_abs_sum_gsps, + session, + model_name=gsp_sum_model_name_ocf_db, + version=pvnet_app.__version__ + ) + + save_sql_forecasts( + forecasts=sql_forecasts, + session=session, + update_national=True, + update_gsp=False, + apply_adjuster=apply_adjuster, + ) + logger.info("Finished forecast") diff --git a/tests/test_app.py b/tests/test_app.py index 931769d..4a32090 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -36,21 +36,22 @@ def test_app(db_session, nwp_data, sat_data, gsp_yields_and_systems, me_latest): # Set model version os.environ["APP_MODEL_VERSION"] = "96ac8c67fa8663844ddcfa82aece51ef94f34453" os.environ["APP_SUMMATION_MODEL_VERSION"] = "4a145d74c725ffc72f482025d3418659a6869c94" + os.environ["SAVE_GSP_SUM"] = "True" # Run prediction app(gsp_ids=list(range(1, 318))) # Check forecasts have been made - # (317 GSPs + 1 National) = 318 forecasts + # (317 GSPs + 1 National + 1 GSP-sum) = 319 forecasts # Doubled for historic and forecast forecasts = db_session.query(ForecastSQL).all() - assert len(forecasts) == 318 * 2 + assert len(forecasts) == 319 * 2 # Check probabilistic added assert "90" in forecasts[0].forecast_values[0].properties assert "10" in forecasts[0].forecast_values[0].properties # 318 GSPs * 16 time steps in forecast - assert len(db_session.query(ForecastValueSQL).all()) == 318 * 16 - assert len(db_session.query(ForecastValueLatestSQL).all()) == 318 * 16 - assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 318 * 16 + assert len(db_session.query(ForecastValueSQL).all()) == 319 * 16 + assert len(db_session.query(ForecastValueLatestSQL).all()) == 319 * 16 + assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 319 * 16 From 400b043220211e9d1712d85f89b566175fad3326 Mon Sep 17 00:00:00 2001 From: James Fulton Date: Fri, 20 Oct 2023 14:44:43 +0000 Subject: [PATCH 2/5] improve logging --- pvnet_app/app.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index a97061a..18c3e93 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -219,6 +219,8 @@ def app( logger.info(f"Using `pvnet` library version: {pvnet.__version__}") logger.info(f"Using {num_workers} workers") + logger.info(f"Using adjduster: {use_adjuster}") + logger.info(f"Saving GSP sum: {save_gsp_sum}") # Allow environment overwrite of model model_name = os.getenv("APP_MODEL", default=default_model_name) From e4f1b0b155aef295c3934108bbb961b285d8f1aa Mon Sep 17 00:00:00 2001 From: James Fulton Date: Fri, 20 Oct 2023 14:46:36 +0000 Subject: [PATCH 3/5] revert tests --- tests/test_app.py | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index 4a32090..ab5c80f 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -42,16 +42,16 @@ def test_app(db_session, nwp_data, sat_data, gsp_yields_and_systems, me_latest): app(gsp_ids=list(range(1, 318))) # Check forecasts have been made - # (317 GSPs + 1 National + 1 GSP-sum) = 319 forecasts + # (317 GSPs + 1 National) = 318 forecasts # Doubled for historic and forecast forecasts = db_session.query(ForecastSQL).all() - assert len(forecasts) == 319 * 2 + assert len(forecasts) == 318 * 2 # Check probabilistic added assert "90" in forecasts[0].forecast_values[0].properties assert "10" in forecasts[0].forecast_values[0].properties # 318 GSPs * 16 time steps in forecast - assert len(db_session.query(ForecastValueSQL).all()) == 319 * 16 - assert len(db_session.query(ForecastValueLatestSQL).all()) == 319 * 16 - assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 319 * 16 + assert len(db_session.query(ForecastValueSQL).all()) == 318 * 16 + assert len(db_session.query(ForecastValueLatestSQL).all()) == 318 * 16 + assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 318 * 16 From 5792c6e2f1c7cc5b61822e93b400bc233f3f588c Mon Sep 17 00:00:00 2001 From: James Fulton Date: Fri, 20 Oct 2023 14:46:58 +0000 Subject: [PATCH 4/5] no adjuster for gsp sum --- pvnet_app/app.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/pvnet_app/app.py b/pvnet_app/app.py index 18c3e93..8f4fe89 100644 --- a/pvnet_app/app.py +++ b/pvnet_app/app.py @@ -549,7 +549,7 @@ def app( session=session, update_national=True, update_gsp=False, - apply_adjuster=apply_adjuster, + apply_adjuster=False, ) From bf00d727eb2b59cd87824072d29cbc836385d096 Mon Sep 17 00:00:00 2001 From: James Fulton Date: Fri, 20 Oct 2023 15:08:32 +0000 Subject: [PATCH 5/5] fix tests --- tests/test_app.py | 13 +++++++------ 1 file changed, 7 insertions(+), 6 deletions(-) diff --git a/tests/test_app.py b/tests/test_app.py index ab5c80f..dfc4c8b 100644 --- a/tests/test_app.py +++ b/tests/test_app.py @@ -1,4 +1,3 @@ -from pvnet_app.app import app import tempfile import zarr import os @@ -39,19 +38,21 @@ def test_app(db_session, nwp_data, sat_data, gsp_yields_and_systems, me_latest): os.environ["SAVE_GSP_SUM"] = "True" # Run prediction + # This import needs to come after the environ vars have been set + from pvnet_app.app import app app(gsp_ids=list(range(1, 318))) # Check forecasts have been made - # (317 GSPs + 1 National) = 318 forecasts + # (317 GSPs + 1 National + GSP-sum) = 319 forecasts # Doubled for historic and forecast forecasts = db_session.query(ForecastSQL).all() - assert len(forecasts) == 318 * 2 + assert len(forecasts) == 319 * 2 # Check probabilistic added assert "90" in forecasts[0].forecast_values[0].properties assert "10" in forecasts[0].forecast_values[0].properties # 318 GSPs * 16 time steps in forecast - assert len(db_session.query(ForecastValueSQL).all()) == 318 * 16 - assert len(db_session.query(ForecastValueLatestSQL).all()) == 318 * 16 - assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 318 * 16 + assert len(db_session.query(ForecastValueSQL).all()) == 319 * 16 + assert len(db_session.query(ForecastValueLatestSQL).all()) == 319 * 16 + assert len(db_session.query(ForecastValueSevenDaysSQL).all()) == 319 * 16