Skip to content

Commit b7cc9c9

Browse files
committed
use if_ impact functions if no if_hazard_type present in exposures
1 parent f985e02 commit b7cc9c9

File tree

2 files changed

+51
-6
lines changed

2 files changed

+51
-6
lines changed

climada/engine/impact.py

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -161,10 +161,13 @@ def calc(self, exposures, impact_funcs, hazard, save_mat=False):
161161
# Get damage functions for this hazard
162162
if_haz = INDICATOR_IF + hazard.tag.haz_type
163163
haz_imp = impact_funcs.get_func(hazard.tag.haz_type)
164-
if if_haz not in exposures:
165-
LOGGER.error('Missing exposures column %s. No exposures with impact'\
166-
+' functions for peril %s.', if_haz, hazard.tag.haz_type)
164+
if if_haz not in exposures and INDICATOR_IF not in exposures:
165+
LOGGER.error('Missing exposures impact functions %s.', INDICATOR_IF)
167166
raise ValueError
167+
elif if_haz not in exposures:
168+
LOGGER.info('Missing exposures impact functions for hazard %s. ' +\
169+
'Using impact functions in %s.', if_haz, INDICATOR_IF)
170+
if_haz = INDICATOR_IF
168171

169172
# Check if deductible and cover should be applied
170173
insure_flag = False
@@ -203,7 +206,7 @@ def calc(self, exposures, impact_funcs, hazard, save_mat=False):
203206
self.imp_mat = self.imp_mat.tocsr()
204207

205208
def plot_eai_exposure(self, mask=None, ignore_zero=True,
206-
pop_name=True, buffer_deg=0.0, extend='neither',
209+
pop_name=True, buffer=0.0, extend='neither',
207210
var_name=None, **kwargs):
208211
"""Plot expected annual impact of each exposure.
209212
@@ -212,7 +215,7 @@ def plot_eai_exposure(self, mask=None, ignore_zero=True,
212215
ignore_zero (bool, optional): flag to indicate if zero and negative
213216
values are ignored in plot. Default: False
214217
pop_name (bool, optional): add names of the populated places
215-
buffer_deg (float, optional): border to add to coordinates.
218+
buffer (float, optional): border to add to coordinates.
216219
Default: 1.0.
217220
extend (str, optional): extend border colorbar with arrows.
218221
[ 'neither' | 'both' | 'min' | 'max' ]
@@ -235,7 +238,7 @@ def plot_eai_exposure(self, mask=None, ignore_zero=True,
235238
kwargs['reduce_C_function'] = np.sum
236239
return u_plot.geo_bin_from_array(self.eai_exp[mask][pos_vals], \
237240
self.coord_exp[mask][pos_vals], var_name, title, pop_name, \
238-
buffer_deg, extend, **kwargs)
241+
buffer, extend, **kwargs)
239242

240243
def _exp_impact(self, exp_iimp, exposures, hazard, imp_fun, insure_flag):
241244
"""Compute impact for inpute exposure indexes and impact function.

climada/engine/test/test_impact.py

Lines changed: 42 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -239,6 +239,48 @@ def test_calc_imp_mat_pass(self):
239239
self.assertTrue(np.allclose(np.array(np.sum(np.multiply(impact.imp_mat.todense(),
240240
impact.frequency.reshape(-1, 1)), axis=0)).reshape(-1), impact.eai_exp))
241241

242+
def test_calc_if_pass(self):
243+
""" Execute when no if_HAZ present, but only if_ """
244+
ent = Entity()
245+
ent.read_excel(ENT_DEMO_TODAY)
246+
ent.exposures.rename(columns={'if_TC':'if_'}, inplace=True)
247+
ent.check()
248+
249+
# Read default hazard file
250+
hazard = Hazard('TC')
251+
hazard.read_mat(HAZ_TEST_MAT)
252+
# Create impact object
253+
impact = Impact()
254+
impact.calc(ent.exposures, ent.impact_funcs, hazard)
255+
256+
# Check result
257+
num_events = len(hazard.event_id)
258+
num_exp = ent.exposures.shape[0]
259+
# Check relative errors as well when absolute value gt 1.0e-7
260+
# impact.at_event == EDS.damage in MATLAB
261+
self.assertEqual(num_events, len(impact.at_event))
262+
self.assertEqual(0, impact.at_event[0])
263+
self.assertEqual(0, impact.at_event[int(num_events/2)])
264+
self.assertAlmostEqual(1.472482938320243e+08, impact.at_event[13809])
265+
self.assertEqual(7.076504723057619e+10, impact.at_event[12147])
266+
self.assertEqual(0, impact.at_event[num_events-1])
267+
# impact.eai_exp == EDS.ED_at_centroid in MATLAB
268+
self.assertEqual(num_exp, len(impact.eai_exp))
269+
self.assertAlmostEqual(1.518553670803242e+08, impact.eai_exp[0])
270+
self.assertAlmostEqual(1.373490457046383e+08, \
271+
impact.eai_exp[int(num_exp/2)], 6)
272+
self.assertTrue(np.isclose(1.373490457046383e+08, \
273+
impact.eai_exp[int(num_exp/2)]))
274+
self.assertAlmostEqual(1.066837260150042e+08, \
275+
impact.eai_exp[num_exp-1], 6)
276+
self.assertTrue(np.isclose(1.066837260150042e+08, \
277+
impact.eai_exp[int(num_exp-1)]))
278+
# impact.tot_value == EDS.Value in MATLAB
279+
# impact.aai_agg == EDS.ED in MATLAB
280+
self.assertAlmostEqual(6.570532945599105e+11, impact.tot_value)
281+
self.assertAlmostEqual(6.512201157564421e+09, impact.aai_agg, 5)
282+
self.assertTrue(np.isclose(6.512201157564421e+09, impact.aai_agg))
283+
242284
class TestIO(unittest.TestCase):
243285
''' Test impact calc method.'''
244286

0 commit comments

Comments
 (0)