Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve impact calc error messages #691

Merged
merged 6 commits into from
Apr 17, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
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 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
40 changes: 39 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,44 @@ 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'] = None
leonie-villiger marked this conversation as resolved.
Show resolved Hide resolved
exp_ws = Exposures()
exp_ws.gdf['impf_WS'] = None
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()
impfset_tc.append(impf)
leonie-villiger marked this conversation as resolved.
Show resolved Hide resolved
impf.haz_type = 'WS'
impfset_ws = ImpactFuncSet()
impfset_ws.append(impf)
leonie-villiger marked this conversation as resolved.
Show resolved Hide resolved
impf.haz_type = ''
impfset_undef = ImpactFuncSet()
impfset_undef.append(impf)
leonie-villiger marked this conversation as resolved.
Show resolved Hide resolved
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