Skip to content

Commit

Permalink
Merge pull request #691 from CLIMADA-project/feature/improve_impact_c…
Browse files Browse the repository at this point in the history
…alc_error_messages

Improve impact calc error messages
  • Loading branch information
leonie-villiger authored Apr 17, 2023
2 parents 7b1bf32 + 3dac142 commit c7bcf32
Show file tree
Hide file tree
Showing 4 changed files with 54 additions and 1 deletion.
1 change: 1 addition & 0 deletions AUTHORS.md
Original file line number Diff line number Diff line change
Expand Up @@ -28,3 +28,4 @@
* Lukas Riedel
* Raphael Portmann
* Nicolas Colombi
* Leonie Villiger
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Removed:
- Modified the method to disaggregate lines in the `lines_polys_handler` utility module in order to better conserve the total length of all lines on average [#679](https://github.com/CLIMADA-project/climada_python/pull/679).
- Added test for non-default impact function id in the `lines_polys_handler` [#676](https://github.com/CLIMADA-project/climada_python/pull/676)
- The sigmoid and step impact functions now require the user to define the hazard type. [#675](https://github.com/CLIMADA-project/climada_python/pull/675)
- Improved error messages produced by `ImpactCalc.impact()` in case hazard type is not found in exposures/impf_set [#691](https://github.com/CLIMADA-project/climada_python/pull/691)

### Fixed

Expand Down
16 changes: 16 additions & 0 deletions climada/engine/impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,22 @@ def impact(self, save_mat=True, assign_centroids=True,
apply_deductible_to_mat : apply deductible to impact matrix
apply_cover_to_mat : apply cover to impact matrix
"""
# check for compability of exposures and hazard type
if all(name not in self.exposures.gdf.columns for
name in ['if_', f'if_{self.hazard.haz_type}',
'impf_', f'impf_{self.hazard.haz_type}']):
raise AttributeError(
"Impact calculation not possible. No impact functions found "
f"for hazard type {self.hazard.haz_type} in exposures."
)

# check for compability of impact function and hazard type
if not self.impfset.get_func(haz_type=self.hazard.haz_type):
raise AttributeError(
"Impact calculation not possible. No impact functions found "
f"for hazard type {self.hazard.haz_type} in impf_set."
)

impf_col = self.exposures.get_impf_column(self.hazard.haz_type)
exp_gdf = self.minimal_exp_gdf(impf_col, assign_centroids, ignore_cover, ignore_deductible)
if exp_gdf.size == 0:
Expand Down
37 changes: 36 additions & 1 deletion climada/engine/test/test_impact_calc.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@

from climada import CONFIG
from climada.entity.entity_def import Entity
from climada.entity import Exposures, ImpactFuncSet
from climada.entity import Exposures, ImpactFuncSet, ImpactFunc
from climada.hazard.base import Hazard
from climada.engine import ImpactCalc, Impact
from climada.engine.impact_calc import LOGGER as ILOG
Expand Down Expand Up @@ -101,6 +101,41 @@ def test_apply_cover_to_mat(self):
imp.todense(), np.array([[0, 0, 1], [0, 1, 0]])
)

def test_error_handling_mismatch_haz_type(self):
"""Test error handling in case hazard type of hazard
does not appear in impf_set or exposures"""
haz_tc = Hazard('TC')
exp_tc = Exposures()
exp_tc.gdf['impf_TC'] = 1
exp_ws = Exposures()
exp_ws.gdf['impf_WS'] = 2
impf = ImpactFunc()
impf.id = 1
impf.intensity = np.array([0, 20])
impf.paa = np.array([0, 1])
impf.mdd = np.array([0, 0.5])
impf.haz_type = 'TC'
impfset_tc = ImpactFuncSet([impf])
impf.haz_type = 'WS'
impfset_ws = ImpactFuncSet([impf])
impf.haz_type = ''
impfset_undef = ImpactFuncSet([impf])
try:
ImpactCalc(exp_ws, impfset_tc, haz_tc).impact()
except Exception as e:
self.assertEqual(str(e), "Impact calculation not possible. No impact "
"functions found for hazard type TC in exposures.")
try:
ImpactCalc(exp_tc, impfset_ws, haz_tc).impact()
except Exception as e:
self.assertEqual(str(e), "Impact calculation not possible. No impact "
"functions found for hazard type TC in impf_set.")
try:
ImpactCalc(exp_tc, impfset_undef, haz_tc).impact()
except Exception as e:
self.assertEqual(str(e), "Impact calculation not possible. No impact "
"functions found for hazard type TC in impf_set.")

def test_calc_impact_TC_pass(self):
"""Test compute impact"""
icalc = ImpactCalc(ENT.exposures, ENT.impact_funcs, HAZ)
Expand Down

0 comments on commit c7bcf32

Please sign in to comment.