def setUp(self): """Set up a cube and plugin for testing.""" self.cube = set_up_variable_cube(np.ones((3, 3), dtype=np.float32)) self.plugin = Threshold(latitude_to_threshold) self.plugin.threshold_coord_name = self.cube.name() self.thresholds = np.array( latitude_to_threshold(self.cube.coord("latitude").points, midlatitude=1.0, tropics=3.0))
def test_threshold_negative(self): """Repeat the test with negative numbers when the threshold is negative.""" expected_result_array = np.zeros_like(self.cube.data) expected_result_array[2:-2][:] = 1 self.cube.data = 0 - self.cube.data plugin = Threshold(lambda lat: latitude_to_threshold( lat, midlatitude=-1e-6, tropics=-1.0)) result = plugin(self.cube) self.assertArrayAlmostEqual(result.data, expected_result_array)
def test_threshold_le(self): """Test equal-to values when we are in le threshold mode.""" expected_result_array = np.ones_like(self.cube.data) plugin = Threshold( lambda lat: latitude_to_threshold(lat, midlatitude=0.5, tropics=1.0), comparison_operator="<=", ) name = "probability_of_{}_below_threshold" expected_name = name.format(self.cube.name()) expected_attribute = "less_than_or_equal_to" result = plugin(self.cube) self.assertEqual(result.name(), expected_name) self.assertEqual( result.coord(var_name="threshold").attributes["spp__relative_to_threshold"], expected_attribute, ) self.assertArrayAlmostEqual(result.data, expected_result_array)
def test_threshold_gt(self): """Test equal-to values when we are in > threshold mode.""" expected_result_array = np.ones_like(self.cube.data) expected_result_array[3][:] = 0 plugin = Threshold( lambda lat: latitude_to_threshold(lat, midlatitude=1e-6, tropics=0.5), comparison_operator=">", ) name = "probability_of_{}_above_threshold" expected_name = name.format(self.cube.name()) expected_attribute = "greater_than" result = plugin(self.cube) self.assertEqual(result.name(), expected_name) self.assertEqual( result.coord(var_name="threshold").attributes["spp__relative_to_threshold"], expected_attribute, ) self.assertArrayAlmostEqual(result.data, expected_result_array)
def test_threshold_unit_conversion(self): """Test data are correctly thresholded when the threshold (mm) is given in units different from that of the input cube (m).""" expected_result_array = np.ones_like(self.cube.data) expected_result_array[3][:] = 0 plugin = Threshold( lambda lat: latitude_to_threshold(lat, midlatitude=1e-3, tropics=500.0), threshold_units="mm", ) result = plugin(self.cube) self.assertArrayAlmostEqual(result.data, expected_result_array) expected_points = ( plugin.threshold_function(self.cube.coord("latitude").points) / 1000 ) expected_coord = AuxCoord( np.array(expected_points, dtype=np.float32), standard_name=self.cube.name(), var_name="threshold", units=self.cube.units, attributes={"spp__relative_to_threshold": "greater_than"}, ) self.assertEqual(result.coord(self.cube.name()), expected_coord)
def setUp(self): """Create a cube with a constant non-zero value spanning latitudes from -60 to +60.""" attributes = {"title": "UKV Model Forecast"} data = np.full((7, 3), fill_value=0.5, dtype=np.float32) self.cube = set_up_variable_cube( data, name="precipitation_amount", units="m", attributes=attributes, standard_grid_metadata="uk_det", domain_corner=(-60, 0), grid_spacing=20, ) self.masked_cube = self.cube.copy() data = np.copy(self.cube.data) mask = np.zeros_like(data) data[0][0] = -32768.0 mask[0][0] = 1 self.masked_cube.data = np.ma.MaskedArray(data, mask=mask) self.plugin = Threshold(lambda lat: latitude_to_threshold( lat, midlatitude=1e-6, tropics=1.0))