Skip to content

Commit 3153651

Browse files
add tests
1 parent 0ba3faa commit 3153651

2 files changed

Lines changed: 44 additions & 4 deletions

File tree

climada/engine/impact.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2340,16 +2340,20 @@ def evaluate(
23402340
np.array
23412341
array of exceeded impacts at given return periods
23422342
"""
2343-
2344-
# sort values of ImpactFreqCurve
23452343
exceedance_frequency = 1 / np.array(return_period)
2346-
frequency = np.diff(1 / np.array(self.return_per)[::-1], prepend=0)
2344+
2345+
# sort return periods of ImpactFreqCurve
2346+
sorted_idxs = np.argsort(self.return_per)
2347+
impacts = np.squeeze(np.array(self.impact)[sorted_idxs])
2348+
rps = np.asarray(self.return_per)[sorted_idxs]
2349+
2350+
frequency = np.diff(1 / np.array(rps)[::-1], prepend=0)
23472351

23482352
return u_interp.preprocess_and_interpolate_ev(
23492353
exceedance_frequency,
23502354
None,
23512355
frequency,
2352-
self.impact,
2356+
impacts,
23532357
log_frequency=log_frequency,
23542358
log_values=log_impact,
23552359
value_threshold=min_impact,

climada/engine/test/test_impact.py

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -321,6 +321,42 @@ def test_ref_value_rp_pass(self):
321321
self.assertEqual("USD", ifc.unit)
322322
self.assertEqual("1/week", ifc.frequency_unit)
323323

324+
def test_evaluate_freq_curve(self):
325+
"""Test evaluate method of freq curve"""
326+
imp = Impact()
327+
imp.frequency = np.ones(4) * 0.1
328+
imp.at_event = np.zeros(4)
329+
imp.at_event[0] = 0.0
330+
imp.at_event[1] = 100.0
331+
imp.at_event[2] = 50.0
332+
imp.at_event[3] = 110.0
333+
imp.unit = "USD"
334+
imp.frequency_unit = "1/year"
335+
336+
ifc = imp.calc_freq_curve()
337+
npt.assert_array_almost_equal(
338+
ifc.evaluate([1, 5, 20], method="stepfunction"), [0.0, 100.0, 110.0]
339+
)
340+
npt.assert_array_almost_equal(
341+
ifc.evaluate([1, 5, 20], method="interpolate"), [np.nan, 100.0, np.nan]
342+
)
343+
npt.assert_array_almost_equal(
344+
ifc.evaluate([1, 5, 20], method="extrapolate_constant"), [0.0, 100.0, 110.0]
345+
)
346+
npt.assert_array_almost_equal(
347+
ifc.evaluate([1, 5, 20], method="extrapolate_constant", bin_decimals=-2),
348+
[0.0, 100.0, 100.0],
349+
)
350+
npt.assert_array_almost_equal(
351+
ifc.evaluate(
352+
[1.0, 2.5, 4, 20],
353+
method="extrapolate",
354+
log_frequency=False,
355+
log_impact=False,
356+
),
357+
[-300.0, 0.0, 75.0, 115.0],
358+
)
359+
324360

325361
class TestImpactPerYear(unittest.TestCase):
326362
"""Test calc_impact_year_set method"""

0 commit comments

Comments
 (0)