Ejemplo n.º 1
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 setUp(self):
        """Use temperature cube to test with."""
        self.cube = set_up_temperature_cube()

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_temperature_cube()))

        self.coeff_names = ["gamma", "delta", "a", "beta"]
Ejemplo n.º 3
0
    def setUp(self):
        """Use temperature cube to test with."""
        self.cube = set_up_temperature_cube()

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_temperature_cube()))

        self.coeff_names = ["gamma", "delta", "a", "beta"]

        self.default_optimised_coeffs = [
            4.55819380e-06, -8.02401974e-09, 1.66667055e+00, 1.00000011e+00]
Ejemplo n.º 4
0
 def setUp(self):
     """
     Create a raw and calibrated cube with with forecast_reference_time and
     forecast_period coordinates.
     """
     self.raw_cube = (add_forecast_reference_time_and_forecast_period(
         set_up_temperature_cube()))
     self.post_processed_percentiles = (
         add_forecast_reference_time_and_forecast_period(
             set_up_temperature_cube()))
     self.post_processed_percentiles.coord("realization").rename(
         "percentile_over_realization")
     self.post_processed_percentiles.coord(
         "percentile_over_realization").points = [10, 50, 90]
Ejemplo n.º 5
0
 def test_fails_if_data_is_not_convertible_to_degrees(self):
     """Test code raises a ValueError if input cube is not convertible to
     degrees."""
     cube = set_up_temperature_cube()
     msg = 'Input cube cannot be converted to degrees'
     with self.assertRaisesRegexp(ValueError, msg):
         WindDirection().process(cube)
Ejemplo n.º 6
0
    def test_basic_mean_predictor(self):
        """
        Test that the plugin returns a numpy float value with
        mean as predictor.
        """
        initial_guess = [5, 1, 0, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        forecast_predictor_data = (forecast_predictor.data.flatten().astype(
            np.float32))
        forecast_variance_data = (forecast_variance.data.flatten().astype(
            np.float32))
        truth_data = truth.data.flatten().astype(np.float32)

        sqrt_pi = np.sqrt(np.pi).astype(np.float32)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        result = plugin.normal_crps_minimiser(initial_guess,
                                              forecast_predictor_data,
                                              truth_data,
                                              forecast_variance_data, sqrt_pi,
                                              predictor_of_mean_flag)

        self.assertIsInstance(result, np.float64)
        self.assertAlmostEqual(result, 16.607763767419634)
Ejemplo n.º 7
0
    def test_normal_mean_predictor_keyerror(self):
        """
        Test that the minimisation has resulted in a KeyError, if the
        distribution that has been requested was not within the dictionary
        containing the minimisation functions.
        """
        initial_guess = [
            -8.70808509e-06, 7.23255721e-06, 2.66662740e+00, 1.00000012e+00
        ]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        distribution = "foo"
        msg = "Distribution requested"
        with self.assertRaisesRegexp(KeyError, msg):
            plugin.crps_minimiser_wrapper(initial_guess, forecast_predictor,
                                          truth, forecast_variance,
                                          predictor_of_mean_flag, distribution)
    def test_truncated_normal_mean_predictor_max_iterations(self):
        """
        Test that the plugin returns a list of coefficients
        equal to specific values, when the ensemble mean is the predictor
        assuming a truncated normal distribution and the value specified
        for the MAX_ITERATIONS is overriden. The coefficients are
        calculated by minimising the CRPS and using a set default value for
        the initial guess.
        """
        warnings.simplefilter("always")
        initial_guess = [5, 1, 0, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed(
            "realization", iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        plugin.MAX_ITERATIONS = 400
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(
            initial_guess, forecast_predictor, truth, forecast_variance,
            predictor_of_mean_flag, distribution)
        self.assertArrayAlmostEqual(
            result, [-0.303343, -0.022553, 0.008502, 1.009565])
Ejemplo n.º 9
0
 def setUp(self):
     """
     Create a cube with forecast_reference_time and
     forecast_period coordinates.
     """
     self.cube = (add_forecast_reference_time_and_forecast_period(
         set_up_temperature_cube()))
Ejemplo n.º 10
0
    def test_truncated_normal_catch_warnings_percentage_change(
            self, warning_list=None):
        """
        Test that two warnings are generated if the minimisation
        does not result in a convergence. The first warning reports a that
        the minimisation did not result in convergence, whilst the second
        warning reports that the percentage change in the final iteration was
        greater than the tolerated value.
        The ensemble mean is the predictor.
        """
        initial_guess = [5000, 1, 0, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(initial_guess,
                                               forecast_predictor, truth,
                                               forecast_variance,
                                               predictor_of_mean_flag,
                                               distribution)
        self.assertTrue(len(warning_list) == 2)
        self.assertTrue(
            any(item.category == UserWarning for item in warning_list))
        self.assertTrue("Minimisation did not result in convergence after" in
                        str(warning_list[0]))
        self.assertTrue("The final iteration resulted in a percentage "
                        "change" in str(warning_list[1]))
Ejemplo n.º 11
0
    def test_basic_members_predictor(self):
        """
        Test that the plugin returns a numpy float array with ensemble members
        as predictor.
        """
        initial_guess = [5, 1, 0, 1, 1, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.copy()
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        forecast_predictor_data = (
            convert_cube_data_to_2d(forecast_predictor).astype(np.float32))
        forecast_variance_data = (forecast_variance.data.flatten().astype(
            np.float32))
        truth_data = truth.data.flatten().astype(np.float32)

        sqrt_pi = np.sqrt(np.pi).astype(np.float32)

        predictor_of_mean_flag = "members"

        plugin = Plugin()
        result = plugin.normal_crps_minimiser(initial_guess,
                                              forecast_predictor_data,
                                              truth_data,
                                              forecast_variance_data, sqrt_pi,
                                              predictor_of_mean_flag)

        self.assertIsInstance(result, np.float64)
        self.assertAlmostEqual(result, 4886.94724835)
Ejemplo n.º 12
0
    def test_basic_truncated_normal_members_predictor(self):
        """Test that the plugin returns a numpy array."""
        initial_guess = [5, 1, 0, 1, 1, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.copy()
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "members"

        plugin = Plugin()
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(initial_guess,
                                               forecast_predictor, truth,
                                               forecast_variance,
                                               predictor_of_mean_flag,
                                               distribution)
        self.assertIsInstance(result, np.ndarray)
        self.assertArrayAlmostEqual(result, [
            6.24021609e+00, 1.35694934e+00, 1.84642787e-03, 5.55444682e-01,
            5.04367388e-01, 6.68575194e-01
        ])
Ejemplo n.º 13
0
    def test_truncated_normal_catch_warnings(self, warning_list=None):
        """
        Test that a warning is generated if the minimisation
        does not result in a convergence.
        The ensemble mean is the predictor.
        """
        initial_guess = [5, 1, 0, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(initial_guess,
                                               forecast_predictor, truth,
                                               forecast_variance,
                                               predictor_of_mean_flag,
                                               distribution)
        self.assertTrue(len(warning_list) == 1)
        self.assertTrue(
            any(item.category == UserWarning for item in warning_list))
        self.assertTrue("Minimisation did not result in convergence after" in
                        str(warning_list[0]))
Ejemplo n.º 14
0
    def test_truncated_normal_members_predictor_keyerror(self):
        """
        Test that the minimisation has resulted in a successful convergence,
        and that the object returned is an OptimizeResult object, when the
        ensemble members are the predictor.
        """
        initial_guess = [
            -8.70808509e-06, 7.23255721e-06, 2.66662740e+00, 1.00000012e+00
        ]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "members"

        plugin = Plugin()
        distribution = "foo"
        msg = "Distribution requested"
        with self.assertRaisesRegexp(KeyError, msg):
            plugin.crps_minimiser_wrapper(initial_guess, forecast_predictor,
                                          truth, forecast_variance,
                                          predictor_of_mean_flag, distribution)
    def setUp(self):
        """Set up temperature cube."""
        current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_temperature_cube()))

        self.cube_data = current_temperature_forecast_cube.data

        current_temperature_spot_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_spot_temperature_cube()))
        self.cube_spot_data = (
            current_temperature_spot_forecast_cube.data)

        for cube in current_temperature_forecast_cube.slices_over(
                "realization"):
            cube.remove_coord("realization")
            break
        self.current_temperature_forecast_cube = cube

        for cube in current_temperature_spot_forecast_cube.slices_over(
                "realization"):
            cube.remove_coord("realization")
            break
        self.current_temperature_spot_forecast_cube = cube
Ejemplo n.º 16
0
    def test_basic_truncated_normal_mean_predictor(self):
        """
        Test that the plugin returns a numpy float value.
        The ensemble mean is the predictor.
        """
        initial_guess = [5, 1, 0, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(initial_guess,
                                               forecast_predictor, truth,
                                               forecast_variance,
                                               predictor_of_mean_flag,
                                               distribution)
        self.assertIsInstance(result, np.ndarray)
        self.assertArrayAlmostEqual(
            result, [-0.08169791, -0.09784413, 0.00822535, 1.00956199])
Ejemplo n.º 17
0
 def setUp(self):
     """Use temperature cube to test with."""
     self.cube = set_up_temperature_cube()
     self.minimum_value = self.cube.data.min()
     self.maximum_value = self.cube.data.max()
     self.processed_cube = self.cube.copy(data=self.cube.data * 2.0 -
                                          self.cube.data.mean())
Ejemplo n.º 18
0
    def test_basic_mean_predictor_bad_value(self):
        """
        Test that the plugin returns a numpy float64 value
        and that the value matches the BAD_VALUE, when the appropriate
        condition is found.
        The ensemble mean is the predictor.
        """
        initial_guess = [1e65, 1e65, 1e65, 1e65]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.collapsed("realization", iris.analysis.MEAN)
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        forecast_predictor_data = (forecast_predictor.data.flatten().astype(
            np.float32))
        forecast_variance_data = (forecast_variance.data.flatten().astype(
            np.float32))
        truth_data = truth.data.flatten().astype(np.float32)

        sqrt_pi = np.sqrt(np.pi).astype(np.float32)

        predictor_of_mean_flag = "mean"

        plugin = Plugin()
        result = plugin.normal_crps_minimiser(initial_guess,
                                              forecast_predictor_data,
                                              truth_data,
                                              forecast_variance_data, sqrt_pi,
                                              predictor_of_mean_flag)

        self.assertIsInstance(result, np.float64)
        self.assertAlmostEqual(result, plugin.BAD_VALUE)
Ejemplo n.º 19
0
 def setUp(self):
     """Use temperature cube to test with."""
     self.cube = set_up_temperature_cube()
     self.cube_ukv = self.cube.extract(iris.Constraint(realization=1))
     self.cube_ukv.remove_coord('realization')
     self.cube_ukv.attributes.update({'grid_id': 'ukvx_standard_v1'})
     self.cube_ukv.attributes.update({'title':
                                      'Operational UKV Model Forecast'})
     self.cube_ukv_t1 = self.cube_ukv.copy()
     self.cube_ukv_t2 = self.cube_ukv.copy()
     add_forecast_reference_time_and_forecast_period(self.cube_ukv,
                                                     fp_point=4.0)
     add_forecast_reference_time_and_forecast_period(self.cube_ukv_t1,
                                                     fp_point=5.0)
     add_forecast_reference_time_and_forecast_period(self.cube_ukv_t2,
                                                     fp_point=6.0)
     add_forecast_reference_time_and_forecast_period(self.cube,
                                                     fp_point=7.0)
     self.cube.attributes.update({'grid_id': 'enukx_standard_v1'})
     self.cube.attributes.update({'title':
                                  'Operational Mogreps UK Model Forecast'})
     self.prob_ukv = set_up_probability_above_threshold_temperature_cube()
     self.prob_ukv.attributes.update({'grid_id': 'ukvx_standard_v1'})
     self.prob_ukv.attributes.update({'title':
                                      'Operational UKV Model Forecast'})
     self.prob_enuk = set_up_probability_above_threshold_temperature_cube()
     self.prob_enuk.attributes.update({'grid_id': 'enukx_standard_v1'})
     self.prob_enuk.attributes.update(
         {'title':
          'Operational Mogreps UK Model Forecast'})
 def setUp(self):
     """Use temperature cube to test with."""
     self.cube = set_up_temperature_cube()
     self.optimised_coeffs = [
         4.55819380e-06, -8.02401974e-09, 1.66667055e+00, 1.00000011e+00
     ]
     self.coeff_names = ["gamma", "delta", "a", "beta"]
Ejemplo n.º 21
0
    def test_truncated_normal_members_predictor_max_iterations(self):
        """
        Test that the plugin returns a list of coefficients
        equal to specific values, when the ensemble members are the predictor
        assuming a truncated normal distribution and the value specified
        for the MAX_ITERATIONS is overriden. The coefficients are
        calculated by minimising the CRPS and using a set default value for
        the initial guess.
        """
        initial_guess = [5, 1, 0, 1, 1, 1]
        initial_guess = np.array(initial_guess, dtype=np.float32)
        cube = set_up_temperature_cube()

        forecast_predictor = cube.copy()
        forecast_variance = cube.collapsed("realization",
                                           iris.analysis.VARIANCE)
        truth = cube.collapsed("realization", iris.analysis.MAX)

        predictor_of_mean_flag = "members"

        plugin = Plugin()
        plugin.MAX_ITERATIONS = 400
        distribution = "truncated gaussian"
        result = plugin.crps_minimiser_wrapper(initial_guess,
                                               forecast_predictor, truth,
                                               forecast_variance,
                                               predictor_of_mean_flag,
                                               distribution)
        self.assertArrayAlmostEqual(
            result, [5.375955, 1.45785, 0.002567, 0.193423, 0.55406, 0.811599])
 def setUp(self):
     """Set up temperature cube."""
     self.current_temperature_forecast_cube = (
         add_forecast_reference_time_and_forecast_period(
             set_up_temperature_cube()))
     self.current_temperature_spot_forecast_cube = (
         add_forecast_reference_time_and_forecast_period(
             set_up_spot_temperature_cube()))
 def setUp(self):
     """Set up temperature cube for testing."""
     cube = (add_forecast_reference_time_and_forecast_period(
         set_up_temperature_cube()))
     percentile_points = np.arange(len(cube.coord("realization").points))
     cube.coord("realization").points = percentile_points
     cube.coord("realization").rename("percentile_over_realization")
     self.current_temperature_cube = cube
 def setUp(self):
     """Use temperature cube to test with."""
     self.cube = set_up_temperature_cube()
     self.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]])
Ejemplo n.º 25
0
 def setUp(self):
     """Set up a cubelist for testing"""
     list_of_cubes = []
     for time in range(402193, 402196):
         cube = set_up_temperature_cube()[0]
         cube.coord('time').points = [time]
         cube.coord('time').bounds = [[time - 1, time]]
         list_of_cubes.append(cube)
     self.cubelist = iris.cube.CubeList(list_of_cubes)
Ejemplo n.º 26
0
 def setUp(self):
     """Set up variables for use in testing."""
     self.directory = mkdtemp()
     self.filepath = os.path.join(self.directory, "temp.nc")
     self.cube = set_up_temperature_cube()
     iris.save(self.cube, self.filepath)
     self.realization_points = np.array([0, 1, 2])
     self.time_points = np.array([402192.5])
     self.latitude_points = np.array([-45., 0., 45.])
     self.longitude_points = np.array([120., 150., 180.])
 def setUp(self):
     """Use temperature cube to test with."""
     self.cube = set_up_temperature_cube()
     self.cube_ukv = self.cube.extract(iris.Constraint(realization=1))
     self.cube_ukv.remove_coord('realization')
     self.cube_ukv.attributes.update({'grid_id': 'ukvx_standard_v1'})
     self.cube_ukv.attributes.update(
         {'title': 'Operational UKV Model Forecast'})
     self.cube.attributes.update({'grid_id': 'enukx_standard_v1'})
     self.cube.attributes.update(
         {'title': 'Operational Mogreps UK Model Forecast'})
Ejemplo n.º 28
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)
Ejemplo n.º 29
0
 def test_ordering_for_realization_coordinate(self):
     """Test that the cube has been reordered, if it is originally in an
     undesirable order and the cube contains a "realization" coordinate."""
     cube = set_up_temperature_cube()
     cube.transpose([3, 2, 1, 0])
     save_netcdf(cube, self.filepath)
     result = load_cube(self.filepath)
     self.assertEqual(result.coord_dims("realization")[0], 0)
     self.assertEqual(result.coord_dims("time")[0], 1)
     self.assertArrayAlmostEqual(result.coord_dims("latitude")[0], 2)
     self.assertArrayAlmostEqual(result.coord_dims("longitude")[0], 3)
Ejemplo n.º 30
0
    def test_3d_cube(self):
        """
        Test that the utility returns the expected data values
        when a 3d cube is input.
        """
        cube = set_up_temperature_cube()
        cube = cube[0]
        data = np.array([[226.15, 237.4, 248.65, 259.9, 271.15,
                          282.4, 293.65, 304.9, 316.15]]).T

        result = convert_cube_data_to_2d(cube)
        self.assertArrayAlmostEqual(result, data)