def test_preserve_dimensions_input(self): """Test that the dimensions on the output cube are the same as the input cube, apart from the additional topographic zone coordinate. """ self.cube.remove_coord("realization") cube = add_dimensions_to_cube( self.cube, OrderedDict([("threshold", 3), ("realization", 4)])) coord_for_masking = "topographic_zone" radii = 2000 result = ApplyNeighbourhoodProcessingWithAMask( coord_for_masking, radii).process(cube, self.mask_cube) expected_dims = list(cube.dim_coords) expected_dims.insert(2, self.mask_cube.coord("topographic_zone")) self.assertEqual(result.dim_coords, tuple(expected_dims)) self.assertEqual(result.coord_dims("realization"), (0,)) self.assertEqual(result.coord_dims("threshold"), (1,)) self.assertEqual(result.coord_dims("topographic_zone"), (2,)) self.assertEqual(result.coord_dims("projection_y_coordinate"), (3,)) self.assertEqual(result.coord_dims("projection_x_coordinate"), (4,))
def test_preserve_dimensions_with_single_point(self): """Test that the dimensions on the output cube are the same as the input cube, apart from the collapsed dimension. Check that a dimension coordinate with a single point is preserved and not demoted to a scalar coordinate.""" self.cube.remove_coord("realization") cube = add_dimensions_to_cube(self.cube, {"threshold": 4, "realization": 1}) coord_for_masking = "topographic_zone" radii = 2000 result = ApplyNeighbourhoodProcessingWithAMask( coord_for_masking, radii).process(cube, self.mask_cube) expected_dims = list(cube.dim_coords) expected_dims.insert(2, self.mask_cube.coord("topographic_zone")) self.assertEqual(result.dim_coords, tuple(expected_dims)) self.assertEqual(result.coord_dims("realization"), (0,)) self.assertEqual(result.coord_dims("threshold"), (1,)) self.assertEqual(result.coord_dims("topographic_zone"), (2,)) self.assertEqual(result.coord_dims("projection_y_coordinate"), (3,)) self.assertEqual(result.coord_dims("projection_x_coordinate"), (4,))
def test_collapse_preserve_dimensions_input(self): """Test that the dimensions on the output cube are the same as the input cube, apart from the additional topographic zone coordinate. """ self.cube.remove_coord("realization") cube = add_dimensions_to_cube( self.cube, OrderedDict([("threshold", 3), ("realization", 4)]) ) coord_for_masking = "topographic_zone" radii = 2000 uncollapsed_result = None mask_data = np.array( [ [ [1, 1, 1, 1, 1], [1, 1, 0, 0, 0], [1, 1, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], ], [ [0, 0, 0, 0, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1], [1, 1, 0, 1, 0], [1, 1, 0, 0, 0], ], [ [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 1, 1], [0, 0, 1, 1, 1], [0, 0, 1, 1, 1], ], ] ) mask_cube = self.mask_cube.copy(mask_data) for collapse_weights in [None, self.weights_cube]: coords_order = [ "realization", "threshold", "topographic_zone", "projection_y_coordinate", "projection_x_coordinate", ] result = ApplyNeighbourhoodProcessingWithAMask( coord_for_masking, radii, collapse_weights=collapse_weights, )(cube, mask_cube) expected_dims = list(cube.dim_coords) if collapse_weights is None: uncollapsed_result = result expected_dims.insert(2, self.mask_cube.coord("topographic_zone")) else: coords_order.remove("topographic_zone") collapsed_result = CollapseMaskedNeighbourhoodCoordinate( coord_for_masking, collapse_weights )(uncollapsed_result) self.assertArrayAlmostEqual(result.data, collapsed_result.data) self.assertEqual(result.dim_coords, tuple(expected_dims)) for dim, coord in enumerate(coords_order): self.assertEqual(result.coord_dims(coord), (dim,))