def test_probabilities_not_monotonically_increasing(self,
                                                        warning_list=None):
        """
        Test that the plugin raises a Warning when the probabilities
        of the Cumulative Distribution Function are not monotonically
        increasing.
        """
        data = np.array([0.05, 0.7, 0.95])
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_threshold_cube(
                    data, "air_temperature", "degreesC",
                    forecast_thresholds=[8, 10, 12], y_dimension_length=1,
                    x_dimension_length=1, spp__relative_to_threshold='above')))
        cube = self.current_temperature_forecast_cube
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        warning_msg = "The probability values used to construct the"
        plugin._probabilities_to_percentiles(
            cube, percentiles, bounds_pairing)
        self.assertTrue(any(warning_msg in str(item)
                            for item in warning_list))
    def test_lots_of_probability_thresholds(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles, if there are lots of thresholds.
        """
        input_probs_1d = np.linspace(1, 0, 30)
        input_probs = np.tile(input_probs_1d, (3, 3, 1, 1)).T

        data = np.array([[[[2.9, 2.9, 2.9],
                           [2.9, 2.9, 2.9],
                           [2.9, 2.9, 2.9]]],
                         [[[14.5, 14.5, 14.5],
                           [14.5, 14.5, 14.5],
                           [14.5, 14.5, 14.5]]],
                         [[[26.099998, 26.099998, 26.099998],
                           [26.099998, 26.099998, 26.099998],
                           [26.099998, 26.099998, 26.099998]]]],
                        dtype=np.float32)

        temperature_values = np.arange(0, 30)
        cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_threshold_cube(
                    input_probs, "air_temperature", "degreesC",
                    forecast_thresholds=temperature_values,
                    spp__relative_to_threshold='above')))
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(
            cube, percentiles, bounds_pairing)

        self.assertArrayAlmostEqual(result.data, data)
    def test_simple_check_data_below(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles when input probabilities are given
        for being below a threshold.

        The input cube contains probabilities that values are below a given
        threshold.
        """
        expected = np.array([8.4, 10.61538462, 11.84615385])
        expected = expected[:, np.newaxis, np.newaxis, np.newaxis]

        data = np.array([0.95, 0.3, 0.05])[::-1]
        data = data[:, np.newaxis, np.newaxis, np.newaxis]

        self.current_temperature_forecast_cube = (
            add_forecast_reference_time_and_forecast_period(
                set_up_probability_threshold_cube(
                    data, "air_temperature", "degreesC",
                    forecast_thresholds=[8, 10, 12], y_dimension_length=1,
                    x_dimension_length=1, spp__relative_to_threshold='above')))
        cube = self.current_temperature_forecast_cube
        cube.coord(var_name="threshold"
                   ).attributes["spp__relative_to_threshold"] = "below"
        percentiles = [10, 50, 90]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(
            cube, percentiles, bounds_pairing)
        self.assertArrayAlmostEqual(result.data, expected)
Exemple #4
0
 def setUp(self):
     """Set up cube """
     data = np.array([0.1, 0.3, 0.4, 0.2, 0.6, 0.7, 0.4, 0.2, 0.1,
                      0.2, 0.2, 0.5, 0.1, 0.3, 0.9, 0.8, 0.5, 0.3,
                      0.6, 0.3, 0.5, 0.6, 0.8, 0.2,
                      0.8, 0.1, 0.2]).reshape(3, 1, 3, 3)
     self.cube = set_up_probability_threshold_cube(
         data, 'air_temperature', 'K', spp__relative_to_threshold='above')
     self.wxcode = np.array(list(WX_DICT.keys()))
     self.wxmeaning = " ".join(WX_DICT.values())
    def test_check_data_multiple_timesteps(self):
        """
        Test that the plugin returns an Iris.cube.Cube with the expected
        data values for the percentiles.
        """
        expected = np.array([[[[8., 8.],
                               [-8., 8.66666667]],
                              [[8., -16.],
                               [8., -16.]]],
                             [[[12., 12.],
                               [12., 12.]],
                              [[10.5, 10.],
                               [10.5, 10.]]],
                             [[[31., 31.],
                               [31., 31.]],
                              [[11.5, 11.33333333],
                               [11.5, 12.]]]], dtype=np.float32)

        data = np.array([[[[0.8, 0.8],
                           [0.7, 0.9]],
                          [[0.8, 0.6],
                           [0.8, 0.6]]],
                         [[[0.6, 0.6],
                           [0.6, 0.6]],
                          [[0.5, 0.4],
                           [0.5, 0.4]]],
                         [[[0.4, 0.4],
                           [0.4, 0.4]],
                          [[0.1, 0.1],
                           [0.1, 0.2]]]], dtype=np.float32)

        cube = set_up_probability_threshold_cube(
            data, "air_temperature", "degreesC", timesteps=2,
            x_dimension_length=2, y_dimension_length=2,
            spp__relative_to_threshold='above')
        self.probability_cube = (
            add_forecast_reference_time_and_forecast_period(
                cube, time_point=np.array([402295.0, 402296.0]),
                fp_point=[2.0, 3.0]))
        cube = self.probability_cube
        percentiles = [20, 60, 80]
        bounds_pairing = (-40, 50)
        plugin = Plugin()
        result = plugin._probabilities_to_percentiles(
            cube, percentiles, bounds_pairing)
        self.assertArrayAlmostEqual(result.data, expected, decimal=5)
Exemple #6
0
def set_up_wxcubes():
    """Set up cubes required for Weather Symbols """
    data_snow = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                          0.0, 0.0, 0.0]).reshape(3, 1, 3, 3)
    snowfall_rate = (
        set_up_probability_threshold_cube(
            data_snow,
            'lwe_snowfall_rate',
            'm s-1',
            spp__relative_to_threshold='above',
            forecast_thresholds=np.array([8.33333333e-09,
                                          2.77777778e-08,
                                          2.77777778e-07])))

    data_rain = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
                          0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                          0.0, 0.0, 0.0]).reshape(3, 1, 3, 3)
    rainfall_rate = (
        set_up_probability_threshold_cube(
            data_rain,
            'rainfall_rate',
            'm s-1',
            spp__relative_to_threshold='above',
            forecast_thresholds=np.array([8.33333333e-09,
                                          2.77777778e-08,
                                          2.77777778e-07])))

    data_snowv = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                           0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                           0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                           0.0, 0.0, 0.0]).reshape(3, 1, 3, 3)
    snowfall_vicinity = (
        set_up_probability_threshold_cube(
            data_snowv,
            'lwe_snowfall_rate_in_vicinity',
            'm s-1',
            spp__relative_to_threshold='above',
            forecast_thresholds=np.array([8.33333333e-09,
                                          2.77777778e-08,
                                          2.77777778e-07])))

    data_rainv = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0,
                           0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 1.0,
                           0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                           0.0, 0.0, 0.0]).reshape(3, 1, 3, 3)
    rainfall_vicinity = (
        set_up_probability_threshold_cube(
            data_rainv,
            'rainfall_rate_in_vicinity',
            'm s-1',
            spp__relative_to_threshold='above',
            forecast_thresholds=np.array([8.33333333e-09,
                                          2.77777778e-08,
                                          2.77777778e-07])))

    data_cloud = np.array([0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0,
                           0.0, 0.0, 0.0, 0.0, 1.0, 1.0,
                           0.0, 0.0, 1.0]).reshape(2, 1, 3, 3)
    cloud = (set_up_probability_threshold_cube(
        data_cloud,
        'cloud_area_fraction',
        '1',
        spp__relative_to_threshold='above',
        forecast_thresholds=np.array([0.1875, 0.8125])))

    data_cld_low = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0,
                             0.0, 0.0, 0.0]).reshape(1, 1, 3, 3)
    cloud_low = (
        set_up_probability_threshold_cube(
            data_cld_low,
            'low_type_cloud_area_fraction',
            '1',
            spp__relative_to_threshold='above',
            forecast_thresholds=np.array([0.85])))

    data_vis = np.array([0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
                         0.0, 0.0, 1.0, 1.0, 0.0, 0.0,
                         0.0, 1.0, 0.0]).reshape(2, 1, 3, 3)
    visibility = (
        set_up_probability_threshold_cube(
            data_vis,
            'visibility_in_air',
            'm',
            spp__relative_to_threshold='below',
            forecast_thresholds=np.array([1000.0, 5000.0])))
    visibility.attributes['relative_to_threshold'] = 'below'

    cubes = iris.cube.CubeList([snowfall_rate, rainfall_rate,
                                snowfall_vicinity, rainfall_vicinity,
                                cloud, cloud_low,
                                visibility])
    return cubes
Exemple #7
0
def set_up_wxcubes_global():
    """Set up cubes required for Weather Symbols """
    data_snow = np.array([
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    ]).reshape(3, 1, 3, 3)
    snowfall_rate = (set_up_probability_threshold_cube(
        data_snow,
        'lwe_snowfall_rate',
        'm s-1',
        relative_to_threshold='above',
        forecast_thresholds=np.array(
            [8.33333333e-09, 2.77777778e-08, 2.77777778e-07])))

    data_rain = np.array([
        0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0,
        0.0, 1.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0
    ]).reshape(3, 1, 3, 3)
    rainfall_rate = (set_up_probability_threshold_cube(
        data_rain,
        'rainfall_rate',
        'm s-1',
        relative_to_threshold='above',
        forecast_thresholds=np.array(
            [8.33333333e-09, 2.77777778e-08, 2.77777778e-07])))

    data_cloud = np.array([
        0.0, 1.0, 0.0, 0.0, 1.0, 1.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 1.0,
        1.0, 0.0, 0.0, 1.0
    ]).reshape(2, 1, 3, 3)
    cloud = (set_up_probability_threshold_cube(data_cloud,
                                               'cloud_area_fraction',
                                               '1',
                                               relative_to_threshold='above',
                                               forecast_thresholds=np.array(
                                                   [0.1875, 0.8125])))

    data_cld_1000ft = np.array([0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 0.0, 0.0,
                                0.0]).reshape(1, 1, 3, 3)
    cloud_below_1000ft = (set_up_probability_threshold_cube(
        data_cld_1000ft, 'cloud_area_fraction_assuming_only'
        '_consider_surface_to_1000_feet_asl',
        '1',
        relative_to_threshold='above',
        forecast_thresholds=np.array([0.85])))

    data_vis = np.array([
        0.0, 0.0, 0.0, 1.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 1.0, 1.0, 0.0,
        0.0, 0.0, 1.0, 0.0
    ]).reshape(2, 1, 3, 3)
    visibility = (set_up_probability_threshold_cube(
        data_vis,
        'visibility_in_air',
        'm',
        relative_to_threshold='below',
        forecast_thresholds=np.array([1000.0, 5000.0])))
    visibility.attributes['relative_to_threshold'] = 'below'

    cubes = iris.cube.CubeList(
        [snowfall_rate, rainfall_rate, cloud, cloud_below_1000ft, visibility])
    return cubes