-
Notifications
You must be signed in to change notification settings - Fork 154
Expand file tree
/
Copy pathtest_base.py
More file actions
214 lines (181 loc) · 7.07 KB
/
test_base.py
File metadata and controls
214 lines (181 loc) · 7.07 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
"""
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/>.
---
Test ImpactFunc class.
"""
import unittest
import numpy as np
from climada.entity.impact_funcs.base import ImpactFunc
class TestEquality(unittest.TestCase):
"""Test equality method"""
def setUp(self):
self.impf1 = ImpactFunc(
haz_type="TC",
id=1,
intensity=np.array([1, 2, 3]),
mdd=np.array([0.1, 0.2, 0.3]),
paa=np.array([0.4, 0.5, 0.6]),
intensity_unit="m/s",
name="Test Impact",
)
self.impf2 = ImpactFunc(
haz_type="TC",
id=1,
intensity=np.array([1, 2, 3]),
mdd=np.array([0.1, 0.2, 0.3]),
paa=np.array([0.4, 0.5, 0.6]),
intensity_unit="m/s",
name="Test Impact",
)
self.impf3 = ImpactFunc(
haz_type="FL",
id=2,
intensity=np.array([4, 5, 6]),
mdd=np.array([0.7, 0.8, 0.9]),
paa=np.array([0.1, 0.2, 0.3]),
intensity_unit="m",
name="Another Impact",
)
def test_reflexivity(self):
self.assertEqual(self.impf1, self.impf1)
def test_symmetry(self):
self.assertEqual(self.impf1, self.impf2)
self.assertEqual(self.impf2, self.impf1)
def test_transitivity(self):
impf4 = ImpactFunc(
haz_type="TC",
id=1,
intensity=np.array([1, 2, 3]),
mdd=np.array([0.1, 0.2, 0.3]),
paa=np.array([0.4, 0.5, 0.6]),
intensity_unit="m/s",
name="Test Impact",
)
self.assertEqual(self.impf1, self.impf2)
self.assertEqual(self.impf2, impf4)
self.assertEqual(self.impf1, impf4)
def test_consistency(self):
self.assertEqual(self.impf1, self.impf2)
self.assertEqual(self.impf1, self.impf2)
def test_comparison_with_none(self):
self.assertNotEqual(self.impf1, None)
def test_different_types(self):
self.assertNotEqual(self.impf1, "Not an ImpactFunc")
def test_inequality(self):
self.assertNotEqual(self.impf1, self.impf3)
self.assertTrue(self.impf1 != self.impf3)
class TestInterpolation(unittest.TestCase):
"""Impact function interpolation test"""
def test_calc_mdr_pass(self):
"""Compute mdr interpolating values."""
intensity = np.arange(0, 100, 10)
paa = np.arange(0, 1, 0.1)
mdd = np.arange(0, 1, 0.1)
imp_fun = ImpactFunc(intensity=intensity, paa=paa, mdd=mdd)
new_inten = 17.2
self.assertEqual(imp_fun.calc_mdr(new_inten), 0.029583999999999996)
def test_from_step(self):
"""Check default impact function: step function"""
inten = (0, 5, 10)
imp_fun = ImpactFunc.from_step_impf(intensity=inten, haz_type="TC", impf_id=2)
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])))
self.assertEqual(imp_fun.haz_type, "TC")
self.assertEqual(imp_fun.id, 2)
def test_from_sigmoid(self):
"""Check default impact function: sigmoid function"""
inten = (0, 100, 5)
imp_fun = ImpactFunc.from_sigmoid_impf(
inten, L=1.0, k=2.0, x0=50.0, haz_type="RF", impf_id=2
)
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)))
self.assertEqual(imp_fun.haz_type, "RF")
self.assertEqual(imp_fun.id, 2)
def test_from_poly_s_shape(self):
"""Check default impact function: polynomial s-shape"""
haz_type = "RF"
threshold = 0.2
half_point = 1
scale = 0.8
exponent = 4
impf_id = 2
unit = "m"
intensity = (0, 5, 5)
def test_aux_vars(impf):
self.assertTrue(np.array_equal(impf.paa, np.ones(5)))
self.assertTrue(np.array_equal(impf.intensity, np.linspace(0, 5, 5)))
self.assertEqual(impf.haz_type, haz_type)
self.assertEqual(impf.id, impf_id)
self.assertEqual(impf.intensity_unit, unit)
impf = ImpactFunc.from_poly_s_shape(
intensity=intensity,
threshold=threshold,
half_point=half_point,
scale=scale,
exponent=exponent,
haz_type=haz_type,
impf_id=impf_id,
intensity_unit=unit,
)
# True value can easily be computed with a calculator
correct_mdd = np.array([0, 0.59836395, 0.78845941, 0.79794213, 0.79938319])
np.testing.assert_array_almost_equal(impf.mdd, correct_mdd)
test_aux_vars(impf)
# If threshold > half_point, mdd should all be 0
impf = ImpactFunc.from_poly_s_shape(
intensity=intensity,
threshold=half_point * 2,
half_point=half_point,
scale=scale,
exponent=exponent,
haz_type=haz_type,
impf_id=impf_id,
intensity_unit=unit,
)
np.testing.assert_array_almost_equal(impf.mdd, np.zeros(5))
test_aux_vars(impf)
# If exponent = 0, mdd should be constant
impf = ImpactFunc.from_poly_s_shape(
intensity=intensity,
threshold=threshold,
half_point=half_point,
scale=scale,
exponent=0,
haz_type=haz_type,
impf_id=impf_id,
intensity_unit=unit,
)
np.testing.assert_array_almost_equal(impf.mdd, np.ones(5) * scale / 2)
test_aux_vars(impf)
# If exponent < 0, raise error.
with self.assertRaisesRegex(ValueError, "Exponent value"):
ImpactFunc.from_poly_s_shape(
intensity=intensity,
threshold=half_point,
half_point=half_point,
scale=scale,
exponent=-1,
haz_type=haz_type,
impf_id=impf_id,
intensity_unit=unit,
)
# Execute Tests
if __name__ == "__main__":
equality_tests = unittest.TestLoader().loadTestsFromTestCase(TestEquality)
interpolation_tests = unittest.TestLoader().loadTestsFromTestCase(TestInterpolation)
unittest.TextTestRunner(verbosity=2).run(
unittest.TestSuite([equality_tests, interpolation_tests])
)