Ejemplo n.º 1
0
 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):
         SquareNeighbourhood(sum_or_fraction=sum_or_fraction)
Ejemplo n.º 2
0
 def test_masked_array_with_nans_re_mask_false(self):
     """Test that the run method produces a cube with correct data when a
        cube containing masked nans is passed in."""
     self.cube.data = np.array([
         [np.nan, 1, 0, 1, 1],
         [1, 1, 1, 0, 0],
         [1, 0, 1, 0, 0],
         [0, 0, 1, 1, 0],
         [0, 1, 1, 0, 1],
     ])
     mask = np.array([
         [0, 0, 1, 1, 0],
         [0, 1, 1, 1, 0],
         [0, 0, 1, 1, 1],
         [0, 0, 1, 1, 0],
         [0, 0, 1, 1, 0],
     ])
     expected_array = np.array([
         [np.nan, 0.666667, 0.600000, 0.500000, 0.50],
         [1.0000, 0.750000, 0.571429, 0.428571, 0.25],
         [1.0000, 1.000000, 0.714286, 0.571429, 0.25],
         [np.nan, 1.000000, 0.666667, 0.571429, 0.25],
         [np.nan, 1.000000, 0.750000, 0.750000, 0.50],
     ])
     self.cube.data = np.ma.masked_where(mask == 0, self.cube.data)
     result = SquareNeighbourhood(re_mask=False).run(self.cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 3
0
 def test_multiple_realizations_nan(self):
     """Test that a cube with correct data is produced by the run method
     for multiple realizations and for when nans are present."""
     expected_1 = np.array([
         [np.nan, 0.8, 0.8333333, 0.8333333, 1.0],
         [1.0, 0.75, 0.77777778, 0.77777778, 1.0],
         [1.0, 0.77777778, 0.77777778, 0.77777778, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     expected_2 = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, np.nan, 1.0, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     cube = self.multi_realization_cube
     cube.data[0, 2, 2] = 0
     cube.data[0, 1, 2] = 0
     cube.data[0, 0, 0] = np.nan
     cube.data[1, 1, 1] = np.nan
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data[0], expected_1)
     self.assertArrayAlmostEqual(result.data[1], expected_2)
Ejemplo n.º 4
0
 def test_masked_array_re_mask_true(self):
     """Test that the run method produces a cube with correct data when a
     cube containing masked data is passed in and re-masking is applied."""
     self.cube.data = np.array([
         [1, 1, 0, 1, 1],
         [1, 1, 1, 0, 0],
         [1, 0, 1, 0, 0],
         [0, 0, 1, 1, 0],
         [0, 1, 1, 0, 1],
     ])
     mask = np.array([
         [0, 0, 1, 1, 0],
         [0, 1, 1, 1, 0],
         [0, 0, 1, 1, 1],
         [0, 0, 1, 1, 0],
         [0, 0, 1, 1, 0],
     ])
     expected_array = np.array([
         [1.0000, 0.666667, 0.600000, 0.500000, 0.50],
         [1.0000, 0.750000, 0.571429, 0.428571, 0.25],
         [1.0000, 1.000000, 0.714286, 0.571429, 0.25],
         [np.nan, 1.000000, 0.666667, 0.571429, 0.25],
         [np.nan, 1.000000, 0.750000, 0.750000, 0.50],
     ])
     expected_mask_array = np.array([
         [True, True, False, False, True],
         [True, False, False, False, True],
         [True, True, False, False, False],
         [True, True, False, False, True],
         [True, True, False, False, True],
     ])
     self.cube.data = np.ma.masked_where(mask == 0, self.cube.data)
     result = SquareNeighbourhood().run(self.cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data.data, expected_array)
     self.assertArrayAlmostEqual(result.data.mask, expected_mask_array)
Ejemplo n.º 5
0
 def test_multiple_times_with_mask(self):
     """Test that the run method produces a cube with correct data when a
     cube containing masked data at multiple time steps is passed in.
     Re-masking is disabled."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=2,
                        num_grid_points=5)
     data = np.array([[[[1, 1, 0, 1, 1], [1, 1, 1, 0, 0], [1, 0, 1, 0, 0],
                        [0, 0, 1, 1, 0], [0, 1, 1, 0, 1]],
                       [[1, 1, 0, 1, 1], [1, 1, 1, 0, 0], [1, 0, 1, 0, 0],
                        [0, 0, 1, 0, 0], [0, 1, 1, 0, 1]]]])
     mask = np.array([[[[0, 0, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1],
                        [0, 0, 1, 1, 0], [0, 0, 1, 1, 0]],
                       [[0, 0, 1, 1, 0], [0, 1, 1, 1, 0], [0, 1, 1, 1, 1],
                        [0, 0, 0, 1, 0], [0, 0, 1, 1, 0]]]])
     masked_data = np.ma.masked_where(mask == 0, data)
     cube.data = masked_data
     expected_array = np.array(
         [[[[1.0000, 0.666667, 0.600000, 0.500000, 0.500000],
            [1.0000, 0.750000, 0.571429, 0.428571, 0.250000],
            [1.0000, 1.000000, 0.714286, 0.571429, 0.250000],
            [np.nan, 1.000000, 0.666667, 0.571429, 0.250000],
            [np.nan, 1.000000, 0.750000, 0.750000, 0.500000]],
           [[1.0000, 0.666667, 0.600000, 0.500000, 0.500000],
            [0.5000, 0.600000, 0.500000, 0.428571, 0.250000],
            [0.5000, 0.750000, 0.428571, 0.333333, 0.000000],
            [0.0000, 0.666667, 0.333333, 0.333333, 0.000000],
            [np.nan, 1.000000, 0.333333, 0.333333, 0.000000]]]])
     result = SquareNeighbourhood(re_mask=False).run(cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 6
0
 def test_complex(self):
     """Test that a cube containing complex numbers is sensibly processed"""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     cube.data = cube.data.astype(complex)
     cube.data[0, 0, 1, 3] = 0.5 + 0.5j
     cube.data[0, 0, 4, 3] = 0.4 + 0.6j
     expected_data = np.array([[
         [[
             1.0 + 0.0j, 1.0 + 0.0j, 0.91666667 + 0.083333333j,
             0.91666667 + 0.083333333j, 0.875 + 0.125j
         ],
          [
              1.0 + 0.0j, 0.88888889 + 0.0j, 0.83333333 + 0.055555556j,
              0.83333333 + 0.055555556j, 0.91666667 + 0.083333333j
          ],
          [
              1.0 + 0.0j, 0.88888889 + 0.0j, 0.83333333 + 0.055555556j,
              0.83333333 + 0.055555556j, 0.91666667 + 0.083333333j
          ],
          [
              1.0 + 0.0j, 0.88888889 + 0.0j, 0.82222222 + 0.066666667j,
              0.82222222 + 0.066666667j, 0.9 + 0.1j
          ], [1.0 + 0.0j, 1.0 + 0.0j, 0.9 + 0.1j, 0.9 + 0.1j, 0.85 + 0.15j]]
     ]])
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data, expected_data)
Ejemplo n.º 7
0
 def test_basic(self):
     """Test that the __repr__ returns the expected string."""
     result = str(SquareNeighbourhood())
     msg = ("<SquareNeighbourhood: weighted_mode: {}, "
            "sum_or_fraction: {}, re_mask: {}>".format(
                True, "fraction", True))
     self.assertEqual(result, msg)
Ejemplo n.º 8
0
 def test_different_widths(self):
     """Test that padding the data in a cube with different widths has
     worked as intended."""
     for sliced_cube in self.cube.slices(
         ["projection_y_coordinate", "projection_x_coordinate"]):
         break
     expected = np.array([[1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 0., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.],
                          [1., 1., 1., 1., 1., 1., 1., 1., 1.]])
     width_x = 1
     width_y = 2
     padded_cube = SquareNeighbourhood().pad_cube_with_halo(
         sliced_cube, width_x, width_y)
     self.assertIsInstance(padded_cube, Cube)
     self.assertArrayAlmostEqual(padded_cube.data, expected)
Ejemplo n.º 9
0
    def test_basic(self):
        """Test setting up cubes to be padded and then passed into
        neighbourhood processing."""
        expected_data = np.array(
            [[0.85714286, 0.75, 0.83333333, 0.85714286, 0.75, 1., 0.85714286],
             [0.85714286, 0.8, 0.75, 0.75, 0.5, np.nan, 0.75],
             [0.75, 0.83333333, 0.75, 0.8, 0.66666667, 1., 0.8],
             [0.8, 0.5, 0.83333333, 0.85714286, 0.75, 1., 0.85714286],
             [0.85714286, 0.8, 0.75, 0.75, 0.5, np.nan, 0.75],
             [0.75, 0.66666667, 1., 1., 1., np.nan, 1.],
             [1., 1., 0.83333333, 0.85714286, 0.75, 1., 0.85714286]])

        grid_cells_x = grid_cells_y = 1
        cube = self.cube
        mask_cube = cube.copy()
        mask_cube.data[::] = 1.0
        mask_cube.data[1, 2] = 0.0
        mask_cube.data[2, 2] = 0.0
        # set_up_cubes_to_be_neighbourhooded would set masked points to 0.0
        cube.data[1, 2] = 0.0
        cube.data[2, 2] = 0.0
        mask_cube.rename('mask_data')

        nbcube = (SquareNeighbourhood()._pad_and_calculate_neighbourhood(
            cube, mask_cube, grid_cells_x, grid_cells_y))
        self.assertIsInstance(nbcube, Cube)
        self.assertArrayAlmostEqual(nbcube.data, expected_data)
Ejemplo n.º 10
0
 def test_with_masked_data(self):
     """Test that removing a halo of points from the data on a cube
     has worked as intended when the input data is masked."""
     expected = np.array([[1., np.nan, 1.], [1., np.nan, 1.], [1., 1., 1.]])
     grid_cells_x = grid_cells_y = 1
     padded_cube = self.padded_cube
     # Set up padded cube and associated mask.
     mask_cube = padded_cube.copy()
     masked_array = np.ones(mask_cube.data.shape)
     masked_array[0, 0, 3, 3] = 0
     masked_array[0, 0, 2, 3] = 0
     mask_cube.rename('mask_data')
     mask_cube.data = masked_array.astype(bool)
     padded_cubes = CubeList([padded_cube, mask_cube])
     # Set up cube without padding and associated mask.
     cube = self.cube
     mask_cube = cube.copy()
     masked_array = np.ones(mask_cube.data.shape)
     masked_array[0, 0, 1, 1] = 0
     masked_array[0, 0, 0, 1] = 0
     mask_cube.rename('mask_data')
     mask_cube.data = masked_array.astype(bool)
     cubes = CubeList([cube, mask_cube])
     nbcube = (SquareNeighbourhood()._remove_padding_and_mask(
         padded_cubes, cubes, padded_cube.name(), grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data.filled(), expected)
Ejemplo n.º 11
0
 def test_masked_array_re_mask_true(self):
     """Test that the run method produces a cube with correct data when a
     cube containing masked data is passed in and re-masking is applied."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     cube.data = np.array([[[[1, 1, 0, 1, 1], [1, 1, 1, 0, 0],
                             [1, 0, 1, 0, 0], [0, 0, 1, 1, 0],
                             [0, 1, 1, 0, 1]]]])
     mask = np.array([[[[0, 0, 1, 1, 0], [0, 1, 1, 1, 0], [0, 0, 1, 1, 1],
                        [0, 0, 1, 1, 0], [0, 0, 1, 1, 0]]]])
     expected_array = np.array(
         [[[[1.0000, 0.666667, 0.600000, 0.500000, 0.50],
            [1.0000, 0.750000, 0.571429, 0.428571, 0.25],
            [1.0000, 1.000000, 0.714286, 0.571429, 0.25],
            [np.nan, 1.000000, 0.666667, 0.571429, 0.25],
            [np.nan, 1.000000, 0.750000, 0.750000, 0.50]]]])
     expected_mask_array = np.array([[[[True, True, False, False, True],
                                       [True, False, False, False, True],
                                       [True, True, False, False, False],
                                       [True, True, False, False, True],
                                       [True, True, False, False, True]]]])
     cube.data = np.ma.masked_where(mask == 0, cube.data)
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data.data, expected_array)
     self.assertArrayAlmostEqual(result.data.mask, expected_mask_array)
Ejemplo n.º 12
0
 def test_for_multiple_times(self):
     """
     Test that the y-dimension and x-dimension accumulation produces the
     intended result when the input cube has multiple times. The input
     cube has an extra time dimension to ensure that a 3d cube is correctly
     handled.
     """
     data = np.array([[[1., 2., 3., 4., 5.], [2., 4., 6., 8., 10.],
                       [3., 6., 8., 11., 14.], [4., 8., 11., 15., 19.],
                       [5., 10., 14., 19., 24.]],
                      [[1., 2., 3., 4., 5.], [2., 4., 6., 8., 10.],
                       [3., 6., 9., 12., 15.], [4., 8., 12., 15., 19.],
                       [5., 10., 15., 19., 24.]],
                      [[0., 1., 2., 3., 4.], [1., 3., 5., 7., 9.],
                       [2., 5., 8., 11., 14.], [3., 7., 11., 15., 19.],
                       [4., 9., 14., 19., 24.]]])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), (0, 1, 3, 3),
                                            (0, 2, 0, 0)),
                        num_time_points=3,
                        num_grid_points=5)
     nan_mask = np.zeros(cube[0, 0, :, :].data.shape, dtype=int).flatten()
     result = SquareNeighbourhood().cumulate_array(cube)
     self.assertIsInstance(result[0], Cube)
     self.assertArrayAlmostEqual(result[0].data, data)
     self.assertArrayAlmostEqual(result[1][0].data, nan_mask)
Ejemplo n.º 13
0
 def test_metadata(self):
     """Test that a cube with correct metadata is produced by the run
     method."""
     self.cube.attributes = {"Conventions": "CF-1.5"}
     self.cube.add_cell_method(CellMethod("mean", coords="time"))
     result = SquareNeighbourhood().run(self.cube, self.RADIUS)
     self.assertTupleEqual(result.cell_methods, self.cube.cell_methods)
     self.assertDictEqual(result.attributes, self.cube.attributes)
Ejemplo n.º 14
0
 def test_basic(self):
     """Test cube with correct data is produced when mean over
        neighbourhood is calculated."""
     nan_masks = [np.zeros(self.cube.data.shape, dtype=int)]
     result = SquareNeighbourhood().mean_over_neighbourhood(
         self.cube, self.width, self.width, nan_masks)
     self.assertIsInstance(result, Cube)
     self.assertArrayAlmostEqual(result.data, self.result)
Ejemplo n.º 15
0
 def test_basic_fraction(self):
     """Test cube with correct data is produced when mean over
        neighbourhood is calculated where the sum_or_fraction option is
        set to "fraction"."""
     result = SquareNeighbourhood().mean_over_neighbourhood(
         self.cube, self.mask, self.width, self.width)
     self.assertIsInstance(result, Cube)
     self.assertArrayAlmostEqual(result.data, self.expected)
Ejemplo n.º 16
0
    def test_multiple_realizations_with_mask(self):
        """Test that the run method produces a cube with correct data when a
        cube containing masked data at multiple realizations is passed in.
        Re-masking is disabled."""

        data = np.array([
            [
                [1, 1, 0, 1, 1],
                [1, 1, 1, 0, 0],
                [1, 0, 1, 0, 0],
                [0, 0, 1, 1, 0],
                [0, 1, 1, 0, 1],
            ],
            [
                [1, 1, 0, 1, 1],
                [1, 1, 1, 0, 0],
                [1, 0, 1, 0, 0],
                [0, 0, 1, 0, 0],
                [0, 1, 1, 0, 1],
            ],
        ])
        mask = np.array([
            [
                [0, 0, 1, 1, 0],
                [0, 1, 1, 1, 0],
                [0, 0, 1, 1, 1],
                [0, 0, 1, 1, 0],
                [0, 0, 1, 1, 0],
            ],
            [
                [0, 0, 1, 1, 0],
                [0, 1, 1, 1, 0],
                [0, 1, 1, 1, 1],
                [0, 0, 0, 1, 0],
                [0, 0, 1, 1, 0],
            ],
        ])
        masked_data = np.ma.masked_where(mask == 0, data)
        self.multi_realization_cube.data = masked_data
        expected_array = np.array([
            [
                [1.0000, 0.666667, 0.600000, 0.500000, 0.500000],
                [1.0000, 0.750000, 0.571429, 0.428571, 0.250000],
                [1.0000, 1.000000, 0.714286, 0.571429, 0.250000],
                [np.nan, 1.000000, 0.666667, 0.571429, 0.250000],
                [np.nan, 1.000000, 0.750000, 0.750000, 0.500000],
            ],
            [
                [1.0000, 0.666667, 0.600000, 0.500000, 0.500000],
                [0.5000, 0.600000, 0.500000, 0.428571, 0.250000],
                [0.5000, 0.750000, 0.428571, 0.333333, 0.000000],
                [0.0000, 0.666667, 0.333333, 0.333333, 0.000000],
                [np.nan, 1.000000, 0.333333, 0.333333, 0.000000],
            ],
        ])
        result = SquareNeighbourhood(re_mask=False).run(
            self.multi_realization_cube, self.RADIUS)
        self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 17
0
 def test_with_masked_data_and_no_remasking(self):
     """Test that removing halo works correctly with remask=False"""
     expected = np.array([[1., 1., 1.], [1., 0., 1.], [1., 1., 1.]])
     grid_cells_x = grid_cells_y = 1
     nbcube = (SquareNeighbourhood(re_mask=False)._remove_padding_and_mask(
         self.padded_cube, self.cube, self.mask_cube, grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data, expected)
Ejemplo n.º 18
0
    def test_complex(self):
        """Test neighbourhooding with an array of complex wind directions"""
        # set up cube with complex wind directions 30-60 degrees
        a = 0.54066949 + 0.82535591j
        b = 0.56100423 + 0.80502117j
        c = 0.5 + 0.8660254j
        d = 0.59150635 + 0.77451905j

        expected_data_complex = np.array(
            [[a, b, b, a, b, c,
              a], [a, 0.55228934 + 0.81373606j, d, b, d, c, b],
             [b, 0.54575318 + 0.82027223j, d, b, d, c, b],
             [b, 0.62200847 + 0.74401694j, b, a, b, c, a],
             [a, 0.55228934 + 0.81373606j, d, b, d, c, b],
             [b, 0.57320508 + 0.79282032j, c, c, c, c, c],
             [c, c, b, a, b, c, a]])

        expected_data_deg = np.array(
            [[
                56.77222443, 55.12808228, 55.12808228, 56.77222443,
                55.12808228, 60.00000381, 56.77222443
            ],
             [
                 56.77222443, 55.83494186, 52.63074112, 55.12808228,
                 52.63074112, 60.00000381, 55.12808228
             ],
             [
                 55.12808228, 56.36291885, 52.63074112, 55.12808228,
                 52.63074112, 60.00000381, 55.12808228
             ],
             [
                 55.12808228, 50.10391235, 55.12808228, 56.77222443,
                 55.12808228, 60.00000381, 56.77222443
             ],
             [
                 56.77222443, 55.83494186, 52.63074112, 55.12808228,
                 52.63074112, 60.00000381, 55.12808228
             ],
             [
                 55.12808228, 54.13326263, 60.00000381, 60.00000381,
                 60.00000381, 60.00000381, 60.00000381
             ],
             [
                 60.00000381, 60.00000381, 55.12808228, 56.77222443,
                 55.12808228, 60.00000381, 56.77222443
             ]],
            dtype=np.float32)

        self.cube.data = WindDirection.deg_to_complex(30. * self.cube.data +
                                                      30.)
        nbcube = (SquareNeighbourhood()._pad_and_calculate_neighbourhood(
            self.cube, self.mask, 1, 1))
        self.assertTrue(np.any(np.iscomplex(nbcube.data)))
        self.assertArrayAlmostEqual(nbcube.data, expected_data_complex)
        self.assertArrayAlmostEqual(WindDirection.complex_to_deg(nbcube.data),
                                    expected_data_deg)
Ejemplo n.º 19
0
 def test_without_masked_data(self):
     """Test that removing a halo of points from the data on a cube
     has worked as intended when the input data is not masked."""
     expected = np.array([[1., 1., 1.], [1., 0., 1.], [1., 1., 1.]])
     grid_cells_x = grid_cells_y = 1
     nbcube = (SquareNeighbourhood()._remove_padding_and_mask(
         self.padded_cube, self.cube, self.no_mask, grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data, expected)
Ejemplo n.º 20
0
 def test_return_type(self):
     """Test that the run_recursion method returns an iris.cube.Cube."""
     edge_width = 1
     alphas_x = RecursiveFilter().set_alphas(self.cube, self.alpha_x, None)
     alphas_y = RecursiveFilter().set_alphas(self.cube, self.alpha_y, None)
     padded_cube = SquareNeighbourhood().pad_cube_with_halo(
         self.cube, edge_width, edge_width)
     result = RecursiveFilter().run_recursion(padded_cube, alphas_x,
                                              alphas_y, self.iterations)
     self.assertIsInstance(result, Cube)
Ejemplo n.º 21
0
 def test_clipping(self):
     """Test that clipping is working"""
     expected = np.array([[1., 1., 1.], [1., 0., 1.], [1., 1., 1.]])
     grid_cells_x = grid_cells_y = 1
     self.padded_cube.data[2, 2] = 1.1
     self.padded_cube.data[3, 3] = -0.1
     nbcube = (SquareNeighbourhood(re_mask=False)._remove_padding_and_mask(
         self.padded_cube, self.cube, self.mask_cube, grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data, expected)
Ejemplo n.º 22
0
 def test_basic_re_mask_false(self):
     """Test that a cube with correct data is produced by the run method."""
     expected_array = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     result = SquareNeighbourhood(re_mask=False).run(self.cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 23
0
 def test_expected_result(self):
     """Test that the run_recursion method returns the expected value."""
     edge_width = 1
     alphas_x = RecursiveFilter().set_alphas(self.cube, self.alpha_x, None)
     alphas_y = RecursiveFilter().set_alphas(self.cube, self.alpha_y, None)
     padded_cube = SquareNeighbourhood().pad_cube_with_halo(
         self.cube, edge_width, edge_width)
     result = RecursiveFilter().run_recursion(padded_cube, alphas_x,
                                              alphas_y, self.iterations)
     expected_result = 0.13382206
     self.assertAlmostEqual(result.data[4][4], expected_result)
Ejemplo n.º 24
0
 def test_metadata(self):
     """Test that a cube with correct metadata is produced by the run
     method."""
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     cube.attributes = {"Conventions": "CF-1.5"}
     cube.add_cell_method(CellMethod("mean", coords="time"))
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertTupleEqual(result.cell_methods, cube.cell_methods)
     self.assertDictEqual(result.attributes, cube.attributes)
Ejemplo n.º 25
0
 def test_basic(self):
     """Test that removing a halo of points from the data on a cube
     has worked as intended."""
     for sliced_cube in self.cube.slices(
         ["projection_y_coordinate", "projection_x_coordinate"]):
         break
     expected = np.array([[0.]])
     width_x = width_y = 1
     padded_cube = SquareNeighbourhood().remove_halo_from_cube(
         sliced_cube, width_x, width_y)
     self.assertIsInstance(padded_cube, Cube)
     self.assertArrayAlmostEqual(padded_cube.data, expected)
Ejemplo n.º 26
0
 def test_nan_array_re_mask_false(self):
     """Test that an array containing nans is handled correctly."""
     expected_array = np.array([
         [np.nan, 1.0, 1.0, 1.0, 1.0],
         [1.0, 0.8750000, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     self.cube.data[0, 0] = np.nan
     result = SquareNeighbourhood(re_mask=False).run(self.cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 27
0
    def set_alphas(self, cube, alpha, alphas_cube):
        """
        Set up the alpha parameter.

        Args:
            cube (Iris.cube.Cube):
                Cube containing the input data to which the recursive filter
                will be applied.
            alpha (Float):
                The constant used to weight the recursive filter in that
                direction: Defined such that 0.0 < alpha < 1.0
            alphas_cube (Iris.cube.Cube or None):
                Cube containing array of alpha values that will be used
                when applying the recursive filter in a specific direction.

        Raises:
            ValueError: If both alphas_cube and alpha are provided.
            ValueError: If alpha and alphas_cube are both set to None
            ValueError: If dimension of alphas array is less than dimension
                        of data array
            ValueError: If dimension of alphas array is greater than dimension
                        of data array

        Returns:
            alphas_cube (Iris.cube.Cube):
                Cube containing a padded array of alpha values
                for the specified direction.
        """
        if alpha is not None and alphas_cube is not None:
            emsg = ("A cube of alpha values and a single float value for alpha"
                    " have both been provded. Only one of these options can be"
                    " set.")
            raise ValueError(emsg)

        if alphas_cube is None:
            if alpha is None:
                emsg = ("A value for alpha must be set if alphas_cube is "
                        "set to None: alpha is currently set as: {}")
                raise ValueError(emsg.format(alpha))
            alphas_cube = cube.copy(
                data=np.ones(cube.data.shape) * alpha)

        if alphas_cube is not None:
            if alphas_cube.data.shape != cube.data.shape:
                emsg = ("Dimensions of alphas array do not match dimensions "
                        "of data array: {} < {}")
                raise ValueError(emsg.format(alphas_cube.data.shape,
                                             cube.data.shape))

        alphas_cube = SquareNeighbourhood().pad_cube_with_halo(
            alphas_cube, self.edge_width, self.edge_width)
        return alphas_cube
Ejemplo n.º 28
0
 def test_basic_re_mask_true(self):
     """Test that a cube with correct data is produced by the run method
     when re-masking is applied."""
     expected_array = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 0.88888889, 0.88888889, 0.88888889, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
     ])
     result = SquareNeighbourhood().run(self.cube, self.RADIUS)
     self.assertIsInstance(result, Cube)
     self.assertArrayAlmostEqual(result.data, expected_array)
Ejemplo n.º 29
0
 def test_with_masked_data(self):
     """Test that removing a halo of points from the data on a cube
     has worked as intended when the input data has an associated mask."""
     expected = np.array([[1., 1., 1.], [1., 0., 1.], [1., 1., 1.]])
     expected_mask = np.array([[False, True, False], [False, False, True],
                               [False, False, False]])
     grid_cells_x = grid_cells_y = 1
     nbcube = (SquareNeighbourhood()._remove_padding_and_mask(
         self.padded_cube, self.cube, self.mask_cube, grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data.data, expected)
     self.assertArrayAlmostEqual(nbcube.data.mask, expected_mask)
Ejemplo n.º 30
0
 def test_basic_re_mask_false(self):
     """Test that a cube with correct data is produced by the run method."""
     data = np.array([[[[1., 1., 1., 1., 1.],
                        [1., 0.88888889, 0.88888889, 0.88888889, 1.],
                        [1., 0.88888889, 0.88888889, 0.88888889, 1.],
                        [1., 0.88888889, 0.88888889, 0.88888889, 1.],
                        [1., 1., 1., 1., 1.]]]])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     result = SquareNeighbourhood(re_mask=False).run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertArrayAlmostEqual(result.data, data)