Пример #1
0
 def test_same_units(self):
     """Test plugin produces the correct weights when the parameters for
        the triangle are in the same units as the input cube's coordinate"""
     WeightsClass = ChooseDefaultWeightsTriangular(2, 1, units=self.units)
     weights = WeightsClass.process(self.cube, self.coord_name)
     expected_weights = np.array([0.33333333, 0.66666667])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #2
0
    def test_unconvertable_units(self):
        """"Test plugin produces the correct weights when the parameters for
            the triangle cannot be converted to the same units as the
            coordinate"""
        WeightsClass = ChooseDefaultWeightsTriangular(7200, 3600, units="m")

        message = r"Unable to convert from 'Unit\('m'\)' to 'Unit\('hours'\)'"
        with self.assertRaisesRegexp(ValueError, message):
            WeightsClass.process(self.cube, self.coord_name)
Пример #3
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"""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(3, 5)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     self.assertIsInstance(weights, np.ndarray)
     self.assertEqual(len(weights), len(coord_vals))
     self.assertEqual(weights.sum(), 1.0)
Пример #4
0
 def test_different_units(self):
     """"Test plugin produces the correct weights when the parameters for
         the triangle are in different units to the input cube's
         coordinate"""
     WeightsClass = ChooseDefaultWeightsTriangular(7200,
                                                   3600,
                                                   units="seconds")
     weights = WeightsClass.process(self.cube, self.coord_name)
     expected_weights = np.array([0.33333333, 0.66666667])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #5
0
 def test_basic_weights(self):
     """Test that the function returns the correct triangular weights in a
        simple case"""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(3, 5)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     expected_weights = np.array([
         0., 0., 0., 0.11111111, 0.22222222, 0.33333333, 0.22222222,
         0.11111111, 0., 0., 0., 0., 0., 0., 0.
     ])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #6
0
 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."""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(3.5, 5)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     expected_weights = np.array([
         0., 0., 0.04, 0.12, 0.2, 0.28, 0.2, 0.12, 0.04, 0., 0., 0., 0., 0.,
         0.
     ])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #7
0
 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"""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(3, 1)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     expected_weights = np.array([
         0.25, 0.375, 0.25, 0.125, 0., 0., 0., 0., 0., 0., 0., 0., 0., 0.,
         0.
     ])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #8
0
 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."""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(2, 3.5)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     expected_weights = np.array([
         0., 0., 0.125, 0.375, 0.375, 0.125, 0., 0., 0., 0., 0., 0., 0., 0.,
         0.
     ])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #9
0
 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."""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(10, 5)
     coord_vals = np.arange(15)
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     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)
Пример #10
0
 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."""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(5, 8)
     coord_vals = np.arange(10)
     coord_vals = np.append(coord_vals, [12, 15, 18, 21, 24])
     weights = TriangularWeightsClass.triangular_weights(coord_vals)
     expected_weights = np.array([
         0., 0., 0., 0., 0.05, 0.1, 0.15, 0.2, 0.25, 0.2, 0.05, 0., 0., 0.,
         0.
     ])
     self.assertArrayAlmostEqual(weights, expected_weights)
Пример #11
0
 def test_cf_unit_input(self):
     """Test the case where an instance of cf_units.Unit is passed in"""
     units = cf_units.Unit("hour")
     WeightsClass = ChooseDefaultWeightsTriangular(5, 8, units=units)
     expected_width = 5
     expected_midpoint = 8
     expected_unit = units
     self.assertEqual(WeightsClass.width, expected_width)
     self.assertEqual(WeightsClass.midpoint, expected_midpoint)
     self.assertEqual(WeightsClass.parameters_units, expected_unit)
Пример #12
0
 def test_string_input(self):
     """Test the case where a string is passed and gets converted to a
        cf_units.Unit instance"""
     units = "hour"
     WeightsClass = ChooseDefaultWeightsTriangular(5, 8, units=units)
     expected_width = 5
     expected_midpoint = 8
     expected_unit = cf_units.Unit("hour")
     self.assertEqual(WeightsClass.width, expected_width)
     self.assertEqual(WeightsClass.midpoint, expected_midpoint)
     self.assertEqual(WeightsClass.parameters_units, expected_unit)
Пример #13
0
 def test_basic(self):
     """Test the repr function formats the arguments correctly"""
     TriangularWeightsClass = ChooseDefaultWeightsTriangular(3, 5)
     result = str(TriangularWeightsClass)
     expected = "<ChooseDefaultTriangularWeights width= 3.0, midpoint= 5.0>"
     self.assertEqual(result, expected)