re #BITBUCKET-3626 adding regression test for regression crop production model

This commit is contained in:
Rich Sharp 2017-05-16 13:53:40 -07:00
parent 40f15046e4
commit a7a600d0ec
3 changed files with 75 additions and 14 deletions

View File

@ -559,6 +559,9 @@ def execute(args):
1.0 - nutrient_table[crop_name]['Percentrefuse'] / 100.0)
LOGGER.info(
"Calculating zonal stats for %s", crop_name)
crop_production_raster_path = os.path.join(
output_dir, _CROP_PRODUCTION_FILE_PATTERN % (
crop_name, file_suffix))
total_yield_lookup['%s_modeled' % crop_name] = (
pygeoprocessing.zonal_statistics(
(crop_production_raster_path, 1),

View File

@ -35,7 +35,7 @@ class CropProductionTests(unittest.TestCase):
@scm.skip_if_data_missing(SAMPLE_DATA_PATH)
@scm.skip_if_data_missing(MODEL_DATA_PATH)
def test_crop_production_percentile(self):
"""Crop Production: test crop production."""
"""Crop Production: test crop production percentile regression."""
from natcap.invest import crop_production_percentile
args = {
@ -83,7 +83,7 @@ class CropProductionTests(unittest.TestCase):
@scm.skip_if_data_missing(SAMPLE_DATA_PATH)
@scm.skip_if_data_missing(MODEL_DATA_PATH)
def test_crop_production_percentile_bad_crop(self):
"""Crop Production: test crop production."""
"""Crop Production: test crop production with a bad crop name."""
from natcap.invest import crop_production_percentile
args = {
@ -109,27 +109,85 @@ class CropProductionTests(unittest.TestCase):
@scm.skip_if_data_missing(SAMPLE_DATA_PATH)
@scm.skip_if_data_missing(MODEL_DATA_PATH)
def test_crop_production_regression(self):
"""Crop Production: test crop production."""
def test_crop_production_percentile_bad_crop(self):
"""Crop Production: test crop regression with a bad crop name."""
from natcap.invest import crop_production_regression
args = {
#'workspace_dir': self.workspace_dir,
'workspace_dir': r"C:\Users\rpsharp\Documents\crop_production_regression",
'workspace_dir': self.workspace_dir,
'results_suffix': '',
'landcover_raster_path': os.path.join(
SAMPLE_DATA_PATH, 'landcover.tif'),
'landcover_to_crop_table_path': os.path.join(
SAMPLE_DATA_PATH, 'landcover_to_badcrop_table.csv'),
'aggregate_polygon_path': os.path.join(
SAMPLE_DATA_PATH, 'aggregate_shape.shp'),
'aggregate_polygon_id': 'id',
'model_data_path': MODEL_DATA_PATH,
'fertilization_rate_table_path': os.path.join(
SAMPLE_DATA_PATH, 'crop_fertilization_rates.csv'),
'nitrogen_fertilization_rate': 29.6,
'phosphorous_fertilization_rate': 8.4,
'potassium_fertilization_rate': 14.2,
}
with open(args['landcover_to_crop_table_path'],
'wb') as landcover_crop_table:
landcover_crop_table.write(
'crop_name,lucode\nfakecrop,20\n')
with self.assertRaises(ValueError):
crop_production_regression.execute(args)
@scm.skip_if_data_missing(SAMPLE_DATA_PATH)
@scm.skip_if_data_missing(MODEL_DATA_PATH)
def test_crop_production_regression(self):
"""Crop Production: test crop production regression model."""
from natcap.invest import crop_production_regression
args = {
'workspace_dir': self.workspace_dir,
'results_suffix': '',
'landcover_raster_path': os.path.join(
SAMPLE_DATA_PATH, 'landcover.tif'),
'k_raster_path': os.path.join(
SAMPLE_DATA_PATH, 'fake_fert_map.tif'),
'p_raster_path': os.path.join(
SAMPLE_DATA_PATH, 'fake_fert_map.tif'),
'n_raster_path': os.path.join(
SAMPLE_DATA_PATH, 'fake_fert_map.tif'),
'landcover_to_crop_table_path': os.path.join(
SAMPLE_DATA_PATH, 'landcover_to_crop_table.csv'),
'aggregate_polygon_path': os.path.join(
SAMPLE_DATA_PATH, 'aggregate_shape.shp'),
'aggregate_polygon_id': 'id',
'model_data_path': MODEL_DATA_PATH
'model_data_path': MODEL_DATA_PATH,
'fertilization_rate_table_path': os.path.join(
SAMPLE_DATA_PATH, 'crop_fertilization_rates.csv'),
'nitrogen_fertilization_rate': 29.6,
'phosphorous_fertilization_rate': 8.4,
'potassium_fertilization_rate': 14.2,
}
crop_production_regression.execute(args)
result_table_path = os.path.join(
args['workspace_dir'], 'aggregate_results.csv')
from natcap.invest import utils
result_table = utils.build_lookup_from_csv(
result_table_path, 'id', to_lower=True, numerical_cast=True)
expected_result_table_path = os.path.join(
TEST_DATA_PATH, 'expected_regression_aggregate_results.csv')
expected_result_table = utils.build_lookup_from_csv(
expected_result_table_path, 'id', to_lower=True,
numerical_cast=True)
for id_key in expected_result_table:
if id_key not in result_table:
self.fail("Expected ID %s in result table" % id_key)
for column_key in expected_result_table[id_key]:
if column_key not in result_table[id_key]:
self.fail(
"Expected column %s in result table" % column_key)
# The tolerance of 3 digits after the decimal was determined by
# experimentation
tolerance_places = 3
numpy.testing.assert_almost_equal(
expected_result_table[id_key][column_key],
result_table[id_key][column_key],
decimal=tolerance_places)

View File

@ -1,6 +1,6 @@
{
"data/invest-data": "144",
"data/invest-test-data": "120",
"data/invest-test-data": "121",
"doc/users-guide": "93d3df555ec8",
"src/pygeoprocessing": "REQUIREMENTS_TXT:pygeoprocessing",
"src/pyinstaller": {