def test_calc_sector_total_impact(self): """Test running total impact calculations.""" sup = SupplyChain() sup.read_wiod16(year='test', range_rows=(5, 117), range_cols=(4, 116), col_iso3=2, col_sectors=1) # Tropical cyclone over Florida and Caribbean hazard = Hazard('TC') hazard.read_mat(HAZ_TEST_MAT) # Read demo entity values # Set the entity default file to the demo one exp = Exposures() exp.read_hdf5(EXP_DEMO_H5) exp.check() exp.gdf.region_id = 840 #assign right id for USA exp.assign_centroids(hazard) impf_tc = IFTropCyclone() impf_tc.set_emanuel_usa() impf_set = ImpactFuncSet() impf_set.append(impf_tc) impf_set.check() sup.calc_sector_direct_impact(hazard, exp, impf_set) sup.calc_indirect_impact(io_approach='ghosh') sup.calc_total_impact() self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.total_impact.shape) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.total_aai_agg.shape)
def test_EU(self): """test with demo data containing France and Germany""" bbox = [-5, 42, 16, 55] haz = RelativeCropyield() haz.set_from_single_run(input_dir=INPUT_DIR, yearrange=(2001, 2005), bbox=bbox, ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) hist_mean = haz.calc_mean(yearrange_mean=(2001, 2005)) haz.set_rel_yield_to_int(hist_mean) haz.centroids.set_region_id() exp = CropProduction() exp.set_from_single_run(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=bbox, yearrange=(2001, 2005), scenario='flexible', unit='t', crop='whe', irr='firr') exp.set_to_usd(INPUT_DIR) exp.assign_centroids(haz, threshold=20) if_cp = ImpactFuncSet() if_def = IFRelativeCropyield() if_def.set_relativeyield() if_cp.append(if_def) if_cp.check() impact = Impact() impact.calc(exp.loc[exp.region_id == 276], if_cp, haz.select(['2002']), save_mat=True) exp_manual = exp.value.loc[exp.region_id == 276].values impact_manual = haz.select(event_names=['2002'], reg_id=276).intensity.multiply(exp_manual) dif = (impact_manual - impact.imp_mat).data self.assertEqual(haz.tag.haz_type, 'RC') self.assertEqual(haz.size, 5) self.assertEqual(haz.centroids.size, 1092) self.assertAlmostEqual(haz.intensity.mean(), -2.0489097e-08) self.assertAlmostEqual(exp.value.max(), 53074789.755290434) self.assertEqual(exp.latitude.values.size, 1092) self.assertAlmostEqual(exp.value[3], 0.0) self.assertAlmostEqual(exp.value[1077], 405026.6857207429) self.assertAlmostEqual(impact.imp_mat.data[3], -176102.5359452465) self.assertEqual(len(dif), 0)
def test_EU_nan(self): """Test whether setting the zeros in exp.value to NaN changes the impact""" bbox = [0, 42, 10, 52] haz = RelativeCropyield() haz.set_from_isimip_netcdf(input_dir=INPUT_DIR, yearrange=(2001, 2005), bbox=bbox, ag_model='lpjml', cl_model='ipsl-cm5a-lr', scenario='historical', soc='2005soc', co2='co2', crop='whe', irr='noirr', fn_str_var=FN_STR_DEMO) hist_mean = haz.calc_mean(yearrange_mean=(2001, 2005)) haz.set_rel_yield_to_int(hist_mean) haz.centroids.set_region_id() exp = CropProduction() exp.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=bbox, yearrange=(2001, 2005), scenario='flexible', unit='t/y', crop='whe', irr='firr') exp.assign_centroids(haz, threshold=20) impf_cp = ImpactFuncSet() impf_def = ImpfRelativeCropyield() impf_def.set_relativeyield() impf_cp.append(impf_def) impf_cp.check() impact = Impact() impact.calc(exp, impf_cp, haz, save_mat=True) exp_nan = CropProduction() exp_nan.set_from_isimip_netcdf(input_dir=INPUT_DIR, filename=FILENAME_LU, hist_mean=FILENAME_MEAN, bbox=[0, 42, 10, 52], yearrange=(2001, 2005), scenario='flexible', unit='t/y', crop='whe', irr='firr') exp_nan.gdf.value[exp_nan.gdf.value == 0] = np.nan exp_nan.assign_centroids(haz, threshold=20) impact_nan = Impact() impact_nan.calc(exp_nan, impf_cp, haz, save_mat=True) self.assertListEqual(list(impact.at_event), list(impact_nan.at_event)) self.assertAlmostEqual(12.056545220060798, impact_nan.aai_agg) self.assertAlmostEqual(12.056545220060798, impact.aai_agg)
def calc_sector_direct_impact(self): """Test running direct impact calculations.""" sup = SupplyChain() sup.read_wiod16(year='test', range_rows=(5, 117), range_cols=(4, 116), col_iso3=2, col_sectors=1) # Tropical cyclone over Florida and Caribbean hazard = Hazard('TC') hazard.read_mat(HAZ_TEST_MAT) # Read demo entity values # Set the entity default file to the demo one exp = Exposures() exp.read_hdf5(EXP_DEMO_H5) exp.check() exp.gdf.region_id = 840 #assign right id for USA exp.assign_centroids(hazard) impf_tc = IFTropCyclone() impf_tc.set_emanuel_usa() impf_set = ImpactFuncSet() impf_set.append(impf_tc) impf_set.check() subsecs = list(range(10)) + list(range(15, 25)) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec=subsecs) self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.reg_dir_imp[0], 'USA') self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, subsecs].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[subsecs].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='manufacturing') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.reg_dir_imp[0], 'USA') self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(4, 23)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(4, 23)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='agriculture') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(0, 1)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(0, 1)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='mining') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(3, 4)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(3, 4)].sum(), places=3) sup.calc_sector_direct_impact(hazard, exp, impf_set, selected_subsec='service') self.assertAlmostEqual((sup.years.shape[0], sup.mriot_data.shape[0]), sup.direct_impact.shape) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual((sup.mriot_data.shape[0], ), sup.direct_aai_agg.shape) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[sup.reg_pos['USA']].sum(), places=3) self.assertAlmostEqual(sup.direct_impact.sum(), sup.direct_impact[:, range(26, 56)].sum(), places=3) self.assertAlmostEqual(sup.direct_aai_agg.sum(), sup.direct_aai_agg[range(26, 56)].sum(), places=3)