Skip to content

Commit 043a15c

Browse files
added tests for two new util functions
1 parent 9f343cb commit 043a15c

File tree

2 files changed

+45
-1
lines changed

2 files changed

+45
-1
lines changed

climada/util/test/test_checker.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,15 @@ def test_prune_csr_matrix(self):
109109
np.testing.assert_array_equal(matrix.data, [3])
110110
self.assertEqual(matrix.nnz, 1)
111111

112+
def test_convert_frequency_unit_to_time_unit(self):
113+
# test frequency unit to time unit conversion
114+
frequency_units = ['1/year', '1/y', '1/month', '1/week', '', 'unknown']
115+
time_units = ['years', 'years', 'months', 'weeks', 'years', 'years']
116+
np.testing.assert_equal(
117+
time_units, [u_check.convert_frequency_unit_to_time_unit(frequency_unit)
118+
for frequency_unit in frequency_units]
119+
)
120+
112121
# Execute Tests
113122
if __name__ == "__main__":
114123
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestChecks)

climada/util/test/test_interpolation.py

Lines changed: 36 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@
2222
import unittest
2323
import numpy as np
2424

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
2626

2727

2828
class TestFitMethods(unittest.TestCase):
@@ -192,6 +192,41 @@ def test_frequency_group(self):
192192
with self.assertRaises(ValueError):
193193
group_frequency(frequency, intensity[::-1])
194194

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+
195230
# Execute Tests
196231
if __name__ == "__main__":
197232
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestFitMethods)

0 commit comments

Comments
 (0)