def test_impact_year_set_sum(self): """Test result against reference value with given events """ imp = Impact() imp.frequency = np.ones(10) * 6.211180124223603e-04 imp.at_event = np.zeros(10) imp.at_event[0] = 0 imp.at_event[1] = 0.400665463736549e9 imp.at_event[2] = 3.150330960044466e9 imp.at_event[3] = 3.715826406781887e9 imp.at_event[4] = 2.900244271902339e9 imp.at_event[5] = 0.778570745161971e9 imp.at_event[6] = 0.698736262566472e9 imp.at_event[7] = 0.381063674256423e9 imp.at_event[8] = 0.569142464157450e9 imp.at_event[9] = 0.467572545849132e9 imp.unit = 'USD' imp.date = np.array([732801, 716160, 718313, 712468, 732802, \ 729285, 732931, 715419, 722404, 718351]) iys_all = imp.calc_impact_year_set() iys = imp.calc_impact_year_set(all_years=False) self.assertEqual(np.around(sum([iys[year] for year in iys])), \ np.around(sum(imp.at_event))) self.assertEqual(sum([iys[year] for year in iys]), \ sum([iys_all[year] for year in iys_all])) self.assertEqual(len(iys), 7) self.assertEqual(len(iys_all), 57) self.assertIn(1951 and 1959 and 2007, iys_all) self.assertTrue(iys_all[1959]>0) self.assertAlmostEqual(3598980534.468811, iys_all[2007]) self.assertEqual(iys[1978], iys_all[1978]) self.assertAlmostEqual(iys[1951], imp.at_event[3])
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