parent
6b0c1670f5
commit
fa05f5e75a
|
@ -386,11 +386,17 @@ def execute(args):
|
|||
_COARSE_YIELD_REGRESSION_PARAMETER_FILE_PATTERN % (
|
||||
crop_name, yield_regression_id, file_suffix))
|
||||
create_coarse_regression_parameter_task = task_graph.add_task(
|
||||
func=pygeoprocessing.reclassify_raster,
|
||||
func=utils._reclassify_raster_op,
|
||||
args=((clipped_climate_bin_raster_path, 1),
|
||||
bin_to_regression_value,
|
||||
coarse_regression_parameter_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} Regression Yield')}},
|
||||
target_path_list=[coarse_regression_parameter_raster_path],
|
||||
dependent_task_list=[crop_climate_bin_task],
|
||||
task_name='create_coarse_regression_parameter_%s_%s' % (
|
||||
|
|
|
@ -188,6 +188,60 @@ class CropProductionTests(unittest.TestCase):
|
|||
with self.assertRaises(ValueError):
|
||||
crop_production_regression.execute(args)
|
||||
|
||||
def test_crop_production_regression_missing_climate_bin(self):
|
||||
"""Crop Production: test crop regression with a missing climate bin."""
|
||||
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'),
|
||||
'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,
|
||||
'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,
|
||||
'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 regression yield table so that a wheat
|
||||
# climate bin value is missing
|
||||
climate_bin_wheat_table_path = os.path.join(
|
||||
MODEL_DATA_PATH, 'climate_regression_yield_tables',
|
||||
'wheat_regression_yield_table.csv')
|
||||
|
||||
bad_climate_bin_wheat_table_path = os.path.join(
|
||||
tmp_copy_model_data_path, 'climate_regression_yield_tables',
|
||||
'wheat_regression_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_regression.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(self):
|
||||
"""Crop Production: test crop production regression model."""
|
||||
from natcap.invest import crop_production_regression
|
||||
|
|
Loading…
Reference in New Issue