-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathbase.py
More file actions
393 lines (337 loc) · 12.4 KB
/
base.py
File metadata and controls
393 lines (337 loc) · 12.4 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
"""
This file is part of CLIMADA.
Copyright (C) 2017 ETH Zurich, CLIMADA contributors listed in AUTHORS.
CLIMADA is free software: you can redistribute it and/or modify it under the
terms of the GNU General Public License as published by the Free
Software Foundation, version 3.
CLIMADA is distributed in the hope that it will be useful, but WITHOUT ANY
WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
PARTICULAR PURPOSE. See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License along
with CLIMADA. If not, see <https://www.gnu.org/licenses/>.
---
Define ImpactFunc class.
"""
__all__ = ["ImpactFunc"]
import logging
from typing import Optional, Union
import matplotlib.pyplot as plt
import numpy as np
import climada.util.checker as u_check
LOGGER = logging.getLogger(__name__)
class ImpactFunc:
"""Contains the definition of one impact function.
Attributes
----------
haz_type : str
hazard type acronym (e.g. 'TC')
id : int or str
id of the impact function. Exposures of the same type
will refer to the same impact function id
name : str
name of the ImpactFunc
intensity_unit : str
unit of the intensity
intensity : np.array
intensity values
mdd : np.array
mean damage (impact) degree for each intensity (numbers
in [0,1])
paa : np.array
percentage of affected assets (exposures) for each
intensity (numbers in [0,1])
"""
def __init__(
self,
haz_type: str = "",
id: Union[str, int] = "",
intensity: Optional[np.ndarray] = None,
mdd: Optional[np.ndarray] = None,
paa: Optional[np.ndarray] = None,
intensity_unit: str = "",
name: str = "",
):
"""Initialization.
Parameters
----------
haz_type : str, optional
Hazard type acronym (e.g. 'TC').
id : int or str, optional
id of the impact function. Exposures of the same type
will refer to the same impact function id.
intensity : np.array, optional
Intensity values. Defaults to empty array.
mdd : np.array, optional
Mean damage (impact) degree for each intensity (numbers
in [0,1]). Defaults to empty array.
paa : np.array, optional
Percentage of affected assets (exposures) for each
intensity (numbers in [0,1]). Defaults to empty array.
intensity_unit : str, optional
Unit of the intensity.
name : str, optional
Name of the ImpactFunc.
"""
self.id = id
self.name = name
self.intensity_unit = intensity_unit
self.haz_type = haz_type
# Followng values defined for each intensity value
self.intensity = intensity if intensity is not None else np.array([])
self.mdd = mdd if mdd is not None else np.array([])
self.paa = paa if paa is not None else np.array([])
def __eq__(self, value: object, /) -> bool:
if isinstance(value, ImpactFunc):
return (
self.haz_type == value.haz_type
and self.id == value.id
and self.name == value.name
and self.intensity_unit == value.intensity_unit
and np.array_equal(self.intensity, value.intensity)
and np.array_equal(self.mdd, value.mdd)
and np.array_equal(self.paa, value.paa)
)
return False
def calc_mdr(self, inten: Union[float, np.ndarray]) -> np.ndarray:
"""Interpolate impact function to a given intensity.
Parameters
----------
inten : float or np.array
intensity, the x-coordinate of the
interpolated values.
Returns
-------
np.array
"""
# return np.interp(inten, self.intensity, self.mdd * self.paa)
return np.interp(inten, self.intensity, self.paa) * np.interp(
inten, self.intensity, self.mdd
)
def plot(self, axis=None, **kwargs):
"""Plot the impact functions MDD, MDR and PAA in one graph, where
MDR = PAA * MDD.
Parameters
----------
axis : matplotlib.axes._subplots.AxesSubplot, optional
axis to use
kwargs : optional
arguments for plot matplotlib function, e.g. marker='x'
Returns
-------
matplotlib.axes._subplots.AxesSubplot
"""
if axis is None:
_, axis = plt.subplots(1, 1)
title = "%s %s" % (self.haz_type, str(self.id))
if self.name != str(self.id):
title += ": %s" % self.name
axis.set_xlabel("Intensity (" + self.intensity_unit + ")")
axis.set_ylabel("Impact (%)")
axis.set_title(title)
axis.plot(self.intensity, self.mdd * 100, "b", label="MDD", **kwargs)
axis.plot(self.intensity, self.paa * 100, "r", label="PAA", **kwargs)
axis.plot(
self.intensity, self.mdd * self.paa * 100, "k--", label="MDR", **kwargs
)
axis.set_xlim((self.intensity.min(), self.intensity.max()))
axis.legend()
return axis
def check(self):
"""Check consistent instance data.
Raises
------
ValueError
"""
num_exp = len(self.intensity)
u_check.size(num_exp, self.mdd, "ImpactFunc.mdd")
u_check.size(num_exp, self.paa, "ImpactFunc.paa")
if num_exp == 0:
LOGGER.warning(
"%s impact function with name '%s' (id=%s) has empty" " intensity.",
self.haz_type,
self.name,
self.id,
)
return
@classmethod
def from_step_impf(
cls,
intensity: tuple[float, float, float],
haz_type: str,
mdd: tuple[float, float] = (0, 1),
paa: tuple[float, float] = (1, 1),
impf_id: int = 1,
**kwargs,
):
"""Step function type impact function.
By default, the impact is 100% above the step.
Useful for high resolution modelling.
Parameters
----------
intensity: tuple(float, float, float)
tuple of 3-intensity numbers: (minimum, threshold, maximum)
haz_type: str
the reference string for the hazard (e.g., 'TC', 'RF', 'WS', ...)
mdd: tuple(float, float)
(min, max) mdd values. The default is (0, 1)
paa: tuple(float, float)
(min, max) paa values. The default is (1, 1)
impf_id : int, optional, default=1
impact function id
kwargs :
keyword arguments passed to ImpactFunc()
Return
------
impf : climada.entity.impact_funcs.ImpactFunc
Step impact function
"""
inten_min, threshold, inten_max = intensity
intensity = np.array([inten_min, threshold, threshold, inten_max])
paa_min, paa_max = paa
paa = np.array([paa_min, paa_min, paa_max, paa_max])
mdd_min, mdd_max = mdd
mdd = np.array([mdd_min, mdd_min, mdd_max, mdd_max])
return cls(
haz_type=haz_type,
id=impf_id,
intensity=intensity,
mdd=mdd,
paa=paa,
**kwargs,
)
def set_step_impf(self, *args, **kwargs):
"""This function is deprecated, use ImpactFunc.from_step_impf instead."""
LOGGER.warning(
"The use of ImpactFunc.set_step_impf is deprecated."
+ "Use ImpactFunc.from_step_impf instead."
)
self.__dict__ = ImpactFunc.from_step_impf(*args, **kwargs).__dict__
@classmethod
def from_sigmoid_impf(
cls,
intensity: tuple[float, float, float],
L: float,
k: float,
x0: float,
haz_type: str,
impf_id: int = 1,
**kwargs,
):
r"""Sigmoid type impact function hinging on three parameter.
This type of impact function is very flexible for any sort of study,
hazard and resolution. The sigmoid is defined as:
.. math::
f(x) = \frac{L}{1+e^{-k(x-x0)}}
For more information: https://en.wikipedia.org/wiki/Logistic_function
This method modifies self (climada.entity.impact_funcs instance)
by assining an id, intensity, mdd and paa to the impact function.
Parameters
----------
intensity : tuple(float, float, float)
tuple of 3 intensity numbers along np.arange(min, max, step)
L : float
"top" of sigmoid
k : float
"slope" of sigmoid
x0 : float
intensity value where f(x)==L/2
haz_type: str
the reference string for the hazard (e.g., 'TC', 'RF', 'WS', ...)
impf_id : int, optional, default=1
impact function id
kwargs :
keyword arguments passed to ImpactFunc()
Return
------
impf : climada.entity.impact_funcs.ImpactFunc
Sigmoid impact function
"""
inten_min, inten_max, inten_step = intensity
intensity = np.arange(inten_min, inten_max, inten_step)
paa = np.ones(len(intensity))
mdd = L / (1 + np.exp(-k * (intensity - x0)))
return cls(
haz_type=haz_type,
id=impf_id,
intensity=intensity,
paa=paa,
mdd=mdd,
**kwargs,
)
def set_sigmoid_impf(self, *args, **kwargs):
"""This function is deprecated, use LitPop.from_countries instead."""
LOGGER.warning(
"The use of ImpactFunc.set_sigmoid_impf is deprecated."
"Use ImpactFunc.from_sigmoid_impf instead."
)
self.__dict__ = ImpactFunc.from_sigmoid_impf(*args, **kwargs).__dict__
@classmethod
def from_poly_s_shape(
cls,
intensity: tuple[float, float, int],
threshold: float,
half_point: float,
scale: float,
exponent: float,
haz_type: str,
impf_id: int = 1,
**kwargs,
):
r"""S-shape polynomial impact function hinging on four parameter.
.. math::
f(I) = \frac{\textrm{luk}(I)^{\textrm{exponent}}}{
1 + \textrm{luk}(I)^{\textrm{exponent}}
}
\cdot \textrm{scale} \\
\textrm{luk}(I) = \frac{\max[I - \textrm{threshold}, 0]}{
\textrm{half_point} - \textrm{threshold}
}
This function is inspired by Emanuel et al. (2011)
https://doi.org/10.1175/WCAS-D-11-00007.1
This method only specifies mdd, and paa = 1 for all intensities.
Parameters
----------
intensity : tuple(float, float, float)
tuple of 3 intensity numbers along np.linsapce(min, max, num)
threshold : float
Intensity threshold below which there is no impact.
In general choose threshold > 0 for computational efficiency
of impacts.
half_point : float
Intensity at which 50% of maximum impact is expected.
If half_point <= threshold, mdd = 0 (and f(I)=0) for all
intensities.
scale : float
Multiplicative factor for the whole function. Typically,
this sets the maximum value at large intensities.
exponent: float
Exponent of the polynomial. Value must be exponent >= 0.
Emanuel et al. (2011) uses the value 3.
haz_type: str
Reference string for the hazard (e.g., 'TC', 'RF', 'WS', ...)
impf_id : int, optional, default=1
Impact function id
kwargs :
keyword arguments passed to ImpactFunc()
Raises
------
ValueError : if exponent <= 0
Returns
-------
impf : climada.entity.impact_funcs.ImpactFunc
s-shaped polynomial impact function
"""
if exponent < 0:
raise ValueError("Exponent value must larger than 0")
inten = np.linspace(*intensity)
if threshold >= half_point:
mdd = np.zeros_like(inten)
else:
luk = (inten - threshold) / (half_point - threshold)
luk[luk < 0] = 0
mdd = scale * luk**exponent / (1 + luk**exponent)
paa = np.ones_like(inten)
impf = cls(
haz_type=haz_type, id=impf_id, intensity=inten, paa=paa, mdd=mdd, **kwargs
)
return impf