Skip to content

Commit

Permalink
HQ validation now partially covered by expressions.
Browse files Browse the repository at this point in the history
  • Loading branch information
phargogh committed Feb 22, 2024
1 parent 831fd99 commit 4b6f932
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 27 deletions.
19 changes: 3 additions & 16 deletions src/natcap/invest/habitat_quality.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,26 +5,22 @@
import os

import numpy
from osgeo import gdal
import pygeoprocessing
import taskgraph
from osgeo import gdal

from .model_metadata import MODEL_METADATA
from . import gettext
from . import spec_utils
from . import utils
from . import validation
from .model_metadata import MODEL_METADATA
from .unit_registry import u
from . import gettext


LOGGER = logging.getLogger(__name__)

MISSING_SENSITIVITY_TABLE_THREATS_MSG = gettext(
'Threats {threats} does not match any column in the sensitivity table. '
'Sensitivity columns: {column_names}') # (set of missing threats, set of found columns)
MISSING_COLUMN_MSG = gettext(
"The column '{column_name}' was not found in the Threat Data table for "
"the corresponding input LULC scenario.")
MISSING_THREAT_RASTER_MSG = gettext(
"A threat raster for threats: {threat_list} was not found or it "
"could not be opened by GDAL.")
Expand Down Expand Up @@ -1116,7 +1112,6 @@ def validate(args, limit_to=None):
bad_threat_paths = []
duplicate_paths = []
threat_path_list = []
bad_threat_columns = []
for lulc_key, lulc_arg in (('_c', 'lulc_cur_path'),
('_f', 'lulc_fut_path'),
('_b', 'lulc_bas_path')):
Expand All @@ -1126,9 +1121,6 @@ def validate(args, limit_to=None):
# threat_raster_folder
for threat, row in threat_df.iterrows():
threat_table_path_col = _THREAT_SCENARIO_MAP[lulc_key]
if threat_table_path_col not in row:
bad_threat_columns.append(threat_table_path_col)
break

# Threat path from threat CSV is relative to CSV
threat_path = row[threat_table_path_col]
Expand All @@ -1151,11 +1143,6 @@ def validate(args, limit_to=None):
duplicate_paths.append(
os.path.basename(threat_path))

if bad_threat_columns:
validation_warnings.append((
['threats_table_path'],
MISSING_COLUMN_MSG.format(column_name=bad_threat_columns[0])))

if bad_threat_paths:
validation_warnings.append((
['threats_table_path'],
Expand Down
30 changes: 19 additions & 11 deletions tests/test_habitat_quality.py
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
"""Module for Regression Testing the InVEST Habitat Quality model."""
import unittest
import tempfile
import shutil
import os
import shutil
import tempfile
import unittest

import numpy
import pygeoprocessing
from osgeo import gdal
from osgeo import osr
from osgeo import ogr
from osgeo import osr
from shapely.geometry import Polygon
import numpy
import pygeoprocessing


def make_raster_from_array(
Expand Down Expand Up @@ -1292,7 +1292,8 @@ def test_habitat_quality_validation_wrong_spatial_types(self):

def test_habitat_quality_validation_missing_sens_header(self):
"""Habitat Quality: test validation for sens threat header."""
from natcap.invest import habitat_quality, utils
from natcap.invest import habitat_quality
from natcap.invest import utils

args = {
'half_saturation_constant': '0.5',
Expand Down Expand Up @@ -1938,7 +1939,8 @@ def test_habitat_quality_argspec_spatial_overlap(self):

def test_habitat_quality_argspec_missing_projection(self):
"""Habitat Quality: raise error on missing projection."""
from natcap.invest import habitat_quality, validation
from natcap.invest import habitat_quality
from natcap.invest import validation

args = {
'half_saturation_constant': '0.5',
Expand Down Expand Up @@ -2014,7 +2016,8 @@ def test_habitat_quality_argspec_missing_projection(self):

def test_habitat_quality_argspec_missing_threat_header(self):
"""Habitat Quality: test validate for a threat header."""
from natcap.invest import habitat_quality, validation
from natcap.invest import habitat_quality
from natcap.invest import validation

args = {
'half_saturation_constant': '0.5',
Expand Down Expand Up @@ -2065,6 +2068,7 @@ def test_habitat_quality_argspec_missing_threat_header(self):
def test_habitat_quality_validate_missing_base_column(self):
"""Habitat Quality: test validate for a missing base column."""
from natcap.invest import habitat_quality
from natcap.invest import validation

args = {
'half_saturation_constant': '0.5',
Expand Down Expand Up @@ -2109,13 +2113,15 @@ def test_habitat_quality_validate_missing_base_column(self):
validate_result = habitat_quality.validate(args, limit_to=None)
expected = [(
['threats_table_path'],
habitat_quality.MISSING_COLUMN_MSG.format(column_name='base_path')
validation.MESSAGES['MATCHED_NO_HEADERS'].format(
header="column", header_name="base_path")
)]
self.assertEqual(validate_result, expected)

def test_habitat_quality_validate_missing_fut_column(self):
"""Habitat Quality: test validate for a missing fut column."""
from natcap.invest import habitat_quality
from natcap.invest import validation

args = {
'half_saturation_constant': '0.5',
Expand Down Expand Up @@ -2159,5 +2165,7 @@ def test_habitat_quality_validate_missing_fut_column(self):
validate_result = habitat_quality.validate(args, limit_to=None)
expected = [(
['threats_table_path'],
habitat_quality.MISSING_COLUMN_MSG.format(column_name='fut_path'))]
validation.MESSAGES['MATCHED_NO_HEADERS'].format(
header='column', header_name='fut_path')
)]
self.assertEqual(validate_result, expected)

0 comments on commit 4b6f932

Please sign in to comment.