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_change_all_hazard_pass(self): """Test _change_all_hazard method""" meas = Measure() meas.hazard_set = HAZ_DEMO_H5 ref_haz = Hazard('TC') ref_haz.read_hdf5(HAZ_DEMO_H5) hazard = Hazard('TC') new_haz = meas._change_all_hazard(hazard) self.assertEqual(new_haz.tag.file_name, ref_haz.tag.file_name) self.assertEqual(new_haz.tag.haz_type, ref_haz.tag.haz_type) self.assertTrue(np.array_equal(new_haz.frequency, ref_haz.frequency)) self.assertTrue(np.array_equal(new_haz.date, ref_haz.date)) self.assertTrue(np.array_equal(new_haz.orig, ref_haz.orig)) self.assertTrue(np.array_equal(new_haz.centroids.coord, ref_haz.centroids.coord)) self.assertTrue(np.array_equal(new_haz.intensity.data, ref_haz.intensity.data)) self.assertTrue(np.array_equal(new_haz.fraction.data, ref_haz.fraction.data))