Example #1
0
    def test_get_measure_pass(self):
        """Test normal functionality of get_measure method."""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'FL'
        meas.append(act_1)
        self.assertIs(act_1, meas.get_measure(name='Mangrove')[0])

        act_2 = Measure()
        act_2.name = 'Seawall'
        act_2.haz_type = 'FL'
        meas.append(act_2)
        self.assertIs(act_1, meas.get_measure(name='Mangrove')[0])
        self.assertIs(act_2, meas.get_measure(name='Seawall')[0])
        self.assertEqual(2, len(meas.get_measure('FL')))
Example #2
0
    def test_get_names_pass(self):
        """Test get_names function."""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'FL'
        meas.append(act_1)
        self.assertEqual(1, len(meas.get_names()))
        self.assertEqual({'FL': ['Mangrove']}, meas.get_names())

        act_2 = Measure()
        act_2.name = 'Seawall'
        act_2.haz_type = 'FL'
        meas.append(act_2)
        self.assertEqual(2, len(meas.get_names('FL')))
        self.assertIn('Mangrove', meas.get_names('FL'))
        self.assertIn('Seawall', meas.get_names('FL'))
Example #3
0
    def test_num_measures_pass(self):
        """Test num_measures function."""
        meas = MeasureSet()
        self.assertEqual(0, meas.size())
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'FL'
        meas.append(act_1)
        self.assertEqual(1, meas.size())
        meas.append(act_1)
        self.assertEqual(1, meas.size())

        act_2 = Measure()
        act_2.name = 'Seawall'
        act_2.haz_type = 'FL'
        meas.append(act_2)
        self.assertEqual(2, meas.size())
Example #4
0
 def test_get_measure_wrong_error(self):
     """Test get_measure method with wrong inputs."""
     meas = MeasureSet()
     act_1 = Measure()
     act_1.name = 'Seawall'
     act_1.haz_type = 'FL'
     meas.append(act_1)
     self.assertEqual([], meas.get_measure('Mangrove'))
Example #5
0
    def test_def_color(self):
        """Test default grey scale used when no color set"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'LaLa'
        act_1.haz_type = 'FL'
        meas.append(act_1)

        act_2 = Measure()
        act_2.name = 'LoLo'
        act_2.haz_type = 'FL'
        meas.append(act_2)

        meas.check()
        self.assertTrue(np.array_equal(meas.get_measure('FL', 'LaLa').color_rgb, np.ones(4)))
        self.assertTrue(np.allclose(meas.get_measure('FL', 'LoLo').color_rgb,
            np.array([0., 0., 0., 1.0])))
Example #6
0
 def test_remove_measure_pass(self):
     """Test remove_measure removes Measure of MeasureSet correcty."""
     meas = MeasureSet()
     act_1 = Measure()
     act_1.name = 'Mangrove'
     act_1.haz_type = 'FL'
     meas.append(act_1)
     meas.remove_measure(name='Mangrove')
     self.assertEqual(0, meas.size())
Example #7
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)
Example #8
0
 def test_remove_wrong_error(self):
     """Test error is raised when invalid inputs."""
     meas = MeasureSet()
     act_1 = Measure()
     act_1.name = 'Mangrove'
     act_1.haz_type = 'FL'
     meas.append(act_1)
     with self.assertLogs('climada.entity.measures.measure_set', level='INFO') as cm:
         meas.remove_measure(name='Seawall')
     self.assertIn('No Measure with name Seawall.', cm.output[0])
    def test_check_name_fail(self):
        """Wrong measures definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'LaLa'
        act_1.haz_type = 'FL'
        meas._data['FL'] = dict()
        meas._data['FL']['LoLo'] = act_1

        with self.assertRaises(ValueError) as cm:
            meas.check()
        self.assertIn('Wrong Measure.name: LoLo != LaLa', str(cm.exception))
Example #10
0
    def test_extend_different_extend(self):
        """Extend MeasureSet with same and new values. The actions
        with repeated name are overwritten."""
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'TC'
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2)
        act_1.hazard_inten_imp = (1, 2)

        act_11 = Measure()
        act_11.name = 'Mangrove'
        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_2 = Measure()
        act_2.name = 'Anything'
        act_2.haz_type = 'TC'
        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)

        meas = MeasureSet()
        meas.append(act_1)
        meas_add = MeasureSet()
        meas_add.append(act_11)
        meas_add.append(act_2)

        meas.extend(meas_add)
        meas.check()

        self.assertEqual(meas.size(), 2)
        self.assertEqual(meas.get_names(), {'TC': ['Mangrove', 'Anything']})
        self.assertEqual(
            meas.get_measure(name=act_1.name)[0].paa_impact, act_11.paa_impact)
Example #11
0
    def test_check_name_fail(self):
        """Wrong measures definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'LaLa'
        act_1.haz_type = 'FL'
        meas._data['FL'] = dict()
        meas._data['FL']['LoLo'] = act_1

        with self.assertLogs('climada.entity.measures.measure_set', level='ERROR') as cm:
            with self.assertRaises(ValueError):
                meas.check()
        self.assertIn('Wrong Measure.name: LoLo != LaLa', cm.output[0])
Example #12
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)
    def test_check_wrongMDD_fail(self):
        """Wrong measures definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'DR'
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1)
        act_1.paa_impact = (1, 2)
        act_1.hazard_inten_imp = (1, 2)
        meas.append(act_1)

        with self.assertRaises(ValueError) as cm:
            meas.check()
        self.assertIn('Measure.mdd_impact has wrong size.', str(cm.exception))
Example #14
0
 def test_attributes_all(self):
     """All attributes are defined"""
     meas = MeasureSet()
     act_1 = Measure()
     act_1.name = 'Seawall'
     self.assertTrue(hasattr(meas, 'tag'))
     self.assertTrue(hasattr(meas, '_data'))
     self.assertTrue(hasattr(act_1, 'name'))
     self.assertTrue(hasattr(act_1, 'color_rgb'))
     self.assertTrue(hasattr(act_1, 'cost'))
     self.assertTrue(hasattr(act_1, 'hazard_freq_cutoff'))
     self.assertTrue(hasattr(act_1, 'hazard_inten_imp'))
     self.assertTrue(hasattr(act_1, 'mdd_impact'))
     self.assertTrue(hasattr(act_1, 'paa_impact'))
     self.assertTrue(hasattr(act_1, 'risk_transf_attach'))
     self.assertTrue(hasattr(act_1, 'risk_transf_cover'))
    def test_check_wrongColor_fail(self):
        """Wrong measures definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'DR'
        act_1.color_rgb = (1, 2)
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2)
        act_1.hazard_inten_imp = (1, 2)
        meas.append(act_1)

        with self.assertRaises(ValueError) as cm:
            meas.check()
        self.assertIn('Invalid Measure.color_rgb size: 2 not in [3, 4].',
                      str(cm.exception))
Example #16
0
    def test_check_wrongPAA_fail(self):
        """Wrong measures definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'TC'
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2, 3, 4)
        act_1.hazard_inten_imp = (1, 2)
        meas.append(act_1)

        with self.assertLogs('climada.util.checker', level='ERROR') as cm:
            with self.assertRaises(ValueError):
                meas.check()
        self.assertIn('Invalid Measure.paa_impact size: 2 != 4.', cm.output[0])
    def test_check_wronginten_fail(self):
        """Wrong intensity definition"""
        meas = MeasureSet()
        act_1 = Measure()
        act_1.haz_type = 'TC'
        act_1.name = 'Mangrove'
        act_1.hazard_inten_imp = (1, 2, 3)
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2)
        meas.append(act_1)

        with self.assertRaises(ValueError) as cm:
            meas.check()
        self.assertIn('Invalid Measure.hazard_inten_imp size: 2 != 3.',
                      str(cm.exception))
Example #18
0
    def test_extend_to_empty_same(self):
        """Extend MeasureSet to empty one."""
        meas = MeasureSet()
        meas_add = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'TC'
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2)
        act_1.hazard_inten_imp = (1, 2)
        meas_add.append(act_1)

        meas.extend(meas_add)
        meas.check()

        self.assertEqual(meas.size(), 1)
        self.assertEqual(meas.get_names(), {'TC': ['Mangrove']})
Example #19
0
    def test_extend_equal_same(self):
        """Extend the same MeasureSet. The inital MeasureSet is obtained."""
        meas = MeasureSet()
        meas_add = MeasureSet()
        act_1 = Measure()
        act_1.name = 'Mangrove'
        act_1.haz_type = 'TC'
        act_1.color_rgb = np.array([1, 1, 1])
        act_1.mdd_impact = (1, 2)
        act_1.paa_impact = (1, 2)
        act_1.hazard_inten_imp = (1, 2)
        meas.append(act_1)
        meas_add.append(act_1)

        meas.extend(meas_add)
        meas.check()

        self.assertEqual(meas.size(), 1)
        self.assertEqual(meas.get_names(), {'TC': ['Mangrove']})
Example #20
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)