I. #8 reclassify check for crop_prod_perc

Adding a test as well
This commit is contained in:
Doug 2020-08-24 14:55:30 -04:00
parent fa05f5e75a
commit a8a4508432
2 changed files with 55 additions and 1 deletions

View File

@ -321,11 +321,17 @@ def execute(args):
_COARSE_YIELD_PERCENTILE_FILE_PATTERN % (
crop_name, yield_percentile_id, file_suffix))
create_coarse_yield_percentile_task = task_graph.add_task(
func=pygeoprocessing.reclassify_raster,
func=utils._reclassify_raster_op,
args=((clipped_climate_bin_raster_path, 1),
bin_to_percentile_yield,
coarse_yield_percentile_raster_path, gdal.GDT_Float32,
_NODATA_YIELD),
kwargs={'values_required': True,
'error_details': {
'raster_name': f'{crop_name} Climate Bin',
'column_name': 'climate_bin',
'table_name': (
f'Climate {crop_name} Percentile Yield')}},
target_path_list=[coarse_yield_percentile_raster_path],
dependent_task_list=[crop_climate_bin_task],
task_name='create_coarse_yield_percentile_%s_%s' % (

View File

@ -157,6 +157,54 @@ class CropProductionTests(unittest.TestCase):
with self.assertRaises(ValueError):
crop_production_percentile.execute(args)
def test_crop_production_percentile_missing_climate_bin(self):
"""Crop Production: test crop percentile with a missing climate bin."""
from natcap.invest import crop_production_percentile
args = {
'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_crop_table.csv'),
'aggregate_polygon_path': os.path.join(
SAMPLE_DATA_PATH, 'aggregate_shape.shp'),
'model_data_path': MODEL_DATA_PATH,
'n_workers': '-1'
}
# copy model data directory to a temp location so that hard coded
# data paths can be altered for this test.
tmp_copy_model_data_path = os.path.join(
self.workspace_dir, 'tmp_model_data')
shutil.copytree(MODEL_DATA_PATH, tmp_copy_model_data_path)
# remove a row from the wheat percentile yield table so that a wheat
# climate bin value is missing
climate_bin_wheat_table_path = os.path.join(
MODEL_DATA_PATH, 'climate_percentile_yield_tables',
'wheat_percentile_yield_table.csv')
bad_climate_bin_wheat_table_path = os.path.join(
tmp_copy_model_data_path, 'climate_percentile_yield_tables',
'wheat_percentile_yield_table.csv')
os.remove(bad_climate_bin_wheat_table_path)
table_df = pandas.read_csv(climate_bin_wheat_table_path)
table_df = table_df[table_df['climate_bin'] != 40]
table_df.to_csv(bad_climate_bin_wheat_table_path)
table_df = None
args['model_data_path'] = tmp_copy_model_data_path
with self.assertRaises(ValueError) as context:
crop_production_percentile.execute(args)
self.assertTrue(
"The missing values found in the wheat Climate Bin raster but not"
" the table are: [40]" in str(context.exception))
def test_crop_production_regression_bad_crop(self):
"""Crop Production: test crop regression with a bad crop name."""
from natcap.invest import crop_production_regression