def test_missing_coordinate(self): """ Basic test that the result is a numpy array with the expected contents. The array contents is also checked. """ cube = self.current_temperature_forecast_cube plen = len(cube.coord("percentile").points) msg = "coordinate is not available" with self.assertRaisesRegex(CoordinateNotFoundError, msg): restore_non_probabilistic_dimensions( cube.data, cube, "nonsense", plen)
def test_if_percentile_is_not_first_dimension_coordinate(self): """ Test that the result have the expected size for the probabilistic dimension and is generally of the expected size. The array contents is also checked. """ cube = self.current_temperature_forecast_cube cube.transpose([3, 2, 1, 0]) plen = len(cube.coord("percentile").points) msg = "coordinate is a dimension coordinate but is not" with self.assertRaisesRegex(ValueError, msg): restore_non_probabilistic_dimensions( cube.data, cube, "percentile", plen)
def test_percentile_is_dimension_coordinate_multiple_timesteps(self): """ Test that the data has been reshaped correctly when multiple timesteps are in the cube. The array contents is also checked. """ expected = np.array([[[[4., 4.71428571], [5.42857143, 6.14285714]], [[6.85714286, 7.57142857], [8.28571429, 9.]]]]) data = np.tile(np.linspace(5, 10, 8), 3).reshape(3, 2, 2, 2) data[0] -= 1 data[1] += 1 data[2] += 3 cube = set_up_cube(data, "air_temperature", "degreesC", timesteps=2, x_dimension_length=2, y_dimension_length=2) cube.coord("realization").rename("percentile") cube.coord("percentile").points = ( np.array([10, 50, 90])) plen = 1 percentile_cube = ( add_forecast_reference_time_and_forecast_period( cube, time_point=np.array([402295.0, 402296.0]), fp_point=[2.0, 3.0])) reshaped_array = ( restore_non_probabilistic_dimensions( percentile_cube[0].data, percentile_cube, "percentile", plen)) self.assertArrayAlmostEqual(reshaped_array, expected)
def test_percentile_is_dimension_coordinate_multiple_timesteps(self): """ Test that the data has been reshaped correctly when multiple timesteps are in the cube. The array contents is also checked. """ expected = np.array([[ [[4.0, 4.71428571], [5.42857143, 6.14285714]], [[6.85714286, 7.57142857], [8.28571429, 9.0]], ]]) data = np.tile(np.linspace(5, 10, 8), 3).reshape(3, 2, 2, 2) data[0] -= 1 data[1] += 1 data[2] += 3 cubelist = CubeList([]) for i, hour in enumerate([7, 8]): cubelist.append( set_up_percentile_cube( data[:, i, :, :].astype(np.float32), np.array([10, 50, 90], dtype=np.float32), units="degC", time=datetime(2015, 11, 23, hour), frt=datetime(2015, 11, 23, 6), )) percentile_cube = cubelist.merge_cube() percentile_cube.transpose([1, 0, 2, 3]) plen = 1 reshaped_array = restore_non_probabilistic_dimensions( percentile_cube[0].data, percentile_cube, "percentile", plen) self.assertArrayAlmostEqual(reshaped_array, expected)
def test_basic(self): """ Basic test that the result is a numpy array with the expected contents. """ cube = self.current_temperature_forecast_cube plen = len(cube.coord("percentile").points) reshaped_array = (restore_non_probabilistic_dimensions( cube.data, cube, "percentile", plen)) self.assertIsInstance(reshaped_array, np.ndarray)
def test_percentile_is_dimension_coordinate_flattened_data(self): """ Test the array size, if a flattened input array is used as the input. The array contents is also checked. """ cube = self.current_temperature_forecast_cube flattened_data = cube.data.flatten() plen = len(cube.coord("percentile").points) reshaped_array = (restore_non_probabilistic_dimensions( flattened_data, cube, "percentile", plen)) self.assertEqual(reshaped_array.shape[0], plen) self.assertEqual(reshaped_array.shape, (3, 1, 3, 3)) self.assertArrayAlmostEqual(reshaped_array, cube.data)
def test_percentile_is_dimension_coordinate(self): """ Test that the result have the expected size for the probabilistic dimension and is generally of the expected size. The array contents is also checked. """ cube = self.current_temperature_forecast_cube plen = len(cube.coord("percentile").points) reshaped_array = (restore_non_probabilistic_dimensions( cube.data, cube, "percentile", plen)) self.assertEqual(reshaped_array.shape[0], plen) self.assertEqual(reshaped_array.shape, cube.data.shape) self.assertArrayAlmostEqual(reshaped_array, cube.data)
def test_percentile_is_not_dimension_coordinate(self): """ Test the array size, if the percentile coordinate is not a dimension coordinate on the cube. The array contents is also checked. """ expected = np.array([[[[226.15, 237.4, 248.65], [259.9, 271.15, 282.4], [293.65, 304.9, 316.15]]]], dtype=np.float32) cube = self.current_temperature_forecast_cube for cube_slice in cube.slices_over("percentile"): break plen = len(cube_slice.coord("percentile").points) reshaped_array = (restore_non_probabilistic_dimensions( cube_slice.data, cube_slice, "percentile", plen)) self.assertEqual(reshaped_array.shape[0], plen) self.assertEqual(reshaped_array.shape, (1, 1, 3, 3)) self.assertArrayAlmostEqual(reshaped_array, expected)