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.assertRaisesRegex(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
 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.assertRaisesRegex(ValueError, msg):
         WeightsUtilities.process_coord(self.cube, self.coordinate,
                                        exp_coord_vals)
 def test_fails_if_can_not_convert_units(self):
     """Test fails if it can not convert units """
     units = 'mm'
     msg = ('Failed to convert coord units ')
     with self.assertRaisesRegex(ValueError, msg):
         WeightsUtilities.process_coord(self.cube, self.coordinate,
                                        self.wrong_coord_vals, units)
 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.assertRaisesRegex(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
 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.assertRaisesRegex(ValueError, msg):
         WeightsUtilities.redistribute_weights(weights_in, missing_weights)
 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)
 def test_basic(self):
     """Test process_coord 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)
 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)
 def test_returns_correct_values_2darray_zero_weights(self):
     """Test normalizing along the columns of the array when there are
        zeros in the input array."""
     weights_in = np.array([[6.0, 3.0, 0.0], [0.0, 1.0, 3.0]])
     result = WeightsUtilities.normalise_weights(weights_in, axis=0)
     expected_result = np.array([[1.0, 0.75, 0.0], [0.0, 0.25, 1.0]])
     self.assertArrayAlmostEqual(result, expected_result)
 def test_returns_correct_values_2darray_axis1(self):
     """Test normalizing along the rows of the array."""
     weights_in = np.array([[6.0, 3.0, 1.0],
                            [4.0, 1.0, 3.0]])
     result = WeightsUtilities.normalise_weights(weights_in, axis=1)
     expected_result = np.array([[0.6, 0.3, 0.1],
                                 [0.5, 0.125, 0.375]])
     self.assertArrayAlmostEqual(result, expected_result)
 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)
 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
     (result_num_of_weights,
      result_missing) = WeightsUtilities.process_coord(
          self.cube, self.coordinate, self.wrong_coord_vals)
     self.assertAlmostEqual(result_num_of_weights, expected_num)
     self.assertArrayAlmostEqual(result_missing, expected_array)
 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)
 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)
Exemple #15
0
    def renormalize_weights(self, nbhood_cube):
        """
        Renormalize the weights taking into account where there are NaNs in the
        result from neighbourhood.

        The weights corresponding to NaNs in the result from neighbourhooding
        with a mask are set to zero and then the weights are renormalized along
        the axis corresponding to the coordinate we want to collapse.

        Args:
            nbhood_cube (iris.cube.Cube):
                The cube that has been through masked neighbourhood processing
                and has the dimension we wish to collapse. Must have the same
                dimensions of the cube.
        """
        # If the weights are masked we want to retain the mask.
        condition = np.isnan(nbhood_cube.data)
        if ma.is_masked(self.weights.data):
            condition = condition & ~self.weights.data.mask

        self.weights.data[condition] = 0.0
        axis = nbhood_cube.coord_dims(self.coord_masked)
        self.weights.data = WeightsUtilities.normalise_weights(
            self.weights.data, axis=axis)
 def test_basic(self):
     """Test that the __repr__ returns the expected string."""
     result = str(WeightsUtilities())
     msg = '<WeightsUtilities>'
     self.assertEqual(result, msg)
 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)
 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)
 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')
 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)
 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)
 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)