def test_design_jacobian(self): design = LocalDesign(TestLocalElement.SimulatedObservationStructure(), SphereMeshSPDE(level=1)) numpy.testing.assert_equal( design.design_matrix().todense(), design.design_jacobian(currentstate=None).todense())
def test_init(self): obs = TestCombinationElement.SimulatedObservationStructure() spde = SphereMeshSPDE(0) design = CombinationDesign( [GrandMeanDesign(2), LocalDesign(obs, spde)]) self.assertEqual(2, len(design.designlist)) self.assertTrue(isinstance(design.designlist[0], GrandMeanDesign)) self.assertTrue(isinstance(design.designlist[1], LocalDesign))
def test_design_function(self): # Build design design = LocalDesign(TestLocalElement.SimulatedObservationStructure(), SphereMeshSPDE(level=1)) # Indices from design matrix indicate vertex weights that produce observation locations observation_indices, vertex_indices = design.design_matrix( ).sorted_indices().nonzero() # A vector that is 176.0 # except in location 0 where it is 82.0 # and in location 1 where it is -8.888 statevector = numpy.tile(176.0, (42, 1)) statevector[vertex_indices[0:3]] = 272.0 statevector[vertex_indices[3:6]] = -8.888 # Evaluate this - should get the two numbers back numpy.testing.assert_almost_equal(design.design_function(statevector), [[272.0], [-8.888]])
def test_design_matrix(self): spde = SphereMeshSPDE(level=1) design = LocalDesign(TestLocalElement.SimulatedObservationStructure(), spde) A = design.design_matrix() # 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]])
def test_element_states(self): obs = TestCombinationElement.SimulatedObservationStructure() spde = SphereMeshSPDE(0) design = CombinationDesign( [GrandMeanDesign(obs), LocalDesign(obs, spde)]) states = design.element_states( numpy.array([ 270.0, 1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0 ])) self.assertTrue(isinstance(states, list)) self.assertEqual(2, len(states)) numpy.testing.assert_equal(states[0], [270.0]) numpy.testing.assert_equal( states[1], [1.0, 2.0, 3.0, 4.0, 5.0, 6.0, 7.0, 8.0, 9.0, 10.0, 11.0, 12.0])
def test_design_jacobian(self): obs = TestCombinationElement.SimulatedObservationStructure() spde = SphereMeshSPDE(1) design = CombinationDesign( [GrandMeanDesign(2), LocalDesign(obs, spde)]) # Make a state vector full of a test value 561 which should never actually get accessed state_vector = numpy.tile(561.0, (47, 1)) J = design.design_jacobian(state_vector) # A-matrix for comparison A = spde.build_A(obs.location_polar_coordinates()) # Should have ones at start for global mean J_expected = numpy.hstack([numpy.array([[1.0], [1.0]]), A.todense()]) # Check as exepcted self.assertEqual(SPARSEFORMAT, J.getformat()) numpy.testing.assert_equal(J_expected, J.todense())
def test_design_function(self): obs = TestCombinationElement.SimulatedObservationStructure() spde = SphereMeshSPDE(1) design = CombinationDesign( [GrandMeanDesign(2), LocalDesign(obs, spde)]) A = spde.build_A(obs.location_polar_coordinates()) observation_indices, vertex_indices = A.sorted_indices().nonzero() # Make a state vector full of a test value 561 which should never actually get accessed state_vector = numpy.tile(561.0, (47, 1)) # Set the grand mean to 20.0 state_vector[0, 0] = 20.0 # Choose some numbers for observations of interest state_vector[(vertex_indices[0:3] + 1), 0] = 43.5 state_vector[(vertex_indices[3:6] + 1), 0] = -27.0 # Should end up with (20.0 + 43.5) and (20.0 - 27.0) y = design.design_function(state_vector) numpy.testing.assert_almost_equal(y, [[63.5], [-7.0]])
def test_design_number_of_state_parameters(self): spde = SphereMeshSPDE(level=1) design = LocalDesign(TestLocalElement.SimulatedObservationStructure(), spde) self.assertEqual(42, design.design_number_of_state_parameters())
def test_isnonlinear(self): design = LocalDesign(TestLocalElement.SimulatedObservationStructure(), SphereMeshSPDE(level=1)) self.assertFalse(design.isnonlinear())