def setUp(self):
     """Set up expected inputs."""
     super().setUp()
     # Set up cubes and associated data arrays for wind speed.
     self.forecast_predictor_mean = self.historic_wind_speed_forecast_cube.collapsed(
         "realization", iris.analysis.MEAN
     )
     self.forecast_predictor_realizations = (
         self.historic_wind_speed_forecast_cube.copy()
     )
     self.forecast_variance = self.historic_wind_speed_forecast_cube.collapsed(
         "realization", iris.analysis.VARIANCE
     )
     self.truth = self.historic_wind_speed_forecast_cube.collapsed(
         "realization", iris.analysis.MAX
     )
     self.forecast_predictor_data = self.forecast_predictor_mean.data.flatten().astype(
         np.float64
     )
     self.forecast_predictor_data_realizations = convert_cube_data_to_2d(
         self.historic_wind_speed_forecast_cube.copy()
     ).astype(np.float64)
     self.forecast_variance_data = self.forecast_variance.data.flatten().astype(
         np.float64
     )
     self.truth_data = self.truth.data.flatten().astype(np.float64)
Example #2
0
    def setUp(self):
        """Set up expected inputs."""
        super().setUp()
        # Set up cubes and associated data arrays for temperature.
        self.forecast_predictor_mean = CubeList(
            [
                self.historic_temperature_forecast_cube.collapsed(
                    "realization", iris.analysis.MEAN
                )
            ]
        )
        self.forecast_predictor_realizations = CubeList(
            [(self.historic_temperature_forecast_cube.copy())]
        )
        self.forecast_predictor_spot = CubeList(
            [
                self.historic_forecast_spot_cube.collapsed(
                    "realization", iris.analysis.MEAN
                )
            ]
        )

        self.fp_additional_predictor_spot = CubeList(
            [self.forecast_predictor_spot[0].copy()]
        )
        self.fp_additional_predictor_spot.extend([self.spot_altitude_cube])

        self.forecast_variance = self.historic_temperature_forecast_cube.collapsed(
            "realization", iris.analysis.VARIANCE
        )
        self.forecast_variance_spot = self.forecast_predictor_spot[0].copy()
        self.forecast_variance_spot.data = self.forecast_variance_spot.data / 10.0

        self.truth = self.historic_temperature_forecast_cube.collapsed(
            "realization", iris.analysis.MAX
        )
        self.forecast_predictor_data = (
            self.forecast_predictor_mean[0].data.flatten().astype(np.float64)
        )
        self.forecast_predictor_data_realizations = convert_cube_data_to_2d(
            self.historic_temperature_forecast_cube.copy()
        ).astype(np.float64)
        self.forecast_variance_data = self.forecast_variance.data.flatten().astype(
            np.float64
        )
        self.truth_data = self.truth.data.flatten().astype(np.float64)

        spatial_product = np.prod(self.truth.shape[-2:])
        self.initial_guess_spot_mean = np.broadcast_to(
            self.initial_guess_for_mean,
            (spatial_product, len(self.initial_guess_for_mean),),
        )
        self.initial_guess_spot_realizations = np.broadcast_to(
            self.initial_guess_for_realization,
            (spatial_product, len(self.initial_guess_for_realization),),
        )
        self.ig_spot_mean_additional_predictor = np.broadcast_to(
            self.initial_guess_mean_additional_predictor,
            (spatial_product, len(self.initial_guess_mean_additional_predictor),),
        )
Example #3
0
 def test_no_transpose(self):
     """
     Test that the utility returns the expected data values
     when the cube is not transposed after slicing.
     """
     data = self.data.T
     result = convert_cube_data_to_2d(self.cube, transpose=False)
     self.assertArrayAlmostEqual(result, data)
Example #4
0
 def test_change_coordinate(self):
     """
     Test that the utility returns the expected data values
     when the cube is sliced along the longitude dimension.
     """
     data = self.data.flatten().reshape(9, 3).T.reshape(9, 3)
     result = convert_cube_data_to_2d(self.cube, coord="longitude")
     self.assertArrayAlmostEqual(result, data)
Example #5
0
 def test_1d_cube(self):
     """
     Test that the utility returns the expected data values
     when a 1d cube is input.
     """
     cube = self.cube[0, 0]
     expected_data = np.array([cube.data.flatten()]).T
     result = convert_cube_data_to_2d(cube)
     self.assertArrayAlmostEqual(result, expected_data, decimal=5)
Example #6
0
 def test_2d_cube(self):
     """
     Test that the utility returns the expected data values
     when a 2d cube is input.
     """
     cube = next(self.cube.slices_over("realization"))
     expected_data = np.array([cube.data.flatten()]).T
     result = convert_cube_data_to_2d(cube)
     self.assertArrayAlmostEqual(result, expected_data, decimal=5)
Example #7
0
    def test_1d_cube(self):
        """
        Test that the utility returns the expected data values
        when a 1d cube is input.
        """
        cube = set_up_temperature_cube()
        cube = cube[0, 0, 0, :]
        data = np.array([[226.15, 237.4, 248.65]]).T

        result = convert_cube_data_to_2d(cube)
        self.assertArrayAlmostEqual(result, data, decimal=5)
Example #8
0
    def setUp(self):
        """Set up expected inputs."""
        super().setUp()
        # Set up cubes and associated data arrays for wind speed.
        self.forecast_predictor_mean = CubeList(
            [
                self.historic_wind_speed_forecast_cube.collapsed(
                    "realization", iris.analysis.MEAN
                )
            ]
        )
        self.forecast_predictor_realizations = CubeList(
            [(self.historic_wind_speed_forecast_cube.copy())]
        )
        cube = self.historic_wind_speed_forecast_cube.collapsed(
            "realization", iris.analysis.MEAN
        )

        altitude_cube = cube[0].copy(data=np.reshape(np.arange(0, 45, 5), (3, 3)))
        altitude_cube.rename("altitude")
        altitude_cube.units = "m"
        for coord in [
            "forecast_period",
            "forecast_reference_time",
            "realization",
            "time",
        ]:
            altitude_cube.remove_coord(coord)

        self.forecast_predictor_mean_additional_predictor = CubeList(
            [
                self.historic_wind_speed_forecast_cube.collapsed(
                    "realization", iris.analysis.MEAN
                ),
                altitude_cube,
            ]
        )

        self.forecast_variance = self.historic_wind_speed_forecast_cube.collapsed(
            "realization", iris.analysis.VARIANCE
        )
        self.truth = self.historic_wind_speed_forecast_cube.collapsed(
            "realization", iris.analysis.MAX
        )
        self.forecast_predictor_data = (
            self.forecast_predictor_mean[0].data.flatten().astype(np.float64)
        )
        self.forecast_predictor_data_realizations = convert_cube_data_to_2d(
            self.historic_wind_speed_forecast_cube.copy()
        ).astype(np.float64)
        self.forecast_variance_data = self.forecast_variance.data.flatten().astype(
            np.float64
        )
        self.truth_data = self.truth.data.flatten().astype(np.float64)
Example #9
0
 def test_5d_cube(self):
     """
     Test that the utility returns the expected data values
     when a 5d cube is input.
     """
     cube = add_coordinate(self.cube, [5, 10], "height", coord_units="m")
     expected_data = np.array([
         cube.data[:, 0, :, :].flatten(),
         cube.data[:, 1, :, :].flatten(),
         cube.data[:, 2, :, :].flatten(),
     ]).T
     result = convert_cube_data_to_2d(cube)
     self.assertArrayAlmostEqual(result, expected_data, decimal=5)
Example #10
0
    def test_5d_cube(self):
        """
        Test that the utility returns the expected data values
        when a 5d cube is input.
        """
        cube1 = set_up_temperature_cube()
        height_coord = iris.coords.AuxCoord([5], standard_name="height")
        cube1.add_aux_coord(height_coord)

        cube2 = set_up_temperature_cube()
        height_coord = iris.coords.AuxCoord([10], standard_name="height")
        cube2.add_aux_coord(height_coord)

        cubes = iris.cube.CubeList([cube1, cube2])
        cube = cubes.merge_cube()

        data = np.array([
            [226.15, 230.15, 232.15],
            [237.4, 241.4, 243.4],
            [248.65, 252.65, 254.65],
            [259.9, 263.9, 265.9],
            [271.15, 275.15, 277.15],
            [282.4, 286.4, 288.4],
            [293.65, 297.65, 299.65],
            [304.9, 308.9, 310.9],
            [316.15, 320.15, 322.15],
            [226.15, 230.15, 232.15],
            [237.4, 241.4, 243.4],
            [248.65, 252.65, 254.65],
            [259.9, 263.9, 265.9],
            [271.15, 275.15, 277.15],
            [282.4, 286.4, 288.4],
            [293.65, 297.65, 299.65],
            [304.9, 308.9, 310.9],
            [316.15, 320.15, 322.15],
        ])

        result = convert_cube_data_to_2d(cube)
        self.assertArrayAlmostEqual(result, data, decimal=5)
    def _calculate_location_parameter_from_realizations(
            self, optimised_coeffs):
        """
        Function to calculate the location parameter when the ensemble
        realizations are the predictor.

        Further information is available in the :mod:`module level docstring \
<improver.calibration.ensemble_calibration>`.

        Args:
            optimised_coeffs (dict):
                A dictionary containing the calibration coefficient names as
                keys with their corresponding values.

        Returns:
            numpy.ndarray:
                Location parameter calculated using the ensemble realizations
                as the predictor.
        """
        forecast_predictor = self.current_forecast

        # Calculate location parameter = a + b1*X1 .... + bn*Xn, where X is the
        # ensemble realizations. The number of b and X terms depends upon the
        # number of ensemble realizations. In this case, b = beta^2.
        beta_values = np.array([], dtype=np.float32)
        for key in optimised_coeffs.keys():
            if key.startswith("beta"):
                beta_values = np.append(beta_values, optimised_coeffs[key])
        a_and_b = np.append(optimised_coeffs["alpha"], beta_values**2)
        forecast_predictor_flat = (
            convert_cube_data_to_2d(forecast_predictor))
        xy_shape = next(forecast_predictor.slices_over("realization")).shape
        col_of_ones = np.ones(np.prod(xy_shape), dtype=np.float32)
        ones_and_predictor = (
            np.column_stack((col_of_ones, forecast_predictor_flat)))
        location_parameter = (
            np.dot(ones_and_predictor, a_and_b).reshape(xy_shape).astype(
                np.float32))
        return location_parameter
Example #12
0
 def test_check_values(self):
     """Test that the utility returns the expected data values."""
     result = convert_cube_data_to_2d(self.cube)
     self.assertArrayAlmostEqual(result, self.data)
Example #13
0
 def test_basic(self):
     """Test that the utility returns an iris.cube.Cube."""
     result = convert_cube_data_to_2d(self.cube)
     self.assertIsInstance(result, np.ndarray)