def setUp(self): """Create reliability calibration tables for testing.""" super().setUp() reliability_cube_format = CalPlugin()._create_reliability_table_cube( self.forecasts, self.expected_threshold_coord) self.reliability_cube = reliability_cube_format.copy( data=self.expected_table) self.different_frt = self.reliability_cube.copy() new_frt = self.different_frt.coord("forecast_reference_time") new_frt.points = new_frt.points + 48 * 3600 new_frt.bounds = new_frt.bounds + 48 * 3600 self.masked_reliability_cube = self.reliability_cube.copy() masked_array = np.zeros(self.reliability_cube.shape, dtype=bool) masked_array[:, :, 0, :2] = True self.masked_reliability_cube.data = np.ma.array( self.reliability_cube.data, mask=masked_array) self.masked_different_frt = self.different_frt.copy() masked_array = np.zeros(self.different_frt.shape, dtype=bool) masked_array[:, :, :2, 0] = True self.masked_different_frt.data = np.ma.array(self.different_frt.data, mask=masked_array) self.overlapping_frt = self.reliability_cube.copy() new_frt = self.overlapping_frt.coord("forecast_reference_time") new_frt.points = new_frt.points + 6 * 3600 new_frt.bounds = new_frt.bounds + 6 * 3600 self.lat_lon_collapse = np.array([ [0.0, 0.0, 1.0, 2.0, 1.0], [0.0, 0.375, 1.5, 1.625, 1.0], [1.0, 2.0, 3.0, 2.0, 1.0], ])
def reliability_cube(forecast_grid, expected_table): from improver.calibration.reliability_calibration import ( ConstructReliabilityCalibrationTables as CalPlugin, ) rel_cube_format = CalPlugin()._create_reliability_table_cube( forecast_grid, forecast_grid.coord(var_name="threshold") ) rel_cube = rel_cube_format.copy(data=expected_table) return rel_cube
def setUp(self): """Create reliability calibration table and forecast cubes for testing.""" forecast_probabilities_0 = np.linspace(0.5, 1, 9, dtype=np.float32) forecast_probabilities_1 = np.linspace(0, 0.4, 9, dtype=np.float32) thresholds = [275.0, 280.0] forecast_probabilities = np.stack( [forecast_probabilities_0, forecast_probabilities_1]).reshape( (2, 3, 3)) self.forecast = set_up_probability_cube(forecast_probabilities, thresholds) self.forecast_thresholds = iris.cube.CubeList() for forecast_slice in self.forecast.slices_over("air_temperature"): self.forecast_thresholds.append(forecast_slice) reliability_cube_format = CalPlugin()._create_reliability_table_cube( self.forecast[0], self.forecast.coord(var_name="threshold")) reliability_cube_format = reliability_cube_format.collapsed( [ reliability_cube_format.coord(axis="x"), reliability_cube_format.coord(axis="y"), ], iris.analysis.SUM, ) # Over forecasting exceeding 275K. reliability_data_0 = np.array( [ [0, 0, 250, 500, 750], # Observation count [0, 250, 500, 750, 1000], # Sum of forecast probability [1000, 1000, 1000, 1000, 1000], # Forecast count ], dtype=np.float32, ) # Under forecasting exceeding 280K. reliability_data_1 = np.array( [ [250, 500, 750, 1000, 1000], # Observation count [0, 250, 500, 750, 1000], # Sum of forecast probability [1000, 1000, 1000, 1000, 1000], # Forecast count ], dtype=np.float32, ) r0 = reliability_cube_format.copy(data=reliability_data_0) r1 = reliability_cube_format.copy(data=reliability_data_1) r1.replace_coord(self.forecast[1].coord(var_name="threshold")) self.reliability_cubelist = iris.cube.CubeList([r0, r1]) self.reliability_cube = self.reliability_cubelist.merge_cube() self.threshold = self.forecast.coord(var_name="threshold") self.plugin = Plugin() self.plugin.threshold_coord = self.threshold
def reliability_table_agg(forecast_grid, truth_grid, reliability_data): reliability_cube_format = CalPlugin().process(forecast_grid, truth_grid) reliability_cube_format = reliability_cube_format.collapsed( [ reliability_cube_format.coord(axis="x"), reliability_cube_format.coord(axis="y"), ], iris.analysis.SUM, ) reliability_table_agg = reliability_cube_format.copy(data=reliability_data) return reliability_table_agg
def setUp(self): """Set up forecast count""" # Set up a reliabilty table with a single threshold thresholds = [275.0] self.forecast = set_up_probability_cube( np.ones((1, 3, 3), dtype=np.float32), thresholds ) reliability_cube_format = CalPlugin()._create_reliability_table_cube( self.forecast, self.forecast.coord(var_name="threshold") ) reliability_cube_format = reliability_cube_format.collapsed( [ reliability_cube_format.coord(axis="x"), reliability_cube_format.coord(axis="y"), ], iris.analysis.SUM, ) self.obs_count = np.array([0, 0, 250, 500, 750], dtype=np.float32) self.forecast_probability_sum = np.array( [0, 250, 500, 750, 1000], dtype=np.float32 ) self.forecast_count = np.array([1000, 1000, 1000, 1000, 1000], dtype=np.float32) reliability_data_0 = np.stack( [self.obs_count, self.forecast_probability_sum, self.forecast_count] ) self.reliability_table = reliability_cube_format.copy(data=reliability_data_0) self.probability_bin_coord = self.reliability_table.coord("probability_bin") # Set up a reliablity table cube with two thresholds reliability_data_1 = np.array( [ [250, 500, 750, 1000, 1000], # Observation count [0, 250, 500, 750, 1000], # Sum of forecast probability [1000, 1000, 1000, 1000, 1000], # Forecast count ], dtype=np.float32, ) reliability_table_1 = self.reliability_table.copy(data=reliability_data_1) reliability_table_1.coord("air_temperature").points = np.array( 278.0, dtype=np.float32 ) self.multi_threshold_rt = iris.cube.CubeList( [self.reliability_table, reliability_table_1] ).merge_cube() # Set up expected resulting reliablity table data for Test__combine_bin_pair self.expected_enforced_monotonic = np.array( [ [0, 250, 500, 1750], # Observation count [0, 250, 500, 1750], # Sum of forecast probability [1000, 1000, 1000, 2000], # Forecast count ] )
def setUp(self): """Create reliability calibration tables for testing.""" super().setUp() reliability_cube_format = CalPlugin()._create_reliability_table_cube( self.forecasts, self.expected_threshold_coord) self.reliability_cube = reliability_cube_format.copy( data=self.expected_table) self.different_frt = self.reliability_cube.copy() new_frt = self.different_frt.coord('forecast_reference_time') new_frt.points = new_frt.points + 48 * 3600 new_frt.bounds = new_frt.bounds + 48 * 3600 self.overlapping_frt = self.reliability_cube.copy() new_frt = self.overlapping_frt.coord('forecast_reference_time') new_frt.points = new_frt.points + 6 * 3600 new_frt.bounds = new_frt.bounds + 6 * 3600 self.lat_lon_collapse = np.array([[0., 0., 1., 2., 1.], [0., 0.375, 1.5, 1.625, 1.], [1., 2., 3., 2., 1.]])
def reliability_table_point_grid(forecast_grid, truth_grid, reliability_data): reliability_cube_format = CalPlugin().process(forecast_grid, truth_grid) data = np.stack([np.stack([reliability_data] * 3, axis=-1)] * 3, axis=-1) reliability_table = reliability_cube_format.copy(data=data) return reliability_table
def reliability_table_point_spot(forecast_spot, truth_spot, reliability_data): reliability_cube_format = CalPlugin().process(forecast_spot, truth_spot) data = np.stack([reliability_data] * 9, axis=-1) reliability_table = reliability_cube_format.copy(data=data) return reliability_table