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)
Exemplo n.º 3
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_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)
 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_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_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_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)