def _calc_impact(self, new_exp, new_ifs, new_haz): """Compute impact and risk transfer of measure implemented over inputs. Parameters: new_exp (Exposures): exposures once measure applied new_ifs (ImpactFuncSet): impact functions once measure applied new_haz (Hazard): hazard once measure applied Returns: Impact, float """ from climada.engine.impact import Impact imp = Impact() imp.calc(new_exp, new_ifs, new_haz) risk_transfer = 0 if self.risk_transf_attach + self.risk_transf_cover > 0: imp_layer = np.minimum(np.maximum(imp.at_event - self.risk_transf_attach, 0), self.risk_transf_cover) risk_transfer = np.sum(imp_layer * imp.frequency) imp.at_event = np.maximum(imp.at_event -imp_layer, 0) imp.aai_agg = np.sum(imp.at_event * imp.frequency) # expected annual impact per exposure no longer valid imp.eai_exp = np.array([]) return imp, risk_transfer
def test_ref_value_insure_pass(self): """Test result against reference value""" # Read demo entity values # Set the entity default file to the demo one ent = Entity() ent.read_excel(ENT_DEMO_TODAY) ent.check() # Read default hazard file hazard = Hazard('TC') hazard.read_mat(HAZ_TEST_MAT) # Create impact object impact = Impact() impact.at_event = np.zeros(hazard.intensity.shape[0]) impact.eai_exp = np.zeros(len(ent.exposures.value)) impact.tot_value = 0 # Assign centroids to exposures ent.exposures.assign_centroids(hazard) # Compute impact for 6th exposure iexp = 5 # Take its impact function imp_id = ent.exposures.if_TC[iexp] imp_fun = ent.impact_funcs.get_func(hazard.tag.haz_type, imp_id) # Compute insure_flag = True impact._exp_impact(np.array([iexp]), ent.exposures, hazard, imp_fun, insure_flag) self.assertEqual(impact.eai_exp.size, ent.exposures.shape[0]) self.assertEqual(impact.at_event.size, hazard.intensity.shape[0]) events_pos = hazard.intensity[:, ent.exposures.centr_TC[iexp]].nonzero( )[0] res_exp = np.zeros((ent.exposures.shape[0])) res_exp[iexp] = np.sum(impact.at_event[events_pos] * hazard.frequency[events_pos]) self.assertTrue(np.array_equal(res_exp, impact.eai_exp)) self.assertEqual(0, impact.at_event[12]) # Check first 3 values self.assertEqual(0, impact.at_event[12]) self.assertEqual(0, impact.at_event[41]) self.assertEqual(1.0626600695059455e+06, impact.at_event[44]) # Check intermediate values self.assertEqual(0, impact.at_event[6281]) self.assertEqual(0, impact.at_event[4998]) self.assertEqual(0, impact.at_event[9527]) self.assertEqual(1.3318063850487845e+08, impact.at_event[7192]) self.assertEqual(4.667108555054083e+06, impact.at_event[8624]) # Check last 3 values self.assertEqual(0, impact.at_event[14349]) self.assertEqual(0, impact.at_event[14347]) self.assertEqual(0, impact.at_event[14309])
def test_write_read_exp_test(self): """Test result against reference value""" # Create impact object num_ev = 5 num_exp = 10 imp_write = Impact() imp_write.tag = { 'exp': Tag('file_exp.p', 'descr exp'), 'haz': TagHaz('TC', 'file_haz.p', 'descr haz'), 'if_set': Tag() } imp_write.event_id = np.arange(num_ev) imp_write.event_name = [ 'event_' + str(num) for num in imp_write.event_id ] imp_write.date = np.ones(num_ev) imp_write.coord_exp = np.zeros((num_exp, 2)) imp_write.coord_exp[:, 0] = 1.5 imp_write.coord_exp[:, 1] = 2.5 imp_write.eai_exp = np.arange(num_exp) * 100 imp_write.at_event = np.arange(num_ev) * 50 imp_write.frequency = np.ones(num_ev) * 0.1 imp_write.tot_value = 1000 imp_write.aai_agg = 1001 imp_write.unit = 'USD' file_name = os.path.join(DATA_FOLDER, 'test.csv') imp_write.write_csv(file_name) imp_read = Impact() imp_read.read_csv(file_name) self.assertTrue(np.array_equal(imp_write.event_id, imp_read.event_id)) self.assertTrue(np.array_equal(imp_write.date, imp_read.date)) self.assertTrue(np.array_equal(imp_write.coord_exp, imp_read.coord_exp)) self.assertTrue(np.array_equal(imp_write.eai_exp, imp_read.eai_exp)) self.assertTrue(np.array_equal(imp_write.at_event, imp_read.at_event)) self.assertTrue(np.array_equal(imp_write.frequency, imp_read.frequency)) self.assertEqual(imp_write.tot_value, imp_read.tot_value) self.assertEqual(imp_write.aai_agg, imp_read.aai_agg) self.assertEqual(imp_write.unit, imp_read.unit) self.assertEqual( 0, len([ i for i, j in zip(imp_write.event_name, imp_read.event_name) if i != j ])) self.assertIsInstance(imp_read.crs, dict)
def test_risk_trans_pass(self): """Test calc_risk_transfer""" # Create impact object imp = Impact() imp.event_id = np.arange(10) imp.event_name = [0, 1, 2, 3, 4, 5, 6, 7, 8, 15] imp.date = np.arange(10) imp.coord_exp = np.array([[1, 2], [2, 3]]) imp.crs = DEF_CRS imp.eai_exp = np.array([1, 2]) imp.at_event = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 15]) imp.frequency = np.ones(10) / 5 imp.tot_value = 10 imp.aai_agg = 100 imp.unit = 'USD' imp.imp_mat = sparse.csr_matrix(np.empty((0, 0))) new_imp, imp_rt = imp.calc_risk_transfer(2, 10) self.assertEqual(new_imp.unit, imp.unit) self.assertEqual(new_imp.tot_value, imp.tot_value) np.testing.assert_array_equal(new_imp.imp_mat.toarray(), imp.imp_mat.toarray()) self.assertEqual(new_imp.event_name, imp.event_name) np.testing.assert_array_almost_equal_nulp(new_imp.event_id, imp.event_id) np.testing.assert_array_almost_equal_nulp(new_imp.date, imp.date) np.testing.assert_array_almost_equal_nulp(new_imp.frequency, imp.frequency) np.testing.assert_array_almost_equal_nulp(new_imp.coord_exp, []) np.testing.assert_array_almost_equal_nulp(new_imp.eai_exp, []) np.testing.assert_array_almost_equal_nulp( new_imp.at_event, [0, 1, 2, 2, 2, 2, 2, 2, 2, 5]) self.assertAlmostEqual(new_imp.aai_agg, 4.0) self.assertEqual(imp_rt.unit, imp.unit) self.assertEqual(imp_rt.tot_value, imp.tot_value) np.testing.assert_array_equal(imp_rt.imp_mat.toarray(), imp.imp_mat.toarray()) self.assertEqual(imp_rt.event_name, imp.event_name) np.testing.assert_array_almost_equal_nulp(imp_rt.event_id, imp.event_id) np.testing.assert_array_almost_equal_nulp(imp_rt.date, imp.date) np.testing.assert_array_almost_equal_nulp(imp_rt.frequency, imp.frequency) np.testing.assert_array_almost_equal_nulp(imp_rt.coord_exp, []) np.testing.assert_array_almost_equal_nulp(imp_rt.eai_exp, []) np.testing.assert_array_almost_equal_nulp( imp_rt.at_event, [0, 0, 0, 1, 2, 3, 4, 5, 6, 10]) self.assertAlmostEqual(imp_rt.aai_agg, 6.2)
def test_risk_trans_pass(self): """ Test calc_risk_transfer """ # Create impact object imp = Impact() imp.event_id = np.arange(10) imp.event_name = [0, 1, 2, 3, 4, 5, 6, 7, 8, 15] imp.date = np.arange(10) imp.coord_exp = np.array([[1, 2], [2, 3]]) imp.crs = DEF_CRS imp.eai_exp = np.array([1, 2]) imp.at_event = np.array([0, 1, 2, 3, 4, 5, 6, 7, 8, 15]) imp.frequency = np.ones(10) / 5 imp.tot_value = 10 imp.aai_agg = 100 imp.unit = 'USD' imp.imp_mat = [] new_imp, imp_rt = imp.calc_risk_transfer(2, 10) self.assertEqual(new_imp.unit, imp.unit) self.assertEqual(new_imp.tot_value, imp.tot_value) self.assertEqual(new_imp.imp_mat, imp.imp_mat) self.assertEqual(new_imp.event_name, imp.event_name) self.assertTrue(np.allclose(new_imp.event_id, imp.event_id)) self.assertTrue(np.allclose(new_imp.date, imp.date)) self.assertTrue(np.allclose(new_imp.frequency, imp.frequency)) self.assertTrue(np.allclose(new_imp.coord_exp, np.array([]))) self.assertTrue(np.allclose(new_imp.eai_exp, np.array([]))) self.assertTrue( np.allclose(new_imp.at_event, np.array([0, 1, 2, 2, 2, 2, 2, 2, 2, 5]))) self.assertAlmostEqual(new_imp.aai_agg, 4.0) self.assertEqual(imp_rt.unit, imp.unit) self.assertEqual(imp_rt.tot_value, imp.tot_value) self.assertEqual(imp_rt.imp_mat, imp.imp_mat) self.assertEqual(imp_rt.event_name, imp.event_name) self.assertTrue(np.allclose(imp_rt.event_id, imp.event_id)) self.assertTrue(np.allclose(imp_rt.date, imp.date)) self.assertTrue(np.allclose(imp_rt.frequency, imp.frequency)) self.assertTrue(np.allclose(imp_rt.coord_exp, np.array([]))) self.assertTrue(np.allclose(imp_rt.eai_exp, np.array([]))) self.assertTrue( np.allclose(imp_rt.at_event, np.array([0, 0, 0, 1, 2, 3, 4, 5, 6, 10]))) self.assertAlmostEqual(imp_rt.aai_agg, 6.2)
def dummy_impact(): imp = Impact() imp.event_id = np.arange(6) imp.event_name = [0, 1, 'two', 'three', 30, 31] imp.date = np.arange(6) imp.coord_exp = np.array([[1, 2], [1.5, 2.5]]) imp.crs = DEF_CRS imp.eai_exp = np.array([7.2, 7.2]) imp.at_event = np.array([0, 2, 4, 6, 60, 62]) imp.frequency = np.array([1 / 6, 1 / 6, 1, 1, 1 / 30, 1 / 30]) imp.tot_value = 7 imp.aai_agg = 14.4 imp.unit = 'USD' imp.imp_mat = sparse.csr_matrix( np.array([[0, 0], [1, 1], [2, 2], [3, 3], [30, 30], [31, 31]])) return imp