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' with self.assertRaises(KeyError): Exposures.from_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' with self.assertRaises(KeyError): Exposures.from_mat(ENT_TEST_MAT, var_names=new_var_names)
def test_cutoff_hazard_pass(self): """Test _cutoff_hazard_damage""" meas = MeasureSet.from_mat(ENT_TEST_MAT) act_1 = meas.get_measure(name='Seawall')[0] haz = Hazard.from_mat(HAZ_TEST_MAT) exp = Exposures.from_mat(ENT_TEST_MAT) exp.gdf.rename(columns={'impf': 'impf_TC'}, inplace=True) exp.check() imp_set = ImpactFuncSet.from_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_cutoff_hazard_region_pass(self): """Test _cutoff_hazard_damage in specific region""" meas = MeasureSet.from_mat(ENT_TEST_MAT) act_1 = meas.get_measure(name='Seawall')[0] act_1.exp_region_id = [1] haz = Hazard.from_mat(HAZ_TEST_MAT) exp = Exposures.from_mat(ENT_TEST_MAT) exp.gdf['region_id'] = np.zeros(exp.gdf.shape[0]) exp.gdf.region_id.values[10:] = 1 exp.check() imp_set = ImpactFuncSet.from_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.gdf.centr_[exp.gdf.region_id == 0]) for i_ev in pos_null: self.assertEqual(new_haz.intensity[i_ev, centr_null].max(), 0)
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.from_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.from_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.from_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.from_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.from_mat(ENT_TEST_MAT, var_names=new_var_names) # Check results self.assertTrue('category_id' not in exp.gdf)
def test_read_demo_pass(self): """Read one single excel file""" # Read demo excel file expo = Exposures.from_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.impf_.dtype)) self.assertEqual(expo.gdf.impf_.shape, (n_expos, )) self.assertEqual(expo.gdf.impf_[0], 1) self.assertEqual(expo.gdf.impf_[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 from_mat(cls, 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 Returns ------- ent : climada.entity.Entity The entity from matlab file """ return cls(exposures=Exposures.from_mat(file_name), disc_rates=DiscRates.from_mat(file_name, description), impact_func_set=ImpactFuncSet.from_mat( file_name, description), measure_set=MeasureSet.from_mat(file_name, description))
def test_no_impact_fail(self): """Error if no impact ids.""" new_var_names = copy.deepcopy(DEF_VAR_MAT) new_var_names['var_name']['impf'] = 'no valid value' with self.assertRaises(KeyError): Exposures.from_mat(ENT_TEST_MAT, var_names=new_var_names)
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.from_mat(ENT_TEST_MAT) exp.gdf.rename(columns={ 'impf_': 'impf_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.from_mat(ENT_TEST_MAT) haz = Hazard.from_mat(HAZ_TEST_MAT) exp.assign_centroids(haz) new_exp = copy.deepcopy(exp) new_exp.gdf['value'] *= 3 new_exp.gdf['impf_TC'].values[:20] = 2 new_exp.gdf['impf_TC'].values[20:40] = 3 new_exp.gdf['impf_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.assertTrue(u_coord.equal_crs(res_exp.crs, exp.crs)) self.assertTrue(u_coord.equal_crs(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.impf_TC.values[:25], new_exp.gdf.impf_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.impf_TC.values[25:], exp.gdf.impf_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 + IMPF_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 + IMPF_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 + IMPF_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 + IMPF_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 + IMPF_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 + IMPF_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()))