def test_renormalise_when_missing_data(self): """ Test we get the expected result when the weights need renormalising to account for missing data that comes from neighbourhood processing where there are no points to process in a given band for a given point. The expected behaviour is that the weights are renormalised and the result for that point takes 100% of the band with a valid value in. """ data = np.array( [ [[1, 1, 0], [1, 1, 0], [0, 0, 0]], [[0, 0, 1], [0, 1, 1], [1, 1, 0]], [[0, 0, 0], [0, 0, 0], [0, 0, np.nan]], ], dtype=np.float32, ) self.cube.data = data expected_data = np.array( [[1.0, 1.0, 1.0], [1.0, 1.0, 0.75], [1.0, 0.75, 0]], dtype=np.float32) plugin = ApplyNeighbourhoodProcessingWithAMask( "topographic_zone", "square", 2000, collapse_weights=self.weights_cube) result = plugin.collapse_mask_coord(self.cube) assert_allclose(result.data, expected_data) self.assertNotIn("topographic_zone", result.coords())
def test_basic(self): """Test we get the expected result with a simple collapse""" expected_data = np.array( [[1.0, 1.0, 1.0], [1.0, 1.0, 0.75], [1.0, 0.75, 0.5]], dtype=np.float32) plugin = ApplyNeighbourhoodProcessingWithAMask( "topographic_zone", 2000, collapse_weights=self.weights_cube) result = plugin.collapse_mask_coord(self.cube) assert_allclose(result.data, expected_data) self.assertNotIn("topographic_zone", result.coords())
def test_masked_weights_data(self): """Test points where weights are masked. Covers the case where sea points may be masked out so they aren't neighbourhood processed.""" self.weights_cube.data[:, 0, 0] = np.nan self.weights_cube.data = np.ma.masked_invalid(self.weights_cube.data) expected_data = np.array( [[np.nan, 1.0, 1.0], [1.0, 1.0, 0.75], [1.0, 0.75, 0.5]], dtype=np.float32) expected_mask = np.array([[True, False, False], [False, False, False], [False, False, False]]) plugin = ApplyNeighbourhoodProcessingWithAMask( "topographic_zone", 2000, collapse_weights=self.weights_cube) result = plugin.collapse_mask_coord(self.cube) assert_allclose(result.data.data, expected_data) assert_allclose(result.data.mask, expected_mask) self.assertNotIn("topographic_zone", result.coords())
def test_masked_weights_and_missing(self): """Test masked weights, and one point has nan in from neighbourhood processing. Covers the case where sea points may be masked out so they aren't neighbourhood processed. The nan point comes from neighbourhood processing where it has found no points to process for a given point in a given band.""" self.weights_cube.data[:, 0, 0] = np.nan self.weights_cube.data = np.ma.masked_invalid(self.weights_cube.data) self.cube.data[2, 2, 2] = np.nan expected_data = np.array( [[np.nan, 1.0, 1.0], [1.0, 1.0, 0.75], [1.0, 0.75, 0.0]], dtype=np.float32) expected_mask = np.array([[True, False, False], [False, False, False], [False, False, False]]) plugin = ApplyNeighbourhoodProcessingWithAMask( "topographic_zone", 2000, collapse_weights=self.weights_cube) result = plugin.collapse_mask_coord(self.cube) assert_allclose(result.data.data, expected_data) assert_allclose(result.data.mask, expected_mask) self.assertNotIn("topographic_zone", result.coords())