def test_exception(self): """Test the expected exception is raised.""" distance = 2000 radius = 2000 msg = "Only a square neighbourhood is accepted" with self.assertRaisesRegexp(ValueError, msg): ProbabilityOfOccurrence(distance, "circular", radius)
def test_basic(self): """Test that the __repr__ returns the expected string.""" result = str(ProbabilityOfOccurrence(2000, "square", 2000)) msg = ('<ProbabilityOfOccurrence: distance: 2000; ' 'neighbourhood_method: square; radii: 2000; ' 'lead_times: None; weighted_mode: True; ' 'ens_factor: 1.0>') self.assertEqual(result, msg)
def test_with_realization(self): """Test when a realization coordinate is present.""" expected = np.array( [[[1, 1., 0.77777778, 0.55555556, 0.33333333], [0.66666667, 0.77777778, 0.77777778, 0.77777778, 0.66666667], [0.33333333, 0.55555556, 0.77777778, 1., 1.], [0., 0.22222222, 0.44444444, 0.66666667, 0.66666667], [0., 0.11111111, 0.22222222, 0.33333333, 0.33333333]]]) distance = 2000 neighbourhood_method = "square" radii = 2000 result = (ProbabilityOfOccurrence(distance, neighbourhood_method, radii).process(self.cube)) self.assertIsInstance(result, Cube) self.assertArrayAlmostEqual(result.data, expected)
def test_additional_arguments(self): """Test when all keyword arguments are passed in.""" expected = np.array( [[[1., 1., 0.8333333, 0.666667, 0.5], [0.66666667, 0.77777778, 0.77777778, 0.77777778, 0.66666667], [0.33333333, 0.55555556, 0.77777778, 1., 1.], [0., 0.22222222, 0.44444444, 0.66666667, 0.66666667], [0., 0.16666667, 0.33333333, 0.5, 0.5]], [[0.77777778, 0.75, 0.73333333, 0.75, 0.77777778], [0.66666667, 0.6875, 0.7, 0.75, 0.8333333], [0.53333333, 0.55, 0.56, 0.6, 0.66666667], [0.41666667, 0.5, 0.55, 0.625, 0.75], [0.22222222, 0.33333333, 0.4, 0.5, 0.66666667]]]) data = np.zeros((1, 2, 5, 5)) data[0, :, 0, 1] = 1.0 data[0, :, 2, 3] = 1.0 y_dimension_values = np.arange(0.0, 10000.0, 2000.0) cube = set_up_cube(data, "lwe_precipitation_rate", "m s-1", y_dimension_values=y_dimension_values, x_dimension_values=y_dimension_values, timesteps=np.array([402192.5, 402195.5])) distance = 2000 neighbourhood_method = "square" radii = [2000, 4000] lead_times = [3, 6] cube.add_aux_coord( AuxCoord(lead_times, "forecast_period", units="hours"), 1) weighted_mode = False ens_factor = 0.9 expected_cube_for_shape = cube.copy() expected_cube_for_shape = expected_cube_for_shape.collapsed( "realization", MEAN) orig_shape = expected_cube_for_shape.data.copy().shape result = (ProbabilityOfOccurrence(distance, neighbourhood_method, radii, lead_times=lead_times, weighted_mode=weighted_mode, ens_factor=ens_factor).process(cube)) self.assertIsInstance(result, Cube) self.assertArrayAlmostEqual(result.data, expected) self.assertEqual(result.data.shape, orig_shape) self.assertEqual(result.coord("forecast_period").units, "hours")
def test_without_realization(self): """Test when a realization coordinate is not present.""" expected = np.array( [[[1., 1., 0.8333333, 0.666667, 0.5], [0.66666667, 0.77777778, 0.77777778, 0.77777778, 0.66666667], [0.33333333, 0.55555556, 0.77777778, 1., 1.], [0., 0.22222222, 0.44444444, 0.66666667, 0.66666667], [0., 0.16666667, 0.33333333, 0.5, 0.5]]]) cube = self.cube[0, :, :, :] cube.remove_coord("realization") distance = 2000 neighbourhood_method = "square" radii = 2000 orig_shape = cube.data.copy().shape result = (ProbabilityOfOccurrence(distance, neighbourhood_method, radii).process(self.cube)) self.assertIsInstance(result, Cube) self.assertEqual(result.data.shape, orig_shape) self.assertArrayAlmostEqual(result.data, expected)
def test_additional_arguments(self): """Test when all keyword arguments are passed in.""" expected = np.array( [[[[1., 1., 0.77777778, 0.55555556, 0.333333], [0.66666667, 0.77777778, 0.77777778, 0.77777778, 0.66666667], [0.33333333, 0.55555556, 0.77777778, 1., 1.], [0., 0.22222222, 0.44444444, 0.66666667, 0.66666667], [0., 0.11111111, 0.22222222, 0.33333333, 0.33333333]], [[0.84, 0.8, 0.76, 0.72, 0.68], [0.68, 0.7, 0.72, 0.74, 0.76], [0.48, 0.52, 0.56, 0.6, 0.64], [0.3, 0.4, 0.5, 0.6, 0.7], [0.12, 0.24, 0.36, 0.48, 0.6]]]]) data = np.zeros((1, 2, 5, 5)) data[0, :, 0, 1] = 1.0 data[0, :, 2, 3] = 1.0 y_dimension_values = np.arange(0.0, 10000.0, 2000.0) cube = set_up_cube(data, "lwe_precipitation_rate", "m s-1", y_dimension_values=y_dimension_values, x_dimension_values=y_dimension_values, timesteps=np.array([402192.5, 402195.5])) distance = 2000 neighbourhood_method = "square" radii = [2000, 4000] lead_times = [3, 6] cube.add_aux_coord( AuxCoord(lead_times, "forecast_period", units="hours"), 1) unweighted_mode = True ens_factor = 0.9 result = (ProbabilityOfOccurrence(distance, neighbourhood_method, radii, lead_times=lead_times, unweighted_mode=unweighted_mode, ens_factor=ens_factor).process(cube)) self.assertIsInstance(result, Cube) self.assertArrayAlmostEqual(result.data, expected)