def test_missing_lulc_demand_value(self): """Hydro: catching missing LULC value in Demand table.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerTests.generate_base_args(self.workspace_dir) args['demand_table_path'] = os.path.join(SAMPLE_DATA, 'water_demand_table.csv') args['sub_watersheds_path'] = os.path.join(SAMPLE_DATA, 'subwatersheds.shp') # remove a row from the biophysical table so that lulc value is missing bad_demand_path = os.path.join(self.workspace_dir, 'bad_demand_table.csv') demand_df = pandas.read_csv(args['demand_table_path']) demand_df = demand_df[demand_df['lucode'] != 2] demand_df.to_csv(bad_demand_path) demand_df = None args['demand_table_path'] = bad_demand_path with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args) self.assertTrue( "The missing values found in the LULC raster but not the table" " are: [2]" in str(cm.exception))
def test_invalid_lulc_veg(self): """Hydro: catching invalid LULC_veg values.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerTests.generate_base_args(self.workspace_dir) new_lulc_veg_path = os.path.join(self.workspace_dir, 'new_lulc_veg.csv') table_df = pandas.read_csv(args['biophysical_table_path']) table_df['LULC_veg'] = [''] * len(table_df.index) table_df.to_csv(new_lulc_veg_path) args['biophysical_table_path'] = new_lulc_veg_path with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args) self.assertTrue('veg value must be either 1 or 0' in str(cm.exception)) table_df = pandas.read_csv(args['biophysical_table_path']) table_df['LULC_veg'] = ['-1'] * len(table_df.index) table_df.to_csv(new_lulc_veg_path) args['biophysical_table_path'] = new_lulc_veg_path with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args) self.assertTrue('veg value must be either 1 or 0' in str(cm.exception))
def test_scarcity(self): """Hydro: testing Scarcity component.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerRegressionTests.generate_base_args(self.workspace_dir) args['water_scarcity_container'] = True args['demand_table_uri'] = os.path.join(SAMPLE_DATA, 'Hydropower', 'input', 'water_demand_table.csv') hydropower_water_yield.execute(args) raster_results = ['aet.tif', 'fractp.tif', 'wyield.tif'] for raster_path in raster_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_rasters_equal( os.path.join(args['workspace_dir'], 'output', 'per_pixel', raster_path), os.path.join(REGRESSION_DATA, raster_path)) vector_results = ['watershed_results_wyield.shp'] for vector_path in vector_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_vectors_equal( os.path.join(args['workspace_dir'], 'output', vector_path), os.path.join(REGRESSION_DATA, 'scarcity', vector_path)) table_results = ['watershed_results_wyield.csv'] for table_path in table_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_csv_equal( os.path.join(args['workspace_dir'], 'output', table_path), os.path.join(REGRESSION_DATA, 'scarcity', table_path))
def test_water_yield_subshed(self): """Hydro: testing water yield component only w/ subwatershed.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerRegressionTests.generate_base_args(self.workspace_dir) args['sub_watersheds_uri'] = os.path.join(SAMPLE_DATA, 'Base_Data', 'Freshwater', 'subwatersheds.shp') hydropower_water_yield.execute(args) raster_results = ['aet.tif', 'fractp.tif', 'wyield.tif'] for raster_path in raster_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_rasters_equal( os.path.join(args['workspace_dir'], 'output', 'per_pixel', raster_path), os.path.join(REGRESSION_DATA, raster_path)) vector_results = [ 'watershed_results_wyield.shp', 'subwatershed_results_wyield.shp' ] for vector_path in vector_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_vectors_equal( os.path.join(args['workspace_dir'], 'output', vector_path), os.path.join(REGRESSION_DATA, 'water_yield', vector_path)) table_results = [ 'watershed_results_wyield.csv', 'subwatershed_results_wyield.csv' ] for table_path in table_results: natcap.invest.pygeoprocessing_0_3_3.testing.assert_csv_equal( os.path.join(args['workspace_dir'], 'output', table_path), os.path.join(REGRESSION_DATA, 'water_yield', table_path))
def test_water_yield_subshed(self): """Hydro: testing water yield component only w/ subwatershed.""" from natcap.invest.hydropower import hydropower_water_yield from natcap.invest import utils args = HydropowerTests.generate_base_args(self.workspace_dir) args['sub_watersheds_path'] = os.path.join(SAMPLE_DATA, 'subwatersheds.shp') args['results_suffix'] = 'test' hydropower_water_yield.execute(args) raster_results = ['aet_test.tif', 'fractp_test.tif', 'wyield_test.tif'] for raster_path in raster_results: model_array = pygeoprocessing.raster_to_numpy_array( os.path.join(args['workspace_dir'], 'output', 'per_pixel', raster_path)) reg_array = pygeoprocessing.raster_to_numpy_array( os.path.join(REGRESSION_DATA, raster_path.replace('_test', ''))) numpy.testing.assert_allclose(model_array, reg_array, rtol=1e-03) vector_results = [ 'watershed_results_wyield_test.shp', 'subwatershed_results_wyield_test.shp' ] for vector_path in vector_results: utils._assert_vectors_equal( os.path.join(args['workspace_dir'], 'output', vector_path), os.path.join(REGRESSION_DATA, 'water_yield', vector_path.replace('_test', ''))) table_results = [ 'watershed_results_wyield_test.csv', 'subwatershed_results_wyield_test.csv' ] for table_path in table_results: base_table = pandas.read_csv( os.path.join(args['workspace_dir'], 'output', table_path)) expected_table = pandas.read_csv( os.path.join(REGRESSION_DATA, 'water_yield', table_path.replace('_test', ''))) pandas.testing.assert_frame_equal(base_table, expected_table)
def test_valuation_subshed(self): """Hydro: testing Valuation component w/ subwatershed.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerTests.generate_base_args(self.workspace_dir) args['demand_table_path'] = os.path.join( SAMPLE_DATA, 'water_demand_table.csv') args['valuation_table_path'] = os.path.join( SAMPLE_DATA, 'hydropower_valuation_table.csv') args['sub_watersheds_path'] = os.path.join( SAMPLE_DATA, 'subwatersheds.shp') hydropower_water_yield.execute(args) raster_results = ['aet.tif', 'fractp.tif', 'wyield.tif'] for raster_path in raster_results: pygeoprocessing.testing.assert_rasters_equal( os.path.join( args['workspace_dir'], 'output', 'per_pixel', raster_path), os.path.join(REGRESSION_DATA, raster_path), 1e-6) vector_results = ['watershed_results_wyield.shp', 'subwatershed_results_wyield.shp'] for vector_path in vector_results: pygeoprocessing.testing.assert_vectors_equal( os.path.join(args['workspace_dir'], 'output', vector_path), os.path.join(REGRESSION_DATA, 'valuation', vector_path), 1e-3) table_results = ['watershed_results_wyield.csv', 'subwatershed_results_wyield.csv'] for table_path in table_results: base_table = pandas.read_csv( os.path.join(args['workspace_dir'], 'output', table_path)) expected_table = pandas.read_csv( os.path.join(REGRESSION_DATA, 'valuation', table_path)) pandas.testing.assert_frame_equal(base_table, expected_table)
def test_validation(self): """Hydro: test failure cases on the validation function.""" from natcap.invest.hydropower import hydropower_water_yield args = HydropowerTests.generate_base_args(self.workspace_dir) # default args should be fine self.assertEqual(hydropower_water_yield.validate(args), []) args_bad_vector = args.copy() args_bad_vector['watersheds_path'] = args_bad_vector['eto_path'] bad_vector_list = hydropower_water_yield.validate(args_bad_vector) self.assertTrue( 'not be opened as a GDAL vector' in bad_vector_list[0][1]) args_bad_raster = args.copy() args_bad_raster['eto_path'] = args_bad_raster['watersheds_path'] bad_raster_list = hydropower_water_yield.validate(args_bad_raster) self.assertTrue( 'not be opened as a GDAL raster' in bad_raster_list[0][1]) args_bad_file = args.copy() args_bad_file['eto_path'] = 'non_existant_file.tif' bad_file_list = hydropower_water_yield.validate(args_bad_file) self.assertTrue('File not found' in bad_file_list[0][1]) args_missing_key = args.copy() del args_missing_key['eto_path'] validation_warnings = hydropower_water_yield.validate(args_missing_key) self.assertEqual(validation_warnings, [(['eto_path'], 'Key is missing from the args dict')]) # ensure that a missing landcover code in the biophysical table will # raise an exception that's helpful args_bad_biophysical_table = args.copy() bad_biophysical_path = os.path.join(self.workspace_dir, 'bad_biophysical_table.csv') with open(bad_biophysical_path, 'wb') as bad_biophysical_file: with open(args['biophysical_table_path'], 'rb') as (biophysical_table_file): lines_to_write = 2 for line in biophysical_table_file.readlines(): bad_biophysical_file.write(line) lines_to_write -= 1 if lines_to_write == 0: break args_bad_biophysical_table['biophysical_table_path'] = ( bad_biophysical_path) with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args_bad_biophysical_table) actual_message = str(cm.exception) self.assertTrue( "The missing values found in the LULC raster but not the table" " are: [2 3]" in actual_message, actual_message) # ensure that a missing landcover code in the demand table will # raise an exception that's helpful args_bad_biophysical_table = args.copy() bad_biophysical_path = os.path.join(self.workspace_dir, 'bad_biophysical_table.csv') with open(bad_biophysical_path, 'wb') as bad_biophysical_file: with open(args['biophysical_table_path'], 'rb') as (biophysical_table_file): lines_to_write = 2 for line in biophysical_table_file.readlines(): bad_biophysical_file.write(line) lines_to_write -= 1 if lines_to_write == 0: break args_bad_demand_table = args.copy() bad_demand_path = os.path.join(self.workspace_dir, 'bad_demand_table.csv') args_bad_demand_table['demand_table_path'] = (bad_demand_path) with open(bad_demand_path, 'wb') as bad_demand_file: with open(os.path.join(SAMPLE_DATA, 'water_demand_table.csv'), 'rb') as (demand_table_file): lines_to_write = 2 for line in demand_table_file.readlines(): bad_demand_file.write(line) lines_to_write -= 1 if lines_to_write == 0: break # ensure that a missing watershed id the valuation table will # raise an exception that's helpful with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args_bad_demand_table) actual_message = str(cm.exception) self.assertTrue( "The missing values found in the LULC raster but not the table" " are: [2 3]" in actual_message, actual_message) args_bad_valuation_table = args.copy() bad_valuation_path = os.path.join(self.workspace_dir, 'bad_valuation_table.csv') args_bad_valuation_table['valuation_table_path'] = (bad_valuation_path) # args contract requires a demand table if there is a valuation table args_bad_valuation_table['demand_table_path'] = os.path.join( SAMPLE_DATA, 'water_demand_table.csv') with open(bad_valuation_path, 'wb') as bad_valuation_file: with open( os.path.join(SAMPLE_DATA, 'hydropower_valuation_table.csv'), 'rb') as (valuation_table_file): lines_to_write = 2 for line in valuation_table_file.readlines(): bad_valuation_file.write(line) lines_to_write -= 1 if lines_to_write == 0: break with self.assertRaises(ValueError) as cm: hydropower_water_yield.execute(args_bad_valuation_table) actual_message = str(cm.exception) self.assertTrue( 'but are not found in the valuation table' in actual_message, actual_message)
args['subsurface_eff_p'] = '0.5' 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)
def test_suffix_underscore(self): """Hydro: testing that a suffix w/ underscore is handled correctly.""" from natcap.invest.hydropower import hydropower_water_yield args = { 'workspace_dir': self.workspace_dir, 'lulc_uri': os.path.join(REGRESSION_DATA, 'smoke', 'lulc_smoke.tif'), 'depth_to_root_rest_layer_uri': os.path.join(REGRESSION_DATA, 'smoke', 'dtr_smoke.tif'), 'precipitation_uri': os.path.join(REGRESSION_DATA, 'smoke', 'precip_smoke.tif'), 'pawc_uri': os.path.join(REGRESSION_DATA, 'smoke', 'pawc_smoke.tif'), 'eto_uri': os.path.join(REGRESSION_DATA, 'smoke', 'eto_smoke.tif'), 'watersheds_uri': os.path.join(REGRESSION_DATA, 'smoke', 'watershed_smoke.shp'), 'biophysical_table_uri': os.path.join(REGRESSION_DATA, 'smoke', 'biophysical_smoke.csv'), 'seasonality_constant': 5, 'water_scarcity_container': True, 'demand_table_uri': os.path.join(REGRESSION_DATA, 'smoke', 'demand_smoke.csv'), 'valuation_container': True, 'valuation_table_uri': os.path.join(REGRESSION_DATA, 'smoke', 'valuation_params_smoke.csv'), 'sub_watersheds_uri': os.path.join(REGRESSION_DATA, 'smoke', 'subwatershed_smoke.shp'), 'results_suffix': '_test' } hydropower_water_yield.execute(args) raster_results = ['aet_test.tif', 'fractp_test.tif', 'wyield_test.tif'] for raster_path in raster_results: self.assertTrue( os.path.exists( os.path.join(args['workspace_dir'], 'output', 'per_pixel', raster_path))) vector_results = [ 'watershed_results_wyield_test.shp', 'subwatershed_results_wyield_test.shp' ] for vector_path in vector_results: self.assertTrue( os.path.exists( os.path.join(args['workspace_dir'], 'output', vector_path))) table_results = [ 'watershed_results_wyield_test.csv', 'subwatershed_results_wyield_test.csv' ] for table_path in table_results: self.assertTrue( os.path.exists( os.path.join(args['workspace_dir'], 'output', table_path)))