def test_fails_ynval_and_slope_set(self): """Test it raises a Value Error if slope and ynval set. """ plugin = LinearWeights(y0val=10.0, slope=-5.0, ynval=5.0) msg = ('Relative end point weight or slope must be set' ' but not both.') with self.assertRaisesRegexp(ValueError, msg): plugin.process(self.cube, self.coord_name, self.coord_vals)
def test_fails_input_not_a_cube(self): """Test it raises a Value Error if not supplied with a cube. """ plugin = LinearWeights() notacube = 0.0 msg = ('The first argument must be an instance of ' 'iris.cube.Cube') with self.assertRaisesRegexp(ValueError, msg): plugin.process(notacube, self.coord_name)
def test_fails_input_not_a_cube(self): """Test it raises a Value Error if not supplied with a cube. """ plugin = LinearWeights(y0val=20.0, ynval=2.0) notacube = 0.0 msg = "The first argument must be an instance of " "iris.cube.Cube" with self.assertRaisesRegex(TypeError, msg): plugin.process(notacube, self.coord_name)
def test_fails_weights_negative(self): """Test it raises a Value Error if weights become negative. """ plugin = LinearWeights(y0val=10.0, slope=-5.0) cubenew = add_realizations(self.cube, 6) coord = cubenew.coord('realization') msg = 'Weights must be positive' with self.assertRaisesRegexp(ValueError, msg): plugin.process(cubenew, coord)
def test_fails_coord_not_in_cube(self): """Test it raises a Value Error if coord not in the cube. """ coord = AuxCoord([], long_name="notset") plugin = LinearWeights() msg = ('The coord for this plugin must be ' 'an existing coordinate in the input cube') with self.assertRaisesRegexp(ValueError, msg): plugin.process(self.cube, coord)
def test_works_scalar_coord(self): """Test it works if scalar coordinate. """ self.cube.add_aux_coord(AuxCoord(1, long_name="scalar_coord", units="no_unit")) coord = self.cube.coord("scalar_coord") plugin = LinearWeights(y0val=20.0, ynval=2.0) result = plugin.process(self.cube, coord) self.assertArrayAlmostEqual(result.data, np.array([1.0]))
def test_works_with_larger_num(self): """Test it works with larger num_of_vals. """ plugin = LinearWeights(y0val=10.0, ynval=5.0) cubenew = add_realizations(self.cube, 6) coord = cubenew.coord('realization') result = plugin.process(cubenew, coord) expected_result = np.array( [0.22222222, 0.2, 0.17777778, 0.15555556, 0.13333333, 0.11111111]) self.assertArrayAlmostEqual(result, expected_result)
def test_works_with_missing_coord(self): """Test it works with missing coord """ plugin = LinearWeights(y0val=10.0, ynval=5.0) cubenew = add_realizations(self.cube, 6) coord_vals = '0, 1, 2, 3, 4, 5, 6' coord_name = 'realization' result = plugin.process(cubenew, coord_name, coord_vals) expected_result = np.array( [0.206349, 0.190476, 0.174603, 0.15873, 0.142857, 0.126984]) self.assertArrayAlmostEqual(result, expected_result)
def test_works_with_larger_num(self): """Test it works with larger num_of_vals. """ plugin = LinearWeights(y0val=10.0, ynval=5.0) cubenew = add_coordinate(self.cube, np.arange(6), "realization", dtype=np.int32) coord = cubenew.coord("realization") result = plugin.process(cubenew, coord) expected_result = np.array( [0.22222222, 0.2, 0.17777778, 0.15555556, 0.13333333, 0.11111111] ) self.assertArrayAlmostEqual(result.data, expected_result)
def test_works_y0val_and_ynval_set(self): """Test it works if y0val and ynval set. """ plugin = LinearWeights(y0val=10.0, ynval=5.0) result = plugin.process(self.cube, self.coord_name, self.coord_vals) expected_result = np.array([0.66666667, 0.33333333]) self.assertArrayAlmostEqual(result, expected_result)
def test_works_defaults_used(self): """Test it works if defaults used. """ plugin = LinearWeights() result = plugin.process(self.cube, self.coord_name, self.coord_vals) expected_result = np.array([0.90909091, 0.09090909]) self.assertArrayAlmostEqual(result, expected_result)
def test_works_scalar_coord(self): """Test it works if scalar coordinate. """ coord = self.cube.coord("scalar_coord") plugin = LinearWeights() result = plugin.process(self.cube, coord) self.assertArrayAlmostEqual(result, np.array([1.0]))
def test_fails_y0val_lessthan_zero(self): """Test it raises a Value Error if y0val less than zero. """ plugin = LinearWeights(y0val=-10.0) msg = ('y0val must be a float >= 0.0') with self.assertRaisesRegexp(ValueError, msg): plugin.process(self.cube, self.coord_name, self.coord_vals)
def test_array_sum_equals_one(self): """Test that the resulting weights add up to one. """ plugin = LinearWeights() result = plugin.process(self.cube, self.coord_name, self.coord_vals) self.assertAlmostEquals(result.sum(), 1.0)
def test_basic(self): """Test that the plugin returns an array of weights. """ plugin = LinearWeights() result = plugin.process(self.cube, self.coord_name, self.coord_vals) self.assertIsInstance(result, np.ndarray)
def test_basic(self): """Test that the plugin returns a cube of weights. """ plugin = LinearWeights(y0val=20.0, ynval=2.0) result = plugin.process(self.cube, self.coord_name) self.assertIsInstance(result, iris.cube.Cube)