Beispiel #1
0
        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)
Beispiel #2
0
        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)
Beispiel #3
0
    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)