def __init__(self, storage_climatology, storage_large_scale, storage_local, covariates_descriptor, insitu_biases=False, breakpoints_file=None, global_biases=False, global_biases_group_list=[], compute_uncertainties=False, method='EXACT', compute_sample=False, sample_size=definitions.GLOBAL_SAMPLE_SHAPE[3], compute_prior_sample=False): super(AnalysisSystem_EUSTACE, self).__init__(components=[ SpaceTimeComponent( ClimatologyDefinition(covariates_descriptor), storage_climatology, True, compute_uncertainties, method, compute_sample, sample_size, compute_prior_sample), SpaceTimeComponent( LargeScaleDefinition(insitu_biases, breakpoints_file), storage_large_scale, True, compute_uncertainties, method, compute_sample, sample_size, compute_prior_sample), SpatialComponent( LocalDefinition(global_biases, global_biases_group_list), storage_local, compute_uncertainties, method, compute_sample, sample_size, compute_prior_sample) ], observable=ObservationSource.TMEAN)
def run_prior_solve(): componentstorage = TestComponentDefinition() solutionstorage = SpaceTimeComponentSolutionStorage_InMemory() printstats=True compute_uncertainties = False method='EXACT' compute_sample=False sample_size=1 compute_prior_sample=True component = SpaceTimeComponent(componentstorage, solutionstorage, printstats, compute_uncertainties, method, compute_sample, sample_size, compute_prior_sample) component_solution = component.component_solution() component_solution.update()
def test_process_observations_compute_sample(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # number_of_samples = 200 c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), compute_sample=True, sample_size=number_of_samples) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertTrue(s.compute_sample) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() numpy.random.seed(0) s.update() # In this case we are considering the last iteration of model solving, hence sample should have been stored computed_sample = s.solutionstorage.state_sample self.assertEqual((2, number_of_samples), computed_sample.shape) numpy.random.seed(0) variate = scipy.random.normal(0.0, 1.0, (2, number_of_samples)) expected_posterior_precision = numpy.array([[13.25, -16.5], [-16.5, 80.65]]) # check result numpy.testing.assert_almost_equal( numpy.dot(variate.T, variate), numpy.dot(computed_sample.T, numpy.dot(expected_posterior_precision, computed_sample)))
def __init__(self): # Build analysis system by referencing the global variables # In distributed computing we would construct these using classes that load from file super(AnalysisSystem_HadCRUT4_InMemory, self).__init__(components=[ SpaceTimeComponent(HadCRUT4_InMemory.definition_climatology, HadCRUT4_InMemory.solution_climatology), SpaceTimeComponent(HadCRUT4_InMemory.definition_large_scale, HadCRUT4_InMemory.solution_large_scale), SpatialComponent(HadCRUT4_InMemory.definition_local, HadCRUT4_InMemory.solution_local) ], observable=ObservationSource.TMEAN)
def __init__(self, storage_climatology, storage_large_scale, storage_local_bias, storage_region_spde, covariates_descriptor, insitu_biases=False, breakpoints_file=None, global_biases=False, global_biases_group_list=[], compute_uncertainties=False, method='EXACT', compute_sample=False, sample_size=definitions.GLOBAL_SAMPLE_SHAPE[3], neighbourhood_level=0, region_index=0, regionspec='LocalSubRegion'): # initialise the OptimizationSystem super(RegionOptimizationSystem_EUSTACE, self).__init__(components=[ SpaceTimeComponent( ClimatologyDefinition(covariates_descriptor), storage_climatology, True, compute_uncertainties, method, compute_sample, sample_size), SpaceTimeComponent( LargeScaleDefinition(insitu_biases, breakpoints_file), storage_large_scale, True, compute_uncertainties, method, compute_sample, sample_size), SpatialComponent( PureBiasComponentDefinition(global_biases, global_biases_group_list), storage_local_bias, compute_uncertainties, method, compute_sample, sample_size), DelayedSpatialComponent( LocalViewDefinition(neighbourhood_level, region_index, regionspec), storage_region_spde, compute_uncertainties, method, compute_sample, sample_size) ], observable=ObservationSource.TMEAN)
def main(): print 'Advanced standard example using a few days of EUSTACE data' parser = argparse.ArgumentParser(description='Advanced standard example using a few days of EUSTACE data') parser.add_argument('outpath', help='directory where the output should be redirected') parser.add_argument('--json_descriptor', default = None, help='a json descriptor containing the covariates to include in the climatology model') parser.add_argument('--land_biases', action='store_true', help='include insitu land homogenization bias terms') parser.add_argument('--global_biases', action='store_true', help='include global satellite bias terms') parser.add_argument('--n_iterations', type=int, default=5, help='number of solving iterations') args = parser.parse_args() # Input data path basepath = os.path.join('/work/scratch/eustace/rawbinary3') # Days to process time_indices = range(int(days_since_epoch(datetime(2006, 2, 1))), int(days_since_epoch(datetime(2006, 2, 2)))) # Sources to use sources = [ 'surfaceairmodel_land', 'surfaceairmodel_ocean', 'surfaceairmodel_ice', 'insitu_land', 'insitu_ocean' ] #SETUP # setup for the seasonal core: climatology covariates setup read from file seasonal_setup = {'n_triangulation_divisions':5, 'n_harmonics':4, 'n_spatial_components':6, 'amplitude':2., 'space_length_scale':5., # length scale in units of degrees } grandmean_amplitude = 15.0 # setup for the large scale component spacetime_setup = {'n_triangulation_divisions':2, 'alpha':2, 'starttime':0, 'endtime':10., 'n_nodes':2, 'overlap_factor':2.5, 'H':1, 'amplitude':1., 'space_lenght_scale':15.0, # length scale in units of degrees 'time_length_scale':15.0 # length scal in units of days } bias_amplitude = .9 # setup for the local component local_setup = {'n_triangulation_divisions':6, 'amplitude':2., 'space_length_scale':2. # length scale in units of degrees } globalbias_amplitude = 15.0 # CLIMATOLOGY COMPONENT: combining the seasonal core along with latitude harmonics, altitude and coastal effects if args.json_descriptor is not None: loader = LoadCovariateElement(args.json_descriptor) loader.check_keys() covariate_elements, covariate_hyperparameters = loader.load_covariates_and_hyperparameters() print('The following fields have been added as covariates of the climatology model') print(loader.data.keys()) else: covariate_elements, covariate_hyperparameters = [], [] climatology_element = CombinationElement( [SeasonalElement(n_triangulation_divisions=seasonal_setup['n_triangulation_divisions'], n_harmonics=seasonal_setup['n_harmonics'], include_local_mean=True), GrandMeanElement()]+covariate_elements) climatology_hyperparameters = CombinationHyperparameters( [SeasonalHyperparameters(n_spatial_components=seasonal_setup['n_spatial_components'], common_log_sigma=numpy.log(seasonal_setup['amplitude']), common_log_rho=numpy.log(numpy.radians(seasonal_setup['space_length_scale']))), CovariateHyperparameters(numpy.log(grandmean_amplitude))] + covariate_hyperparameters ) climatology_component = SpaceTimeComponent(ComponentStorage_InMemory(climatology_element, climatology_hyperparameters), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED', compute_sample=True, sample_size=definitions.GLOBAL_SAMPLE_SHAPE[3]) # LARGE SCALE (kronecker product) COMPONENT: combining large scale trends with bias terms accounting for homogeneization effects if args.land_biases: bias_element, bias_hyperparameters = [InsituLandBiasElement(BREAKPOINTS_FILE)], [CovariateHyperparameters(numpy.log(bias_amplitude))] print('Adding bias terms for insitu land homogenization') else: bias_element, bias_hyperparameters = [], [] large_scale_element = CombinationElement( [SpaceTimeKroneckerElement(n_triangulation_divisions=spacetime_setup['n_triangulation_divisions'], alpha=spacetime_setup['alpha'], starttime=spacetime_setup['starttime'], endtime=spacetime_setup['endtime'], n_nodes=spacetime_setup['n_nodes'], overlap_factor=spacetime_setup['overlap_factor'], H=spacetime_setup['H'])] + bias_element) large_scale_hyperparameters = CombinationHyperparameters( [SpaceTimeSPDEHyperparameters(space_log_sigma=numpy.log(spacetime_setup['amplitude']), space_log_rho=numpy.log(numpy.radians(spacetime_setup['space_lenght_scale'])), time_log_rho=numpy.log(spacetime_setup['time_length_scale']))] + bias_hyperparameters) large_scale_component = SpaceTimeComponent(ComponentStorage_InMemory(large_scale_element, large_scale_hyperparameters), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED', compute_sample=True, sample_size=definitions.GLOBAL_SAMPLE_SHAPE[3]) # LOCAL COMPONENT: combining local scale variations with global satellite bias terms if args.global_biases: bias_elements = [BiasElement(groupname, 1) for groupname in GLOBAL_BIASES_GROUP_LIST] bias_hyperparameters = [CovariateHyperparameters(numpy.log(globalbias_amplitude)) for index in range(len(GLOBAL_BIASES_GROUP_LIST))] print('Adding global bias terms for all the surfaces') else: bias_elements, bias_hyperparameters = [], [] local_scale_element = CombinationElement([LocalElement(n_triangulation_divisions=local_setup['n_triangulation_divisions'])] + bias_elements) local_scale_hyperparameters = CombinationHyperparameters([LocalHyperparameters(log_sigma=numpy.log(local_setup['amplitude']), log_rho=numpy.log(numpy.radians(local_setup['space_length_scale'])))] + bias_hyperparameters) local_component = SpatialComponent(ComponentStorage_InMemory(local_scale_element, local_scale_hyperparameters), SpatialComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED', compute_sample=True, sample_size=definitions.GLOBAL_SAMPLE_SHAPE[3]) # Analysis system using the specified components, for the Tmean observable print 'Analysing inputs' analysis_system = AnalysisSystem( [ climatology_component, large_scale_component, local_component ], ObservationSource.TMEAN) # Object to load raw binary inputs at time indices inputloaders = [ AnalysisSystemInputLoaderRawBinary_Sources(basepath, source, time_indices) for source in sources ] for iteration in range(args.n_iterations): message = 'Iteration {}'.format(iteration) print(message) # Update with data analysis_system.update(inputloaders, time_indices) print 'Computing outputs' # Produce an output for each time index for time_index in time_indices: # Get date for output outputdate = inputloaders[0].datetime_at_time_index(time_index) print 'Evaluating output grid: ', outputdate #Configure output grid outputstructure = OutputRectilinearGridStructure( time_index, outputdate, latitudes=numpy.linspace(-90.+definitions.GLOBAL_FIELD_RESOLUTION/2., 90.-definitions.GLOBAL_FIELD_RESOLUTION/2., num=definitions.GLOBAL_FIELD_SHAPE[1]), longitudes=numpy.linspace(-180.+definitions.GLOBAL_FIELD_RESOLUTION/2., 180.-definitions.GLOBAL_FIELD_RESOLUTION/2., num=definitions.GLOBAL_FIELD_SHAPE[2])) # Evaluate expected value at these locations for field in ['MAP', 'post_STD']: print 'Evaluating: ',field result_expected_value = analysis_system.evaluate_expected_value('MAP', outputstructure, 'GRID_CELL_AREA_AVERAGE', [1,1], 1000) result_expected_uncertainties = analysis_system.evaluate_expected_value('post_STD', outputstructure, 'GRID_CELL_AREA_AVERAGE', [1,1], 1000) print 'Evaluating: climatology fraction' climatology_fraction = analysis_system.evaluate_climatology_fraction(outputstructure, [1,1], 1000) print 'Evaluating: the sample' sample = analysis_system.evaluate_projected_sample(outputstructure) # Make output filename pathname = 'eustace_example_output_{0:04d}{1:02d}{2:02d}.nc'.format(outputdate.year, outputdate.month, outputdate.day) pathname = os.path.join(args.outpath, pathname) print 'Saving: ', pathname # Save results filebuilder = FileBuilderGlobalField( pathname, time_index, 'Infilling Example', 'UNVERSIONED', definitions.TAS.name, '', 'Example data only', 'eustace.analysis.advanced_standard.examples.example_eustace_few_days', '') filebuilder.add_global_field(definitions.TAS, result_expected_value.reshape(definitions.GLOBAL_FIELD_SHAPE)) filebuilder.add_global_field(definitions.TASUNCERTAINTY, result_expected_uncertainties.reshape(definitions.GLOBAL_FIELD_SHAPE)) filebuilder.add_global_field(definitions.TAS_CLIMATOLOGY_FRACTION, climatology_fraction.reshape(definitions.GLOBAL_FIELD_SHAPE)) for index in range(definitions.GLOBAL_SAMPLE_SHAPE[3]): variable = copy.deepcopy(definitions.TASENSEMBLE) variable.name = variable.name + '_' + str(index) selected_sample = sample[:,index].ravel()+result_expected_value filebuilder.add_global_field(variable, selected_sample.reshape(definitions.GLOBAL_FIELD_SHAPE)) filebuilder.save_and_close() print 'Complete'
def test_mini_world_large_and_local(self): # Use a number of time steps number_of_simulated_time_steps = 30 # Large-scale spatial variability simulated_large_variation = 10.0 # Local variability simulated_local_variation = 1.0 # Iterations to use number_of_solution_iterations = 5 # Build system # Large-scale factor element_large = SpaceTimeKroneckerElement( n_triangulation_divisions=1, alpha=2, starttime=0, endtime=number_of_simulated_time_steps + 1, n_nodes=number_of_simulated_time_steps + 2, overlap_factor=2.5, H=1) initial_hyperparameters_large = SpaceTimeSPDEHyperparameters( space_log_sigma=0.0, space_log_rho=numpy.log(numpy.radians(5.0)), time_log_rho=numpy.log(1.0 / 365.0)) component_large = SpaceTimeComponent( ComponentStorage_InMemory(element_large, initial_hyperparameters_large), SpaceTimeComponentSolutionStorage_InMemory()) # And a local process component_local = SpatialComponent( ComponentStorage_InMemory( LocalElement(n_triangulation_divisions=3), LocalHyperparameters(log_sigma=0.0, log_rho=numpy.log(numpy.radians(5.0)))), SpatialComponentSolutionStorage_InMemory()) analysis_system = AnalysisSystem([component_large, component_local], ObservationSource.TMEAN, log=StringIO()) # analysis_system = AnalysisSystem([ component_large ], ObservationSource.TMEAN) # analysis_system = AnalysisSystem([ component_local ], ObservationSource.TMEAN) # use fixed locations from icosahedron fixed_locations = cartesian_to_polar2d( MeshIcosahedronSubdivision.build(3).points) # random measurement at each location numpy.random.seed(8976) field_basis = simulated_large_variation * numpy.random.randn( fixed_locations.shape[0]) # some time function that varies over a year time_basis = numpy.cos( numpy.linspace(0.1, 1.75 * numpy.pi, number_of_simulated_time_steps)) # kronecker product of the two large_scale_process = numpy.kron(field_basis, numpy.expand_dims(time_basis, 1)) # Random local changes where mean change at each time is zero # local_process = simulated_local_variation * numpy.random.randn(large_scale_process.shape[0], large_scale_process.shape[1]) # local_process -= numpy.tile(local_process.mean(axis=1), (local_process.shape[1], 1)).T local_process = numpy.zeros(large_scale_process.shape) somefield = simulated_local_variation * numpy.random.randn( 1, large_scale_process.shape[1]) somefield -= somefield.ravel().mean() local_process[10, :] = somefield local_process[11, :] = -somefield # Add the two processes measurement = large_scale_process + local_process # Simulated inputs simulated_input_loader = SimulatedInputLoader(fixed_locations, measurement, 0.001) # Simulate evaluation of this time index simulated_time_indices = range(number_of_simulated_time_steps) # All systems linear so single update should be ok analysis_system.update([simulated_input_loader], simulated_time_indices) # Get all results result = numpy.zeros(measurement.shape) for t in range(number_of_simulated_time_steps): result[t, :] = analysis_system.evaluate_expected_value( 'MAP', SimulatedObservationStructure(t, fixed_locations, None, None), flag='POINTWISE') disparity_large_scale = (numpy.abs(result - large_scale_process)).ravel().max() # print 'large scale disparity: ', disparity_large_scale disparity_overall = (numpy.abs(result - measurement)).ravel().max() # print 'overall disparity: ', disparity_overall numpy.testing.assert_almost_equal(result, measurement, decimal=4) self.assertTrue(disparity_overall < 1E-4)
def test_mini_world_noiseless(self): number_of_simulated_time_steps = 1 # Build system element = SeasonalElement(n_triangulation_divisions=3, n_harmonics=5, include_local_mean=True) hyperparameters = SeasonalHyperparameters(n_spatial_components=6, common_log_sigma=0.0, common_log_rho=0.0) component = SpaceTimeComponent( ComponentStorage_InMemory(element, hyperparameters), SpaceTimeComponentSolutionStorage_InMemory()) analysis_system = AnalysisSystem([component], ObservationSource.TMEAN, log=StringIO()) # use fixed locations from icosahedron fixed_locations = cartesian_to_polar2d( MeshIcosahedronSubdivision.build(3).points) # random measurement at each location numpy.random.seed(8976) field_basis = numpy.random.randn(fixed_locations.shape[0]) #print(field_basis.shape) #time_basis = numpy.array(harmonics_list) # some time function that varies over a year #decimal_years = numpy.array([datetime_to_decimal_year(epoch_plus_days(step)) for step in range(number_of_simulated_time_steps)]) time_basis = numpy.cos( numpy.linspace(0.1, 1.75 * numpy.pi, number_of_simulated_time_steps)) # kronecker product of the two #print(numpy.expand_dims(time_basis, 1)) measurement = numpy.kron(field_basis, numpy.expand_dims( time_basis, 1)) #numpy.expand_dims(time_basis, 1)) #print(measurement.shape) # Simulated inputs simulated_input_loader = SimulatedInputLoader(fixed_locations, measurement, 0.0001) # Simulate evaluation of this time index simulated_time_indices = range(number_of_simulated_time_steps) # Iterate for iteration in range(5): analysis_system.update([simulated_input_loader], simulated_time_indices) # Get all results result = numpy.zeros(measurement.shape) for t in range(number_of_simulated_time_steps): result[t, :] = analysis_system.evaluate_expected_value( 'MAP', SimulatedObservationStructure(t, fixed_locations, None, None), flag='POINTWISE') # Should be very close to original because specified noise is low numpy.testing.assert_almost_equal(result, measurement) max_disparity = (numpy.abs(result - measurement)).ravel().max() self.assertTrue(max_disparity < 1E-5) # 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_noiseless(self): # Use a number of time steps number_of_simulated_time_steps = 1 # Build system element = SpaceTimeFactorElement( n_triangulation_divisions=3, alpha=2, starttime=0, endtime=number_of_simulated_time_steps + 1, n_nodes=number_of_simulated_time_steps + 2, overlap_factor=2.5, H=1) initial_hyperparameters = SpaceTimeSPDEHyperparameters( space_log_sigma=0.0, space_log_rho=numpy.log(numpy.radians(45.0)), time_log_rho=numpy.log(3.0 / 365.0)) component = SpaceTimeComponent( ComponentStorage_InMemory(element, initial_hyperparameters), SpaceTimeComponentSolutionStorage_InMemory()) analysis_system = AnalysisSystem([component], ObservationSource.TMEAN, log=StringIO()) # use fixed locations from icosahedron fixed_locations = cartesian_to_polar2d( MeshIcosahedronSubdivision.build(3).points) # random measurement at each location numpy.random.seed(8976) field_basis = 10.0 * numpy.random.randn(fixed_locations.shape[0]) # some time function that varies over a year time_basis = numpy.cos( numpy.linspace(0.1, 1.75 * numpy.pi, number_of_simulated_time_steps)) # kronecker product of the two measurement = numpy.kron(field_basis, numpy.expand_dims(time_basis, 1)) # Simulated inputs simulated_input_loader = SimulatedInputLoader(fixed_locations, measurement, 0.01) # Simulate evaluation of this time index simulated_time_indices = range(number_of_simulated_time_steps) # Iterate for iteration in range(5): analysis_system.update([simulated_input_loader], simulated_time_indices) # Get all results result = numpy.zeros(measurement.shape) for t in range(number_of_simulated_time_steps): result[t, :] = analysis_system.evaluate_expected_value( 'MAP', SimulatedObservationStructure(t, fixed_locations, None, None), flag='POINTWISE') # Should be very close to original because specified noise is low numpy.testing.assert_almost_equal(result, measurement) max_disparity = (numpy.abs(result - measurement)).ravel().max() self.assertTrue(max_disparity < 1E-5)
def main(): print 'EUSTACE example using HadCRUT4 monthly data' # Input data path input_basepath = os.path.join(WORKSPACE_PATH, 'data/incoming/HadCRUT4.5.0.0') # Input filenames input_filenames = [ 'hadcrut4_median_netcdf.nc', 'hadcrut4_uncorrelated_supplementary.nc', 'hadcrut4_blended_uncorrelated.nc' ] # Months to process time_indices = range(2) # Climatology component climatology_component = SpaceTimeComponent(ComponentStorage_InMemory(SeasonalElement(n_triangulation_divisions=5, n_harmonics=5, include_local_mean=True), SeasonalHyperparameters(n_spatial_components=6, common_log_sigma=1.0, common_log_rho=0.0)), SpaceTimeComponentSolutionStorage_InMemory()) # Number of factors for large scale (factor analysis) component and initial hyperparameters n_factors = 5 factors = [ ] factor_hyperparameters = [ ] for factor_index in range(n_factors): factor_hyperparameters.append( SpaceTimeSPDEHyperparameters( space_log_sigma=0.0, space_log_rho=numpy.log(10.0 * numpy.pi/180 + 25.0 * numpy.pi/180 *(n_factors - factor_index) / n_factors), time_log_rho=numpy.log(1/12.0 + 6/12.0*(n_factors - factor_index) / n_factors)) ) factors.append( SpaceTimeFactorElement(n_triangulation_divisions=5, alpha=2, starttime=0, endtime=36, overlap_factor=2.5, H=1) ) # Large scale (factor analysis) component large_scale_component = SpaceTimeComponent(ComponentStorage_InMemory(CombinationElement(factors), CombinationHyperparameters(factor_hyperparameters)), SpaceTimeComponentSolutionStorage_InMemory()) # Local component local_component = SpatialComponent(ComponentStorage_InMemory(LocalElement(n_triangulation_divisions=4), LocalHyperparameters(log_sigma=0.0, log_rho=numpy.log(10.0 * numpy.pi/180))), SpatialComponentSolutionStorage_InMemory()) print 'Analysing inputs' # Analysis system using the specified components, for the Tmean observable analysis_system = AnalysisSystem( [ climatology_component, large_scale_component, local_component ], ObservationSource.TMEAN) # Make filelist input_filelist = [ os.path.join(input_basepath, filename) for filename in input_filenames ] # Object to load HadCRUT4 inputs at time indices inputloader = AnalysisSystemInputLoaderHadCRUT4(input_filelist) # Update with data analysis_system.update([ inputloader ], time_indices) print 'Computing outputs' # Produce an output for each time index for time_index in time_indices: # Make output filename outputdate = inputloader.datetime_at_time_index(time_index) pathname = 'example_output_{0:04d}{1:02d}.nc'.format(outputdate.year, outputdate.month) print 'Saving: ', pathname # Configure output grid outputstructure = OutputRectilinearGridStructure( time_index, outputdate, latitudes=numpy.linspace(-87.5, 87.5, num=36), longitudes=numpy.linspace(-177.5, 177.5, num=72)) # Evaluate expected value at these locations result_expected_value = analysis_system.evaluate_expected_value(outputstructure) # Save results filebuilder = FileBuilderHadCRUT4ExampleOutput(pathname, outputstructure) filebuilder.add_global_field(TAS_ANOMALY, result_expected_value.reshape(1,36,72)) filebuilder.save_and_close() print 'Complete'
def test_process_observations_no_uncertainties(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), sample_size=666) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertFalse(s.compute_uncertainties) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() s.update() numpy.testing.assert_almost_equal( s.solutionstorage.state, numpy.array([-0.60697861, 1.78530506])) # No marginal variances should have been computed at all, same for the sample self.assertEqual(None, s.solutionstorage.state_marginal_std) self.assertEqual(None, s.solutionstorage.state_sample) for time, expected_array in zip([21, 532], [ numpy.array([-1.5 * -0.60697861 + 2.2 * 1.78530506]), numpy.array([3.3 * 1.78530506]) ]): # Observation at time t=t* should be design matrix for that time multiplied by expected state numpy.testing.assert_almost_equal( s.solution_observation_expected_value( TestSpaceTimeComponentSolution.TestObservations(t=time)), expected_array) # In this case we are considering a generical model solving iteration for the model, no marginal variances stored, hence we expect 0. as observation uncertainties numpy.testing.assert_array_equal( s.solution_observation_expected_uncertainties( TestSpaceTimeComponentSolution.TestObservations(t=time)), 0.) # check samples are zero numpy.testing.assert_array_equal( 0., s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations(t=time))) # check default number of samples self.assertEqual( s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations( t=time)).shape[1], 666)
def test_process_observations_compute_uncertainties(self): # Our test system is: # # ( [ 2.0 0.0 ] + [ -1.5 0.0 ] [ 5.0 0.0 ] [ -1.5 2.2 ] ) x = [ -1.5 0.0 ] [ 10.0 3.0 ] [ 7.0 - 2.0 ] # ( [ 0.0 2.0 ] [ 2.2 3.3 ] [ 0.0 5.0 ] [ 0.0 3.3 ] ) [ 2.2 3.3 ] [ 3.0 10.0 ] [ 9.0 - 3.0 ] # # [ 13.25 -16.5 ] x = [ -37.5 ] # [-16.5 80.65 ] [ 154.0 ] # # => x = [ -0.60697861 ] # [ 1.78530506 ] # c = SpaceTimeComponent(ComponentStorage_InMemory( TestSpaceTimeComponentSolution.TestElement(), CovariateHyperparameters(-0.5 * numpy.log(2.0))), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True) s = c.component_solution() self.assertIsInstance(s, SpaceTimeComponentSolution) self.assertTrue(s.compute_uncertainties) test_offset = numpy.array([2.0, 3.0]) s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=21), test_offset[0:1]) s.update_time_step() s.process_observations( TestSpaceTimeComponentSolution.TestObservations(t=532), test_offset[1:2]) s.update_time_step() s.update() # No sample should have been computed at all self.assertEqual(None, s.solutionstorage.state_sample) # In this case we are considering the last iteration of model solving, hence marginal variances should have been stored expected_marginal_std = numpy.sqrt( numpy.diag( numpy.linalg.inv(numpy.array([[13.25, -16.5], [-16.5, 80.65]])))) numpy.testing.assert_array_almost_equal( s.solutionstorage.state_marginal_std, expected_marginal_std) # Now we compute the projection of marginal variances onto the given observations for time in [532]: # Observation at time t=t* should be design matrix for that time multiplied by expected state expected_projection = TestSpaceTimeComponentSolution.TestDesign( t=time).design_function(expected_marginal_std) numpy.testing.assert_almost_equal( s.solution_observation_expected_uncertainties( TestSpaceTimeComponentSolution.TestObservations(t=time)), expected_projection) # check samples are zero numpy.testing.assert_array_equal( 0., s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations(t=time))) # check default number of samples self.assertEqual( s.solution_observation_projected_sample( TestSpaceTimeComponentSolution.TestObservations( t=time)).shape[1], 1)
def main(): print 'Advanced standard example using a few days of EUSTACE data' parser = argparse.ArgumentParser( description='Advanced standard example using a few days of EUSTACE data' ) parser.add_argument('outpath', help='directory where the output should be redirected') parser.add_argument( '--json_descriptor', default=None, help= 'a json descriptor containing the covariates to include in the climatology model' ) parser.add_argument('--land_biases', action='store_true', help='include insitu land homogenization bias terms') parser.add_argument('--global_biases', action='store_true', help='include global satellite bias terms') parser.add_argument('--n_iterations', type=int, default=5, help='number of solving iterations') args = parser.parse_args() # Input data path basepath = os.path.join('/work/scratch/eustace/rawbinary3') # Days to process #time_indices = range(int(days_since_epoch(datetime(2006, 2, 1))), int(days_since_epoch(datetime(2006, 2, 2)))) #time_indices = range(int(days_since_epoch(datetime(1906, 2, 1))), int(days_since_epoch(datetime(1906, 2, 2)))) date_list = [ datetime(2006, 1, 1) + relativedelta(days=k) for k in range(3) ] #backwards_list = [date_list[i] for i in range(11, -1, -1)] #date_list = backwards_list time_indices = [int(days_since_epoch(date)) for date in date_list] # Sources to use sources = [ 'surfaceairmodel_land', 'surfaceairmodel_ocean', 'surfaceairmodel_ice', 'insitu_land', 'insitu_ocean' ] sources = ['insitu_land', 'insitu_ocean'] #sources = [ 'surfaceairmodel_land' ] # CLIMATOLOGY COMPONENT: combining the seasonal core along with latitude harmonics, altitude and coastal effects if args.json_descriptor is not None: loader = LoadCovariateElement(args.json_descriptor) loader.check_keys() covariate_elements, covariate_hyperparameters = loader.load_covariates_and_hyperparameters( ) print( 'The following fields have been added as covariates of the climatology model' ) print(loader.data.keys()) else: covariate_elements, covariate_hyperparameters = [], [] #climatology_element = CombinationElement( [SeasonalElement(n_triangulation_divisions=2, n_harmonics=2, include_local_mean=False), GrandMeanElement()]+covariate_elements) #climatology_hyperparameters = CombinationHyperparameters( [SeasonalHyperparameters(n_spatial_components=2, common_log_sigma=0.0, common_log_rho=0.0), CovariateHyperparameters(numpy.log(15.0))] + covariate_hyperparameters ) climatology_element = CombinationElement([ GrandMeanElement(), ] + covariate_elements) climatology_hyperparameters = CombinationHyperparameters([ CovariateHyperparameters(numpy.log(15.0)), ] + covariate_hyperparameters) #climatology_element =SeasonalElement(n_triangulation_divisions=2, n_harmonics=2, include_local_mean=False) #climatology_hyperparameters = SeasonalHyperparameters(n_spatial_components=2, common_log_sigma=0.0, common_log_rho=0.0) climatology_component = SpaceTimeComponent( ComponentStorage_InMemory(climatology_element, climatology_hyperparameters), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED') # LARGE SCALE (kronecker product) COMPONENT: combining large scale trends with bias terms accounting for homogeneization effects if args.land_biases: bias_element, bias_hyperparameters = [ InsituLandBiasElement(BREAKPOINTS_FILE) ], [CovariateHyperparameters(numpy.log(.9))] print('Adding bias terms for insitu land homogenization') else: bias_element, bias_hyperparameters = [], [] large_scale_element = CombinationElement([ SpaceTimeKroneckerElement(n_triangulation_divisions=2, alpha=2, starttime=-30, endtime=365 * 1 + 30, n_nodes=12 * 1 + 2, overlap_factor=2.5, H=1) ] + bias_element) large_scale_hyperparameters = CombinationHyperparameters([ SpaceTimeSPDEHyperparameters(space_log_sigma=0.0, space_log_rho=numpy.log( numpy.radians(15.0)), time_log_rho=numpy.log(15.0)) ] + bias_hyperparameters) large_scale_component = SpaceTimeComponent( ComponentStorage_InMemory(large_scale_element, large_scale_hyperparameters), SpaceTimeComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED') # LOCAL COMPONENT: combining local scale variations with global satellite bias terms if args.global_biases: bias_elements = [ BiasElement(groupname, 1) for groupname in GLOBAL_BIASES_GROUP_LIST ] bias_hyperparameters = [ CovariateHyperparameters(numpy.log(15.0)) for index in range(3) ] print('Adding global bias terms for all the surfaces') else: bias_elements, bias_hyperparameters = [], [] n_triangulation_divisions_local = 7 local_log_sigma = numpy.log(5) local_log_rho = numpy.log(numpy.radians(5.0)) local_element = NonStationaryLocal( n_triangulation_divisions=n_triangulation_divisions_local) n_local_nodes = local_element.spde.n_latent_variables() local_scale_element = CombinationElement([local_element] + bias_elements) local_hyperparameters = ExpandedLocalHyperparameters( log_sigma=numpy.repeat(local_log_sigma, n_local_nodes), log_rho=numpy.repeat(local_log_rho, n_local_nodes)) local_scale_hyperparameters = CombinationHyperparameters( [local_hyperparameters] + bias_hyperparameters) local_component = DelayedSpatialComponent( ComponentStorage_InMemory(local_scale_element, local_scale_hyperparameters), SpatialComponentSolutionStorage_InMemory(), compute_uncertainties=True, method='APPROXIMATED') print "hyperparameter storage:", local_component.storage.hyperparameters print 'Analysing inputs' # Analysis system using the specified components, for the Tmean observable ##analysis_system = AnalysisSystem( ## [ climatology_component, large_scale_component, local_component ], ## ObservationSource.TMEAN) analysis_system = OptimizationSystem( [climatology_component, local_component], ObservationSource.TMEAN) # Object to load raw binary inputs at time indices inputloaders = [ AnalysisSystemInputLoaderRawBinary_Sources(basepath, source, time_indices) for source in sources ] for iteration in range(args.n_iterations): message = 'Iteration {}'.format(iteration) print(message) # Update with data analysis_system.update(inputloaders, time_indices) ################################################## # Optimize local model hyperparameters # Loop over local regions, generate optimization systems, fit hyperparameters and save # split spde and bias models for local component into two components global_spde_sub_component_definition = ComponentStorage_InMemory( CombinationElement([local_element]), CombinationHyperparameters([local_hyperparameters])) global_spde_sub_component_storage_solution = SpatialComponentSolutionStorage_InMemory( ) global_spde_sub_component = DelayedSpatialComponent( global_spde_sub_component_definition, global_spde_sub_component_storage_solution) bias_sub_component_definition = ComponentStorage_InMemory( CombinationElement(bias_elements), CombinationHyperparameters(bias_hyperparameters)) bias_sub_component_storage_solution = SpatialComponentSolutionStorage_InMemory( ) bias_sub_component = DelayedSpatialComponent( bias_sub_component_definition, bias_sub_component_storage_solution) element_optimisation_flags = [True, False, False, False] # one spde, three biases for time_key in time_indices: split_states_time(local_component, global_spde_sub_component, bias_sub_component, element_optimisation_flags, time_key) # Define subregions and extract their states neighbourhood_level = 1 n_subregions = global_spde_sub_component.storage.element_read( ).combination[0].spde.n_triangles_at_level(neighbourhood_level) hyperparameter_file_template = "local_hyperparameters.%i.%i.%i.npy" fit_hyperparameters = True optimization_component_index = 2 if fit_hyperparameters: for region_index in range(n_subregions): # Setup model for local subregion of neighours with super triangle view_flags = [ True, ] region_element = CombinationElement([ LocalSubRegion(n_triangulation_divisions_local, neighbourhood_level, region_index) ]) region_hyperparameters = ExtendedCombinationHyperparameters([ LocalHyperparameters(log_sigma=local_log_sigma, log_rho=local_log_rho) ]) region_component_storage_solution = SpatialComponentSolutionStorage_InMemory( ) region_sub_component = DelayedSpatialComponent( ComponentStorage_InMemory(region_element, region_hyperparameters), region_component_storage_solution) for time_key in time_indices: print "region_index, time_key:", region_index, time_key extract_local_view_states_time(global_spde_sub_component, region_sub_component, view_flags, time_key) print "running optimization for region:", region_index region_optimization_system = OptimizationSystem([ climatology_component, bias_sub_component, region_sub_component ], ObservationSource.TMEAN) for time_key in time_indices: region_optimization_system.update_component_time( inputloaders, optimization_component_index, time_key) # commented version that works for few days inputs #region_optimization_system.components[optimization_component_index].component_solution().optimize() #region_optimization_system.components[optimization_component_index].storage.hyperparameters.get_array() #hyperparameter_file = os.path.join(args.outpath, hyperparameter_file_template % (n_triangulation_divisions_local, neighbourhood_level, region_index) ) #region_sub_component.storage.hyperparameters.values_to_npy_savefile( hyperparameter_file ) # replaced with version for full processing based json dump of input files - need to generate the input_descriptor dict hyperparameter_file = os.path.join( args.outpath, hyperparameter_file_template % (n_triangulation_divisions_local, neighbourhood_level, region_index)) region_optimization_system.process_inputs( input_descriptor, optimization_component_index, time_indices) region_optimization_system.optimize_component( optimization_component_index, hyperparameter_storage_file=hyperparameter_file) fitted_hyperparameters_converted = region_sub_component.storage.hyperparameters.get_array( ) fitted_hyperparameters_converted[0] = numpy.exp( fitted_hyperparameters_converted[0]) fitted_hyperparameters_converted[1] = numpy.exp( fitted_hyperparameters_converted[1]) * 180.0 / numpy.pi print 'fitted_hyperparameters_converted:', fitted_hyperparameters_converted # Setup model for the super triangle without neighbours for hyperparameter merging region_spdes = [] region_hyperparameter_values = [] for region_index in range(n_subregions): # Redefine the region sub component as a supertriangle rather than a neighbourhood region_element = CombinationElement([ LocalSuperTriangle(n_triangulation_divisions_local, neighbourhood_level, region_index) ]) region_hyperparameters = ExtendedCombinationHyperparameters([ LocalHyperparameters(log_sigma=local_log_sigma, log_rho=local_log_rho) ]) region_component_storage_solution = SpatialComponentSolutionStorage_InMemory( ) region_sub_component = DelayedSpatialComponent( ComponentStorage_InMemory(region_element, region_hyperparameters), region_component_storage_solution) # Read the optimized hyperparameters hyperparameter_file = os.path.join( args.outpath, hyperparameter_file_template % (n_triangulation_divisions_local, neighbourhood_level, region_index)) region_sub_component.storage.hyperparameters.values_from_npy_savefile( hyperparameter_file) # Append the spde model and hyperparameters to their lists for merging region_spdes.append(region_element.combination[0].spde) region_hyperparameter_values.append( region_sub_component.storage.hyperparameters.get_array()) # merge and save hyperparameters full_spde = local_element.spde new_hyperparameter_values, global_sigma_design, global_rho_design = full_spde.merge_local_parameterisations( region_spdes, region_hyperparameter_values, merge_method='exp_average') local_hyperparameters.set_array(new_hyperparameter_values) hyperparameter_file_merged = "merged_hyperparameters.%i.%i.npy" % ( n_triangulation_divisions_local, neighbourhood_level) local_hyperparameters.values_to_npy_savefile( os.path.join(args.outpath, hyperparameter_file_merged)) # Refit local model with the optimized hyperparameters analysis_system.update_component(inputloaders, 1, time_indices) ################################################## print 'Computing outputs' # Produce an output for each time index for time_index in time_indices: # Get date for output outputdate = inputloaders[0].datetime_at_time_index(time_index) print 'Evaluating output grid: ', outputdate #Configure output grid outputstructure = OutputRectilinearGridStructure( time_index, outputdate, latitudes=numpy.linspace(-89.875, 89.875, num=definitions.GLOBAL_FIELD_SHAPE[1]), longitudes=numpy.linspace(-179.875, 179.875, num=definitions.GLOBAL_FIELD_SHAPE[2])) # print 'Size of grid : ', outputstructure.number_of_observations() # Evaluate expected value at these locations result_expected_value = analysis_system.evaluate_expected_value( 'MAP', outputstructure, 'POINTWISE') result_expected_uncertainties = analysis_system.evaluate_expected_value( 'post_STD', outputstructure, 'POINTWISE') # Make output filename pathname = 'eustace_example_output_{0:04d}{1:02d}{2:02d}.nc'.format( outputdate.year, outputdate.month, outputdate.day) pathname = os.path.join(args.outpath, pathname) print 'Saving: ', pathname # Save results filebuilder = FileBuilderGlobalField( pathname, time_index, 'Infilling Example', 'UNVERSIONED', definitions.TAS.name, '', 'Example data only', 'eustace.analysis.advanced_standard.examples.example_eustace_few_days', '') filebuilder.add_global_field( definitions.TAS, result_expected_value.reshape(definitions.GLOBAL_FIELD_SHAPE)) filebuilder.add_global_field( definitions.TASUNCERTAINTY, result_expected_uncertainties.reshape( definitions.GLOBAL_FIELD_SHAPE)) filebuilder.save_and_close() print 'Complete'