def test_cutoff_hazard_pass(self): """Test _cutoff_hazard_damage""" meas = MeasureSet() meas.read_mat(ENT_TEST_MAT) act_1 = meas.get_measure(name='Seawall')[0] haz = Hazard('TC') haz.read_mat(HAZ_TEST_MAT) exp = Exposures() exp.read_mat(ENT_TEST_MAT) exp.gdf.rename(columns={'if_': 'if_TC'}, inplace=True) exp.check() imp_set = ImpactFuncSet() imp_set.read_mat(ENT_TEST_MAT) new_haz = act_1._cutoff_hazard_damage(exp, imp_set, haz) self.assertFalse(id(new_haz) == id(haz)) pos_no_null = np.array([6249, 7697, 9134, 13500, 13199, 5944, 9052, 9050, 2429, 5139, 9053, 7102, 4096, 1070, 5948, 1076, 5947, 7432, 5949, 11694, 5484, 6246, 12147, 778, 3326, 7199, 12498, 11698, 6245, 5327, 4819, 8677, 5970, 7101, 779, 3894, 9051, 5976, 3329, 5978, 4282, 11697, 7193, 5351, 7310, 7478, 5489, 5526, 7194, 4283, 7191, 5328, 4812, 5528, 5527, 5488, 7475, 5529, 776, 5758, 4811, 6223, 7479, 7470, 5480, 5325, 7477, 7318, 7317, 11696, 7313, 13165, 6221]) all_haz = np.arange(haz.intensity.shape[0]) all_haz[pos_no_null] = -1 pos_null = np.argwhere(all_haz > 0).reshape(-1) for i_ev in pos_null: self.assertEqual(new_haz.intensity[i_ev, :].max(), 0)
def test_no_impact_fail(self): """Error if no impact ids.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['imp'] = 'no valid value' exp = Exposures() with self.assertRaises(KeyError): exp.read_mat(ENT_TEST_MAT, var_names=new_var_names)
def test_no_refyear_pass(self): """Not error if no value unit.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['ref'] = 'no valid ref' exp = Exposures() exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertEqual(2018, exp.ref_year)
def test_no_assigned_pass(self): """Not error if no value unit.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['ass'] = 'no valid assign' exp = Exposures() exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertTrue('centr_' not in exp.gdf)
def test_no_unit_pass(self): """Not error if no value unit.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['uni'] = 'no valid unit' exp = Exposures() exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertEqual('USD', exp.value_unit)
def test_no_region_pass(self): """Not error if no region id.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['reg'] = 'no valid region' exp = Exposures() exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertTrue('region_id' not in exp.gdf)
def test_no_category_pass(self): """Not error if no category id.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['cat'] = 'no valid category' exp = Exposures() exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertTrue('category_id' not in exp.gdf)
def test_no_coord_fail(self): """Error if no coordinates.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['lat'] = 'no valid Latitude' exp = Exposures() with self.assertRaises(KeyError): exp.read_mat(ENT_TEST_MAT, var_names=new_var_names) new_var_names['var_name']['lat'] = 'nLatitude' new_var_names['var_name']['lon'] = 'no valid Longitude' exp = Exposures() with self.assertRaises(KeyError): exp.read_mat(ENT_TEST_MAT, var_names=new_var_names)
def test_read_demo_pass(self): """Read one single excel file""" # Read demo excel file expo = Exposures() expo.read_mat(ENT_TEST_MAT) # Check results n_expos = 50 self.assertEqual(expo.gdf.index.shape, (n_expos, )) self.assertEqual(expo.gdf.index[0], 0) self.assertEqual(expo.gdf.index[n_expos - 1], n_expos - 1) self.assertEqual(expo.gdf.value.shape, (n_expos, )) self.assertEqual(expo.gdf.value[0], 13927504367.680632) self.assertEqual(expo.gdf.value[n_expos - 1], 12624818493.687229) self.assertEqual(expo.gdf.deductible.shape, (n_expos, )) self.assertEqual(expo.gdf.deductible[0], 0) self.assertEqual(expo.gdf.deductible[n_expos - 1], 0) self.assertEqual(expo.gdf.cover.shape, (n_expos, )) self.assertEqual(expo.gdf.cover[0], 13927504367.680632) self.assertEqual(expo.gdf.cover[n_expos - 1], 12624818493.687229) self.assertIn('int', str(expo.gdf.if_.dtype)) self.assertEqual(expo.gdf.if_.shape, (n_expos, )) self.assertEqual(expo.gdf.if_[0], 1) self.assertEqual(expo.gdf.if_[n_expos - 1], 1) self.assertIn('int', str(expo.gdf.category_id.dtype)) self.assertEqual(expo.gdf.category_id.shape, (n_expos, )) self.assertEqual(expo.gdf.category_id[0], 1) self.assertEqual(expo.gdf.category_id[n_expos - 1], 1) self.assertIn('int', str(expo.gdf.centr_.dtype)) self.assertEqual(expo.gdf.centr_.shape, (n_expos, )) self.assertEqual(expo.gdf.centr_[0], 47) self.assertEqual(expo.gdf.centr_[n_expos - 1], 46) self.assertTrue('region_id' not in expo.gdf) self.assertEqual(expo.gdf.latitude.shape, (n_expos, )) self.assertEqual(expo.gdf.latitude[0], 26.93389900000) self.assertEqual(expo.gdf.latitude[n_expos - 1], 26.34795700000) self.assertEqual(expo.gdf.longitude[0], -80.12879900000) self.assertEqual(expo.gdf.longitude[n_expos - 1], -80.15885500000) self.assertEqual(expo.ref_year, 2016) self.assertEqual(expo.value_unit, 'USD') self.assertEqual(expo.tag.file_name, str(ENT_TEST_MAT))
def test_cutoff_hazard_region_pass(self): """Test _cutoff_hazard_damage in specific region""" meas = MeasureSet() meas.read_mat(ENT_TEST_MAT) act_1 = meas.get_measure(name='Seawall')[0] act_1.exp_region_id = [1] haz = Hazard('TC') haz.read_mat(HAZ_TEST_MAT) exp = Exposures() exp.read_mat(ENT_TEST_MAT) exp['region_id'] = np.zeros(exp.shape[0]) exp.region_id.values[10:] = 1 exp.check() imp_set = ImpactFuncSet() imp_set.read_mat(ENT_TEST_MAT) new_haz = act_1._cutoff_hazard_damage(exp, imp_set, haz) self.assertFalse(id(new_haz) == id(haz)) pos_no_null = np.array([ 6249, 7697, 9134, 13500, 13199, 5944, 9052, 9050, 2429, 5139, 9053, 7102, 4096, 1070, 5948, 1076, 5947, 7432, 5949, 11694, 5484, 6246, 12147, 778, 3326, 7199, 12498, 11698, 6245, 5327, 4819, 8677, 5970, 7101, 779, 3894, 9051, 5976, 3329, 5978, 4282, 11697, 7193, 5351, 7310, 7478, 5489, 5526, 7194, 4283, 7191, 5328, 4812, 5528, 5527, 5488, 7475, 5529, 776, 5758, 4811, 6223, 7479, 7470, 5480, 5325, 7477, 7318, 7317, 11696, 7313, 13165, 6221 ]) all_haz = np.arange(haz.intensity.shape[0]) all_haz[pos_no_null] = -1 pos_null = np.argwhere(all_haz > 0).reshape(-1) centr_null = np.unique(exp.centr_[exp.region_id == 0]) for i_ev in pos_null: self.assertEqual(new_haz.intensity[i_ev, centr_null].max(), 0)
class Entity(object): """Collects exposures, impact functions, measures and discount rates. Default values set when empty constructor. Attributes: exposures (Exposures): exposures impact_funcs (ImpactFucs): impact functions measures (MeasureSet): measures disc_rates (DiscRates): discount rates def_file (str): Default file from configuration file """ def __init__(self): """ Empty initializator """ self.exposures = Exposures() self.disc_rates = DiscRates() self.impact_funcs = ImpactFuncSet() self.measures = MeasureSet() def read_mat(self, file_name, description=''): """Read MATLAB file of climada. Parameters: file_name (str, optional): file name(s) or folder name containing the files to read description (str or list(str), optional): one description of the data or a description of each data file Raises: ValueError """ self.exposures = Exposures() self.exposures.read_mat(file_name) self.disc_rates = DiscRates() self.disc_rates.read_mat(file_name, description) self.impact_funcs = ImpactFuncSet() self.impact_funcs.read_mat(file_name, description) self.measures = MeasureSet() self.measures.read_mat(file_name, description) def read_excel(self, file_name, description=''): """Read csv or xls or xlsx file following climada's template. Parameters: file_name (str, optional): file name(s) or folder name containing the files to read description (str or list(str), optional): one description of the data or a description of each data file Raises: ValueError """ self.exposures = Exposures(pd.read_excel(file_name)) self.exposures.tag = Tag() self.exposures.tag.file_name = file_name self.exposures.tag.description = description self.disc_rates = DiscRates() self.disc_rates.read_excel(file_name, description) self.impact_funcs = ImpactFuncSet() self.impact_funcs.read_excel(file_name, description) self.measures = MeasureSet() self.measures.read_excel(file_name, description) def write_excel(self, file_name): """ Write excel file following template. """ self.exposures.to_excel(file_name) self.impact_funcs.write_excel(file_name) self.measures.write_excel(file_name) self.disc_rates.write_excel(file_name) def check(self): """Check instance attributes. Raises: ValueError """ self.disc_rates.check() self.exposures.check() self.impact_funcs.check() self.measures.check() def __setattr__(self, name, value): """Check input type before set""" if name == "exposures": if not isinstance(value, Exposures): LOGGER.error("Input value is not (sub)class of Exposures.") raise ValueError elif name == "impact_funcs": if not isinstance(value, ImpactFuncSet): LOGGER.error("Input value is not (sub)class of ImpactFuncSet.") raise ValueError elif name == "measures": if not isinstance(value, MeasureSet): LOGGER.error("Input value is not (sub)class of MeasureSet.") raise ValueError elif name == "disc_rates": if not isinstance(value, DiscRates): LOGGER.error("Input value is not (sub)class of DiscRates.") raise ValueError super().__setattr__(name, value) def __str__(self): return 'Exposures: \n' + self.exposures.tag.__str__() + \ '\nDiscRates: \n' + self.disc_rates.tag.__str__() + \ '\nImpactFuncSet: \n' + self.impact_funcs.tag.__str__() + \ '\nMeasureSet: \n' + self.measures.tag.__str__() __repr__ = __str__
def test_filter_exposures_pass(self): """Test _filter_exposures method with two values""" meas = Measure() meas.exp_region_id = [3, 4] meas.haz_type = 'TC' exp = Exposures() exp.read_mat(ENT_TEST_MAT) exp.gdf.rename(columns={'if_': 'if_TC', 'centr_': 'centr_TC'}, inplace=True) exp.gdf['region_id'] = np.ones(exp.gdf.shape[0]) exp.gdf.region_id.values[:exp.gdf.shape[0] // 2] = 3 exp.gdf.region_id[0] = 4 exp.check() imp_set = ImpactFuncSet() imp_set.read_mat(ENT_TEST_MAT) haz = Hazard('TC') haz.read_mat(HAZ_TEST_MAT) exp.assign_centroids(haz) new_exp = copy.deepcopy(exp) new_exp.gdf['value'] *= 3 new_exp.gdf['if_TC'].values[:20] = 2 new_exp.gdf['if_TC'].values[20:40] = 3 new_exp.gdf['if_TC'].values[40:] = 1 new_ifs = copy.deepcopy(imp_set) new_ifs.get_func('TC')[1].intensity += 1 ref_ifs = copy.deepcopy(new_ifs) new_haz = copy.deepcopy(haz) new_haz.intensity *= 4 res_exp, res_ifs, res_haz = meas._filter_exposures(exp, imp_set, haz, new_exp.copy(deep=True), new_ifs, new_haz) # unchanged meta data self.assertEqual(res_exp.ref_year, exp.ref_year) self.assertEqual(res_exp.value_unit, exp.value_unit) self.assertEqual(res_exp.tag.file_name, exp.tag.file_name) self.assertEqual(res_exp.tag.description, exp.tag.description) self.assertEqual(res_exp.crs, exp.crs) self.assertEqual(res_exp.gdf.crs, exp.gdf.crs) # regions (that is just input data, no need for testing, but it makes the changed and unchanged parts obious) self.assertTrue(np.array_equal(res_exp.gdf.region_id.values[0], 4)) self.assertTrue(np.array_equal(res_exp.gdf.region_id.values[1:25], np.ones(24) * 3)) self.assertTrue(np.array_equal(res_exp.gdf.region_id.values[25:], np.ones(25))) # changed exposures self.assertTrue(np.array_equal(res_exp.gdf.value.values[:25], new_exp.gdf.value.values[:25])) self.assertTrue(np.all(np.not_equal(res_exp.gdf.value.values[:25], exp.gdf.value.values[:25]))) self.assertTrue(np.all(np.not_equal(res_exp.gdf.if_TC.values[:25], new_exp.gdf.if_TC.values[:25]))) self.assertTrue(np.array_equal(res_exp.gdf.latitude.values[:25], new_exp.gdf.latitude.values[:25])) self.assertTrue(np.array_equal(res_exp.gdf.longitude.values[:25], new_exp.gdf.longitude.values[:25])) # unchanged exposures self.assertTrue(np.array_equal(res_exp.gdf.value.values[25:], exp.gdf.value.values[25:])) self.assertTrue(np.all(np.not_equal(res_exp.gdf.value.values[25:], new_exp.gdf.value.values[25:]))) self.assertTrue(np.array_equal(res_exp.gdf.if_TC.values[25:], exp.gdf.if_TC.values[25:])) self.assertTrue(np.array_equal(res_exp.gdf.latitude.values[25:], exp.gdf.latitude.values[25:])) self.assertTrue(np.array_equal(res_exp.gdf.longitude.values[25:], exp.gdf.longitude.values[25:])) # unchanged impact functions self.assertEqual(list(res_ifs.get_func().keys()), [meas.haz_type]) self.assertEqual(res_ifs.get_func()[meas.haz_type][1].id, imp_set.get_func()[meas.haz_type][1].id) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][1].intensity, imp_set.get_func()[meas.haz_type][1].intensity)) self.assertEqual(res_ifs.get_func()[meas.haz_type][3].id, imp_set.get_func()[meas.haz_type][3].id) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][3].intensity, imp_set.get_func()[meas.haz_type][3].intensity)) # changed impact functions self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].intensity, ref_ifs.get_func()[meas.haz_type][1].intensity)) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].paa, ref_ifs.get_func()[meas.haz_type][1].paa)) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].mdd, ref_ifs.get_func()[meas.haz_type][1].mdd)) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].intensity, ref_ifs.get_func()[meas.haz_type][3].intensity)) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].paa, ref_ifs.get_func()[meas.haz_type][3].paa)) self.assertTrue(np.array_equal(res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].mdd, ref_ifs.get_func()[meas.haz_type][3].mdd)) # unchanged hazard self.assertTrue(np.array_equal(res_haz.intensity[:, :36].toarray(), haz.intensity[:, :36].toarray())) self.assertTrue(np.array_equal(res_haz.intensity[:, 37:46].toarray(), haz.intensity[:, 37:46].toarray())) self.assertTrue(np.array_equal(res_haz.intensity[:, 47:].toarray(), haz.intensity[:, 47:].toarray())) # changed hazard self.assertTrue(np.array_equal(res_haz.intensity[[36, 46]].toarray(), new_haz.intensity[[36, 46]].toarray()))
def test_filter_exposures_pass(self): """Test _filter_exposures method with -1""" meas = Measure() meas.exp_region_id = 3 meas.haz_type = 'TC' exp = Exposures() exp.read_mat(ENT_TEST_MAT) exp.rename(columns={ 'if_': 'if_TC', 'centr_': 'centr_TC' }, inplace=True) exp['region_id'] = np.ones(exp.shape[0]) exp.region_id.values[:exp.shape[0] // 2] = 3 imp_set = ImpactFuncSet() imp_set.read_mat(ENT_TEST_MAT) haz = Hazard('TC') haz.read_mat(HAZ_TEST_MAT) new_exp = copy.deepcopy(exp) new_exp['value'] *= 3 new_exp['if_TC'].values[:20] = 2 new_exp['if_TC'].values[20:40] = 3 new_exp['if_TC'].values[40:] = 1 new_ifs = copy.deepcopy(imp_set) new_ifs.get_func('TC')[1].intensity += 1 ref_ifs = copy.deepcopy(new_ifs) new_haz = copy.deepcopy(haz) new_haz.intensity *= 4 res_exp, res_ifs, res_haz = meas._filter_exposures( exp, imp_set, haz, new_exp, new_ifs, new_haz) # unchanged exposures self.assertEqual(res_exp.ref_year, exp.ref_year) self.assertEqual(res_exp.value_unit, exp.value_unit) self.assertEqual(res_exp.tag.file_name, exp.tag.file_name) self.assertEqual(res_exp.tag.description, exp.tag.description) self.assertTrue( np.array_equal(res_exp.value.values[exp.shape[0] // 2:], new_exp.value.values[:exp.shape[0] // 2])) self.assertTrue( np.array_equal(res_exp.region_id.values[exp.shape[0] // 2:], np.ones(exp.shape[0] // 2) * 3)) self.assertTrue( np.array_equal(res_exp.if_TC.values[exp.shape[0] // 2:], new_exp.if_TC.values[:exp.shape[0] // 2])) self.assertTrue( np.array_equal(res_exp.latitude.values[exp.shape[0] // 2:], new_exp.latitude.values[:exp.shape[0] // 2])) self.assertTrue( np.array_equal(res_exp.longitude.values[exp.shape[0] // 2:], new_exp.longitude.values[:exp.shape[0] // 2])) # changed exposures self.assertTrue( np.array_equal(res_exp.value.values[:exp.shape[0] // 2], exp.value.values[exp.shape[0] // 2:])) self.assertTrue( np.array_equal(res_exp.region_id.values[:exp.shape[0] // 2], np.ones(exp.shape[0] // 2))) self.assertTrue( np.array_equal(res_exp.if_TC.values[:exp.shape[0] // 2], exp.if_TC.values[exp.shape[0] // 2:])) self.assertTrue( np.array_equal(res_exp.latitude.values[:exp.shape[0] // 2], exp.latitude.values[exp.shape[0] // 2:])) self.assertTrue( np.array_equal(res_exp.longitude.values[:exp.shape[0] // 2], exp.longitude.values[exp.shape[0] // 2:])) # unchanged impact functions self.assertEqual(list(res_ifs.get_func().keys()), [meas.haz_type]) self.assertEqual(res_ifs.get_func()[meas.haz_type][1].id, imp_set.get_func()[meas.haz_type][1].id) self.assertTrue( np.array_equal(res_ifs.get_func()[meas.haz_type][1].intensity, imp_set.get_func()[meas.haz_type][1].intensity)) self.assertEqual(res_ifs.get_func()[meas.haz_type][3].id, imp_set.get_func()[meas.haz_type][3].id) self.assertTrue( np.array_equal(res_ifs.get_func()[meas.haz_type][3].intensity, imp_set.get_func()[meas.haz_type][3].intensity)) # changed impact functions self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].intensity, ref_ifs.get_func()[meas.haz_type][1].intensity)) self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].paa, ref_ifs.get_func()[meas.haz_type][1].paa)) self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][1 + IF_ID_FACT].mdd, ref_ifs.get_func()[meas.haz_type][1].mdd)) self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].intensity, ref_ifs.get_func()[meas.haz_type][3].intensity)) self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].paa, ref_ifs.get_func()[meas.haz_type][3].paa)) self.assertTrue( np.array_equal( res_ifs.get_func()[meas.haz_type][3 + IF_ID_FACT].mdd, ref_ifs.get_func()[meas.haz_type][3].mdd)) # unchanged hazard self.assertTrue( np.array_equal(res_haz.intensity[:, :36].todense(), haz.intensity[:, :36].todense())) self.assertTrue( np.array_equal(res_haz.intensity[:, 37:46].todense(), haz.intensity[:, 37:46].todense())) self.assertTrue( np.array_equal(res_haz.intensity[:, 47:].todense(), haz.intensity[:, 47:].todense())) # changed hazard self.assertTrue( np.array_equal(res_haz.intensity[[36, 46]].todense(), new_haz.intensity[[36, 46]].todense()))