Skip to content

Commit 9a516e1

Browse files
author
Chahan Kropf
committed
Cosmetics: Improve error message, move test to own class
1 parent 4b5ae95 commit 9a516e1

File tree

2 files changed

+119
-115
lines changed

2 files changed

+119
-115
lines changed

climada/engine/impact_calc.py

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ def _return_impact(self, imp_mat_gen, save_mat):
236236
if isinstance(self.hazard, HazardForecast):
237237
raise ValueError(
238238
"Saving impact matrix is required when using HazardForecast."
239+
"Please set save_mat=True."
239240
)
240241
imp_mat = None
241242
at_event, eai_exp, aai_agg = self.stitch_risk_metrics(imp_mat_gen)
@@ -246,11 +247,9 @@ def _return_impact(self, imp_mat_gen, save_mat):
246247
if isinstance(self.hazard, HazardForecast):
247248
return ImpactForecast.from_impact(
248249
impact, self.hazard.lead_time, self.hazard.member
249-
) # return ImpactForecast object
250-
else:
251-
return (
252-
impact # return normal impact object if hazard is not a HazardForecast
253250
)
251+
else:
252+
return impact
254253

255254
def _return_empty(self, save_mat):
256255
"""
@@ -277,6 +276,7 @@ def _return_empty(self, save_mat):
277276
if isinstance(self.hazard, HazardForecast):
278277
raise ValueError(
279278
"Saving impact matrix is required when using HazardForecast."
279+
"Please set save_mat=True."
280280
)
281281
imp_mat = None
282282
impact = Impact.from_eih(
@@ -285,11 +285,9 @@ def _return_empty(self, save_mat):
285285
if isinstance(self.hazard, HazardForecast):
286286
return ImpactForecast.from_impact(
287287
impact, self.hazard.lead_time, self.hazard.member
288-
) # return ImpactForecast object
289-
else:
290-
return (
291-
impact # return normal impact object if hazard is not a HazardForecast
292288
)
289+
else:
290+
return impact
293291

294292
def minimal_exp_gdf(
295293
self, impf_col, assign_centroids, ignore_cover, ignore_deductible

climada/engine/test/test_impact_calc.py

Lines changed: 113 additions & 107 deletions
Original file line numberDiff line numberDiff line change
@@ -305,113 +305,6 @@ def test_calc_impact_RF_pass(self):
305305
# fmt: on
306306
check_impact(self, impact, haz, exp, aai_agg, eai_exp, at_event, imp_mat_array)
307307

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-
391-
def test_impact_forecast_empty_impmat_error(self):
392-
"""Test that error is raised when trying to compute impact forecast
393-
without saving impact matrix
394-
"""
395-
lead_time = pd.timedelta_range("1h", periods=6).to_numpy()
396-
member = np.arange(6)
397-
_haz = Hazard.from_hdf5(get_test_file("test_hazard_US_flood_random_locations"))
398-
haz_fc = HazardForecast.from_hazard(_haz, lead_time=lead_time, member=member)
399-
400-
exp = Exposures.from_hdf5(
401-
get_test_file("test_exposure_US_flood_random_locations")
402-
)
403-
impf_set = ImpactFuncSet.from_excel(
404-
Path(__file__).parent / "data" / "flood_imp_func_set.xls"
405-
)
406-
icalc = ImpactCalc(exp, impf_set, haz_fc)
407-
with self.assertRaises(ValueError) as cm:
408-
icalc.impact(assign_centroids=False, save_mat=False)
409-
the_exception = cm.exception
410-
self.assertEqual(
411-
the_exception.args[0],
412-
"Saving impact matrix is required when using HazardForecast.",
413-
)
414-
415308
def test_empty_impact(self):
416309
"""Check that empty impact is returned if no centroids match the exposures"""
417310
exp = ENT.exposures.copy()
@@ -716,6 +609,118 @@ def test_single_exp_zero_mdr(self):
716609
check_impact(self, imp, haz, exp, aai_agg, eai_exp, at_event, at_event)
717610

718611

612+
class TestImpactCalcForecast(unittest.TestCase):
613+
"""Test impact calc for forecast hazard"""
614+
615+
def test_impactForecast(self):
616+
"""Test that ImpactForecast is returned correctly"""
617+
lead_time = pd.timedelta_range("1h", periods=6).to_numpy()
618+
member = np.arange(6)
619+
_haz = Hazard.from_hdf5(get_test_file("test_hazard_US_flood_random_locations"))
620+
haz_fc = HazardForecast.from_hazard(_haz, lead_time=lead_time, member=member)
621+
622+
exp = Exposures.from_hdf5(
623+
get_test_file("test_exposure_US_flood_random_locations")
624+
)
625+
impf_set = ImpactFuncSet.from_excel(
626+
Path(__file__).parent / "data" / "flood_imp_func_set.xls"
627+
)
628+
icalc = ImpactCalc(exp, impf_set, haz_fc)
629+
impact = icalc.impact(assign_centroids=False)
630+
aai_agg = 161436.05112960344
631+
eai_exp = np.array(
632+
[
633+
1.61159701e05,
634+
1.33742847e02,
635+
0.00000000e00,
636+
4.21352988e-01,
637+
1.42185609e02,
638+
0.00000000e00,
639+
0.00000000e00,
640+
0.00000000e00,
641+
]
642+
)
643+
at_event = np.array(
644+
[
645+
0.00000000e00,
646+
0.00000000e00,
647+
9.85233619e04,
648+
3.41245461e04,
649+
7.73566566e07,
650+
0.00000000e00,
651+
0.00000000e00,
652+
]
653+
)
654+
# fmt: off
655+
imp_mat_array = np.array(
656+
[
657+
[
658+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
659+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
660+
],
661+
[
662+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
663+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
664+
],
665+
[
666+
0.00000000e00, 6.41965663e04, 0.00000000e00, 2.02249434e02,
667+
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
668+
],
669+
[
670+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
671+
3.41245461e04, 0.00000000e00, 0.00000000e00, 0.00000000e00,
672+
],
673+
[
674+
7.73566566e07, 0.00000000e00, 0.00000000e00, 0.00000000e00,
675+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
676+
],
677+
[
678+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
679+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
680+
],
681+
[
682+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
683+
0.00000000e00, 0.00000000e00, 0.00000000e00, 0.00000000e00,
684+
],
685+
]
686+
)
687+
# fmt: on
688+
check_impact(
689+
self, impact, haz_fc, exp, aai_agg, eai_exp, at_event, imp_mat_array
690+
)
691+
692+
# additional test to check that impact is indeed ImpactForecast
693+
self.assertIsInstance(impact, ImpactForecast)
694+
np.testing.assert_array_equal(impact.lead_time, lead_time)
695+
self.assertIs(impact.lead_time.dtype, lead_time.dtype)
696+
np.testing.assert_array_equal(impact.member, member)
697+
698+
def test_impact_forecast_empty_impmat_error(self):
699+
"""Test that error is raised when trying to compute impact forecast
700+
without saving impact matrix
701+
"""
702+
lead_time = pd.timedelta_range("1h", periods=6).to_numpy()
703+
member = np.arange(6)
704+
_haz = Hazard.from_hdf5(get_test_file("test_hazard_US_flood_random_locations"))
705+
haz_fc = HazardForecast.from_hazard(_haz, lead_time=lead_time, member=member)
706+
707+
exp = Exposures.from_hdf5(
708+
get_test_file("test_exposure_US_flood_random_locations")
709+
)
710+
impf_set = ImpactFuncSet.from_excel(
711+
Path(__file__).parent / "data" / "flood_imp_func_set.xls"
712+
)
713+
icalc = ImpactCalc(exp, impf_set, haz_fc)
714+
with self.assertRaises(ValueError) as cm:
715+
icalc.impact(assign_centroids=False, save_mat=False)
716+
no_impmat_exception = cm.exception
717+
self.assertEqual(
718+
no_impmat_exception.args[0],
719+
"Saving impact matrix is required when using HazardForecast."
720+
"Please set save_mat=True.",
721+
)
722+
723+
719724
class TestImpactMatrixCalc(unittest.TestCase):
720725
"""Verify the computation of the impact matrix"""
721726

@@ -1011,6 +1016,7 @@ def test_skip_mat(self, from_eih_mock):
10111016
# Execute Tests
10121017
if __name__ == "__main__":
10131018
TESTS = unittest.TestLoader().loadTestsFromTestCase(TestImpactCalc)
1019+
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestImpactCalcForecast))
10141020
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestReturnImpact))
10151021
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestImpactMatrix))
10161022
TESTS.addTests(unittest.TestLoader().loadTestsFromTestCase(TestImpactMatrixCalc))

0 commit comments

Comments
 (0)