def read_att_excel(measures, dfr, var_names): """Read Excel measures attributes""" num_mes = len(dfr.index) for idx in range(0, num_mes): meas = Measure() meas.name = dfr[var_names['col_name']['name']][idx] try: meas.haz_type = dfr[var_names['col_name']['haz']][idx] except KeyError: pass meas.color_rgb = np.fromstring( dfr[var_names['col_name']['color']][idx], dtype=float, sep=' ') meas.cost = dfr[var_names['col_name']['cost']][idx] meas.hazard_freq_cutoff = dfr[var_names['col_name'] ['haz_frq']][idx] meas.hazard_set = dfr[var_names['col_name']['haz_set']][idx] # Search for (a, b) values, put a = 1 otherwise try: meas.hazard_inten_imp = ( dfr[var_names['col_name']['haz_int_a']][idx], dfr[var_names['col_name']['haz_int_b']][idx]) except KeyError: meas.hazard_inten_imp = ( 1, dfr['hazard intensity impact'][idx]) try: meas.exposures_set = dfr[var_names['col_name'] ['exp_set']][idx] meas.exp_region_id = ast.literal_eval( dfr[var_names['col_name']['exp_reg']][idx]) except KeyError: pass except ValueError: meas.exp_region_id = dfr[var_names['col_name'] ['exp_reg']][idx] meas.mdd_impact = (dfr[var_names['col_name']['mdd_a']][idx], dfr[var_names['col_name']['mdd_b']][idx]) meas.paa_impact = (dfr[var_names['col_name']['paa_a']][idx], dfr[var_names['col_name']['paa_b']][idx]) meas.imp_fun_map = dfr[var_names['col_name']['fun_map']][idx] meas.risk_transf_attach = dfr[var_names['col_name'] ['risk_att']][idx] meas.risk_transf_cover = dfr[var_names['col_name'] ['risk_cov']][idx] try: meas.risk_transf_cost_factor = dfr[var_names['col_name'] ['risk_fact']][idx] except KeyError: pass measures.append(meas)
def read_att_mat(measures, data, file_name, var_names): """Read MATLAB measures attributes""" num_mes = len(data[var_names['var_name']['name']]) for idx in range(0, num_mes): meas = Measure() meas.name = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['name']][idx][0]) color_str = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['color']][idx][0]) meas.color_rgb = np.fromstring(color_str, dtype=float, sep=' ') meas.cost = data[var_names['var_name']['cost']][idx][0] meas.haz_type = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz']][idx][0]) meas.hazard_freq_cutoff = data[var_names['var_name'] ['haz_frq']][idx][0] meas.hazard_set = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['haz_set']][idx][0]) try: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a']][idx][0], data[var_names['var_name']['haz_int_b']][0][idx]) except KeyError: meas.hazard_inten_imp = ( data[var_names['var_name']['haz_int_a'][:-2]][idx][0], 0) # different convention of signes followed in MATLAB! meas.mdd_impact = ( data[var_names['var_name']['mdd_a']][idx][0], data[var_names['var_name']['mdd_b']][idx][0]) meas.paa_impact = ( data[var_names['var_name']['paa_a']][idx][0], data[var_names['var_name']['paa_b']][idx][0]) meas.imp_fun_map = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['fun_map']][idx][0]) meas.exposures_set = hdf5.get_str_from_ref( file_name, data[var_names['var_name']['exp_set']][idx][0]) exp_region_id = data[var_names['var_name']['exp_reg']][idx][0] if exp_region_id: meas.exp_region_id = [exp_region_id] meas.risk_transf_attach = data[var_names['var_name'] ['risk_att']][idx][0] meas.risk_transf_cover = data[var_names['var_name'] ['risk_cov']][idx][0] measures.append(meas)
def test_not_filter_exposures_pass(self): """Test _filter_exposures method with []""" meas = Measure() meas.exp_region_id = [] exp = Exposures() imp_set = ImpactFuncSet() haz = Hazard('TC') new_exp = Exposures() new_ifs = ImpactFuncSet() new_haz = Hazard('TC') res_exp, res_ifs, res_haz = meas._filter_exposures(exp, imp_set, haz, new_exp, new_ifs, new_haz) self.assertTrue(res_exp is new_exp) self.assertTrue(res_ifs is new_ifs) self.assertTrue(res_haz is new_haz) self.assertTrue(res_exp is not exp) self.assertTrue(res_ifs is not imp_set) self.assertTrue(res_haz is not haz)
def test_write_read_file(self): """ Write and read excel file""" act_1 = Measure() act_1.name = 'Mangrove' act_1.haz_type = 'TC' act_1.color_rgb = np.array([1, 1, 1]) act_1.cost = 10 act_1.mdd_impact = (1, 2) act_1.paa_impact = (1, 2) act_1.hazard_inten_imp = (1, 2) act_1.risk_transf_cover = 500 act_11 = Measure() act_11.name = 'Something' act_11.haz_type = 'TC' act_11.color_rgb = np.array([1, 1, 1]) act_11.mdd_impact = (1, 2) act_11.paa_impact = (1, 3) act_11.hazard_inten_imp = (1, 2) act_11.exp_region_id = 2 act_2 = Measure() act_2.name = 'Anything' act_2.haz_type = 'FL' act_2.color_rgb = np.array([1, 1, 1]) act_2.mdd_impact = (1, 2) act_2.paa_impact = (1, 2) act_2.hazard_inten_imp = (1, 2) act_2.hazard_freq_cutoff = 30 act_2.imp_fun_map = 'map' meas_set = MeasureSet() meas_set.append(act_1) meas_set.append(act_11) meas_set.append(act_2) file_name = os.path.join(DATA_DIR, 'test_meas.xlsx') meas_set.write_excel(file_name) meas_read = MeasureSet() meas_read.read_excel(file_name, 'test') self.assertEqual(meas_read.tag.file_name, file_name) self.assertEqual(meas_read.tag.description, 'test') meas_list = meas_read.get_measure('TC') meas_list.extend(meas_read.get_measure('FL')) for meas in meas_list: if meas.name == 'Mangrove': meas_ref = act_1 elif meas.name == 'Something': meas_ref = act_11 elif meas.name == 'Anything': meas_ref = act_2 self.assertEqual(meas_ref.name, meas.name) self.assertEqual(meas_ref.haz_type, meas.haz_type) self.assertEqual(meas_ref.cost, meas.cost) self.assertEqual(meas_ref.hazard_set, meas.hazard_set) self.assertEqual(meas_ref.hazard_freq_cutoff, meas.hazard_freq_cutoff) self.assertEqual(meas_ref.exposures_set, meas.exposures_set) self.assertEqual(meas_ref.exp_region_id, meas.exp_region_id) self.assertTrue(np.array_equal(meas_ref.color_rgb, meas.color_rgb)) self.assertEqual(meas_ref.mdd_impact, meas.mdd_impact) self.assertEqual(meas_ref.paa_impact, meas.paa_impact) self.assertEqual(meas_ref.hazard_inten_imp, meas.hazard_inten_imp) self.assertEqual(meas_ref.imp_fun_map, meas.imp_fun_map) self.assertEqual(meas_ref.risk_transf_attach, meas.risk_transf_attach) self.assertEqual(meas_ref.risk_transf_cover, meas.risk_transf_cover)
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()))