def test_radii_varying_with_lead_time_with_interpolation_check_data(self):
        """Test that a cube with the correct data is returned for the
        following conditions:
        1. The radius varies with lead time.
        2. Linear interpolation is required to create values for the radii
        which are required but were not specified within the 'radii'
        argument."""

        expected = np.ones_like(self.multi_time_cube.data)
        expected[0, 6:9, 6:9] = (
            [0.91666667, 0.875, 0.91666667],
            [0.875, 0.83333333, 0.875],
            [0.91666667, 0.875, 0.91666667],
        )

        expected[1, 5:10, 5:10] = SINGLE_POINT_RANGE_3_CENTROID

        expected[2, 4:11, 4:11] = (
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9825, 0.97, 0.9625, 0.96, 0.9625, 0.97, 0.9825],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
        )

        radii = [5600, 9500]
        lead_times = [2, 4]
        neighbourhood_method = CircularNeighbourhood()
        plugin = NBHood(neighbourhood_method, radii, lead_times)
        result = plugin(self.multi_time_cube)
        self.assertArrayAlmostEqual(result.data, expected)
    def test_radii_varying_with_lead_time_check_data(self):
        """
        Test that the expected data is produced when the radius
        varies with lead time and that a cube is returned.
        """

        expected = np.ones_like(self.multi_time_cube.data)
        expected[0, 6:9, 6:9] = (
            [0.91666667, 0.875, 0.91666667],
            [0.875, 0.83333333, 0.875],
            [0.91666667, 0.875, 0.91666667],
        )

        expected[1, 5:10, 5:10] = SINGLE_POINT_RANGE_3_CENTROID

        expected[2, 4:11, 4:11] = (
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9825, 0.97, 0.9625, 0.96, 0.9625, 0.97, 0.9825],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
        )

        radii = [5600, 7600, 9500]
        lead_times = [2, 3, 4]
        neighbourhood_method = CircularNeighbourhood()
        plugin = NBHood(neighbourhood_method, radii, lead_times)
        result = plugin(self.multi_time_cube)
        self.assertArrayAlmostEqual(result.data, expected)
        self.assertIsInstance(result, Cube)
 def test_sum_or_fraction(self):
     """Test that a ValueError is raised if an invalid option is passed
     in for sum_or_fraction."""
     sum_or_fraction = "nonsense"
     msg = "option is invalid"
     with self.assertRaisesRegex(ValueError, msg):
         CircularNeighbourhood(sum_or_fraction=sum_or_fraction)
Exemplo n.º 4
0
 def test_callable(self):
     """Test that the __repr__ returns the expected string."""
     result = str(NBHood(CircularNeighbourhood(), 10000))
     msg = ('<BaseNeighbourhoodProcessing: neighbourhood_method: '
            '<CircularNeighbourhood: weighted_mode: True, '
            'sum_or_fraction: fraction>; '
            'radii: 10000.0; lead_times: None; ens_factor: 1.0>')
     self.assertEqual(result, msg)
Exemplo n.º 5
0
 def test_realizations_and_source_realizations_fails(self):
     """Raises error if realizations and source realizations both set."""
     self.cube.attributes.update({'source_realizations': [0, 1, 2, 3]})
     msg = ('Realizations and attribute source_realizations should not'
            ' both be set')
     with self.assertRaisesRegexp(ValueError, msg):
         neighbourhood_method = CircularNeighbourhood()
         NBHood(neighbourhood_method, self.RADIUS).process(self.cube)
 def test_mask_cube(self):
     """Test that a NotImplementedError is raised, if a mask cube is passed
     in when using a circular neighbourhood, as this option is not
     supported."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2),), num_grid_points=5)[0, 0]
     msg = "The use of a mask cube with a circular kernel is " "not yet implemented."
     with self.assertRaisesRegex(NotImplementedError, msg):
         CircularNeighbourhood().run(cube, self.RADIUS, mask_cube=cube)
 def test_basic(self):
     """Test that the plugin returns an iris.cube.Cube."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     ranges = (2, 2)
     result = (CircularNeighbourhood(
         weighted_mode=False).apply_circular_kernel(cube, ranges))
     self.assertIsInstance(result, Cube)
 def test_single_point_range_1(self):
     """Test behaviour with a non-zero point with unit range."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     expected[0][0][7][7] = 0.0
     ranges = (1, 1)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 9
0
 def test_basic_float_cube_lead_times_is_none(self):
     """Test _find_radii returns an unaltered radius if
     the lead times are none, and this radius is a float."""
     neighbourhood_method = CircularNeighbourhood()
     radius = 6300
     plugin = NBHood(neighbourhood_method, radius)
     result = plugin._find_radii(cube_lead_times=None)
     expected_result = 6300.0
     self.assertIsInstance(result, float)
     self.assertAlmostEqual(result, expected_result)
Exemplo n.º 10
0
 def test_single_point(self):
     """Test behaviour for a single non-zero grid cell."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][5 + index][5:10] = slice_
     ranges = (3, 3)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 11
0
 def test_single_point_almost_corner(self):
     """Test behaviour for a non-zero grid cell quite near a corner."""
     cube = set_up_cube(zero_point_indices=[(0, 0, 2, 2)
                                            ])  # Just within corner range.
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][index][0:5] = slice_
     ranges = (3, 3)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 12
0
 def test_single_point_range_5(self):
     """Test behaviour with a non-zero point with a large range."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     for time_index in range(len(expected)):
         for index, slice_ in enumerate(SINGLE_POINT_RANGE_5_CENTROID):
             expected[0][time_index][3 + index][3:12] = slice_
     ranges = (5, 5)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 13
0
    def test_single_point_range_1(self):
        """Test behaviour with a non-zero point and unit range."""

        self.cube.data[7, 7] = 0

        expected = np.ones_like(self.cube.data)
        expected[7][7] = 0.0
        ranges = 1
        result = CircularNeighbourhood(
            weighted_mode=True).apply_circular_kernel(self.cube, ranges)
        self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 14
0
 def test_radii_varying_with_lead_time_mismatch(self):
     """
     Test that the desired error message is raised, if there is a mismatch
     between the number of radii and the number of lead times.
     """
     radii = [10000, 20000, 30000]
     lead_times = [2, 3]
     msg = "There is a mismatch in the number of radii"
     with self.assertRaisesRegexp(ValueError, msg):
         neighbourhood_method = CircularNeighbourhood()
         NBHood(neighbourhood_method, radii, lead_times=lead_times)
Exemplo n.º 15
0
 def test_basic_float_cube_lead_times_is_none(self):
     """Test _find_radii returns a float with the correct value."""
     neighbourhood_method = CircularNeighbourhood()
     ens_factor = 0.8
     num_ens = 2.0
     radius = 6300
     plugin = NBHood(neighbourhood_method, radius, ens_factor=ens_factor)
     result = plugin._find_radii(num_ens)
     expected_result = 3563.8181771801998
     self.assertIsInstance(result, float)
     self.assertAlmostEquals(result, expected_result)
Exemplo n.º 16
0
 def test_basic_returns_float(self):
     """Test returns float."""
     neighbourhood_method = CircularNeighbourhood()
     radii = 20.0
     ens_factor = 1.0
     num_ens = 3.0
     width = 20.0
     result = NBHood(neighbourhood_method, radii,
                     ens_factor=ens_factor).adjust_nsize_for_ens(
                         num_ens, width)
     self.assertIsInstance(result, float)
 def test_single_point_adjacent_edge(self):
     """Test behaviour for a single non-zero grid cell near the edge."""
     cube = set_up_cube(
         zero_point_indices=[(0, 0, 7, 1)])  # Range 3 goes over the edge.
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][5 + index][0:4] = slice_[1:]
     ranges = 3
     result = (
         CircularNeighbourhood(
             weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 18
0
 def test_returns_unchanged_for_ens1(self):
     """Test returns unchanged value when num_ens = 1.0."""
     expected = 20.0
     neighbourhood_method = CircularNeighbourhood()
     radii = 20.0
     ens_factor = 0.8
     num_ens = 1.0
     width = 20.0
     result = NBHood(neighbourhood_method, radii,
                     ens_factor=ens_factor).adjust_nsize_for_ens(
                         num_ens, width)
     self.assertAlmostEqual(result, expected)
 def test_multiple_realizations(self):
     """Test when the cube has a realization dimension."""
     cube = set_up_cube(num_realization_points=4)
     radii = 5600
     neighbourhood_method = CircularNeighbourhood()
     result = NBHood(neighbourhood_method, radii).process(cube)
     self.assertIsInstance(result, Cube)
     expected = np.ones([4, 1, 16, 16])
     expected[0, 0, 6:9, 6:9] = ([0.91666667, 0.875,
                                  0.91666667], [0.875, 0.83333333, 0.875],
                                 [0.91666667, 0.875, 0.91666667])
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 20
0
 def test_returns_adjusted_values(self):
     """Test returns the correct values."""
     expected = 9.2376043070399998
     neighbourhood_method = CircularNeighbourhood()
     radii = 20.0
     ens_factor = 0.8
     num_ens = 3.0
     width = 20.0
     result = NBHood(neighbourhood_method, radii,
                     ens_factor=ens_factor).adjust_nsize_for_ens(
                         num_ens, width)
     self.assertAlmostEqual(result, expected)
Exemplo n.º 21
0
    def test_single_point_range_5(self):
        """Test behaviour with a non-zero point and a large range."""

        self.cube.data[7, 7] = 0

        expected = np.ones_like(self.cube.data)
        for index, slice_ in enumerate(SINGLE_POINT_RANGE_5_CENTROID):
            expected[3 + index][3:12] = slice_
        ranges = 5
        result = CircularNeighbourhood(
            weighted_mode=True).apply_circular_kernel(self.cube, ranges)
        self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 22
0
 def test_no_realizations(self):
     """Test when the array has no realization coord."""
     cube = set_up_cube_with_no_realizations()
     radii = 5600
     neighbourhood_method = CircularNeighbourhood()
     result = NBHood(neighbourhood_method, radii).process(cube)
     self.assertIsInstance(result, Cube)
     expected = np.ones([1, 16, 16])
     expected[0, 6:9, 6:9] = ([0.91666667, 0.875,
                               0.91666667], [0.875, 0.83333333, 0.875],
                              [0.91666667, 0.875, 0.91666667])
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 23
0
 def test_single_point_flat(self):
     """Test behaviour for a single non-zero grid cell, flat weighting.
     Note that this gives one more grid cell range than weighted! As the
     affected area is one grid cell more in each direction, an equivalent
     range of 2 was chosen for this test."""
     cube = set_up_cube()
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_2_CENTROID_FLAT):
         expected[0][0][5 + index][5:10] = slice_
     ranges = (2, 2)
     result = (CircularNeighbourhood(
         weighted_mode=False).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 24
0
    def test_radii_varying_with_lead_time_with_interpolation(self):
        """Test that a cube is returned for the following conditions:
        1. The radius varies with lead time.
        2. Linear interpolation is required to create values for the radii
        which are required but were not specified within the 'radii'
        argument."""

        radii = [10000, 30000]
        lead_times = [2, 4]
        neighbourhood_method = CircularNeighbourhood()
        plugin = NBHood(neighbourhood_method, radii, lead_times)
        result = plugin(self.multi_time_cube)
        self.assertIsInstance(result, Cube)
    def test_basic(self):
        """Test that a cube with correct data is produced by the run method"""
        data = np.array([[0.992, 0.968, 0.96, 0.968, 0.992],
                         [0.968, 0.944, 0.936, 0.944, 0.968],
                         [0.96, 0.936, 0.928, 0.936, 0.96],
                         [0.968, 0.944, 0.936, 0.944, 0.968],
                         [0.992, 0.968, 0.96, 0.968, 0.992]])

        cube = set_up_cube(
            zero_point_indices=((0, 0, 2, 2),), num_grid_points=5)[0, 0]
        result = CircularNeighbourhood().run(cube, self.RADIUS)
        self.assertIsInstance(cube, Cube)
        self.assertArrayAlmostEqual(result.data, data)
Exemplo n.º 26
0
 def test_multi_point_multitimes(self):
     """Test behaviour for points over multiple times."""
     cube = set_up_cube(zero_point_indices=[(0, 0, 10, 10), (0, 1, 7, 7)],
                        num_time_points=2)
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][0][8 + index][8:13] = slice_
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         expected[0][1][5 + index][5:10] = slice_
     ranges = (3, 3)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 27
0
 def test_single_point_adjacent_corner(self):
     """Test behaviour for a non-zero grid cell near the corner."""
     cube = set_up_cube(
         zero_point_indices=[(0, 0, 1, 1)])  # Kernel goes over the corner.
     expected = np.ones_like(cube.data)
     for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
         if index == 0:
             continue
         expected[0][0][index - 1][0:4] = slice_[1:]
     ranges = (3, 3)
     result = (
         CircularNeighbourhood(
             weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 28
0
    def test_single_point_almost_edge(self):
        """Test behaviour for a non-zero grid cell quite near the edge."""

        self.cube.data[7, 2] = 0

        # Just within range of the edge.

        expected = np.ones_like(self.cube.data)
        for index, slice_ in enumerate(SINGLE_POINT_RANGE_3_CENTROID):
            expected[5 + index][0:5] = slice_
        ranges = 3
        result = CircularNeighbourhood(
            weighted_mode=True).apply_circular_kernel(self.cube, ranges)
        self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 29
0
    def test_radii_varying_with_lead_time_multiple_thresholds(self):
        """Test that a cube is returned for the following conditions:
        1. The radius varies wtih lead time.
        2. The cube contains multiple thresholds."""

        data = np.ones((2, 16, 16), dtype=np.float32)
        data[1, 7, 7] = 0
        cube = set_up_probability_cube(
            data,
            thresholds=[278, 279],
            spatial_grid="equalarea",
        )

        time_points = [
            datetime(2017, 11, 10, 2),
            datetime(2017, 11, 10, 3),
            datetime(2017, 11, 10, 4),
        ]
        cube = add_coordinate(
            cube,
            coord_points=time_points,
            coord_name="time",
            is_datetime="true",
            order=[1, 0, 2, 3],
        )

        lead_times = [2, 3, 4]
        radii = [5600, 7600, 9500]
        expected = np.ones_like(cube.data)
        expected[1, 0, 6:9, 6:9] = (
            [0.91666667, 0.875, 0.91666667],
            [0.875, 0.83333333, 0.875],
            [0.91666667, 0.875, 0.91666667],
        )
        expected[1, 1, 5:10, 5:10] = SINGLE_POINT_RANGE_3_CENTROID
        expected[1, 2, 4:11, 4:11] = (
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9825, 0.97, 0.9625, 0.96, 0.9625, 0.97, 0.9825],
            [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985],
            [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925],
            [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1],
        )
        neighbourhood_method = CircularNeighbourhood()
        plugin = NBHood(neighbourhood_method, radii, lead_times)
        result = plugin(cube)
        self.assertIsInstance(result, Cube)
        self.assertArrayEqual(result[0].data, expected[0])
        self.assertArrayAlmostEqual(result[1].data, expected[1])
Exemplo n.º 30
0
 def test_single_point_range_5_small_domain(self):
     """Test behaviour - non-zero point, small domain, large range.
     This exhibits the undesirable edge reflection behaviour."""
     cube = set_up_cube(zero_point_indices=((0, 0, 1, 1), ),
                        num_grid_points=4)
     expected = np.array(
         [[[[0.97636177, 0.97533402, 0.97636177, 0.97944502],
            [0.97533402, 0.97430627, 0.97533402, 0.97841727],
            [0.97636177, 0.97533402, 0.97636177, 0.97944502],
            [0.97944502, 0.97841727, 0.97944502, 0.98252826]]]])
     ranges = (5, 5)
     result = (CircularNeighbourhood(
         weighted_mode=True).apply_circular_kernel(cube, ranges))
     self.assertArrayAlmostEqual(result.data, expected)