Skip to content

Commit

Permalink
Merge branch 'main' into feature/routing-refactor
Browse files Browse the repository at this point in the history
  • Loading branch information
emlys committed Dec 16, 2024
2 parents df352de + 9f7b67b commit 4be31bf
Show file tree
Hide file tree
Showing 6 changed files with 65 additions and 5 deletions.
2 changes: 1 addition & 1 deletion .github/actions/setup_env/action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ runs:
cat environment.yml
- name: Setup conda environment
uses: mamba-org/setup-micromamba@v1
uses: mamba-org/setup-micromamba@v2
with:
environment-file: environment.yml
environment-name: env
Expand Down
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
2 changes: 1 addition & 1 deletion requirements-dev.txt
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

virtualenv>=12.0.1
pytest
pytest-subtests<0.14.0
pytest-subtests!=0.14.0 # https://github.com/pytest-dev/pytest-subtests/issues/173
wheel>=0.27.0
pypiwin32; sys_platform == 'win32' # pip-only

Expand Down
3 changes: 2 additions & 1 deletion src/natcap/invest/urban_nature_access.py
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,8 @@
'projection_units': u.meter,
'about': (
"A map of LULC codes. "
"All values in this raster must have corresponding entries "
"Each land use/land cover type must be assigned a unique integer "
"code. All values in this raster must have corresponding entries "
"in the LULC attribute table. For this model in particular, "
"the urban nature types are of importance. Non-nature types "
"are not required to be uniquely identified. All outputs "
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 4be31bf

Please sign in to comment.