def test_base_regression(self): """SDR base regression test on sample data. Execute SDR with sample data and checks that the output files are generated and that the aggregate shapefile fields are the same as the regression case. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args( 'sdr_test_workspace') #self.workspace_dir) # make args explicit that this is a base run of SWY sdr.execute(args) expected_results = { 'usle_tot': 14.25030517578, 'sed_retent': 443994.1875, 'sed_export': 0.87300693989, 'sed_dep': 9.32623577118, } vector_path = os.path.join(args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path)
def test_base_regression(self): """SDR base regression test on sample data. Execute SDR with sample data and checks that the output files are generated and that the aggregate shapefile fields are the same as the regression case. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # make args explicit that this is a base run of SWY gpkg_driver = ogr.GetDriverByName('GPKG') base_vector = ogr.Open(args['watersheds_path']) target_watersheds_path = os.path.join( args['workspace_dir'], 'input_watersheds.gpkg') target_vector = gpkg_driver.CopyDataSource( base_vector, target_watersheds_path) base_vector = None target_vector = None args['watersheds_path'] = target_watersheds_path sdr.execute(args) expected_results = { 'usle_tot': 12.69931602478, 'sed_retent': 402704.96875, 'sed_export': 0.7930983305, 'sed_dep': 8.58807754517, } vector_path = os.path.join( args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path)
def test_base_regression(self): """SDR base regression test on sample data. Execute SDR with sample data and checks that the output files are generated and that the aggregate shapefile fields are the same as the regression case. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # make args explicit that this is a base run of SWY sdr.execute(args) expected_results = { 'usle_tot': 14.25030517578, 'sed_retent': 308382.125, 'sed_export': 0.60502111912, 'sed_dep': 9.05251502991 } vector_path = os.path.join(args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path) # We only need to test that the drainage mask exists. Functionality # for that raster is tested elsewhere self.assertTrue( os.path.exists( os.path.join(args['workspace_dir'], 'intermediate_outputs', 'what_drains_to_stream.tif')))
def test_missing_lulc_value(self): """SDR test for ValueError when LULC value not found in table.""" from natcap.invest.sdr import sdr import pandas # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # make args explicit that this is a base run of SWY gpkg_driver = ogr.GetDriverByName('GPKG') base_vector = ogr.Open(args['watersheds_path']) target_watersheds_path = os.path.join(args['workspace_dir'], 'input_watersheds.gpkg') target_vector = gpkg_driver.CopyDataSource(base_vector, target_watersheds_path) base_vector = None target_vector = None args['watersheds_path'] = target_watersheds_path # remove a row from the biophysical table so that lulc value is missing bad_biophysical_path = os.path.join(self.workspace_dir, 'bad_biophysical_table.csv') bio_df = pandas.read_csv(args['biophysical_table_path']) bio_df = bio_df[bio_df['lucode'] != 2] bio_df.to_csv(bad_biophysical_path) bio_df = None args['biophysical_table_path'] = bad_biophysical_path with self.assertRaises(ValueError) as context: sdr.execute(args) self.assertTrue( "The missing values found in the LULC raster but not the table" " are: [2.]" in str(context.exception))
def test_base_usle_p_nan(self): """SDR test expected exception for USLE_P not a number.""" from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) args['biophysical_table_path'] = os.path.join( REGRESSION_DATA, 'biophysical_table_invalid_value.csv') with self.assertRaises(ValueError): sdr.execute(args)
def test_base_usle_c_too_large(self): """SDR test exepected exception for USLE_C > 1.0.""" from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) args['biophysical_table_path'] = os.path.join( REGRESSION_DATA, 'biophysical_table_too_large.csv') with self.assertRaises(ValueError) as context: sdr.execute(args) self.assertIn( f'A value in the biophysical table is not a number ' f'within range 0..1.', str(context.exception))
def test_regression_with_undefined_nodata(self): """SDR base regression test with undefined nodata values. Execute SDR with sample data with all rasters having undefined nodata values. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # args_copy = args.copy() # args_copy['workspace_dir'] = 'sdr_test_workspace' # set all input rasters to have undefined nodata values tmp_dir = os.path.join(args['workspace_dir'], 'nodata_raster_dir') os.makedirs(tmp_dir) for path_key in ['erodibility_path', 'erosivity_path', 'lulc_path']: target_path = os.path.join(tmp_dir, os.path.basename(args[path_key])) datatype = pygeoprocessing.get_raster_info( args[path_key])['datatype'] pygeoprocessing.new_raster_from_base(args[path_key], target_path, datatype, [None]) base_raster = gdal.OpenEx(args[path_key], gdal.OF_RASTER) base_band = base_raster.GetRasterBand(1) base_array = base_band.ReadAsArray() base_band = None base_raster = None target_raster = gdal.OpenEx(target_path, gdal.OF_RASTER | gdal.GA_Update) target_band = target_raster.GetRasterBand(1) target_band.WriteArray(base_array) target_band = None target_raster = None args[path_key] = target_path sdr.execute(args) expected_results = { 'sed_retent': 443994.1875, 'sed_export': 0.87300693989, 'usle_tot': 14.25030517578, } vector_path = os.path.join(args['workspace_dir'], 'watershed_results_sdr.shp') # make args explicit that this is a base run of SWY assert_expected_results_in_vector(expected_results, vector_path)
def test_base_regression(self): """SDR base regression test on sample data. Execute SDR with sample data and checks that the output files are generated and that the aggregate shapefile fields are the same as the regression case. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # make args explicit that this is a base run of SWY sdr.execute(args) expected_results = { 'usle_tot': 14.25030517578, 'sed_retent': 308382.125, 'sed_export': 0.60502111912, 'sed_dep': 9.05251502991 } vector_path = os.path.join(args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path) # We only need to test that the drainage mask exists. Functionality # for that raster is tested elsewhere self.assertTrue( os.path.exists( os.path.join(args['workspace_dir'], 'intermediate_outputs', 'what_drains_to_stream.tif'))) # Check that sed_deposition does not have any negative, non-nodata # values, even if they are very small. sed_deposition_path = os.path.join(args['workspace_dir'], 'sed_deposition.tif') sed_dep_nodata = pygeoprocessing.get_raster_info( sed_deposition_path)['nodata'][0] sed_dep_array = pygeoprocessing.raster_to_numpy_array( sed_deposition_path) negative_non_nodata_mask = ( (~numpy.isclose(sed_dep_array, sed_dep_nodata)) & (sed_dep_array < 0)) self.assertEqual( numpy.count_nonzero(sed_dep_array[negative_non_nodata_mask]), 0)
def test_lucode_not_a_number(self): """SDR test expected exception for invalid data in lucode column.""" from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) args['biophysical_table_path'] = os.path.join( self.workspace_dir, 'biophysical_table_invalid_lucode.csv') invalid_value = 'forest' with open(args['biophysical_table_path'], 'w') as file: file.write(f'desc,lucode,usle_p,usle_c\n' f'0,{invalid_value},0.5,0.5\n') with self.assertRaises(ValueError) as context: sdr.execute(args) self.assertIn( f'Value "{invalid_value}" from the "lucode" column of the ' f'biophysical table is not a number.', str(context.exception))
def test_non_square_dem(self): """SDR non-square DEM pixels. Execute SDR with a non-square DEM and get a good result back. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) args['dem_path'] = os.path.join(SAMPLE_DATA, 'dem_non_square.tif') # make args explicit that this is a base run of SWY sdr.execute(args) expected_results = { 'sed_retent': 345797.375, 'sed_export': 0.63070225716, 'usle_tot': 11.46732711792, } vector_path = os.path.join( args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path)
def test_drainage_regression(self): """SDR drainage layer regression test on sample data. Execute SDR with sample data and a drainage layer and checks that the output files are generated and that the aggregate shapefile fields are the same as the regression case. """ from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) args['drainage_path'] = os.path.join( REGRESSION_DATA, 'sample_drainage.tif') sdr.execute(args) expected_results = { 'sed_retent': 436809.59375, 'sed_export': 0.94600570202, 'usle_tot': 11.59875869751, } vector_path = os.path.join( args['workspace_dir'], 'watershed_results_sdr.shp') assert_expected_results_in_vector(expected_results, vector_path)
def test_missing_lulc_value(self): """SDR test for ValueError when LULC value not found in table.""" import pandas from natcap.invest.sdr import sdr # use predefined directory so test can clean up files during teardown args = SDRTests.generate_base_args(self.workspace_dir) # remove a row from the biophysical table so that lulc value is missing bad_biophysical_path = os.path.join(self.workspace_dir, 'bad_biophysical_table.csv') bio_df = pandas.read_csv(args['biophysical_table_path']) bio_df = bio_df[bio_df['lucode'] != 2] bio_df.to_csv(bad_biophysical_path) bio_df = None args['biophysical_table_path'] = bad_biophysical_path with self.assertRaises(ValueError) as context: sdr.execute(args) self.assertIn( "The missing values found in the LULC raster but not the table" " are: [2.]", str(context.exception))
args['calc_sequestration'] = False args['carbon_pools_path'] = BioTable args['do_redd'] = False args['do_valuation'] = False args['lulc_cur_path'] = LULC # Seasonal Water Yield args['workspace_dir'] = os.path.join( os.path.split(os.getcwd())[0], 'RESULTS', '02-Seasonal-Water-Yield') swy.execute(args) # Anual Water Yield args['workspace_dir'] = os.path.join( os.path.split(os.getcwd())[0], 'RESULTS', '01-Anual-Water-Yield') awy.execute(args) # Sediment Delivery Ratio args['workspace_dir'] = os.path.join( os.path.split(os.getcwd())[0], 'RESULTS', '03-Sediment-Delivery-Ratio') sdr.execute(args) # Nutrient Delivery Ratio args['workspace_dir'] = os.path.join( os.path.split(os.getcwd())[0], 'RESULTS', '04-Nutrient-Delivery-Ratio') ndr.execute(args) # Carbons args['workspace_dir'] = os.path.join( os.path.split(os.getcwd())[0], 'RESULTS', '05-Carbons') carbon.execute(args)