Skip to content

Commit

Permalink
Merge pull request #1773 from phargogh/pr-1772-resolution
Browse files Browse the repository at this point in the history
Resolving merge conflict into release/3.15.0
  • Loading branch information
davemfish authored Feb 5, 2025
2 parents 6aaff8d + 2174849 commit cf415ac
Show file tree
Hide file tree
Showing 3 changed files with 34 additions and 16 deletions.
6 changes: 6 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,12 @@ Unreleased Changes
* Auto-scrolling of log output is halted on user-initiated scrolling,
enabling easier inspection of log output while a model is running
(`InVEST #1533 <https://github.com/natcap/invest/issues/1533>`_).
* 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
Expand Down
7 changes: 3 additions & 4 deletions src/natcap/invest/annual_water_yield.py
Original file line number Diff line number Diff line change
Expand Up @@ -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": {
Expand Down Expand Up @@ -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
Expand Down
37 changes: 25 additions & 12 deletions tests/test_annual_water_yield.py
Original file line number Diff line number Diff line change
@@ -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')
Expand Down Expand Up @@ -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
Expand All @@ -89,21 +88,21 @@ 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:
annual_water_yield.execute(args)
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(
Expand All @@ -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:
Expand Down Expand Up @@ -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)

Expand Down Expand Up @@ -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'))

0 comments on commit cf415ac

Please sign in to comment.