Exemplo n.º 1
0
    def test_design_function(self):

        spde = SphereMeshSPDE(level=1, sparse_format=SPARSEFORMAT)
        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            spde,
            n_harmonics=3,
            include_local_mean=True)

        # Initialise zero state vector
        state_vector = numpy.zeros((294, 1))

        # Get indices of nonzero design elements
        A_simulated = spde.build_A(
            TestSeasonalElement.SimulatedObservationStructure(
            ).location_polar_coordinates())
        observation_indices, vertex_indices = A_simulated.sorted_indices(
        ).nonzero()

        # This should select observation 1
        state_vector[vertex_indices[3:6], 0] = 1.0

        # And this will add 0.309016994375 to it
        state_vector[vertex_indices[3:6] + 84, 0] = 1.0

        # Apply state vector
        y = design.design_function(state_vector)

        # Check observation 1 selected with appropriate sum
        numpy.testing.assert_almost_equal(
            y, numpy.array([[0.0], [1.309016994375]]))
Exemplo n.º 2
0
    def test_design_number_of_state_parameters(self):

        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            SphereMeshSPDE(level=1),
            n_harmonics=3,
            include_local_mean=True)
        self.assertEqual(294, design.design_number_of_state_parameters())
Exemplo n.º 3
0
    def test_isnonlinear(self):

        spde = SphereMeshSPDE(level=1, sparse_format=SPARSEFORMAT)
        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            spde,
            n_harmonics=3,
            include_local_mean=True)
        self.assertFalse(design.isnonlinear())
Exemplo n.º 4
0
    def test_design_jacobian(self):

        spde = SphereMeshSPDE(level=2, sparse_format=SPARSEFORMAT)
        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            spde,
            n_harmonics=2,
            include_local_mean=False)
        A = design.design_matrix()
        J = design.design_jacobian(numpy.array([]))
        self.assertEqual(SPARSEFORMAT, J.getformat())
        numpy.testing.assert_almost_equal(J.todense(), A.todense())
Exemplo n.º 5
0
    def test_design_matrix(self):

        spde = SphereMeshSPDE(level=1, sparse_format=SPARSEFORMAT)
        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            spde,
            n_harmonics=3,
            include_local_mean=True)
        A = design.design_matrix()

        # Check shape and sparse format
        self.assertEqual((2, 294), A.shape)
        self.assertEqual(SPARSEFORMAT, A.getformat())

        # Check that kronecker expansion worked as expected
        numpy.testing.assert_almost_equal(
            A[:, 0:42].todense() * 0.951056516295, A[:, 42:84].todense())
        numpy.testing.assert_almost_equal(
            A[:, 0:42].todense() * -0.809016994375, A[:, 252:294].todense())
Exemplo n.º 6
0
    def test_build_A_time(self):

        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            SphereMeshSPDE(level=2),
            n_harmonics=3,
            include_local_mean=True)
        A = design.build_A_time()

        # Simulated datetime(1977, 3, 15) is precisely 0.2 through the year (which is 72 degrees of 360 degree cycle)
        # Should give coefficients:
        #   1.0
        #   sin(72 degrees)
        #   cos(72 degrees)
        #   sin(144 degrees)
        #   cos(144 degrees)
        #   sin(216 degrees)
        #   cos(216 degrees)
        numpy.testing.assert_almost_equal(A, [[
            1.0, 0.951056516295, 0.309016994375, 0.587785252292,
            -0.809016994375, -0.587785252292, -0.809016994375
        ]])
Exemplo n.º 7
0
    def test_init(self):

        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            SphereMeshSPDE(level=2),
            n_harmonics=5,
            include_local_mean=True)
        self.assertTrue(
            isinstance(design.observationstructure,
                       TestSeasonalElement.SimulatedObservationStructure))
        self.assertEqual(2, design.spde.triangulation.level)
        self.assertEqual(5, design.n_harmonics)
        self.assertEqual(True, design.include_local_mean)
Exemplo n.º 8
0
    def test_build_A_space(self):

        spde = SphereMeshSPDE(level=1, sparse_format=SPARSEFORMAT)
        design = SeasonalElementDesign(
            TestSeasonalElement.SimulatedObservationStructure(),
            spde,
            n_harmonics=3,
            include_local_mean=True)
        A = design.build_A_space()

        # Check sparse format
        self.assertEqual(SPARSEFORMAT, A.getformat())

        # Shape should be number of obs x number of vertices (basis functions)
        self.assertEqual((2, 42), A.shape)

        # Two triangles means 6 points are non-zero
        self.assertEqual(6, len(A.nonzero()[0]))

        # Get and check indices of nonzero design elements
        observation_indices, vertex_indices = A.sorted_indices().nonzero()
        numpy.testing.assert_equal(observation_indices, [0, 0, 0, 1, 1, 1])
        self.assertEqual((6, ), vertex_indices.shape)

        # Vertices of observations 0 and 1
        # (one row per vertex, one column per coordinate)
        vertices0 = spde.triangulation.points[vertex_indices[0:3], :]
        vertices1 = spde.triangulation.points[vertex_indices[3:6], :]

        # Multiply vertices by the weights from A and sum to get cartesian locations
        testpoint0 = A[0, vertex_indices[0:3]] * vertices0
        testpoint1 = A[1, vertex_indices[3:6]] * vertices1

        # Check results correspond to original polar coordinates
        numpy.testing.assert_almost_equal(cartesian_to_polar2d(testpoint0),
                                          [[15.0, -7.0]])
        numpy.testing.assert_almost_equal(cartesian_to_polar2d(testpoint1),
                                          [[5.0, 100.0]])