Skip to content

Commit 4b5ae95

Browse files
committed
Raise value error when computing impact with impact forecast without saving impact matrix
1 parent 59c0e5b commit 4b5ae95

File tree

2 files changed

+32
-0
lines changed

2 files changed

+32
-0
lines changed

climada/engine/impact_calc.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -233,6 +233,10 @@ def _return_impact(self, imp_mat_gen, save_mat):
233233
imp_mat, self.hazard.frequency
234234
)
235235
else:
236+
if isinstance(self.hazard, HazardForecast):
237+
raise ValueError(
238+
"Saving impact matrix is required when using HazardForecast."
239+
)
236240
imp_mat = None
237241
at_event, eai_exp, aai_agg = self.stitch_risk_metrics(imp_mat_gen)
238242

@@ -270,6 +274,10 @@ def _return_empty(self, save_mat):
270274
(self.n_events, self.n_exp_pnt), dtype=np.float64
271275
)
272276
else:
277+
if isinstance(self.hazard, HazardForecast):
278+
raise ValueError(
279+
"Saving impact matrix is required when using HazardForecast."
280+
)
273281
imp_mat = None
274282
impact = Impact.from_eih(
275283
self.exposures, self.hazard, at_event, eai_exp, aai_agg, imp_mat

climada/engine/test/test_impact_calc.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -388,6 +388,30 @@ def test_impactForecast(self):
388388
self.assertIs(impact.lead_time.dtype, lead_time.dtype)
389389
np.testing.assert_array_equal(impact.member, member)
390390

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+
391415
def test_empty_impact(self):
392416
"""Check that empty impact is returned if no centroids match the exposures"""
393417
exp = ENT.exposures.copy()

0 commit comments

Comments
 (0)