Skip to content

Commit

Permalink
Merge pull request natcap#1703 from claire-simpson/feature/1671-na-ke…
Browse files Browse the repository at this point in the history
…y-err

Raise Exception for NA keys in reclassify_raster and test HQ missing lulc value
  • Loading branch information
davemfish authored Dec 10, 2024
2 parents 4044ae2 + 36267de commit f933907
Show file tree
Hide file tree
Showing 3 changed files with 61 additions and 2 deletions.
4 changes: 4 additions & 0 deletions HISTORY.rst
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,8 @@ Unreleased Changes
* Fixed an issue on Windows where GDAL fails to find its DLLs due to
an interfering GDAL installation on the PATH, such as from anaconda.
https://github.com/natcap/invest/issues/1643
* Improved error handling of NA values in raster reclassification to provide
a more descriptive message.
* Workbench
* Several small updates to the model input form UI to improve usability
and visual consistency (https://github.com/natcap/invest/issues/912).
Expand Down Expand Up @@ -81,6 +83,8 @@ Unreleased Changes
(https://github.com/natcap/invest/issues/1615).
* Rarity values are now output in CSV format (as well as in raster format)
(https://github.com/natcap/invest/issues/721).
* Improved error handling when there is a missing LULC value in the
sensitivity table (https://github.com/natcap/invest/issues/1671).
* Pollination
* Fixed an issue with nodata handling that was causing some outputs to be
filled either with the float32 value for positive infinity, or else with
Expand Down
14 changes: 12 additions & 2 deletions src/natcap/invest/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -741,14 +741,24 @@ def reclassify_raster(
None
Raises:
ValueError if ``values_required`` is ``True`` and a pixel value from
``raster_path_band`` is not a key in ``value_map``.
ValueError:
- if ``values_required`` is ``True`` and a pixel value from
``raster_path_band`` is not a key in ``value_map``.
TypeError:
- if there is a ``None`` or ``NA`` key in ``value_map``.
"""
# Error early if 'error_details' keys are invalid
raster_name = error_details['raster_name']
column_name = error_details['column_name']
table_name = error_details['table_name']

# check keys in value map to ensure none are NA or None
if any((key is pandas.NA or key is None)
for key in value_map):
error_message = (f"Missing or NA value in '{column_name}' column"
f" in {table_name} table.")
raise TypeError(error_message)

try:
pygeoprocessing.reclassify_raster(
raster_path_band, value_map, target_raster_path, target_datatype,
Expand Down
45 changes: 45 additions & 0 deletions tests/test_habitat_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -2133,3 +2133,48 @@ def test_habitat_quality_validate_missing_fut_column(self):
header='column', header_name='fut_path')
)]
self.assertEqual(validate_result, expected)

def test_habitat_quality_missing_lulc_val_in_sens_table(self):
"""Habitat Quality: test for empty value in LULC column of
sensitivity table. Expects TypeError"""
from natcap.invest import habitat_quality

args = {
'half_saturation_constant': '0.5',
'workspace_dir': self.workspace_dir,
'n_workers': -1,
}

lulc_array = numpy.ones((100, 100), dtype=numpy.int8)
args['lulc_cur_path'] = os.path.join(
args['workspace_dir'], 'lc_samp_cur_b.tif')
make_raster_from_array(
lulc_array, args['lulc_cur_path'])

args['sensitivity_table_path'] = os.path.join(
args['workspace_dir'], 'sensitivity_samp.csv')
with open(args['sensitivity_table_path'], 'w') as open_table:
open_table.write('LULC,NAME,HABITAT,threat_1,threat_2\n')
open_table.write('1,"lulc 1",1,1,1\n')
open_table.write(',"lulc 2",0.5,0.5,1\n') # missing LULC value
open_table.write('3,"lulc 3",0,0.3,1\n')

make_threats_raster(
args['workspace_dir'], threat_values=[1, 1],
dtype=numpy.int8, gdal_type=gdal.GDT_Int32, nodata_val=None)

args['threats_table_path'] = os.path.join(
args['workspace_dir'], 'threats_samp.csv')

# create the threat CSV table
with open(args['threats_table_path'], 'w') as open_table:
open_table.write(
'MAX_DIST,WEIGHT,THREAT,DECAY,BASE_PATH,CUR_PATH,FUT_PATH\n')
open_table.write(
'0.04,0.7,threat_1,linear,,threat_1_c.tif,threat_1_f.tif\n')
open_table.write(
'0.07,1.0,threat_2,exponential,,threat_2_c.tif,'
'threat_2_f.tif\n')

with self.assertRaises(TypeError):
habitat_quality.execute(args)

0 comments on commit f933907

Please sign in to comment.