|
1 | 1 | """ |
2 | | -Define MeasureSet class. |
| 2 | +Define Measure class. |
3 | 3 | """ |
4 | 4 |
|
5 | | -__all__ = ['MeasureSet', |
6 | | - 'FILE_EXT'] |
| 5 | +__all__ = ['Measure'] |
7 | 6 |
|
8 | | -import os |
9 | 7 | import copy |
10 | 8 | import logging |
| 9 | +import numpy as np |
11 | 10 |
|
12 | | -from climada.entity.measures.measure import Measure |
13 | | -from climada.entity.measures.source import READ_SET |
14 | | -from climada.util.files_handler import to_list, get_file_names |
15 | | -from climada.entity.tag import Tag |
| 11 | +from climada.entity.impact_funcs.impact_func_set import ImpactFuncSet |
| 12 | +import climada.util.checker as check |
16 | 13 |
|
17 | 14 | LOGGER = logging.getLogger(__name__) |
18 | 15 |
|
19 | | -FILE_EXT = {'.mat': 'MAT', |
20 | | - '.xls': 'XLS', |
21 | | - '.xlsx': 'XLS' |
22 | | - } |
23 | | -""" Supported files format to read from """ |
24 | | - |
25 | | -class MeasureSet(object): |
26 | | - """Contains measures of type Measure. Loads from |
27 | | - files with format defined in FILE_EXT. |
| 16 | +class Measure(): |
| 17 | + """Contains the definition of one measure. |
28 | 18 |
|
29 | 19 | Attributes: |
30 | | - tag (Tag): information about the source data |
31 | | - _data (dict): cotains Measure classes. It's not suppossed to be |
32 | | - directly accessed. Use the class methods instead. |
| 20 | + name (str): name of the action |
| 21 | + color_rgb (np.array): integer array of size 3. Gives color code of |
| 22 | + this measure in RGB |
| 23 | + cost (float): cost |
| 24 | + hazard_freq_cutoff (float): hazard frequency cutoff |
| 25 | + hazard_intensity (tuple): parameter a and b |
| 26 | + mdd_impact (tuple): parameter a and b of the impact over the mean |
| 27 | + damage (impact) degree |
| 28 | + paa_impact (tuple): parameter a and b of the impact over the |
| 29 | + percentage of affected assets (exposures) |
| 30 | + risk_transf_attach (float): risk transfer attach |
| 31 | + risk_transf_cover (float): risk transfer cover |
33 | 32 | """ |
34 | 33 |
|
35 | | - def __init__(self, file_name='', description=''): |
36 | | - """Fill values from file, if provided. |
37 | | -
|
38 | | - Parameters: |
39 | | - file_name (str or list(str), optional): absolute file name(s) or |
40 | | - folder name containing the files to read |
41 | | - description (str or list(str), optional): one description of the |
42 | | - data or a description of each data file |
43 | | -
|
44 | | - Raises: |
45 | | - ValueError |
46 | | -
|
47 | | - Examples: |
48 | | - Fill MeasureSet with values and check consistency data: |
49 | | -
|
50 | | - >>> act_1 = Measure() |
51 | | - >>> act_1.name = 'Seawall' |
52 | | - >>> act_1.color_rgb = np.array([0.1529, 0.2510, 0.5451]) |
53 | | - >>> act_1.hazard_intensity = (1, 0) |
54 | | - >>> act_1.mdd_impact = (1, 0) |
55 | | - >>> act_1.paa_impact = (1, 0) |
56 | | - >>> meas = MeasureSet() |
57 | | - >>> meas.add_Measure(act_1) |
58 | | - >>> meas.tag.description = "my dummy MeasureSet." |
59 | | - >>> meas.check() |
60 | | -
|
61 | | - Read measures from file and checks consistency data: |
62 | | -
|
63 | | - >>> meas = MeasureSet(ENT_TEMPLATE_XLS) |
64 | | - """ |
65 | | - self.clear() |
66 | | - if file_name != '': |
67 | | - self.read(file_name, description) |
68 | | - |
69 | | - def clear(self): |
70 | | - """Reinitialize attributes.""" |
71 | | - self.tag = Tag() |
72 | | - self._data = dict() # {name: Measure()} |
| 34 | + def __init__(self): |
| 35 | + """ Empty initialization.""" |
| 36 | + self.name = '' |
| 37 | + self.haz_type = '' |
| 38 | + self.color_rgb = np.array([0, 0, 0]) |
| 39 | + self.cost = 0 |
73 | 40 |
|
74 | | - def add_measure(self, meas): |
75 | | - """Add an Measure. |
76 | | -
|
77 | | - Parameters: |
78 | | - meas (Measure): Measure instance |
79 | | -
|
80 | | - Raises: |
81 | | - ValueError |
82 | | - """ |
83 | | - if not isinstance(meas, Measure): |
84 | | - LOGGER.error("Input value is not of type Measure.") |
85 | | - raise ValueError |
86 | | - if meas.name == 'NA': |
87 | | - LOGGER.error("Input Measure's name not set.") |
88 | | - raise ValueError |
89 | | - self._data[meas.name] = meas |
90 | | - |
91 | | - def remove_measure(self, name=None): |
92 | | - """Remove Measure with provided name. Delete all Measures if no input |
93 | | - name |
94 | | -
|
95 | | - Parameters: |
96 | | - name (str, optional): measure name |
97 | | -
|
98 | | - Raises: |
99 | | - ValueError |
100 | | - """ |
101 | | - if name is not None: |
102 | | - try: |
103 | | - del self._data[name] |
104 | | - except KeyError: |
105 | | - LOGGER.warning('No Measure with name %s.', name) |
106 | | - else: |
107 | | - self._data = dict() |
| 41 | + # related to change in hazard |
| 42 | + self.hazard_set = '' |
| 43 | + self.hazard_freq_cutoff = 0 |
108 | 44 |
|
109 | | - def get_measure(self, name=None): |
110 | | - """Get Measure with input name. Get all if no name provided. |
| 45 | + # related to change in exposures |
| 46 | + self.exposures_set = '' |
| 47 | + self.exp_region_id = -1 |
111 | 48 |
|
112 | | - Parameters: |
113 | | - name (str, optional): measure name |
| 49 | + # related to change in impact functions |
| 50 | + self.hazard_inten_imp = () # parameter a and b |
| 51 | + self.mdd_impact = () # parameter a and b |
| 52 | + self.paa_impact = () # parameter a and b |
| 53 | + self.imp_fun_map = '' |
114 | 54 |
|
115 | | - Returns: |
116 | | - list(Measure) |
117 | | - """ |
118 | | - if name is not None: |
119 | | - try: |
120 | | - return self._data[name] |
121 | | - except KeyError: |
122 | | - return list() |
123 | | - else: |
124 | | - return list(self._data.values()) |
125 | | - |
126 | | - def get_names(self): |
127 | | - """Get all Measure names""" |
128 | | - return list(self._data.keys()) |
129 | | - |
130 | | - def num_measures(self): |
131 | | - """Get number of measures contained """ |
132 | | - return len(self._data.keys()) |
| 55 | + # risk transfer |
| 56 | + self.risk_transf_attach = 0 |
| 57 | + self.risk_transf_cover = 0 |
133 | 58 |
|
134 | 59 | def check(self): |
135 | | - """Check instance attributes. |
| 60 | + """ Check consistent instance data. |
136 | 61 |
|
137 | 62 | Raises: |
138 | 63 | ValueError |
139 | 64 | """ |
140 | | - for act_name, act in self._data.items(): |
141 | | - if (act_name != act.name) | (act.name == 'NA'): |
142 | | - raise ValueError('Wrong Measure.name: %s != %s' %\ |
143 | | - (act_name, act.name)) |
144 | | - act.check() |
145 | | - |
146 | | - def read(self, files, descriptions='', var_names=None): |
147 | | - """Read and check MeasureSet. |
| 65 | + check.size(3, self.color_rgb, 'Measure.color_rgb') |
| 66 | + check.size(2, self.hazard_inten_imp, 'Measure.hazard_inten_imp') |
| 67 | + check.size(2, self.mdd_impact, 'Measure.mdd_impact') |
| 68 | + check.size(2, self.paa_impact, 'Measure.paa_impact') |
148 | 69 |
|
149 | | - Parameters: |
150 | | - files (str or list(str)): absolute file name(s) or folder name |
151 | | - containing the files to read |
152 | | - descriptions (str or list(str), optional): one description of the |
153 | | - data or a description of each data file |
154 | | - var_names (dict or list(dict), default): name of the variables in |
155 | | - the file (default: DEF_VAR_NAME defined in the source modules) |
| 70 | + def implement(self, exposures, imp_fun_set, hazard): |
| 71 | + """Implement measure with all its defined parameters.""" |
| 72 | + new_haz = self.change_hazard(hazard) |
| 73 | + new_exp = self.change_exposures(exposures) |
| 74 | + new_ifs = self.change_imp_func(imp_fun_set) |
156 | 75 |
|
157 | | - Raises: |
158 | | - ValueError |
159 | | - """ |
160 | | - all_files = get_file_names(files) |
161 | | - desc_list = to_list(len(all_files), descriptions, 'descriptions') |
162 | | - var_list = to_list(len(all_files), var_names, 'var_names') |
163 | | - self.clear() |
164 | | - for file, desc, var in zip(all_files, desc_list, var_list): |
165 | | - self.append(self._read_one(file, desc, var)) |
| 76 | + return new_exp, new_ifs, new_haz |
166 | 77 |
|
167 | | - def append(self, meas): |
168 | | - """Check and append measures of input MeasureSet to current MeasureSet. |
169 | | - Overwrite Measure if same name. |
| 78 | + def change_hazard(self, hazard): |
| 79 | + """Apply measure to hazard of the same type. |
170 | 80 |
|
171 | 81 | Parameters: |
172 | | - meas (MeasureSet): MeasureSet instance to append |
173 | | -
|
174 | | - Raises: |
175 | | - ValueError |
176 | | - """ |
177 | | - meas.check() |
178 | | - if self.num_measures() == 0: |
179 | | - self.__dict__ = meas.__dict__.copy() |
180 | | - return |
181 | | - |
182 | | - self.tag.append(meas.tag) |
183 | | - for measure in meas.get_measure(): |
184 | | - self.add_measure(measure) |
185 | | - |
186 | | - @staticmethod |
187 | | - def get_sup_file_format(): |
188 | | - """ Get supported file extensions that can be read. |
| 82 | + hazard (Hazard): hazard instance |
189 | 83 |
|
190 | 84 | Returns: |
191 | | - list(str) |
| 85 | + Hazard |
192 | 86 | """ |
193 | | - return list(FILE_EXT.keys()) |
| 87 | + # TODO: implement |
| 88 | + return hazard |
194 | 89 |
|
195 | | - @staticmethod |
196 | | - def get_def_file_var_names(src_format): |
197 | | - """Get default variable names for given file format. |
| 90 | + def change_exposures(self, exposures): |
| 91 | + """Apply measure to exposures. |
198 | 92 |
|
199 | 93 | Parameters: |
200 | | - src_format (str): extension of the file, e.g. '.xls', '.mat'. |
| 94 | + exposures (Exposures): exposures instance |
201 | 95 |
|
202 | 96 | Returns: |
203 | | - dict: dictionary with variable names |
| 97 | + Exposures |
204 | 98 | """ |
205 | | - try: |
206 | | - if '.' not in src_format: |
207 | | - src_format = '.' + src_format |
208 | | - return copy.deepcopy(READ_SET[FILE_EXT[src_format]][0]) |
209 | | - except KeyError: |
210 | | - LOGGER.error('File extension not supported: %s.', src_format) |
211 | | - raise ValueError |
212 | | - |
213 | | - @staticmethod |
214 | | - def _read_one(file_name, description='', var_names=None): |
215 | | - """Read input file. |
| 99 | + # TODO: implement |
| 100 | + return exposures |
216 | 101 |
|
217 | | - Parameters: |
218 | | - file_name (str): name of the source file |
219 | | - description (str, optional): description of the source data |
220 | | - var_names (dict), optional): name of the variables in the file |
| 102 | + def change_imp_func(self, imp_set): |
| 103 | + """Apply measure to impact functions of the same hazard type. |
221 | 104 |
|
222 | | - Raises: |
223 | | - ValueError |
| 105 | + Parameters: |
| 106 | + imp_set (ImpactFuncSet): impact functions to be modified |
224 | 107 |
|
225 | 108 | Returns: |
226 | | - MeasureSet |
| 109 | + ImpactFuncSet |
227 | 110 | """ |
228 | | - LOGGER.info('Reading file: %s', file_name) |
229 | | - new_meas = MeasureSet() |
230 | | - new_meas.tag = Tag(file_name, description) |
231 | | - |
232 | | - extension = os.path.splitext(file_name)[1] |
233 | | - try: |
234 | | - reader = READ_SET[FILE_EXT[extension]][1] |
235 | | - except KeyError: |
236 | | - LOGGER.error('Input file extension not supported: %s.', extension) |
237 | | - raise ValueError |
238 | | - reader(new_meas, file_name, var_names) |
239 | | - |
240 | | - return new_meas |
241 | | - |
242 | | - def __str__(self): |
243 | | - return self.tag.__str__() |
244 | | - |
245 | | - __repr__ = __str__ |
| 111 | + # all impact functions of one hazard?? |
| 112 | + new_imp_set = ImpactFuncSet() |
| 113 | + |
| 114 | + for imp_fun in imp_set.get_func(self.haz_type): |
| 115 | + new_if = copy.copy(imp_fun) |
| 116 | + new_if.intensity = np.maximum(new_if.intensity * \ |
| 117 | + self.hazard_inten_imp[0] - self.hazard_inten_imp[1], 0.0) |
| 118 | + new_if.mdd = np.maximum(new_if.mdd * self.mdd_impact[0] - \ |
| 119 | + self.mdd_impact[1], 0.0) |
| 120 | + new_if.paa = np.maximum(new_if.paa * self.paa_impact[0] - \ |
| 121 | + self.paa_impact[1], 0.0) |
| 122 | + new_imp_set.add_func(new_if) |
| 123 | + |
| 124 | + if not new_imp_set.size(): |
| 125 | + LOGGER.info('No impact function of hazard %s found.', self.haz_type) |
| 126 | + |
| 127 | + return new_imp_set |
0 commit comments