def test_error_unmatched_weights(self): """Test error when weights shape doesn't match length of blend dimension (in this case 3 weights for 2 blend slices)""" weights = generate_matching_weights_array([0.7, 0.1, 0.2], 1) percentiles = np.array([0, 50, 100]) perc_data = np.array([[1.0, 2.0], [5.0, 5.0], [10.0, 9.0]]) with self.assertRaisesRegex(ValueError, "Weights shape does not match data"): PercentileBlendingAggregator.aggregate(perc_data, 1, percentiles, weights)
def test_blend_percentiles(self): """Test blend_percentile function works""" weights = np.array([0.38872692, 0.33041788, 0.2808552]) percentiles = np.array( [0.0, 10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0, 100.0] ) result = PercentileBlendingAggregator.blend_percentiles( PERCENTILE_VALUES, percentiles, weights ) expected_result_array = np.array( [ 12.70237152, 16.65161847, 17.97408712, 18.86356829, 19.84089805, 20.77406153, 21.39078426, 21.73778353, 22.22440125, 23.53863876, 24.64542338, ] ) self.assertArrayAlmostEqual(result, expected_result_array)
def test_blend_percentile_aggregate(self): """Test blend_percentile_aggregate function works""" weights = np.array([0.8, 0.2]) percentiles = np.array([0, 20, 40, 60, 80, 100]) result = PercentileBlendingAggregator.aggregate( np.reshape(PERCENTILE_DATA, (6, 2, 2, 2)), 1, percentiles, weights, 0) expected_result_array = np.reshape(BLENDED_PERCENTILE_DATA2, (6, 2, 2)) self.assertArrayAlmostEqual(result, expected_result_array)
def test_only_one_point_to_blend(self): """Test case where there is only one point in the coordinate we are blending over.""" weights = np.array([1.0]) percentiles = np.array([20.0, 50.0, 80.0]) percentile_values = np.array([[5.0, 6.0, 7.0]]) result = PercentileBlendingAggregator.blend_percentiles( percentile_values, percentiles, weights) expected_result = np.array([5.0, 6.0, 7.0]) self.assertArrayAlmostEqual(result, expected_result)
def test_three_percentiles_symmetric_case(self): """Test that when three percentiles are provided the correct values are returned, not a simple average""" weights = np.array([0.5, 0.5]) percentiles = np.array([20.0, 50.0, 80.0]) percentile_values = np.array([[5.0, 6.0, 7.0], [5.0, 6.5, 7.0]]) result = PercentileBlendingAggregator.blend_percentiles( percentile_values, percentiles, weights) expected_result = np.array([5.0, 6.2, 7.0]) self.assertArrayAlmostEqual(result, expected_result)
def test_two_percentiles(self): """Test that when two percentiles are provided, the extreme values in the set of thresholds we are blending are returned""" weights = np.array([0.5, 0.5]) percentiles = np.array([30., 60.]) percentile_values = np.array([[5.0, 8.0], [6.0, 7.0]]) result = PercentileBlendingAggregator.blend_percentiles( percentile_values, percentiles, weights) expected_result = np.array([5.0, 8.0]) self.assertArrayAlmostEqual(result, expected_result)
def test_3D_simple_case(self): """ Test that for a simple case with only one point and an extra internal dimension behaves as expected""" weights = np.array([0.5, 0.5]) percentiles = np.array([0, 50, 100]) perc_data = np.array([[[1.0], [2.0]], [[5.0], [6.0]], [[10.0], [9.0]]]) result = PercentileBlendingAggregator.aggregate( perc_data, 1, percentiles, weights, 0) expected_result = np.array([[1.0], [5.555555], [10.0]]) self.assertArrayAlmostEqual(result, expected_result)
def test_2D_simple_case(self): """ Test that for a simple case with only one point in the resulting array the function behaves as expected""" weights = np.array([0.8, 0.2]) percentiles = np.array([0, 50, 100]) perc_data = np.array([[1.0, 2.0], [5.0, 5.0], [10.0, 9.0]]) result = PercentileBlendingAggregator.aggregate( perc_data, 1, percentiles, weights, 0) expected_result = np.array([1.0, 5.0, 10.0]) self.assertArrayAlmostEqual(result, expected_result)
def test_blend_percentile_aggregate(self): """Test blend_percentile_aggregate function works""" weights = generate_matching_weights_array([0.6, 0.3, 0.1], 4) percentiles = np.array([0, 20, 40, 60, 80, 100]).astype(np.float32) result = PercentileBlendingAggregator.aggregate( PERCENTILE_DATA, 1, percentiles, weights) self.assertArrayAlmostEqual( result, BLENDED_PERCENTILE_DATA, )
def test_blend_percentile_aggregate_reorder2(self): """Test blend_percentile_aggregate works with out of order dims 2""" weights = np.array([0.8, 0.2]) percentiles = np.array([0, 20, 40, 60, 80, 100]) perc_data = np.reshape(PERCENTILE_DATA, (6, 2, 2, 2)) perc_data = np.moveaxis(perc_data, [0, 1], [1, 2]) result = PercentileBlendingAggregator.aggregate( perc_data, 2, percentiles, weights, 1) expected_result_array = np.reshape(BLENDED_PERCENTILE_DATA2, (6, 2, 2)) expected_result_array = np.moveaxis(expected_result_array, 0, 1) self.assertArrayAlmostEqual(result, expected_result_array)
def test_blend_percentile_aggregate(self): """Test blend_percentile_aggregate function works""" weights = np.array([0.6, 0.3, 0.1]) weights = generate_matching_weights_array(weights, (4, 6, 3)) weights = np.moveaxis(weights, (0, 1, 2), (2, 1, 0)) percentiles = np.array([0, 20, 40, 60, 80, 100]).astype(np.float32) result = PercentileBlendingAggregator.aggregate( np.reshape(PERCENTILE_DATA, (6, 3, 2, 2)), 1, percentiles, weights, 0 ) self.assertArrayAlmostEqual( result, BLENDED_PERCENTILE_DATA, )
def test_4D_simple_case(self): """ Test that for a simple case with only one point and 4D input data it behaves as expected""" weights = np.array([0.5, 0.5]) percentiles = np.array([0, 50, 100]) perc_data = np.array([1.0, 3.0, 2.0, 4.0, 5.0, 6.0]) input_shape = (3, 2, 1, 1) perc_data = perc_data.reshape(input_shape) result = PercentileBlendingAggregator.aggregate( perc_data, 1, percentiles, weights, 0) expected_result = np.array([[[1.0]], [[3.5]], [[6.0]]]) expected_result_shape = (3, 1, 1) self.assertArrayAlmostEqual(result, expected_result) self.assertEqual(result.shape, expected_result_shape)
def test_blend_percentile_aggregate_reorder2(self): """Test blend_percentile_aggregate works with out of order dims 2""" weights = np.array([0.6, 0.3, 0.1]) weights = generate_matching_weights_array(weights, (4, 6, 3)) weights = np.moveaxis(weights, (0, 1, 2), (2, 1, 0)) percentiles = np.array([0, 20, 40, 60, 80, 100]) perc_data = np.reshape(PERCENTILE_DATA, (6, 3, 2, 2)) perc_data = np.moveaxis(perc_data, [0, 1], [1, 2]) result = PercentileBlendingAggregator.aggregate( perc_data, 2, percentiles, weights, 1) expected_result_array = BLENDED_PERCENTILE_DATA expected_result_array = np.moveaxis(expected_result_array, 0, 1) self.assertArrayAlmostEqual(result, expected_result_array)
def test_basic(self): """Test that the __repr__ returns the expected string.""" result = str(PercentileBlendingAggregator()) msg = '<PercentileBlendingAggregator>' self.assertEqual(result, msg)