Skip to content

Commit f499389

Browse files
Raise clear error for empty Hazard in ImpactCalc.impact
Computing impact with a Hazard containing zero events previously crashed deep inside ``np.array_split`` with the obscure message "number sections must be larger than 0". This adds an early guard in ``ImpactCalc.impact`` that raises a ``ValueError`` with a clear explanation when ``self.hazard.size == 0``. Closes #814
1 parent 3133447 commit f499389

3 files changed

Lines changed: 26 additions & 0 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ Code freeze date: YYYY-MM-DD
2323

2424
- Fixed asset count in impact logging message [#1195](https://github.com/CLIMADA-project/climada_python/pull/1195).
2525
- `Hazard.from_raster_xarray` now returns a sparse matrix instead of a sparse array [#1261](https://github.com/CLIMADA-project/climada_python/pull/1261).
26+
- `ImpactCalc.impact` now raises a clear `ValueError` when the supplied `Hazard` contains no events, instead of failing later inside `np.array_split` with an obscure message [#814](https://github.com/CLIMADA-project/climada_python/issues/814).
2627

2728
### Deprecated
2829
- `Impact.calc_freq_curve()` should not be given the parameter `return_per`. Use the parameter `return_periods` in `Impact.calc_freq_curve().interpolate()` instead.

climada/engine/impact_calc.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -136,6 +136,15 @@ def impact(
136136
climada.entity.exposures.assign_centroids : assign centroids to exposures explicitly
137137
"""
138138
# TODO: consider refactoring, making use of Exposures.hazard_impf
139+
# check that the hazard contains at least one event; an empty hazard
140+
# otherwise produces an obscure error from ``np.array_split`` deeper in
141+
# the calculation (see GH #814).
142+
if self.hazard.size == 0:
143+
raise ValueError(
144+
"Impact calculation not possible. The hazard object contains "
145+
"no events. Please provide a Hazard with at least one event."
146+
)
147+
139148
# check for compatibility of exposures and hazard type
140149
if all(
141150
name not in self.exposures.gdf.columns

climada/engine/test/test_impact_calc.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -147,6 +147,22 @@ def test_error_handling_mismatch_haz_type(self):
147147
"functions found for hazard type TC in impf_set.",
148148
)
149149

150+
def test_error_handling_empty_hazard(self):
151+
"""An empty Hazard must raise a clear ValueError (see GH #814)."""
152+
haz_empty = Hazard("TC")
153+
exp = Exposures()
154+
exp.gdf["impf_TC"] = 1
155+
impf = ImpactFunc(
156+
haz_type="TC",
157+
id=1,
158+
intensity=np.array([0, 20]),
159+
paa=np.array([0, 1]),
160+
mdd=np.array([0, 0.5]),
161+
)
162+
impfset = ImpactFuncSet([impf])
163+
with self.assertRaisesRegex(ValueError, "no events"):
164+
ImpactCalc(exp, impfset, haz_empty).impact()
165+
150166
def test_error_handling_mismatch_impf_ids(self):
151167
"""Test error handling in case impf ids in exposures
152168
does not appear in impf_set"""

0 commit comments

Comments
 (0)