Beispiel #1
0
    def test_change_imp_func_pass(self):
        """Test _change_imp_func"""
        meas = MeasureSet()
        meas.read_mat(ENT_TEST_MAT)
        act_1 = meas.get_measure(name='Mangroves')[0]

        imp_set = ImpactFuncSet()
        imp_tc = ImpactFunc()
        imp_tc.haz_type = 'XX'
        imp_tc.id = 1
        imp_tc.intensity = np.arange(10, 100, 10)
        imp_tc.intensity[0] = 0.
        imp_tc.intensity[-1] = 100.
        imp_tc.mdd = np.array([0.0, 0.0, 0.021857142857143, 0.035887500000000,
                               0.053977415307403, 0.103534246575342, 0.180414000000000,
                               0.410796000000000, 0.410796000000000])
        imp_tc.paa = np.array([0, 0.005000000000000, 0.042000000000000, 0.160000000000000,
                               0.398500000000000, 0.657000000000000, 1.000000000000000,
                               1.000000000000000, 1.000000000000000])
        imp_set.append(imp_tc)
        new_imp = act_1._change_imp_func(imp_set).get_func('XX')[0]

        self.assertTrue(np.array_equal(new_imp.intensity, np.array([4., 24., 34., 44.,
            54., 64., 74., 84., 104.])))
        self.assertTrue(np.array_equal(new_imp.mdd, np.array([0, 0, 0.021857142857143, 0.035887500000000,
            0.053977415307403, 0.103534246575342, 0.180414000000000, 0.410796000000000, 0.410796000000000])))
        self.assertTrue(np.array_equal(new_imp.paa, np.array([0, 0.005000000000000, 0.042000000000000,
            0.160000000000000, 0.398500000000000, 0.657000000000000, 1.000000000000000,
            1.000000000000000, 1.000000000000000])))
        self.assertFalse(id(new_imp) == id(imp_tc))
Beispiel #2
0
    def _fill_dfr(self, dfr, var_names):
        def _get_xls_funcs(dfr, var_names):
            """Parse individual impact functions."""
            dist_func = []
            for (haz_type,
                 imp_id) in zip(dfr[var_names['col_name']['peril']],
                                dfr[var_names['col_name']['func_id']]):
                if (haz_type, imp_id) not in dist_func:
                    dist_func.append((haz_type, imp_id))
            return dist_func

        try:
            dist_func = _get_xls_funcs(dfr, var_names)
            for haz_type, imp_id in dist_func:
                df_func = dfr[dfr[var_names['col_name']['peril']] == haz_type]
                df_func = df_func[df_func[var_names['col_name']['func_id']] ==
                                  imp_id]

                func = ImpactFunc()
                func.haz_type = haz_type
                func.id = imp_id
                # check that the unit of the intensity is the same
                try:
                    if len(df_func[var_names['col_name']
                                   ['name']].unique()) != 1:
                        raise ValueError(
                            'Impact function with two different names.')
                    func.name = df_func[var_names['col_name']
                                        ['name']].values[0]
                except KeyError:
                    func.name = str(func.id)

                # check that the unit of the intensity is the same, if provided
                try:
                    if len(df_func[var_names['col_name']
                                   ['unit']].unique()) != 1:
                        raise ValueError('Impact function with two different \
                                         intensity units.')
                    func.intensity_unit = \
                                    df_func[var_names['col_name']['unit']].values[0]
                except KeyError:
                    pass

                func.intensity = df_func[var_names['col_name']['inten']].values
                func.mdd = df_func[var_names['col_name']['mdd']].values
                func.paa = df_func[var_names['col_name']['paa']].values

                self.append(func)

        except KeyError as err:
            LOGGER.error("Not existing variable: %s", str(err))
            raise err
Beispiel #3
0
    def __init__(self):
        """ Empty initialization.

        Parameters:
            if_id (int, optional): impact function id. Default: 1
            intensity (np.array, optional): intensity array SPEI [-]. 
                default: intensity defintion 1 (minimum)
                default_sum: intensity definition 3 (sum over all drought months)

        Raises:
            ValueError
        """
        ImpactFunc.__init__(self)
 def test_set_step(self):
     """Check default impact function: step function"""
     inten = (0, 5, 10)
     imp_fun = ImpactFunc.from_step_impf(inten)
     self.assertTrue(np.array_equal(imp_fun.paa, np.ones(4)))
     self.assertTrue(np.array_equal(imp_fun.mdd, np.array([0, 0, 1, 1])))
     self.assertTrue(
         np.array_equal(imp_fun.intensity, np.array([0, 5, 5, 10])))
 def test_set_sigmoid(self):
     """Check default impact function: sigmoid function"""
     inten = (0, 100, 5)
     imp_fun = ImpactFunc.from_sigmoid_impf(inten, L=1.0, k=2., x0=50.)
     self.assertTrue(np.array_equal(imp_fun.paa, np.ones(20)))
     self.assertEqual(imp_fun.mdd[10], 0.5)
     self.assertEqual(imp_fun.mdd[-1], 1.0)
     self.assertTrue(np.array_equal(imp_fun.intensity, np.arange(0, 100,
                                                                 5)))
Beispiel #6
0
 def test_calc_mdr_pass(self):
     """Compute mdr interpolating values."""
     imp_fun = ImpactFunc()
     imp_fun.intensity = np.arange(0, 100, 10)
     imp_fun.paa = np.arange(0, 1, 0.1)
     imp_fun.mdd = np.arange(0, 1, 0.1)
     new_inten = 17.2
     self.assertEqual(imp_fun.calc_mdr(new_inten), 0.029583999999999996)
Beispiel #7
0
    def read_mat(self, file_name, description='', var_names=DEF_VAR_MAT):
        """Read MATLAB file generated with previous MATLAB CLIMADA version.

        Parameters:
            file_name (str): absolute file name
            description (str, optional): description of the data
            var_names (dict, optional): name of the variables in the file
        """
        def _get_hdf5_funcs(imp, file_name, var_names):
            """Get rows that fill every impact function and its name."""
            func_pos = dict()
            for row, (fun_id, fun_type) in enumerate(
                    zip(imp[var_names['var_name']['fun_id']].squeeze(),
                        imp[var_names['var_name']['peril']].squeeze())):
                type_str = u_hdf5.get_str_from_ref(file_name, fun_type)
                key = (type_str, int(fun_id))
                if key not in func_pos:
                    func_pos[key] = list()
                func_pos[key].append(row)
            return func_pos

        def _get_hdf5_str(imp, idxs, file_name, var_name):
            """Get rows with same string in var_name."""
            prev_str = ""
            for row in idxs:
                cur_str = u_hdf5.get_str_from_ref(file_name, imp[var_name][row][0])
                if prev_str == "":
                    prev_str = cur_str
                elif prev_str != cur_str:
                    LOGGER.error("Impact function with two different %s.", var_name)
                    raise ValueError
            return prev_str

        imp = u_hdf5.read(file_name)
        self.clear()
        self.tag.file_name = str(file_name)
        self.tag.description = description

        try:
            imp = imp[var_names['sup_field_name']]
        except KeyError:
            pass
        try:
            imp = imp[var_names['field_name']]
            funcs_idx = _get_hdf5_funcs(imp, file_name, var_names)
            for imp_key, imp_rows in funcs_idx.items():
                func = ImpactFunc()
                func.haz_type = imp_key[0]
                func.id = imp_key[1]
                # check that this function only has one intensity unit, if provided
                try:
                    func.intensity_unit = _get_hdf5_str(imp, imp_rows,
                                                        file_name,
                                                        var_names['var_name']['unit'])
                except KeyError:
                    pass
                # check that this function only has one name
                try:
                    func.name = _get_hdf5_str(imp, imp_rows, file_name,
                                              var_names['var_name']['name'])
                except KeyError:
                    func.name = str(func.id)
                func.intensity = np.take(imp[var_names['var_name']['inten']], imp_rows)
                func.mdd = np.take(imp[var_names['var_name']['mdd']], imp_rows)
                func.paa = np.take(imp[var_names['var_name']['paa']], imp_rows)
                self.append(func)
        except KeyError as err:
            LOGGER.error("Not existing variable: %s", str(err))
            raise err
Beispiel #8
0
 def __init__(self):
     ImpactFunc.__init__(self)
     self.haz_type = 'RF'
     self.intensity_unit = 'm'
     self.continent = ''
Beispiel #9
0
 def __init__(self):
     ImpactFunc.__init__(self)
     self.haz_type = 'TC'
Beispiel #10
0
    def from_mat(cls, file_name, description='', var_names=None):
        """Read MATLAB file generated with previous MATLAB CLIMADA version.

        Parameters
        ----------
        file_name : str
            absolute file name
        description : str, optional
            description of the data
        var_names : dict, optional
            name of the variables in the file

        Return
        ------
        impf_set : climada.entity.impact_func_set.ImpactFuncSet
            Impact func set as defined in matlab file.
        """
        if var_names is None:
            var_names = DEF_VAR_MAT

        def _get_hdf5_funcs(imp, file_name, var_names):
            """Get rows that fill every impact function and its name."""
            func_pos = dict()
            for row, (fun_id, fun_type) in enumerate(
                    zip(imp[var_names['var_name']['fun_id']].squeeze(),
                        imp[var_names['var_name']['peril']].squeeze())):
                type_str = u_hdf5.get_str_from_ref(file_name, fun_type)
                key = (type_str, int(fun_id))
                if key not in func_pos:
                    func_pos[key] = list()
                func_pos[key].append(row)
            return func_pos

        def _get_hdf5_str(imp, idxs, file_name, var_name):
            """Get rows with same string in var_name."""
            prev_str = ""
            for row in idxs:
                cur_str = u_hdf5.get_str_from_ref(file_name,
                                                  imp[var_name][row][0])
                if prev_str == "":
                    prev_str = cur_str
                elif prev_str != cur_str:
                    raise ValueError("Impact function with two different %s." %
                                     var_name)
            return prev_str

        imp = u_hdf5.read(file_name)
        impf_set = cls()
        impf_set.tag.file_name = str(file_name)
        impf_set.tag.description = description

        try:
            imp = imp[var_names['sup_field_name']]
        except KeyError:
            pass
        try:
            imp = imp[var_names['field_name']]
            funcs_idx = _get_hdf5_funcs(imp, file_name, var_names)
            for imp_key, imp_rows in funcs_idx.items():
                func = ImpactFunc()
                func.haz_type = imp_key[0]
                func.id = imp_key[1]
                # check that this function only has one intensity unit, if provided
                try:
                    func.intensity_unit = _get_hdf5_str(
                        imp, imp_rows, file_name,
                        var_names['var_name']['unit'])
                except KeyError:
                    pass
                # check that this function only has one name
                try:
                    func.name = _get_hdf5_str(imp, imp_rows, file_name,
                                              var_names['var_name']['name'])
                except KeyError:
                    func.name = str(func.id)
                func.intensity = np.take(imp[var_names['var_name']['inten']],
                                         imp_rows)
                func.mdd = np.take(imp[var_names['var_name']['mdd']], imp_rows)
                func.paa = np.take(imp[var_names['var_name']['paa']], imp_rows)
                impf_set.append(func)
        except KeyError as err:
            raise KeyError("Not existing variable: %s" % str(err)) from err

        return impf_set
Beispiel #11
0
    def test_change_exposures_if_pass(self):
        """Test _change_exposures_if"""
        meas = Measure()
        meas.imp_fun_map = '1to3'
        meas.haz_type = 'TC'

        imp_set = ImpactFuncSet()
        imp_tc = ImpactFunc()
        imp_tc.haz_type = 'TC'
        imp_tc.id = 1
        imp_tc.intensity = np.arange(10, 100, 10)
        imp_tc.mdd = np.arange(10, 100, 10)
        imp_tc.paa = np.arange(10, 100, 10)
        imp_set.append(imp_tc)

        imp_tc = ImpactFunc()
        imp_tc.haz_type = 'TC'
        imp_tc.id = 3
        imp_tc.intensity = np.arange(10, 100, 10)
        imp_tc.mdd = np.arange(10, 100, 10) * 2
        imp_tc.paa = np.arange(10, 100, 10) * 2

        exp = Exposures()
        exp.read_hdf5(EXP_DEMO_H5)
        new_exp = meas._change_exposures_if(exp)

        self.assertEqual(new_exp.ref_year, exp.ref_year)
        self.assertEqual(new_exp.value_unit, exp.value_unit)
        self.assertEqual(new_exp.tag.file_name, exp.tag.file_name)
        self.assertEqual(new_exp.tag.description, exp.tag.description)
        self.assertTrue(np.array_equal(new_exp.gdf.value.values, exp.gdf.value.values))
        self.assertTrue(np.array_equal(new_exp.gdf.latitude.values, exp.gdf.latitude.values))
        self.assertTrue(np.array_equal(new_exp.gdf.longitude.values, exp.gdf.longitude.values))
        self.assertTrue(np.array_equal(exp.gdf[INDICATOR_IF + 'TC'].values, np.ones(new_exp.gdf.shape[0])))
        self.assertTrue(np.array_equal(new_exp.gdf[INDICATOR_IF + 'TC'].values, np.ones(new_exp.gdf.shape[0]) * 3))
 def __init__(self):
     ImpactFunc.__init__(self)
     self.haz_type = 'RC'
     self.intensity_unit = ''