From b13e90ef52a763fc90eb4c9942f058169d8eb8e2 Mon Sep 17 00:00:00 2001 From: davemfish Date: Wed, 22 Jan 2025 09:29:12 -0500 Subject: [PATCH 1/6] set PROJ_LIB and PROJ_DATA variables in the exe runtime hook. #1742 --- exe/hooks/rthook.py | 1 + 1 file changed, 1 insertion(+) diff --git a/exe/hooks/rthook.py b/exe/hooks/rthook.py index 492344124..c007eef05 100644 --- a/exe/hooks/rthook.py +++ b/exe/hooks/rthook.py @@ -3,6 +3,7 @@ import sys os.environ['PROJ_LIB'] = os.path.join(sys._MEIPASS, 'proj') +os.environ['PROJ_DATA'] = os.path.join(sys._MEIPASS, 'proj') if platform.system() == 'Darwin': # Rtree will look in this directory first for libspatialindex_c.dylib. From 33d50c5d671e41ab5459aa13bc16133d9f7d58ef Mon Sep 17 00:00:00 2001 From: davemfish Date: Wed, 22 Jan 2025 15:47:37 -0500 Subject: [PATCH 2/6] a note for HISTORY. #1742 --- HISTORY.rst | 3 +++ 1 file changed, 3 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index 8419211bd..42637cdf4 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -38,6 +38,9 @@ .. Unreleased Changes ------------------ +* General + * Fixed an issue where a user's PROJ_DATA environment variable could + trigger a RuntimeError about a missing proj.db file. 3.14.3 (2024-12-19) ------------------- From 76006437f0b9bc9e5cc8c0ff9494c0f1c0e9a476 Mon Sep 17 00:00:00 2001 From: davemfish Date: Wed, 22 Jan 2025 15:54:39 -0500 Subject: [PATCH 3/6] fix RST markup. #1742 --- HISTORY.rst | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/HISTORY.rst b/HISTORY.rst index 42637cdf4..30ea51436 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -35,12 +35,12 @@ .. :changelog: -.. - Unreleased Changes - ------------------ + +Unreleased Changes +------------------ * General * Fixed an issue where a user's PROJ_DATA environment variable could - trigger a RuntimeError about a missing proj.db file. + trigger a RuntimeError about a missing proj.db file. 3.14.3 (2024-12-19) ------------------- From ec5d2e7c4661d54e1e0dcf3ddc229d37eeb0ab74 Mon Sep 17 00:00:00 2001 From: James Douglass Date: Tue, 4 Feb 2025 16:15:50 -0800 Subject: [PATCH 4/6] Updating fetch depth to fix versioning. RE:#1767 --- .github/workflows/release-part-1.yml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/release-part-1.yml b/.github/workflows/release-part-1.yml index 94ddd1a2a..b106bcec2 100644 --- a/.github/workflows/release-part-1.yml +++ b/.github/workflows/release-part-1.yml @@ -36,6 +36,7 @@ jobs: - uses: actions/checkout@v4 if: env.RUN == 'true' with: + fetch-depth: 0 # fetch entire history, for versioning token: ${{ secrets.AUTORELEASE_BOT_PAT }} - name: Install dependencies From 49a07a2358830e9cf3ed6a45570cb85b3e84898c Mon Sep 17 00:00:00 2001 From: James Douglass Date: Tue, 4 Feb 2025 16:32:24 -0800 Subject: [PATCH 5/6] Updating and testing validation. The demand table is now required when the valuation table is provided. RE:#1769 --- src/natcap/invest/annual_water_yield.py | 7 ++--- tests/test_annual_water_yield.py | 37 +++++++++++++++++-------- 2 files changed, 28 insertions(+), 16 deletions(-) diff --git a/src/natcap/invest/annual_water_yield.py b/src/natcap/invest/annual_water_yield.py index fdaeaa862..501f1ce6b 100644 --- a/src/natcap/invest/annual_water_yield.py +++ b/src/natcap/invest/annual_water_yield.py @@ -250,11 +250,11 @@ } }, "index_col": "lucode", - "required": False, + "required": "valuation_table_path", "about": gettext( "A table of water demand for each LULC class. Each LULC code " "in the LULC raster must have a corresponding row in this " - "table."), + "table. Required if 'valuation_table_path' is provided."), "name": gettext("water demand table") }, "valuation_table_path": { @@ -501,14 +501,13 @@ def execute(args): a path to an input CSV table of LULC classes, showing consumptive water use for each landuse / land-cover type (cubic meters per year) to calculate - water scarcity. + water scarcity. Required if ``valuation_table_path`` is provided. args['valuation_table_path'] (string): (optional) if a non-empty string, a path to an input CSV table of hydropower stations with the following fields to calculate valuation: 'ws_id', 'time_span', 'discount', 'efficiency', 'fraction', 'cost', 'height', 'kw_price' - Required if ``calculate_valuation`` is True. args['n_workers'] (int): (optional) The number of worker processes to use for processing this model. If omitted, computation will take diff --git a/tests/test_annual_water_yield.py b/tests/test_annual_water_yield.py index e450f4b7e..6a1a4c7ff 100644 --- a/tests/test_annual_water_yield.py +++ b/tests/test_annual_water_yield.py @@ -1,14 +1,13 @@ """Module for Regression Testing the InVEST Annual Water Yield module.""" -import unittest -import tempfile -import shutil import os +import shutil +import tempfile +import unittest -import pandas import numpy -from osgeo import gdal +import pandas import pygeoprocessing - +from osgeo import gdal REGRESSION_DATA = os.path.join( os.path.dirname(__file__), '..', 'data', 'invest-test-data', 'annual_water_yield') @@ -74,7 +73,7 @@ def test_invalid_lulc_veg(self): with self.assertRaises(ValueError) as cm: annual_water_yield.execute(args) self.assertTrue('veg value must be either 1 or 0' in str(cm.exception)) - + def test_missing_lulc_value(self): """Hydro: catching missing LULC value in Biophysical table.""" from natcap.invest import annual_water_yield @@ -89,7 +88,7 @@ def test_missing_lulc_value(self): bio_df = bio_df[bio_df['lucode'] != 2] bio_df.to_csv(bad_biophysical_path) bio_df = None - + args['biophysical_table_path'] = bad_biophysical_path with self.assertRaises(ValueError) as cm: @@ -97,13 +96,13 @@ def test_missing_lulc_value(self): self.assertTrue( "The missing values found in the LULC raster but not the table" " are: [2]" in str(cm.exception)) - + def test_missing_lulc_demand_value(self): """Hydro: catching missing LULC value in Demand table.""" from natcap.invest import annual_water_yield args = AnnualWaterYieldTests.generate_base_args(self.workspace_dir) - + args['demand_table_path'] = os.path.join( SAMPLE_DATA, 'water_demand_table.csv') args['sub_watersheds_path'] = os.path.join( @@ -117,7 +116,7 @@ def test_missing_lulc_demand_value(self): demand_df = demand_df[demand_df['lucode'] != 2] demand_df.to_csv(bad_demand_path) demand_df = None - + args['demand_table_path'] = bad_demand_path with self.assertRaises(ValueError) as cm: @@ -247,7 +246,8 @@ def test_valuation_subshed(self): def test_validation(self): """Hydro: test failure cases on the validation function.""" - from natcap.invest import annual_water_yield, validation + from natcap.invest import annual_water_yield + from natcap.invest import validation args = AnnualWaterYieldTests.generate_base_args(self.workspace_dir) @@ -367,3 +367,16 @@ def test_validation(self): self.assertTrue( 'but are not found in the valuation table' in actual_message, actual_message) + + # if the demand table is missing but the valuation table is present, + # make sure we have a validation error. + args_missing_demand_table = args.copy() + args_missing_demand_table['demand_table_path'] = '' + args_missing_demand_table['valuation_table_path'] = ( + os.path.join(SAMPLE_DATA, 'hydropower_valuation_table.csv')) + validation_warnings = annual_water_yield.validate( + args_missing_demand_table) + self.assertEqual(len(validation_warnings), 1) + self.assertEqual( + validation_warnings[0], + (['demand_table_path'], 'Input is required but has no value')) From b45b26d52c240807bdb14bbfc464cc95d515a656 Mon Sep 17 00:00:00 2001 From: James Douglass Date: Tue, 4 Feb 2025 16:34:42 -0800 Subject: [PATCH 6/6] Noting change in HISTORY. RE:#1769 --- HISTORY.rst | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/HISTORY.rst b/HISTORY.rst index a4139ec97..0b29b76b1 100644 --- a/HISTORY.rst +++ b/HISTORY.rst @@ -45,6 +45,12 @@ Unreleased Changes * Now testing and building against Python 3.13. No longer testing and building with Python 3.8, which reached EOL. https://github.com/natcap/invest/issues/1755 +* Annual Water Yield + * Fixed an issue where the model would crash if the valuation table was + provided, but the demand table was not. Validation will now warn about + this, and the ``MODEL_SPEC`` has been improved to reflect that this table + is now required when doing valuation. + https://github.com/natcap/invest/issues/1769 * Carbon * Updated styling of the HTML report generated by the carbon model, for visual consistency with the Workbench (`InVEST #1732