def test_3D_weights_3D_cube_percentile_weighted_mean(self): """Test a 3D cube of weights results in a 3D array of weights of the same shape as the data cube. In this case the cube is a percentile cube and so has an additional dimension that must be accounted for.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") (coord_dim, ) = self.perc_cube.coord_dims(self.coord) (perc_dim, ) = self.perc_cube.coord_dims(perc_coord) expected = np.empty_like(self.perc_cube.data) expected = np.moveaxis(expected, [coord_dim, perc_dim], [0, 1]) result = plugin.percentile_weights(self.perc_cube, self.weights3d, perc_coord) self.assertEqual(expected.shape, result.shape) self.assertArrayEqual(self.weights3d.data, result[:, 0, :, :])
def test_1D_weights_3D_cube_percentile_weighted_mean(self): """Test a 1D cube of weights results in a 3D array of weights of an appropriate shape for a percentile cube. In this case the collapse coordinate is moved to the leading position and the percentile coordinate is second.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") (coord_dim, ) = self.perc_cube.coord_dims(self.coord) (perc_dim, ) = self.perc_cube.coord_dims(perc_coord) expected = np.empty_like(self.perc_cube.data) expected = np.moveaxis(expected, [coord_dim, perc_dim], [0, 1]) result = plugin.percentile_weights(self.perc_cube, self.weights1d, perc_coord) self.assertEqual(expected.shape, result.shape) self.assertArrayEqual(self.weights1d.data, result[:, 0, 0, 0])
def test_no_weights_cube_provided(self): """Test that if a weights cube is not provided, the function generates a weights array that will equally weight all fields along the blending coordinate.""" plugin = WeightedBlendAcrossWholeDimension(self.coord) perc_coord = self.perc_cube.coord("percentile") (coord_dim, ) = self.perc_cube.coord_dims(self.coord) (perc_dim, ) = self.perc_cube.coord_dims(perc_coord) (blending_coord_length, ) = self.perc_cube.coord(self.coord).shape expected = np.empty_like(self.perc_cube.data) expected = np.moveaxis(expected, [coord_dim, perc_dim], [0, 1]) expected = np.full_like(expected, 1.0 / blending_coord_length) result = plugin.percentile_weights(self.perc_cube, None, perc_coord) self.assertEqual(expected.shape, result.shape) self.assertArrayEqual(expected, result)
def test_no_weights_cube_provided(self): """Test that if a weights cube is not provided, the function generates a weights array that will equally weight all fields along the blending coordinate.""" perc_cube = percentile_cube() coord = "forecast_reference_time" plugin = WeightedBlendAcrossWholeDimension(coord, 'weighted_mean') perc_coord = perc_cube.coord('percentile_over_realization') coord_dim, = perc_cube.coord_dims(coord) perc_dim, = perc_cube.coord_dims(perc_coord) blending_coord_length, = perc_cube.coord(coord).shape expected = np.empty_like(perc_cube.data) expected = np.moveaxis(expected, [coord_dim, perc_dim], [0, 1]) expected = np.full_like(expected, 1. / blending_coord_length) result = plugin.percentile_weights(perc_cube, None, perc_coord) self.assertEqual(expected.shape, result.shape) self.assertArrayEqual(expected, result)