示例#1
0
 def test_fails_weight_less_than_zero(self):
     """Test it fails if weight less than zero. """
     weights_in = np.array([-0.1, 1.1])
     missing_weights = np.ones(2)
     msg = ('Weights should be positive or at least one > 0.0')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
示例#2
0
 def test_fails_sum__not_equal_to_one(self):
     """Test it fails if sum of input weights not equal to one. """
     weights_in = np.array([3.0, 2.0, 1.0])
     missing_weights = np.ones(3)
     msg = ('Sum of weights must be 1.0')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
示例#3
0
 def test_fails_less_points_in_coord(self):
     """Test fails if less points in coord than in cube. """
     exp_coord_vals = self.exp_coord_vals[0]
     msg = ('The cube coordinate has more points ' 'than requested coord, ')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.process_coord(self.cube, self.coordinate,
                                        exp_coord_vals)
示例#4
0
 def test_fails_mismatch_array_sizes(self):
     """Test it fails if weights and missing_weights not the same size."""
     weights_in = np.array([0.7, 0.2, 0.1])
     missing_weights = np.ones(2)
     msg = ('Arrays weights and forecast_present not the same size')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
示例#5
0
 def test_fails_if_can_not_convert_units(self):
     """Test fails if it can not convert units """
     units = 'mm'
     exp_coord_vals = '402191.5, 402192.5, 402193.5'
     msg = ('Failed to convert coord units ')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.process_coord(self.cube, self.coordinate,
                                        exp_coord_vals, units)
示例#6
0
 def test_all_missing(self):
     """Test it raises the correct error when none of the expected
        coordinate values are present on the cube."""
     weights_in = np.array([0.6, 0.3, 0.1])
     missing_weights = np.zeros(3)
     msg = 'None of the expected forecasts were found.'
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
示例#7
0
 def test_basic(self):
     """Test process_cord returns num and array of missing_weights. """
     (result_num_of_weights,
      result_missing) = WeightsUtilities.process_coord(
          self.cube, self.coordinate, self.exp_coord_vals)
     self.assertIsInstance(result_num_of_weights, int)
     self.assertIsInstance(result_missing, np.ndarray)
示例#8
0
 def test_basic(self):
     """Test that the function returns an array of weights. """
     weights_in = np.array([0.6, 0.3, 0.1])
     missing_weights = np.ones(3)
     result = WeightsUtilities.redistribute_weights(weights_in,
                                                    missing_weights)
     self.assertIsInstance(result, np.ndarray)
示例#9
0
 def test_no_points_set_in_coord(self):
     """Test returns num in coordinate if no points set in coord. """
     expected_num = len(self.cube_coord.points)
     expected_array = np.ones(expected_num)
     (result_num_of_weights,
      result_missing) = WeightsUtilities.process_coord(
          self.cube, self.coordinate)
     self.assertAlmostEquals(result_num_of_weights, expected_num)
     self.assertArrayAlmostEqual(result_missing, expected_array)
示例#10
0
 def test_finds_missing_points(self):
     """Test correct values are returned for case where not all expected
        coordinate values are present in cube."""
     expected_num = 3
     expected_array = np.ones(expected_num)
     expected_array[0] = 0.0
     exp_coord_vals = '402191.5, 402192.5, 402193.5'
     (result_num_of_weights,
      result_missing) = WeightsUtilities.process_coord(
          self.cube, self.coordinate, exp_coord_vals)
     self.assertAlmostEquals(result_num_of_weights, expected_num)
     self.assertArrayAlmostEqual(result_missing, expected_array)
示例#11
0
 def test_returns_correct_values_evenly(self):
     """Test it returns the correct values, method is evenly."""
     weights_in = np.array([
         0.41957573, 0.25174544, 0.15104726, 0.09062836, 0.05437701,
         0.03262621
     ])
     missing_weights = np.ones(6)
     missing_weights[2] = 0.0
     result = WeightsUtilities.redistribute_weights(weights_in,
                                                    missing_weights)
     expected_result = np.array(
         [0.44978518, 0.28195489, 0.12083781, 0.08458647, 0.06283566])
     self.assertArrayAlmostEqual(result, expected_result)
示例#12
0
 def test_returns_correct_values_proportional(self):
     """Test it returns the correct values, method is proportional."""
     weights_in = np.array([
         0.41957573, 0.25174544, 0.15104726, 0.09062836, 0.05437701,
         0.03262621
     ])
     missing_weights = np.ones(6)
     missing_weights[2] = 0.0
     result = WeightsUtilities.redistribute_weights(weights_in,
                                                    missing_weights,
                                                    method='proportional')
     expected_result = np.array(
         [0.49422742, 0.29653645, 0.10675312, 0.06405187, 0.03843112])
     self.assertArrayAlmostEqual(result, expected_result)
示例#13
0
 def test_fails_weight_less_than_zero(self):
     """Test it fails if weight less than zero. """
     weights_in = np.array([-1.0, 0.1])
     msg = ('Weights must be positive')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.normalise_weights(weights_in)
示例#14
0
 def test_fails_coord_not_in_cube(self):
     """ Test process_cord fails if coord not in cube """
     msg = ('The coord for this plugin must be '
            'an existing coordinate in the input cube.')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.process_coord(self.cube, 'not_in_cube', '0')
示例#15
0
 def test_array_sum_equals_one(self):
     """Test that the resulting weights add up to one. """
     weights_in = np.array([1.0, 2.0, 3.0])
     result = WeightsUtilities.normalise_weights(weights_in)
     self.assertAlmostEquals(result.sum(), 1.0)
示例#16
0
 def test_fails_sum_equals_zero(self):
     """Test it fails if sum of input weights is zero. """
     weights_in = np.array([0.0, 0.0, 0.0])
     msg = ('Sum of weights must be > 0.0')
     with self.assertRaisesRegexp(ValueError, msg):
         WeightsUtilities.normalise_weights(weights_in)
示例#17
0
 def test_returns_correct_values(self):
     """Test it returns the correct values. """
     weights_in = np.array([6.0, 3.0, 1.0])
     result = WeightsUtilities.normalise_weights(weights_in)
     expected_result = np.array([0.6, 0.3, 0.1])
     self.assertArrayAlmostEqual(result, expected_result)
示例#18
0
 def test_basic(self):
     """Test that the __repr__ returns the expected string."""
     result = str(WeightsUtilities())
     msg = '<WeightsUtilities>'
     self.assertEqual(result, msg)
示例#19
0
 def test_basic(self):
     """Test that the function returns an array of weights. """
     weights_in = np.array([1.0, 2.0, 3.0])
     result = WeightsUtilities.normalise_weights(weights_in)
     self.assertIsInstance(result, np.ndarray)