def test_handles_angle_wrap(self): """Test that code correctly handles 360 and 0 degrees.""" expected_out = 1 + 0j result = WindDirection().deg_to_complex(0) self.assertAlmostEqual(result, expected_out) expected_out = 1 - 0j result = WindDirection().deg_to_complex(360) self.assertAlmostEqual(result, expected_out)
def test_returns_expected_values(self): """Test that the function returns correct 2D arrays of floats. """ expected_wind_mean = (np.array( [[176.63627625, 46.00244522, 90.0, 90.0], [170.0, 170.0, 47.0, 36.54423141], [333.41320801, 320.03521729, 10.0, 10.0]])) expected_r_vals = np.array([[0.5919044, 0.99634719, 0.2, 0.6], [1.0, 1.0, 1.0, 0.92427504], [0.87177974, 0.91385943, 1.0, 1.0]]) expected_confidence_measure = (np.array( [[0.73166388, 0.95813018, 0.6, 0.8], [1.0, 1.0, 1.0, 0.84808648], [0.75270665, 0.83861077, 1.0, 1.0]])) result_cube, r_vals_cube, confidence_measure_cube = ( WindDirection().process(self.cube)) result = result_cube.data r_vals = r_vals_cube.data confidence_measure = confidence_measure_cube.data self.assertIsInstance(result, np.ndarray) self.assertIsInstance(r_vals, np.ndarray) self.assertIsInstance(confidence_measure, np.ndarray) self.assertArrayAlmostEqual(result, expected_wind_mean) self.assertArrayAlmostEqual(r_vals, expected_r_vals) self.assertArrayAlmostEqual(confidence_measure, expected_confidence_measure)
def test_fails_if_data_is_not_convertible_to_degrees(self): """Test code raises a ValueError if input cube is not convertible to degrees.""" cube = set_up_temperature_cube() msg = 'Input cube cannot be converted to degrees' with self.assertRaisesRegexp(ValueError, msg): WindDirection().process(cube)
def test_fails_if_data_is_not_cube(self): """Test code raises a Type Error if input cube is not a cube.""" input_data = 50.0 msg = ('Wind direction input is not a cube, but' ' {0}'.format(type(input_data))) with self.assertRaisesRegexp(TypeError, msg): WindDirection().process(input_data)
def test_fails_if_data_is_not_array(self): """Test code raises a Type Error if input data not an array.""" input_data = 0 - 1j msg = ('Input data is not a numpy array, but' ' {}'.format(type(input_data))) with self.assertRaisesRegexp(TypeError, msg): WindDirection().complex_to_deg(input_data)
def test_basic(self): """Test that the plugin returns expected data types. """ result_cube, r_vals_cube, confidence_measure_cube = ( WindDirection().process(self.cube)) self.assertIsInstance(result_cube, Cube) self.assertIsInstance(r_vals_cube, Cube) self.assertIsInstance(confidence_measure_cube, Cube)
def test_return_single_precision(self): """Test that the function returns data of float32.""" result_cube, r_vals_cube, confidence_measure_cube = ( WindDirection().process(self.cube)) self.assertEqual(result_cube.dtype, np.float32) self.assertEqual(r_vals_cube.dtype, np.float32) self.assertEqual(confidence_measure_cube.dtype, np.float32)
def test_runs_function(self): """First element has two angles directly opposite (90 & 270 degs). Therefore the calculated mean angle of 180 degs is basically meaningless with an r value of nearly zero. So the code subistites the wind direction taken from the first ensemble value in its place.""" expected_out = np.array([[90.0, 55.0], [280.0, 0.0]]) result = WindDirection.wind_dir_decider(WIND_DIR_DEG, WIND_DIR_DEG_MEAN, WIND_DIR_R_VALS, 0.01) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, expected_out)
def test_returns_confidence(self): """First element has two angles directly opposite (90 & 270 degs). Therefore the calculated mean angle of 180 degs is basically meaningless. This code calculates a confidence measure based on how far the individual ensemble realizationss are away from the mean point.""" expected_out = np.array([[0.0, 0.95638061], [0.91284426, 0.91284426]]) result = WindDirection().calc_confidence_measure( WIND_DIR_COMPLEX, WIND_DIR_DEG_MEAN, WIND_DIR_R_VALS, 0.01, 0) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, expected_out)
def test_basic(self): """Test that the __repr__ returns the expected string.""" result = str(WindDirection()) msg = ('<WindDirection>') self.assertEqual(result, msg)
def test_converts_array(self): """Test that code can find r-values from array of complex numbers.""" expected_out = np.ones(COMPLEX_ANGLES.shape, dtype=np.float32) result = WindDirection().find_r_values(COMPLEX_ANGLES) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, expected_out)
def test_converts_single(self): """Tests that r-value correctly extracted from complex value.""" expected_out = 2.0 complex_in = 1.4142135624 + 1.4142135624j # Complex for angle=45 and r=2 result = WindDirection().find_r_values(complex_in) self.assertAlmostEqual(result, expected_out)
def test_converts_array(self): """Tests that array of complex values are converted to degrees.""" result = WindDirection().complex_to_deg(COMPLEX_ANGLES) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, DEGREE_ANGLES)
def test_handles_angle_wrap(self): """Test that code correctly handles 360 and 0 degrees.""" # Input is complex for 0 and 360 deg - both should return 0.0. input_data = np.array([1 + 0j, 1 - 0j]) result = WindDirection().complex_to_deg(input_data) self.assertTrue((result == 0.0).all())
def test_converts_array(self): """Tests that array of floats is converted to complex array.""" result = WindDirection().deg_to_complex(DEGREE_ANGLES) self.assertIsInstance(result, np.ndarray) self.assertArrayAlmostEqual(result, COMPLEX_ANGLES)
def test_converts_single(self): """Tests that degree angle value is converted to complex.""" expected_out = 0.707106781187 + 0.707106781187j result = WindDirection().deg_to_complex(45.0) self.assertAlmostEqual(result, expected_out)