Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ Code freeze date: YYYY-MM-DD

- Fixed asset count in impact logging message [#1195](https://github.com/CLIMADA-project/climada_python/pull/1195).
- `Hazard.from_raster_xarray` now returns a sparse matrix instead of a sparse array [#1261](https://github.com/CLIMADA-project/climada_python/pull/1261).
- `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).

### Deprecated
- `Impact.calc_freq_curve()` should not be given the parameter `return_per`. Use the parameter `return_periods` in `Impact.calc_freq_curve().interpolate()` instead.
Expand Down
9 changes: 9 additions & 0 deletions climada/engine/impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,15 @@ def impact(
climada.entity.exposures.assign_centroids : assign centroids to exposures explicitly
"""
# TODO: consider refactoring, making use of Exposures.hazard_impf
# check that the hazard contains at least one event; an empty hazard
# otherwise produces an obscure error from ``np.array_split`` deeper in
# the calculation (see GH #814).
if self.hazard.size == 0:
raise ValueError(
"Impact calculation not possible. The hazard object contains "
"no events. Please provide a Hazard with at least one event."
)

# check for compatibility of exposures and hazard type
if all(
name not in self.exposures.gdf.columns
Expand Down
16 changes: 16 additions & 0 deletions climada/engine/test/test_impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,22 @@ def test_error_handling_mismatch_haz_type(self):
"functions found for hazard type TC in impf_set.",
)

def test_error_handling_empty_hazard(self):
"""An empty Hazard must raise a clear ValueError (see GH #814)."""
haz_empty = Hazard("TC")
exp = Exposures()
exp.gdf["impf_TC"] = 1
impf = ImpactFunc(
haz_type="TC",
id=1,
intensity=np.array([0, 20]),
paa=np.array([0, 1]),
mdd=np.array([0, 0.5]),
)
impfset = ImpactFuncSet([impf])
with self.assertRaisesRegex(ValueError, "no events"):
ImpactCalc(exp, impfset, haz_empty).impact()

def test_error_handling_mismatch_impf_ids(self):
"""Test error handling in case impf ids in exposures
does not appear in impf_set"""
Expand Down
Loading