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_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