def test_interpolation(self): """Test that interpolation is working as expected in _find_radii.""" fp_points = np.array([2, 3, 4]) radii = [10000, 30000] lead_times = [2, 4] plugin = NBHood(radii=radii, lead_times=lead_times) result = plugin._find_radii(cube_lead_times=fp_points) expected_result = np.array([10000.0, 20000.0, 30000.0]) self.assertArrayAlmostEqual(result, expected_result)
def test_basic_float_cube_lead_times_is_none(self): """Test _find_radii returns an unaltered radius if the lead times are none, and this radius is a float.""" neighbourhood_method = CircularNeighbourhood() radius = 6300 plugin = NBHood(neighbourhood_method, radius) result = plugin._find_radii(cube_lead_times=None) expected_result = 6300.0 self.assertIsInstance(result, float) self.assertAlmostEqual(result, expected_result)
def test_basic_array_cube_lead_times_an_array(self): """Test _find_radii returns an array with the correct values.""" fp_points = np.array([2, 3, 4]) radii = [10000, 20000, 30000] lead_times = [1, 3, 5] plugin = NBHood(radii, lead_times=lead_times) result = plugin._find_radii(cube_lead_times=fp_points) expected_result = np.array([15000.0, 20000.0, 25000.0]) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, expected_result)
def test_basic_float_cube_lead_times_is_none(self): """Test _find_radii returns a float with the correct value.""" neighbourhood_method = CircularNeighbourhood() ens_factor = 0.8 num_ens = 2.0 radius = 6300 plugin = NBHood(neighbourhood_method, radius, ens_factor=ens_factor) result = plugin._find_radii(num_ens) expected_result = 3563.8181771801998 self.assertIsInstance(result, float) self.assertAlmostEquals(result, expected_result)
def test_interpolation(self): """Test that interpolation is working as expected in _find_radii.""" neighbourhood_method = CircularNeighbourhood fp_points = np.array([2, 3, 4]) radii = [10000, 30000] lead_times = [2, 4] plugin = NBHood(neighbourhood_method(), radii=radii, lead_times=lead_times) result = plugin._find_radii(cube_lead_times=fp_points) expected_result = np.array([10000., 20000., 30000.]) self.assertArrayAlmostEqual(result, expected_result)
def test_source_realizations(self): """Test when the array has source_realization attribute.""" realization_list = [0, 1, 2, 3] cube = (set_up_cube_with_no_realizations( source_realizations=realization_list)) radii = 5600 neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii) result = plugin.process(cube) self.assertIsInstance(result, Cube) expected = np.ones([1, 16, 16]) expected[0, 6:9, 6:9] = ([0.91666667, 0.875, 0.91666667], [0.875, 0.83333333, 0.875], [0.91666667, 0.875, 0.91666667]) self.assertArrayAlmostEqual(result.data, expected)
def test_radii_varying_with_lead_time(self): """ Test that a cube is returned when the radius varies with lead time. """ cube = set_up_cube(num_time_points=3) iris.util.promote_aux_coord_to_dim_coord(cube, "time") time_points = cube.coord("time").points fp_points = [2, 3, 4] cube = add_forecast_reference_time_and_forecast_period( cube, time_point=time_points, fp_point=fp_points) radii = [10000, 20000, 30000] lead_times = [2, 3, 4] neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii, lead_times) result = plugin.process(cube) self.assertIsInstance(result, Cube)
def test_interpolation(self): """Test that interpolation is working as expected in _find_radii.""" fp_points = np.array([2, 3, 4]) neighbourhood_method = CircularNeighbourhood() ens_factor = 0.8 num_ens = 4.0 fp_points = np.array([2, 3, 4]) radii = [10000, 30000] lead_times = [2, 4] plugin = NBHood(neighbourhood_method, radii, lead_times=lead_times, ens_factor=ens_factor) result = plugin._find_radii(num_ens, cube_lead_times=fp_points) expected_result = np.array([4000., 8000., 12000.]) self.assertArrayAlmostEqual(result, expected_result)
def test_basic_array_cube_lead_times_an_array(self): """Test _find_radii returns an array with the correct values.""" neighbourhood_method = CircularNeighbourhood ens_factor = 0.9 num_ens = 2.0 fp_points = np.array([2, 3, 4]) radii = [10000, 20000, 30000] lead_times = [2, 3, 4] plugin = NBHood(neighbourhood_method(), radii, lead_times=lead_times, ens_factor=ens_factor) result = plugin._find_radii(num_ens, cube_lead_times=fp_points) expected_result = np.array([6363.961031, 12727.922061, 19091.883092]) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, expected_result)
def test_radii_varying_with_lead_time_with_interpolation(self): """Test that a cube is returned for the following conditions: 1. The radius varies with lead time. 2. Linear interpolation is required to create values for the radii which are required but were not specified within the 'radii' argument.""" cube = set_up_cube(num_time_points=3) iris.util.promote_aux_coord_to_dim_coord(cube, "time") time_points = cube.coord("time").points fp_points = [2, 3, 4] cube = add_forecast_reference_time_and_forecast_period( cube, time_point=time_points, fp_point=fp_points) radii = [10000, 30000] lead_times = [2, 4] neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii, lead_times) result = plugin.process(cube) self.assertIsInstance(result, Cube)
def test_radii_varying_with_lead_time_fp_seconds(self): """ Test that a cube fp coord is unchanged by the lead time calculation. """ cube = set_up_cube(num_time_points=3) iris.util.promote_aux_coord_to_dim_coord(cube, "time") time_points = cube.coord("time").points fp_points = [2, 3, 4] cube = add_forecast_reference_time_and_forecast_period( cube, time_point=time_points, fp_point=fp_points) cube.coord("forecast_period").convert_units("seconds") radii = [10000, 20000, 30000] lead_times = [2, 3, 4] neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii, lead_times) result = plugin.process(cube) self.assertIsInstance(result, Cube) self.assertEqual(cube.coord("forecast_period").units, "seconds")
def test_radii_varying_with_lead_time_with_interpolation_check_data(self): """Test that a cube with the correct data is returned for the following conditions: 1. The radius varies with lead time. 2. Linear interpolation is required to create values for the radii which are required but were not specified within the 'radii' argument.""" cube = set_up_cube(zero_point_indices=((0, 0, 7, 7), ( 0, 1, 7, 7, ), (0, 2, 7, 7)), num_time_points=3) expected = np.ones_like(cube.data) expected[0, 0, 6:9, 6:9] = ([0.91666667, 0.875, 0.91666667], [0.875, 0.83333333, 0.875], [0.91666667, 0.875, 0.91666667]) expected[0, 1, 5:10, 5:10] = SINGLE_POINT_RANGE_3_CENTROID expected[0, 2, 4:11, 4:11] = ([ 1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1 ], [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925], [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985], [ 0.9825, 0.97, 0.9625, 0.96, 0.9625, 0.97, 0.9825 ], [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985], [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925], [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1]) iris.util.promote_aux_coord_to_dim_coord(cube, "time") time_points = cube.coord("time").points fp_points = [2, 3, 4] cube = add_forecast_reference_time_and_forecast_period( cube, time_point=time_points, fp_point=fp_points) radii = [5600, 9500] lead_times = [2, 4] neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii, lead_times) result = plugin.process(cube) self.assertArrayAlmostEqual(result.data, expected)
def test_radii_varying_with_lead_time_check_data(self): """ Test that the expected data is produced when the radius varies with lead time. """ cube = set_up_cube(zero_point_indices=((0, 0, 7, 7), ( 0, 1, 7, 7, ), (0, 2, 7, 7)), num_time_points=3) expected = np.ones_like(cube.data) expected[0, 0, 6:9, 6:9] = ([0.91666667, 0.875, 0.91666667], [0.875, 0.83333333, 0.875], [0.91666667, 0.875, 0.91666667]) expected[0, 1, 5:10, 5:10] = SINGLE_POINT_RANGE_3_CENTROID expected[0, 2, 4:11, 4:11] = ([ 1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1 ], [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925], [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985], [ 0.9825, 0.97, 0.9625, 0.96, 0.9625, 0.97, 0.9825 ], [0.985, 0.9725, 0.965, 0.9625, 0.965, 0.9725, 0.985], [0.9925, 0.98, 0.9725, 0.97, 0.9725, 0.98, 0.9925], [1, 0.9925, 0.985, 0.9825, 0.985, 0.9925, 1]) iris.util.promote_aux_coord_to_dim_coord(cube, "time") time_points = cube.coord("time").points fp_points = [2, 3, 4] cube = add_forecast_reference_time_and_forecast_period( cube, time_point=time_points, fp_point=fp_points) radii = [5600, 7600, 9500] lead_times = [2, 3, 4] neighbourhood_method = CircularNeighbourhood() plugin = NBHood(neighbourhood_method, radii, lead_times) result = plugin.process(cube) self.assertArrayAlmostEqual(result.data, expected)