def test_Altitude_Covariate(self): altitude_element = GeographyBasedElement(self.altitude_datafile, 'lat', 'lon', 'dem', 1.0) altitude_element.load() numpy.testing.assert_array_equal(-89.875, altitude_element.latitude[0, 0]) numpy.testing.assert_array_equal(-84.875, altitude_element.latitude[20, 0]) numpy.testing.assert_array_equal(-89.875, altitude_element.latitude[0, 10]) numpy.testing.assert_array_equal(-84.875, altitude_element.latitude[20, 10]) numpy.testing.assert_array_equal(-179.875, altitude_element.longitude[0, 0]) numpy.testing.assert_array_equal(-179.875, altitude_element.longitude[20, 0]) numpy.testing.assert_array_equal(-177.375, altitude_element.longitude[0, 10]) numpy.testing.assert_array_equal(-177.375, altitude_element.longitude[20, 10]) numpy.testing.assert_array_equal(2779, altitude_element.covariate[0, 0]) numpy.testing.assert_array_equal(2861, altitude_element.covariate[1, 1]) design = altitude_element.element_design( TestRealCovariate.SimulatedObservationStructureAltitude()) altitude_design = design.design_matrix() numpy.testing.assert_almost_equal( numpy.array([2779, 2861, 2820, 2860.344]).reshape(-1, 1), altitude_design.todense())
def test_mini_world_geography_based_mock_data(self): """Testing on a simple mock data file, with mock covariate values""" # GENERATING OBSERVATIONS # Simulated locations: they will exactly sits on the grid points of the covariate datafile locations = numpy.array([[0.0, 0.0], [0.0, 0.5], [0.05, 0.0]]) # Simulated measurements: simple linear relation of type: y = 2*x measurement = numpy.array([2., 2., 2.]) # Simulated errors uncorrelatederror = 0.1 * numpy.ones(measurement.shape) # Simulated inputs simulated_input_loader = SimulatedInputLoader(locations, measurement, uncorrelatederror) # Simulate evaluation of this time index simulated_time_indices = [0] # GENERATING THE MODEL # Local component geography_covariate_element = GeographyBasedElement( self.covariate_file.name, 'lat', 'lon', 'covariate', 1.0) geography_covariate_element.load() geography_based_component = SpatialComponent( ComponentStorage_InMemory( geography_covariate_element, CovariateHyperparameters(-0.5 * numpy.log(10.))), SpatialComponentSolutionStorage_InMemory()) # GENERATING THE ANALYSIS # Analysis system using the specified components, for the Tmean observable analysis_system = AnalysisSystem([geography_based_component], ObservationSource.TMEAN, log=StringIO()) # Update with data analysis_system.update([simulated_input_loader], simulated_time_indices) # Check state vector directly statevector = analysis_system.components[ 0].solutionstorage.partial_state_read(0).ravel() # These are the nodes where observations were put (see SimulatedObservationSource above) # - check they correspond to within 3 times the stated noise level self.assertAlmostEqual(2., statevector[0], delta=0.3) # Also check entire state vector within outer bounds set by obs self.assertTrue(all(statevector < 2.0)) # And check output corresponds too # (evaluate result on output structure same as input) simulated_output_structure = SimulatedObservationStructure( 0, locations, None, None) result = analysis_system.evaluate_expected_value( 'MAP', simulated_output_structure, flag='POINTWISE') numpy.testing.assert_almost_equal( statevector[0] * numpy.ones(len(measurement)), result)
def test_load(self): a = GeographyBasedElement(self.covariate_file.name, 'lat', 'lon', 'covariate', 1.0) a.load() for original_array, file_array, name in zip( [self.latitude, self.longitude, self.covariate], [a.latitude, a.longitude, a.covariate], ['latitude', 'longitude', 'covariate']): numpy.testing.assert_array_equal(original_array, file_array, err_msg="Testing vaues of " + name + " array")
def test_element_prior(self): geography = GeographyBasedElement(self.covariate_file.name, 'lat', 'lon', 'covariate', 1.0) value = 3.333 prior = geography.element_prior(CovariateHyperparameters(value)) self.assertIsInstance(prior, GeographyBasedPrior) # As an example check precision - should be diagonals with exp(-2 x hyperparameter) precision = prior.prior_precision() self.assertEqual(SPARSEFORMAT, precision.getformat()) self.assertEqual(1, precision.nnz) self.assertAlmostEqual(numpy.exp(-2.0 * value), precision[0, 0]) precision_derivative = prior.prior_precision_derivative(0) self.assertEqual(SPARSEFORMAT, precision_derivative.getformat()) self.assertEqual(1, precision_derivative.nnz) self.assertAlmostEqual(-2.0 * precision[0, 0], precision_derivative[0, 0])
def test_element_design(self): geography = GeographyBasedElement(self.covariate_file.name, 'lat', 'lon', 'covariate', 1.0) geography.load() design = geography.element_design(SimulatedObservationStructure()) self.assertTrue(isinstance(design, GeographyBasedElementDesign)) self.assertTrue( isinstance(design.function_class, GeographyBasedCovariateFunction)) for original_array, file_array, name in zip( [self.latitude, self.longitude, self.covariate], [ design.function_class.latitude, design.function_class.longitude, design.function_class.covariate ], ['latitude', 'longitude', 'covariate']): numpy.testing.assert_array_equal(original_array, file_array, err_msg="Testing vaues of " + name + " array") self.assertEqual(design.design_number_of_state_parameters(), GeographyBasedElement.NUMBER_OF_STATE_PARAMETERS)
def test_CoastalInfluence_Covariate(self): coastalinfluence_element = GeographyBasedElement( self.coastal_influence_datafile, 'lat', 'lon', 'coastal_influence', 1.0) coastalinfluence_element.load() numpy.testing.assert_array_equal( -89.875, coastalinfluence_element.latitude[0, 0]) numpy.testing.assert_array_equal( -84.875, coastalinfluence_element.latitude[20, 0]) numpy.testing.assert_array_equal( -89.875, coastalinfluence_element.latitude[0, 10]) numpy.testing.assert_array_equal( -84.875, coastalinfluence_element.latitude[20, 10]) numpy.testing.assert_array_equal( -179.875, coastalinfluence_element.longitude[0, 0]) numpy.testing.assert_array_equal( -179.875, coastalinfluence_element.longitude[20, 0]) numpy.testing.assert_array_equal( -177.375, coastalinfluence_element.longitude[0, 10]) numpy.testing.assert_array_equal( -177.375, coastalinfluence_element.longitude[20, 10]) numpy.testing.assert_array_almost_equal( 2.6666667, coastalinfluence_element.covariate[19, 82]) numpy.testing.assert_array_almost_equal( 100.0, coastalinfluence_element.covariate[20, 83]) design = coastalinfluence_element.element_design( TestRealCovariate.SimulatedObservationStructureCoastalInfluence()) coastalinfluence_design = design.design_matrix() numpy.testing.assert_almost_equal( numpy.array([2.6666667, 14.9382706, 100.0, 55.604321]).reshape(-1, 1), coastalinfluence_design.todense())
def test_mini_world_altitude_with_latitude(self): """Testing using altitude as a covariate""" # GENERATING OBSERVATIONS # Simulated locations: they will exactly sits on the grid points of the covariate datafile DEM = Dataset(self.altitude_datafile) latitude = DEM.variables['lat'][:] longitude = DEM.variables['lon'][:] altitude = DEM.variables['dem'][:] indices = numpy.stack( (numpy.array([1, 3, 5, 7, 8, 9, 10, 11 ]), numpy.array([0, 0, 0, 0, 0, 0, 0, 0])), axis=1) selected_location = [] altitude_observations = [] for couple in indices: selected_location.append([ latitude[couple[0], couple[1]], longitude[couple[0], couple[1]] ]) altitude_observations.append(altitude[couple[0], couple[1]]) DEM.close() locations = numpy.array(selected_location) # Simulated model is y = z + a*cos(2x) + c*cos(4*x) + b*sin(2x) + d*sin(4*x), with z = altitude, x = latitude, a=b=c=d=0 slope = 1e-3 measurement = slope * numpy.array(altitude_observations) # Simulated errors uncorrelatederror = 0.1 * numpy.ones(measurement.shape) # Simulated inputs simulated_input_loader = SimulatedInputLoader(locations, measurement, uncorrelatederror) # Simulate evaluation of this time index simulated_time_indices = [0] # GENERATING THE MODEL # Local component geography_covariate_element = GeographyBasedElement( self.altitude_datafile, 'lat', 'lon', 'dem', 1.0) geography_covariate_element.load() combined_element = CombinationElement( [geography_covariate_element, LatitudeHarmonicsElement()]) combined_hyperparamters = CombinationHyperparameters([ CovariateHyperparameters(-0.5 * numpy.log(10.)), CombinationHyperparameters([ CovariateHyperparameters(-0.5 * numpy.log(p)) for p in [10.0, 10.0, 10.0, 10.0] ]) ]) combined_component = SpatialComponent( ComponentStorage_InMemory(combined_element, combined_hyperparamters), SpatialComponentSolutionStorage_InMemory()) # GENERATING THE ANALYSIS # Analysis system using the specified components, for the Tmean observable analysis_system = AnalysisSystem([combined_component], ObservationSource.TMEAN, log=StringIO()) # Update with data analysis_system.update([simulated_input_loader], simulated_time_indices) # Check state vector directly statevector = analysis_system.components[ 0].solutionstorage.partial_state_read(0).ravel() # These are the nodes where observations were put (see SimulatedObservationSource above) # - check they correspond to within 3 times the stated noise level self.assertAlmostEqual(slope, statevector[0], delta=0.3) self.assertAlmostEqual(0., statevector[1], delta=0.3) self.assertAlmostEqual(0., statevector[2], delta=0.3) self.assertAlmostEqual(0., statevector[3], delta=0.3) self.assertAlmostEqual(0., statevector[4], delta=0.3) # And check output corresponds too # (evaluate result on output structure same as input) simulated_output_structure = SimulatedObservationStructure( 0, locations, None, None) result = analysis_system.evaluate_expected_value( 'MAP', simulated_output_structure, flag='POINTWISE') expected = statevector[0]*numpy.array(altitude_observations)\ + statevector[1]*LatitudeFunction(numpy.cos, 2.0).compute(locations[:,0]).ravel()\ + statevector[2]*LatitudeFunction(numpy.sin, 2.0).compute(locations[:,0]).ravel()\ + statevector[3]*LatitudeFunction(numpy.cos, 4.0).compute(locations[:,0]).ravel()\ + statevector[4]*LatitudeFunction(numpy.sin, 2.0).compute(locations[:,0]).ravel() numpy.testing.assert_almost_equal(expected, result) # test output gridding, pointwise limit outputstructure = OutputRectilinearGridStructure( 2, epoch_plus_days(2), latitudes=numpy.linspace(-60., 60., num=5), longitudes=numpy.linspace(-90., 90, num=10)) pointwise_result = analysis_system.evaluate_expected_value( 'MAP', outputstructure, 'POINTWISE') pointwise_limit_result = analysis_system.evaluate_expected_value( 'MAP', outputstructure, 'GRID_CELL_AREA_AVERAGE', [1, 1], 10) numpy.testing.assert_array_almost_equal(pointwise_result, pointwise_limit_result)
def test_mini_world_altitude(self): """Testing using altitude as a covariate""" # GENERATING OBSERVATIONS # Simulated locations: they will exactly sits on the grid points of the covariate datafile DEM = Dataset(self.altitude_datafile) latitude = DEM.variables['lat'][:] longitude = DEM.variables['lon'][:] altitude = DEM.variables['dem'][:] indices = numpy.stack( (numpy.array([1, 3, 267, 80, 10, 215, 17, 120]), numpy.array([2, 256, 9, 110, 290, 154, 34, 151])), axis=1) selected_location = [] altitude_observations = [] for couple in indices: selected_location.append([ latitude[couple[0], couple[1]], longitude[couple[0], couple[1]] ]) altitude_observations.append(altitude[couple[0], couple[1]]) DEM.close() locations = numpy.array(selected_location) # Simulated measurements: simple linear relation of type: y = PI*x measurement = numpy.pi * numpy.array(altitude_observations) # Simulated errors uncorrelatederror = 0.1 * numpy.ones(measurement.shape) # Simulated inputs simulated_input_loader = SimulatedInputLoader(locations, measurement, uncorrelatederror) # Simulate evaluation of this time index simulated_time_indices = [0] # GENERATING THE MODEL # Local component geography_covariate_element = GeographyBasedElement( self.altitude_datafile, 'lat', 'lon', 'dem', 1.0) geography_covariate_element.load() geography_based_component = SpatialComponent( ComponentStorage_InMemory( geography_covariate_element, CovariateHyperparameters(-0.5 * numpy.log(10.))), SpatialComponentSolutionStorage_InMemory()) # GENERATING THE ANALYSIS # Analysis system using the specified components, for the Tmean observable analysis_system = AnalysisSystem([geography_based_component], ObservationSource.TMEAN, log=StringIO()) # Update with data analysis_system.update([simulated_input_loader], simulated_time_indices) # Check state vector directly statevector = analysis_system.components[ 0].solutionstorage.partial_state_read(0).ravel() # These are the nodes where observations were put (see SimulatedObservationSource above) # - check they correspond to within 3 times the stated noise level self.assertAlmostEqual(numpy.pi, statevector[0], delta=0.3) # Also check entire state vector within outer bounds set by obs self.assertTrue(all(statevector < numpy.pi)) # And check output corresponds too # (evaluate result on output structure same as input) simulated_output_structure = SimulatedObservationStructure( 0, locations, None, None) result = analysis_system.evaluate_expected_value( 'MAP', simulated_output_structure, flag='POINTWISE') numpy.testing.assert_almost_equal( statevector[0] * numpy.array(altitude_observations), result)
def test_init(self): a = GeographyBasedElement('a', 'b', 'c', 'd', 'e') self.assertFalse(a.isnonlinear())