Exemplo n.º 1
0
 def test_exception(self):
     """Test an exception is raised if the chosen coordinate is
     non-uniform."""
     coord_points = np.arange(10., 60., 10.)
     coord_points[0] = -200.
     self.cube.coord("projection_x_coordinate").points = coord_points
     coord = self.cube.coord("projection_x_coordinate")
     width = 1
     method = "add"
     msg = "Non-uniform increments between grid points"
     with self.assertRaisesRegexp(ValueError, msg):
         SquareNeighbourhood.pad_coord(coord, width, method)
 def test_use_mask_cube_occurrences_not_masked(self):
     """Test that the plugin returns an iris.cube.Cube with the correct
     data array if a mask cube is used and the mask cube does not mask
     out the occurrences."""
     expected = 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_grid_points=5, num_time_points=1)
     cube = iris.util.squeeze(cube)
     mask_cube = cube.copy()
     mask_cube.data = 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.]])
     radius = 2000
     neighbourhood_method = SquareNeighbourhood()
     result = NBHood(neighbourhood_method, radius).process(
         cube, mask_cube)
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 3
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)
Exemplo n.º 4
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."""
     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(
         [[[np.nan, np.nan, 0.57142857, 0.5, np.nan],
           [np.nan, 0.75, 0.57142857, 0.42857143, np.nan],
           [np.nan, np.nan, 0.71428571, 0.57142857, 0.2],
           [np.nan, np.nan, 0.66666667, 0.57142857, np.nan],
           [np.nan, np.nan, 0.66666667, 0.66666667, np.nan]],
          [[np.nan, np.nan, 0.57142857, 0.5, np.nan],
           [np.nan, 0.6, 0.5, 0.42857143, np.nan],
           [np.nan, 0.75, 0.42857143, 0.33333333, 0.],
           [np.nan, np.nan, np.nan, 0.33333333, np.nan],
           [np.nan, np.nan, 0.4, 0.4, np.nan]]])
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertArrayAlmostEqual(result.data.filled(), expected_array)
Exemplo n.º 5
0
 def test_use_mask_cube_occurrences_masked_irregular(self):
     """Test that the plugin returns an iris.cube.Cube with the correct
     data array if a mask cube is used and the mask cube does mask
     out the occurrences. In this case, an irregular mask is applied."""
     expected = np.array([
         [1.000000, 1.000000, 1.000000, 1.000000, 1.000000],
         [1.000000, 0.000000, 0.833333, 0.000000, 1.000000],
         [1.000000, 0.000000, 0.833333, 0.875000, 1.000000],
         [1.000000, 0.857143, 0.833333, 0.857143, 1.000000],
         [1.000000, 0.000000, 1.000000, 0.000000, 0.000000],
     ])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_grid_points=5,
                        num_time_points=1)
     cube = iris.util.squeeze(cube)
     mask_cube = cube.copy()
     mask_cube.data = np.array([
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 0.0, 1.0, 0.0, 1.0],
         [1.0, 0.0, 1.0, 1.0, 1.0],
         [1.0, 1.0, 1.0, 1.0, 1.0],
         [1.0, 0.0, 1.0, 0.0, 0.0],
     ])
     radius = 2000
     neighbourhood_method = SquareNeighbourhood()
     result = NBHood(neighbourhood_method, radius)(cube, mask_cube)
     self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 6
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)
Exemplo n.º 7
0
 def test_no_y_dimension_coordinate(self):
     """Test that a cube is created with the expected y and x coordinates
     within the output cube, if the input cube only has a
     projection_y_coordinate dimension coordinate."""
     for sliced_cube in self.cube.slices(["projection_x_coordinate"]):
         break
     data = sliced_cube.data
     coord_x = sliced_cube.coord("projection_x_coordinate")
     coord_y = sliced_cube.coord("projection_y_coordinate")
     new_cube = SquareNeighbourhood()._create_cube_with_new_data(
         sliced_cube, data, coord_x, coord_y)
     self.assertIsInstance(new_cube, Cube)
     self.assertArrayAlmostEqual(new_cube.data, data)
     self.assertEqual(new_cube.coord_dims("projection_x_coordinate")[0], 0)
     self.assertTrue(
         new_cube.coords("projection_y_coordinate", dim_coords=False))
Exemplo n.º 8
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)
Exemplo n.º 9
0
    def test_use_mask_cube_occurrences_masked_irregular(self):
        """Test that the plugin returns an iris.cube.Cube with the correct
        data array if a mask cube is used and the mask cube does mask
        out the occurrences. In this case, an irregular mask is applied."""
        expected = np.array([
            [1.000000, 1.000000, 1.000000, 1.000000, 1.000000],
            [1.000000, 0.000000, 0.833333, 0.000000, 1.000000],
            [1.000000, 0.000000, 0.833333, 0.875000, 1.000000],
            [1.000000, 0.857143, 0.833333, 0.857143, 1.000000],
            [1.000000, 0.000000, 1.000000, 0.000000, 0.000000],
        ])

        data = np.ones((5, 5), dtype=np.float32)
        data[2, 2] = 0
        cube = set_up_variable_cube(
            data,
            spatial_grid="equalarea",
        )

        mask_cube = cube.copy()
        mask_cube.data = np.array([
            [1.0, 1.0, 1.0, 1.0, 1.0],
            [1.0, 0.0, 1.0, 0.0, 1.0],
            [1.0, 0.0, 1.0, 1.0, 1.0],
            [1.0, 1.0, 1.0, 1.0, 1.0],
            [1.0, 0.0, 1.0, 0.0, 0.0],
        ])
        radius = 2000
        neighbourhood_method = SquareNeighbourhood()
        result = NBHood(neighbourhood_method, radius)(cube, mask_cube)
        self.assertArrayAlmostEqual(result.data, expected)
Exemplo n.º 10
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)
Exemplo n.º 11
0
    def test_xy_order(self):
        """Test that a cube is created with the expected order for the y and x
        coordinates within the output cube, if the input cube has dimensions
        of projection_x_coordinate and projection_y_coordinate."""
        for sliced_cube in self.cube.slices(
            ["projection_x_coordinate", "projection_y_coordinate"]):
            break
        data = sliced_cube.data
        coord_x = sliced_cube.coord("projection_x_coordinate")
        coord_y = sliced_cube.coord("projection_y_coordinate")

        new_cube = SquareNeighbourhood()._create_cube_with_new_data(
            sliced_cube, data, coord_x, coord_y)
        self.assertIsInstance(new_cube, Cube)
        self.assertArrayAlmostEqual(new_cube.data, data)
        self.assertEqual(new_cube.coord_dims("projection_y_coordinate")[0], 1)
        self.assertEqual(new_cube.coord_dims("projection_x_coordinate")[0], 0)
Exemplo n.º 12
0
 def test_without_masked_data(self):
     """Test setting up cubes to be neighbourhooded when the input cube
     does not contain masked arrays."""
     cubes = (SquareNeighbourhood._set_up_cubes_to_be_neighbourhooded(
         self.cube))
     self.assertIsInstance(cubes, CubeList)
     self.assertEqual(len(cubes), 1)
     self.assertEqual(cubes[0], self.cube)
Exemplo n.º 13
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)
Exemplo n.º 14
0
    def test_forecast_period(self):
        """Test that a cube is created with the expected order for coordinates
        within the output cube, if the input cube has dimensions of
        projection_y_coordinate and projection_x_coordinate, and where the
        input cube also has a forecast_period coordinate."""
        for sliced_cube in self.cube.slices(
            ["projection_y_coordinate", "projection_x_coordinate"]):
            break
        fp_coord = DimCoord(np.array([10]), standard_name="forecast_period")
        sliced_cube.add_aux_coord(fp_coord)
        data = sliced_cube.data
        coord_x = sliced_cube.coord("projection_x_coordinate")
        coord_y = sliced_cube.coord("projection_y_coordinate")

        new_cube = SquareNeighbourhood()._create_cube_with_new_data(
            sliced_cube, data, coord_x, coord_y)
        self.assertIsInstance(new_cube, Cube)
        self.assertArrayAlmostEqual(new_cube.data, data)
        self.assertEqual(new_cube.coord("forecast_period").points, 10)
        self.assertEqual(new_cube.coord_dims("projection_y_coordinate")[0], 0)
        self.assertEqual(new_cube.coord_dims("projection_x_coordinate")[0], 1)
Exemplo n.º 15
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)
Exemplo n.º 16
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
     padded_cubes = CubeList([self.padded_cube])
     cubes = CubeList([self.cube])
     nbcube = (SquareNeighbourhood()._remove_padding_and_mask(
         padded_cubes, cubes, self.padded_cube.name(), grid_cells_x,
         grid_cells_y))
     self.assertIsInstance(nbcube, Cube)
     self.assertArrayAlmostEqual(nbcube.data, expected)
Exemplo n.º 17
0
 def test_basic(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().run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertArrayAlmostEqual(result.data, data)
Exemplo n.º 18
0
 def test_remove(self):
     """Test the functionality to remove padding from the chosen
     coordinate. Includes a test that the coordinate bounds array is
     modified to reflect the new values."""
     expected = np.array([30.])
     expected_bounds = np.array([expected - 5, expected + 5]).T
     coord = self.cube.coord("projection_x_coordinate")
     width = 1
     method = "remove"
     new_coord = SquareNeighbourhood.pad_coord(coord, width, method)
     self.assertIsInstance(new_coord, DimCoord)
     self.assertArrayAlmostEqual(new_coord.points, expected)
     self.assertArrayEqual(new_coord.bounds, expected_bounds)
Exemplo n.º 19
0
 def test_nan_mask(self):
     """Test the correct result is returned when a nan must be substituted
        into the final array. Note: this type of data should also be masked,
        so the expected_data array looks strange because there is further
        processing to be done on it."""
     cube = self.padded_cube
     nan_masks = [np.zeros([9, 9]).astype(bool)]
     nan_masks[0][2, 2] = True
     expected_data = self.padded_result
     expected_data[0, 0] = np.nan
     result = SquareNeighbourhood().mean_over_neighbourhood(
         cube, self.width, self.width, nan_masks)
     self.assertArrayAlmostEqual(result.data[2:-2, 2:-2], expected_data)
Exemplo n.º 20
0
 def test_add(self):
     """Test the functionality to add padding to the chosen coordinate.
     Includes a test that the coordinate bounds array is modified to reflect
     the new values."""
     expected = np.array([-10., 0., 10., 20., 30., 40., 50., 60., 70.])
     coord = self.cube.coord("projection_x_coordinate")
     expected_bounds = np.array([expected - 5, expected + 5]).T
     width = 1
     method = "add"
     new_coord = SquareNeighbourhood.pad_coord(coord, width, method)
     self.assertIsInstance(new_coord, DimCoord)
     self.assertArrayAlmostEqual(new_coord.points, expected)
     self.assertArrayEqual(new_coord.bounds, expected_bounds)
Exemplo n.º 21
0
 def test_nan_array(self):
     """Test that the an array containing nans is handled correctly."""
     data = np.array([[np.nan, 0.777778, 1., 1., 1.],
                      [0.777778, 0.77777778, 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)
     cube.data[0, 0, 0, 0] = np.nan
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertArrayAlmostEqual(result.data, data)
Exemplo n.º 22
0
 def test_multiple_times(self):
     """Test mean over neighbourhood with more than two dimensions."""
     data = np.array([self.padded_data, self.padded_data])
     cube = Cube(data, long_name='two times test')
     cube.add_dim_coord(self.padded_x_coord, 1)
     cube.add_dim_coord(self.padded_y_coord, 2)
     t_coord = DimCoord([0, 1], standard_name='time')
     cube.add_dim_coord(t_coord, 0)
     nan_masks = [np.zeros(cube.data[0].shape, dtype=int)] * 2
     result = SquareNeighbourhood().mean_over_neighbourhood(
         cube, self.width, self.width, nan_masks)
     self.assertArrayAlmostEqual(result.data[0, 2:-2, 2:-2],
                                 self.padded_result)
     self.assertArrayAlmostEqual(result.data[1, 2:-2, 2:-2],
                                 self.padded_result)
Exemplo n.º 23
0
 def test_basic(self):
     """
     Test that the y-dimension and x-dimension accumulation produces the
     intended result. A 2d cube is passed in.
     """
     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.]])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     nan_mask = np.zeros(cube.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)
Exemplo n.º 24
0
 def test_with_masked_data(self):
     """Test setting up cubes to be neighbourhooded when the input cube
     contains masked arrays."""
     cube = self.cube
     data = cube.data
     cube.data[0, 0, 1, 3] = 0.5
     cube.data[0, 0, 3, 3] = 0.5
     cube.data = np.ma.masked_equal(data, 0.5)
     mask = np.logical_not(cube.data.mask.astype(int))
     data = cube.data.data * mask
     cubes = (SquareNeighbourhood._set_up_cubes_to_be_neighbourhooded(
         cube.copy()))
     self.assertIsInstance(cubes, CubeList)
     self.assertEqual(len(cubes), 2)
     self.assertArrayAlmostEqual(cubes[0].data, data)
     self.assertArrayAlmostEqual(cubes[1].data, mask)
Exemplo n.º 25
0
 def test_nan_array(self):
     """Test correct nanmask is returned when array containing nan data
        is input."""
     data = np.array([[0., 1., 2., 3., 4.], [1., 3., 5., 7., 9.],
                      [2., 5., 7., 10., 13.], [3., 7., 10., 14., 18.],
                      [4., 9., 13., 18., 23.]])
     nanmask = np.zeros([5, 5]).astype(bool)
     nanmask[0, 0] = True
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ),
                        num_time_points=1,
                        num_grid_points=5)
     cube.data[0, 0, 0, 0] = np.nan
     result, nan_masks = SquareNeighbourhood().cumulate_array(cube)
     self.assertIsInstance(result, Cube)
     self.assertArrayAlmostEqual(result.data, data)
     self.assertArrayAlmostEqual(nan_masks[0], nanmask)
    def test_use_mask_cube_occurrences_masked(self):
        """Test that the plugin returns an iris.cube.Cube with the correct
        data array if a mask cube is used and the mask cube does mask
        out the occurrences."""

        data = np.ones((5, 5), dtype=np.float32)
        data[2, 2] = 0
        cube = set_up_variable_cube(data, spatial_grid="equalarea",)

        expected_data = data
        mask_cube = cube.copy()

        radius = 2000
        neighbourhood_method = SquareNeighbourhood()
        result = NBHood(neighbourhood_method, radius)(cube, mask_cube)
        self.assertArrayAlmostEqual(result.data, expected_data)
Exemplo n.º 27
0
 def test_zero_width(self):
     """Test that removing a halo of points from the data on a cube
     has worked as intended, if a width of zero is specified."""
     self.cube = set_up_cube(zero_point_indices=((0, 0, 5, 5), ),
                             num_time_points=1,
                             num_grid_points=10)
     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., 0., 1., 1., 1., 1.]])
     width_x = 0
     width_y = 2
     padded_cube = SquareNeighbourhood().remove_halo_from_cube(
         sliced_cube, width_x, width_y)
     self.assertIsInstance(padded_cube, Cube)
     self.assertArrayAlmostEqual(padded_cube.data, expected)
Exemplo n.º 28
0
 def test_multiple_times(self):
     """Test that a cube with correct data is produced by the run method
     when multiple times are supplied."""
     expected_1 = 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.]])
     expected_2 = np.array([[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.], [1., 1., 1., 1., 1.]])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), (0, 1, 1, 2)),
                        num_time_points=2,
                        num_grid_points=5)
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertArrayAlmostEqual(result.data[0], expected_1)
     self.assertArrayAlmostEqual(result.data[1], expected_2)
Exemplo n.º 29
0
 def test_multiple_times_nan(self):
     """Test that a cube with correct data is produced by the run method
     for multiple times and for when nans are present."""
     expected_1 = np.array(
         [[np.nan, 0.666667, 0.88888889, 0.88888889, 1.],
          [0.777778, 0.66666667, 0.77777778, 0.77777778, 1.],
          [1., 0.77777778, 0.77777778, 0.77777778, 1.],
          [1., 0.88888889, 0.88888889, 0.88888889, 1.],
          [1., 1., 1., 1., 1.]])
     expected_2 = np.array([[0.88888889, 0.88888889, 0.88888889, 1., 1.],
                            [0.88888889, np.nan, 0.88888889, 1., 1.],
                            [0.88888889, 0.88888889, 0.88888889, 1., 1.],
                            [1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.]])
     cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), (0, 0, 1, 2)),
                        num_time_points=2,
                        num_grid_points=5)
     cube.data[0, 0, 0, 0] = np.nan
     cube.data[0, 1, 1, 1] = np.nan
     result = SquareNeighbourhood().run(cube, self.RADIUS)
     self.assertIsInstance(cube, Cube)
     self.assertArrayAlmostEqual(result.data[0], expected_1)
     self.assertArrayAlmostEqual(result.data[1], expected_2)
Exemplo n.º 30
0
 def test_without_masked_data(self):
     """Test setting up cubes to be padded and then passed into
     neighbourhood processing when the input cubes do not contain masked
     arrays."""
     expected = np.array(
         [[
             1.66666667, 1.66666667, -1.22222222, -1.22222222, -1.22222222,
             -1.33333333, 1.66666667
         ],
          [
              1.66666667, 1.66666667, -1.22222222, -1.22222222, -1.22222222,
              -1.33333333, 2.44444444
          ],
          [
              3.22222222, 4., 0.88888889, 0.88888889, 0.88888889, 1.,
              -1.22222222
          ],
          [
              -1.22222222, -1.22222222, 0.88888889, 0.88888889, 0.88888889,
              1., -1.22222222
          ],
          [
              -1.22222222, -1.22222222, 0.88888889, 0.88888889, 0.88888889,
              1., -1.22222222
          ], [-1.22222222, -1.22222222, 1., 1., 1., 1., -2.11111111],
          [
              -2.88888889, -3.66666667, -1.22222222, -1.22222222,
              -1.22222222, -1.33333333, 1.66666667
          ]])
     grid_cells_x = grid_cells_y = 1
     cubes = CubeList([self.cube])
     nbcubes = (SquareNeighbourhood()._pad_and_calculate_neighbourhood(
         cubes, grid_cells_x, grid_cells_y))
     self.assertIsInstance(nbcubes, CubeList)
     self.assertEqual(len(nbcubes), 1)
     self.assertArrayAlmostEqual(nbcubes[0].data, expected)