def test_percentiles_different_coordinate_orders(self): """Test the result of the percentile aggregation is the same regardless of the coordinate order in the input cube. Most importantly, the result should be the same regardless of on which side of the collapsing coordinate the percentile coordinate falls.""" coord = "forecast_reference_time" plugin = WeightedBlendAcrossWholeDimension(coord) weights = None percentile_leading = percentile_cube() time_leading = percentile_cube() time_leading.transpose([1, 0, 2, 3]) pl_perc_coord = percentile_leading.coord("percentile") tl_perc_coord = time_leading.coord("percentile") result_percentile_leading = plugin.percentile_weighted_mean( percentile_leading, weights, pl_perc_coord ) result_time_leading = plugin.percentile_weighted_mean( time_leading, weights, tl_perc_coord ) self.assertArrayAlmostEqual( result_percentile_leading.data, BLENDED_PERCENTILE_DATA_EQUAL_WEIGHTS ) self.assertArrayAlmostEqual( result_time_leading.data, BLENDED_PERCENTILE_DATA_EQUAL_WEIGHTS )
def test_with_weights(self): """Test function when a data cube and a weights cube are provided.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") result = plugin.percentile_weighted_mean(self.perc_cube, self.weights1d, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA)
def test_without_weights(self): """Test function when a data cube is provided, but no weights cube which should result in equal weightings.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") result = plugin.percentile_weighted_mean(self.perc_cube, None, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA_EQUAL_WEIGHTS)
def test_with_weights(self): """Test function when a data cube and a weights cube are provided.""" perc_cube = percentile_cube() coord = "forecast_reference_time" plugin = WeightedBlendAcrossWholeDimension(coord, 'weighted_mean') perc_coord = perc_cube.coord('percentile_over_realization') result = plugin.percentile_weighted_mean(perc_cube, self.weights1d, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA)
def test_with_weights_perc_as_float64(self): """Test function when a data cube and a weights cube are provided. But with a percentile coord that is float64""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") perc_coord.points = perc_coord.points.astype(np.float64) result = plugin.percentile_weighted_mean(self.perc_cube, self.weights1d, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA) self.assertEqual(result.coord("percentile").points.dtype, np.float64)
def test_without_weights(self): """Test function when a data cube is provided, but no weights cube which should result in equal weightings.""" perc_cube = percentile_cube() coord = "forecast_reference_time" plugin = WeightedBlendAcrossWholeDimension(coord, 'weighted_mean') perc_coord = perc_cube.coord('percentile_over_realization') result = plugin.percentile_weighted_mean(perc_cube, None, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA_EQUAL_WEIGHTS)
def test_with_spatially_varying_weights(self): """Test function when a data cube and a multi dimensional weights cube are provided. This tests spatially varying weights, where each x-y position is weighted differently in each slice along the blending coordinate.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") result = plugin.percentile_weighted_mean(self.perc_cube, self.weights3d, perc_coord) self.assertIsInstance(result, iris.cube.Cube) self.assertArrayAlmostEqual(result.data, BLENDED_PERCENTILE_DATA_SPATIAL_WEIGHTS)
def test_percentiles_another_coordinate_order(self): """Test the result of the percentile aggregation is the same regardless of the coordinate order in the input cube. In this case a spatial coordinate is moved to the leading position. The resulting array is of a different shape, but the data is the same. We reorder the expected values to confirm this.""" coord = "forecast_reference_time" plugin = WeightedBlendAcrossWholeDimension(coord) weights = None longitude_leading = percentile_cube() longitude_leading.transpose([3, 0, 1, 2]) perc_coord = longitude_leading.coord("percentile") result = plugin.percentile_weighted_mean(longitude_leading, weights, perc_coord) self.assertArrayAlmostEqual( result.data, np.moveaxis(BLENDED_PERCENTILE_DATA_EQUAL_WEIGHTS, [2], [0]) )