Skip to content

Commit dfa0198

Browse files
committed
Add full impactcalc test for impactForecast
1 parent bd3502f commit dfa0198

File tree

1 file changed

+87
-1
lines changed

1 file changed

+87
-1
lines changed

climada/engine/test/test_impact_calc.py

Lines changed: 87 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -26,14 +26,17 @@
2626

2727
import geopandas as gpd
2828
import numpy as np
29+
import pandas as pd
2930
from scipy import sparse
3031

3132
from climada import CONFIG
3233
from climada.engine import Impact, ImpactCalc
3334
from climada.engine.impact_calc import LOGGER as ILOG
35+
from climada.engine.impact_forecast import ImpactForecast
3436
from climada.entity import Exposures, ImpactFunc, ImpactFuncSet, ImpfTropCyclone
3537
from climada.entity.entity_def import Entity
3638
from climada.hazard.base import Centroids, Hazard
39+
from climada.hazard.forecast import HazardForecast
3740
from climada.test import get_test_file
3841
from climada.util.api_client import Client
3942
from climada.util.config import Config
@@ -47,7 +50,7 @@
4750

4851

4952
def check_impact(self, imp, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array=None):
50-
"""Test properties of imapcts"""
53+
"""Test properties of impacts"""
5154
self.assertEqual(len(haz.event_id), len(imp.at_event))
5255
self.assertIsInstance(imp, Impact)
5356
np.testing.assert_allclose(imp.coord_exp[:, 0], exp.latitude)
@@ -302,6 +305,89 @@ def test_calc_impact_RF_pass(self):
302305
# fmt: on
303306
check_impact(self, impact, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array)
304307

308+
def test_impactForecast(self):
309+
"""Test that ImpactForecast is returned correctly"""
310+
lead_time = pd.timedelta_range("1h", periods=6).to_numpy()
311+
member = np.arange(6)
312+
_haz = Hazard.from_hdf5(get_test_file("test_hazard_US_flood_random_locations"))
313+
haz_fc = HazardForecast.from_hazard(_haz, lead_time=lead_time, member=member)
314+
315+
exp = Exposures.from_hdf5(
316+
get_test_file("test_exposure_US_flood_random_locations")
317+
)
318+
impf_set = ImpactFuncSet.from_excel(
319+
Path(__file__).parent / "data" / "flood_imp_func_set.xls"
320+
)
321+
icalc = ImpactCalc(exp, impf_set, haz_fc)
322+
impact = icalc.impact(assign_centroids=False)
323+
aai_agg = 161436.05112960344
324+
eai_exp = np.array(
325+
[
326+
1.61159701e05,
327+
1.33742847e02,
328+
0.00000000e00,
329+
4.21352988e-01,
330+
1.42185609e02,
331+
0.00000000e00,
332+
0.00000000e00,
333+
0.00000000e00,
334+
]
335+
)
336+
at_event = np.array(
337+
[
338+
0.00000000e00,
339+
0.00000000e00,
340+
9.85233619e04,
341+
3.41245461e04,
342+
7.73566566e07,
343+
0.00000000e00,
344+
0.00000000e00,
345+
]
346+
)
347+
# fmt: off
348+
imp_mat_array = np.array(
349+
[
350+
[
351+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
352+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
353+
],
354+
[
355+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
356+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
357+
],
358+
[
359+
0.00000000e00, 6.41965663e04, 0.00000000e00, 2.02249434e02,
360+
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
361+
],
362+
[
363+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
364+
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
365+
],
366+
[
367+
7.73566566e07, 0.00000000e00, 0.00000000e00, 0.00000000e00,
368+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
369+
],
370+
[
371+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
372+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
373+
],
374+
[
375+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
376+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
377+
],
378+
]
379+
)
380+
# fmt: on
381+
check_impact(
382+
self, impact, haz_fc, exp, aai_agg, eai_exp, at_event, imp_mat_array
383+
)
384+
385+
# additional test to check that impact is indeed ImpactForecast
386+
self.assertIsInstance(impact, ImpactForecast)
387+
np.testing.assert_array_equal(impact.lead_time, lead_time)
388+
self.assertIs(impact.lead_time.dtype, lead_time.dtype)
389+
np.testing.assert_array_equal(impact.member, member)
390+
305391
def test_empty_impact(self):
306392
"""Check that empty impact is returned if no centroids match the exposures"""
307393
exp = ENT.exposures.copy()

0 commit comments

Comments
 (0)