|
22 | 22 | import unittest |
23 | 23 | import numpy as np |
24 | 24 |
|
25 | | -from climada.util.interpolation import interpolate_ev, stepfunction_ev, group_frequency |
| 25 | +from climada.util.interpolation import interpolate_ev, stepfunction_ev, group_frequency, preprocess_and_interpolate_ev |
26 | 26 |
|
27 | 27 |
|
28 | 28 | class TestFitMethods(unittest.TestCase): |
@@ -192,6 +192,41 @@ def test_frequency_group(self): |
192 | 192 | with self.assertRaises(ValueError): |
193 | 193 | group_frequency(frequency, intensity[::-1]) |
194 | 194 |
|
| 195 | + def test_preprocess_and_interpolate_ev(self): |
| 196 | + """Test wrapper function""" |
| 197 | + frequency = np.array([.1, .9]) |
| 198 | + values = np.array([100., 10.]) |
| 199 | + test_frequency = np.array([.01, .55, 10.]) |
| 200 | + test_values = np.array([1., 55., 1000.]) |
| 201 | + |
| 202 | + # test interpolation |
| 203 | + np.testing.assert_allclose( |
| 204 | + [np.nan, 55., np.nan], |
| 205 | + preprocess_and_interpolate_ev(test_frequency, None, frequency, values) |
| 206 | + ) |
| 207 | + np.testing.assert_allclose( |
| 208 | + [np.nan, .55, np.nan], |
| 209 | + preprocess_and_interpolate_ev(None, test_values, frequency, values) |
| 210 | + ) |
| 211 | + |
| 212 | + # test extrapolation with constants |
| 213 | + np.testing.assert_allclose( |
| 214 | + [100. , 55., 0.], |
| 215 | + preprocess_and_interpolate_ev(test_frequency, None, frequency, values, |
| 216 | + method='extrapolate_constant', y_asymptotic=0.) |
| 217 | + ) |
| 218 | + np.testing.assert_allclose( |
| 219 | + [1., .55, np.nan], |
| 220 | + preprocess_and_interpolate_ev(None, test_values, frequency, values, |
| 221 | + method='extrapolate_constant') |
| 222 | + ) |
| 223 | + |
| 224 | + # test error raising |
| 225 | + with self.assertRaises(ValueError): |
| 226 | + preprocess_and_interpolate_ev(test_frequency, test_values, frequency, values) |
| 227 | + with self.assertRaises(ValueError): |
| 228 | + preprocess_and_interpolate_ev(None, None, frequency, values) |
| 229 | + |
195 | 230 | # Execute Tests |
196 | 231 | if __name__ == "__main__": |
197 | 232 | TESTS = unittest.TestLoader().loadTestsFromTestCase(TestFitMethods) |
|
0 commit comments