def test_basic_weights(self): """Test that the function returns the correct triangular weights in a simple case""" width = 3 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.0, 0.0, 0.0, 0.11111111, 0.22222222, 0.33333333, 0.22222222, 0.11111111, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_unevenly_spaced_coord(self): """Test the case where the input coordinate is not equally spaced. This represents the case where the data changes to 3 hourly. In this case the weights are assigned according to the value in the coordinate.""" width = 5 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(10) coord_vals = np.append(coord_vals, [12, 15, 18, 21, 24]) midpoint = 8 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.0, 0.0, 0.0, 0.0, 0.05, 0.1, 0.15, 0.2, 0.25, 0.2, 0.05, 0.0, 0.0, 0.0, 0.0, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_non_integer_width(self): """Test when the width of the triangle does not fall on a grid point. This only affects the slope of the triangle slightly.""" width = 3.5 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.0, 0.0, 0.04, 0.12, 0.2, 0.28, 0.2, 0.12, 0.04, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_non_integer_midpoint(self): """Test the case where the midpoint of the triangle is not a point in the input coordinate. In this case we do not sample the peak of the triangle.""" width = 2 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 3.5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.0, 0.0, 0.125, 0.375, 0.375, 0.125, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_large_width(self): """Test the case where the width of the triangle is larger than the coordinate input. In this case all the weights are non-zero but still form the shape of a triangle.""" width = 10 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.055556, 0.066667, 0.077778, 0.088889, 0.1, 0.111111, 0.1, 0.088889, 0.077778, 0.066667, 0.055556, 0.044444, 0.033333, 0.022222, 0.011111, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_midpoint_at_edge(self): """Test that the correct triangular weights are returned for a case where the midpoint is close to the end of the input coordinate. In this case the triangle is cut off at the end of the coordinate""" width = 3 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 1 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) expected_weights = np.array([ 0.25, 0.375, 0.25, 0.125, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, 0.0, ]) self.assertArrayAlmostEqual(weights, expected_weights)
def test_basic(self): """Test that the function returns a numpy array. Also check that the length of the weights is correct and they add up to 1.0""" width = 3 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) self.assertIsInstance(weights, np.ndarray) self.assertEqual(len(weights), len(coord_vals)) self.assertEqual(weights.sum(), 1.0)
def test_basic(self): """Test that the function returns a numpy array. Also check that the length of the weights is correct and they add up to 1.0""" width = 3 triangular_weights_instance = ChooseDefaultWeightsTriangular(width) coord_vals = np.arange(15) midpoint = 5 weights = triangular_weights_instance.triangular_weights( coord_vals, midpoint, width) self.assertIsInstance(weights, np.ndarray) self.assertEqual(len(weights), len(coord_vals)) # Using float32s for weights can give 1.0000001 here - good enough. self.assertAlmostEqual(weights.sum(), 1.0, places=6)