示例#1
0
    def test_write_imp_mat(self):
        """ Test write_excel_imp_mat function """
        impact = Impact()
        impact.imp_mat = sparse.lil_matrix(np.zeros((5, 4)))
        impact.imp_mat[0, :] = np.arange(4)
        impact.imp_mat[1, :] = np.arange(4)*2
        impact.imp_mat[2, :] = np.arange(4)*3
        impact.imp_mat[3, :] = np.arange(4)*4
        impact.imp_mat[4, :] = np.arange(4)*5
        impact.imp_mat = impact.imp_mat.tocsr()

        file_name = os.path.join(DATA_FOLDER, 'test_imp_mat')
        impact.write_sparse_csr(file_name)
        read_imp_mat = Impact().read_sparse_csr(file_name+'.npz')
        for irow in range(5):
            self.assertTrue(np.array_equal(np.array(read_imp_mat[irow, :].todense()).reshape(-1),
                np.array(impact.imp_mat[irow, :].todense()).reshape(-1)))
示例#2
0
    def test_write_imp_mat(self):
        """Test write_excel_imp_mat function"""
        impact = Impact()
        impact.imp_mat = np.zeros((5, 4))
        impact.imp_mat[0, :] = np.arange(4)
        impact.imp_mat[1, :] = np.arange(4) * 2
        impact.imp_mat[2, :] = np.arange(4) * 3
        impact.imp_mat[3, :] = np.arange(4) * 4
        impact.imp_mat[4, :] = np.arange(4) * 5
        impact.imp_mat = sparse.csr_matrix(impact.imp_mat)

        file_name = DATA_FOLDER.joinpath('test_imp_mat')
        impact.write_sparse_csr(file_name)
        read_imp_mat = Impact().read_sparse_csr(f'{file_name}.npz')
        for irow in range(5):
            np.testing.assert_array_equal(
                read_imp_mat[irow, :].toarray(), impact.imp_mat[irow, :].toarray())
示例#3
0
    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)
示例#5
0
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