def test_mask_cube(self): """Test that a NotImplementedError is raised if a mask cube is passed in when generating percentiles from a circular neighbourhood, as this option is not supported.""" self.cube.data[2, 2] = 0 radius = 4000.0 msg = "The use of a mask cube with a circular kernel is " "not yet implemented." with self.assertRaisesRegex(NotImplementedError, msg): GeneratePercentilesFromACircularNeighbourhood().run( self.cube, radius, mask_cube=self.cube)
def test_single_point_lat_long(self): """Test behaviour for a single grid cell on lat long grid.""" data = np.ones((16, 16), dtype=np.float32) data[7, 7] = 0 cube = set_up_variable_cube(data, spatial_grid="latlon",) msg = "Unable to convert from" radius = 6000.0 with self.assertRaisesRegex(ValueError, msg): GeneratePercentilesFromACircularNeighbourhood().run(cube, radius)
def test_coord_is_dim_vector(self): """Test that the percentile coord is added as the zeroth dimension when multiple percentiles are used.""" cube = set_up_cube( zero_point_indices=((0, 0, 2, 2),), num_time_points=1, num_grid_points=5) result = ( GeneratePercentilesFromACircularNeighbourhood( ).make_percentile_cube(cube)) self.assertEqual( result.coord_dims("percentiles_over_neighbourhood")[0], 0)
def test_mask_cube(self): """Test that a NotImplementedError is raised, if a mask cube is passed in when generating percentiles from 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] radius = 4000.0 msg = "The use of a mask cube with a circular kernel is " "not yet implemented." with self.assertRaisesRegex(NotImplementedError, msg): GeneratePercentilesFromACircularNeighbourhood().run(cube, radius, mask_cube=cube)
def test_coord_present(self): """Test that the percentile coord is added.""" cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), ), num_time_points=1, num_grid_points=5) result = (GeneratePercentilesFromACircularNeighbourhood(). make_percentile_cube(cube)) self.assertIsInstance(result.coord('percentile'), iris.coords.Coord) self.assertArrayEqual( result.coord('percentile').points, DEFAULT_PERCENTILES) self.assertArrayEqual(result[0].data, cube.data) self.assertDictEqual(cube.metadata._asdict(), result.metadata._asdict())
def test_number_of_percentiles_equals_number_of_points(self): """Test when the number of percentiles is equal to the number of points used to construct the percentiles.""" expected = np.array( [[[[[1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 0.2, 1., 1., 1.], [1., 1., 0.2, 0.2, 0.2, 1., 1.], [1., 1., 1., 0.2, 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.4, 1., 1., 1.], [1., 1., 0.4, 0.4, 0.4, 1., 1.], [1., 1., 1., 0.4, 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.6, 1., 1., 1.], [1., 1., 0.6, 0.6, 0.6, 1., 1.], [1., 1., 1., 0.6, 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.8, 1., 1., 1.], [1., 1., 0.8, 0.8, 0.8, 1., 1.], [1., 1., 1., 0.8, 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., 1.], [1., 1., 1., 1., 1., 1., 1.]]]]]) cube = set_up_cube( zero_point_indices=[(0, 0, 3, 3)], num_grid_points=7) percentiles = np.array([5, 10, 15, 20, 25]) radius = 2000. result = ( GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run( cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_almost_edge(self): """Test behaviour for a non-zero grid cell quite near the edge.""" cube = set_up_cube(zero_point_indices=[(0, 0, 1, 1)], num_grid_points=3) # Just within range of the edge. slice_2d = cube[0, 0, :, :] expected = np.array([[[0.5, 0.5, 0.5], [0.5, 0.5, 0.5], [0.5, 0.5, 0.5]], [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]], [[1., 1., 1.], [1., 1., 1.], [1., 1., 1.]]]) percentiles = np.array([10, 50, 90]) kernel = np.array([[0., 0., 1., 0., 0.], [0., 1., 1., 1., 0.], [1., 1., 1., 1., 1.], [0., 1., 1., 1., 0.], [0., 0., 1., 0., 0.]]) result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).pad_and_unpad_cube(slice_2d, kernel)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point(self): """Test behaviour for a single non-zero grid cell.""" expected = np.array([[[[[1., 1., 1., 1., 1.], [1., 1., 0.4, 1., 1.], [1., 0.4, 0.4, 0.4, 1.], [1., 1., 0.4, 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.]]]]]) percentiles = np.array([10, 50, 90]) radius = 2000. result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(self.cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_point_pair(self): """Test behaviour for two nearby non-zero grid cells.""" expected = np.array([[[[[1., 1., 1., 1., 1.], [1., 1., 1., 1., 1.], [1., 0., 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., 1., 1., 1., 1.]]]]]) cube = set_up_cube(zero_point_indices=((0, 0, 2, 2), (0, 0, 2, 1)), num_grid_points=5) percentiles = np.array([25, 50, 75]) radius = 2000. result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_multi_point_multitimes(self): """Test behaviour for points over multiple times.""" cube = set_up_cube( zero_point_indices=((0, 0, 2, 2), (0, 1, 2, 1)), num_time_points=2, num_grid_points=5) expected = np.array( [[[[[1., 1., 1., 1., 1.], [1., 1., 0.4, 1., 1.], [1., 0.4, 0.4, 0.4, 1.], [1., 1., 0.4, 1., 1.], [1., 1., 1., 1., 1.]], [[1., 1., 1., 1., 1.], [1., 0.4, 1., 1., 1.], [0.4, 0.4, 0.4, 1., 1.], [1., 0.4, 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.]]], [[[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.]]]]]) percentiles = np.array([10, 50, 90]) radius = 2000. result = ( GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run( cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_low_percentiles(self): """Test behaviour with low percentiles.""" expected = np.array([[[[[1., 1., 1., 1., 1.], [1., 1., 0.2, 1., 1.], [1., 0.2, 0.2, 0.2, 1.], [1., 1., 0.2, 1., 1.], [1., 1., 1., 1., 1.]]], [[[1., 1., 1., 1., 1.], [1., 1., 0.4, 1., 1.], [1., 0.4, 0.4, 0.4, 1.], [1., 1., 0.4, 1., 1.], [1., 1., 1., 1., 1.]]], [[[1., 1., 1., 1., 1.], [1., 1., 0.8, 1., 1.], [1., 0.8, 0.8, 0.8, 1.], [1., 1., 0.8, 1., 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) percentiles = np.array([5, 10, 20]) radius = 2000. result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_on_corner(self): """Test behaviour for a single non-zero grid cell on the corner.""" expected = np.array([[[0., 0.4, 1., 1., 1.], [0.4, 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.]]]) cube = set_up_cube(zero_point_indices=[(0, 0, 0, 0)], num_grid_points=5) # Point is right on the corner. slice_2d = cube[0, 0, :, :] percentiles = np.array([10, 50, 90]) kernel = np.array([[0., 1., 0.], [1., 1., 1.], [0., 1., 0.]]) result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).pad_and_unpad_cube(slice_2d, kernel)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_masked_to_null(self): """Test behaviour with a masked non-zero point. The behaviour here is not right, as the mask is ignored. This comes directly from the numpy.percentile base behaviour.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.4, 1.0, 1.0], [1.0, 0.4, 0.4, 0.4, 1.0], [1.0, 1.0, 0.4, 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, 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, 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, 1.0, 1.0, 1.0], ], ] ) self.cube.data[2, 2] = 0 mask = np.zeros_like(self.cube.data) mask[2, 2] = 1 self.cube.data = np.ma.masked_array(self.cube.data, mask=mask) percentiles = np.array([10, 50, 90]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(self.cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_masked_other_point(self): """Test behaviour with a non-zero point next to a masked point. The behaviour here is not right, as the mask is ignored.""" expected = np.array([[[[[1., 1., 1., 1., 1.], [1., 1., 0.4, 1., 1.], [1., 0.4, 0.4, 0.4, 1.], [1., 1., 0.4, 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.]]]]]) cube = self.cube mask = np.zeros_like(cube.data) mask[0, 0, 2, 3] = 1 cube.data = np.ma.masked_array(cube.data, mask=mask) percentiles = np.array([10, 50, 90]) radius = 2000. result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_on_corner(self): """Test behaviour for a single non-zero grid cell on the corner.""" expected = np.array( [ [ [0.0, 0.4, 1.0, 1.0, 1.0], [0.4, 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, 1.0], ], [ [0.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, 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, 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], ], ] ) self.cube.data[0, 0] = 0 # Point is right on the corner. percentiles = np.array([10, 50, 90]) kernel = np.array([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]]) result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).pad_and_unpad_cube(self.cube, kernel) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point(self): """Test behaviour for a single non-zero grid cell.""" expected = np.array([ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.4, 1.0, 1.0], [1.0, 0.4, 0.4, 0.4, 1.0], [1.0, 1.0, 0.4, 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, 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, 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, 1.0, 1.0, 1.0], ], ]) data = np.ones((5, 5), dtype=np.float32) data[2, 2] = 0 cube = set_up_variable_cube( data, spatial_grid="equalarea", ) percentiles = np.array([10, 50, 90]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_adjacent_edge(self): """Test behaviour for a single non-zero grid cell near the edge.""" self.cube.data[2, 1] = 0 # Range 3 goes over the edge expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 0.4, 1.0, 1.0, 1.0], [0.4, 0.4, 0.4, 1.0, 1.0], [1.0, 0.4, 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], [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, 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, 1.0, 1.0, 1.0, 1.0], ], ] ) percentiles = np.array([10, 50, 90]) kernel = np.array([[0.0, 1.0, 0.0], [1.0, 1.0, 1.0], [0.0, 1.0, 0.0]]) result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).pad_and_unpad_cube(self.cube, kernel) self.assertArrayAlmostEqual(result.data, expected)
def test_point_pair(self): """Test behaviour for two nearby non-zero grid cells.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 0.0, 0.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], [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, 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, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) self.cube.data[2, 2] = 0 self.cube.data[2, 1] = 0 percentiles = np.array([25, 50, 75]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(self.cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_single_point_low_percentiles(self): """Test behaviour with low percentiles.""" expected = np.array( [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.2, 1.0, 1.0], [1.0, 0.2, 0.2, 0.2, 1.0], [1.0, 1.0, 0.2, 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, 0.4, 1.0, 1.0], [1.0, 0.4, 0.4, 0.4, 1.0], [1.0, 1.0, 0.4, 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, 0.8, 1.0, 1.0], [1.0, 0.8, 0.8, 0.8, 1.0], [1.0, 1.0, 0.8, 1.0, 1.0], [1.0, 1.0, 1.0, 1.0, 1.0], ], ] ) self.cube.data[2, 2] = 0 percentiles = np.array([5, 10, 20]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles ).run(self.cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_number_of_points_half_of_number_of_percentiles(self): """Test when the number of points is half the number of percentiles.""" expected = np.array([ [ [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, 0.1, 1.0, 1.0, 1.0], [1.0, 1.0, 0.1, 0.1, 0.1, 1.0, 1.0], [1.0,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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, 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, 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, 1.0, 1.0, 1.0, 1.0], ], ]) data = np.ones((7, 7), dtype=np.float32) data[3, 3] = 0 cube = set_up_variable_cube( data, spatial_grid="equalarea", ) percentiles = np.array([2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_basic(self): """Test that the __repr__ returns the expected string.""" result = str(GeneratePercentilesFromACircularNeighbourhood()) msg = ('<GeneratePercentilesFromACircularNeighbourhood: ' 'percentiles: {}>'.format(DEFAULT_PERCENTILES)) self.assertEqual(str(result), msg)
def test_number_of_points_half_of_number_of_percentiles(self): """Test when the number of points is half the number of percentiles.""" expected = np.array([[[[[1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 1., 1., 1., 1.], [1., 1., 1., 0.1, 1., 1., 1.], [1., 1., 0.1, 0.1, 0.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., 0.2, 1., 1., 1.], [1., 1., 0.2, 0.2, 0.2, 1., 1.], [1., 1., 1., 0.2, 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.3, 1., 1., 1.], [1., 1., 0.3, 0.3, 0.3, 1., 1.], [1., 1., 1., 0.3, 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.4, 1., 1., 1.], [1., 1., 0.4, 0.4, 0.4, 1., 1.], [1., 1., 1., 0.4, 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.5, 1., 1., 1.], [1., 1., 0.5, 0.5, 0.5, 1., 1.], [1., 1., 1., 0.5, 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.6, 1., 1., 1.], [1., 1., 0.6, 0.6, 0.6, 1., 1.], [1., 1., 1., 0.6, 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.7, 1., 1., 1.], [1., 1., 0.7, 0.7, 0.7, 1., 1.], [1., 1., 1., 0.7, 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.8, 1., 1., 1.], [1., 1., 0.8, 0.8, 0.8, 1., 1.], [1., 1., 1., 0.8, 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.9, 1., 1., 1.], [1., 1., 0.9, 0.9, 0.9, 1., 1.], [1., 1., 1., 0.9, 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., 1.], [1., 1., 1., 1., 1., 1., 1.]]]]]) cube = set_up_cube(zero_point_indices=[(0, 0, 3, 3)], num_grid_points=7) percentiles = np.array([2.5, 5, 7.5, 10, 12.5, 15, 17.5, 20, 22.5, 25]) radius = 2000. result = (GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius)) self.assertArrayAlmostEqual(result.data, expected)
def test_multi_point_multitimes(self): """Test behaviour for points over multiple times.""" data = np.ones((5, 5), dtype=np.float32) cube = set_up_variable_cube( data, spatial_grid="equalarea", ) time_points = [ datetime(2017, 11, 10, 2), datetime(2017, 11, 10, 3), ] cube = add_coordinate( cube, coord_points=time_points, coord_name="time", is_datetime="true", ) cube.data[0, 2, 2] = 0 cube.data[1, 2, 1] = 0 expected = np.array([ [ [ [1.0, 1.0, 1.0, 1.0, 1.0], [1.0, 1.0, 0.4, 1.0, 1.0], [1.0, 0.4, 0.4, 0.4, 1.0], [1.0, 1.0, 0.4, 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, 0.4, 1.0, 1.0, 1.0], [0.4, 0.4, 0.4, 1.0, 1.0], [1.0, 0.4, 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], [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, 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, 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, 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, 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], ], ], ]) percentiles = np.array([10, 50, 90]) radius = 2000.0 result = GeneratePercentilesFromACircularNeighbourhood( percentiles=percentiles).run(cube, radius) self.assertArrayAlmostEqual(result.data, expected)
def test_basic(self): """Test that the plugin returns an iris.cube.Cube.""" result = GeneratePercentilesFromACircularNeighbourhood( ).make_percentile_cube(self.cube) self.assertIsInstance(result, Cube)