Esempio n. 1
0
    def test_invalid_interpolate_parameter_value(self):
        """Test where a value to interpolate lies outside the spline points."""

        x_data1 = np.linspace(-5.0, 5.0, num=11)
        x_data2 = np.linspace(0.0, 10.0, num=11)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2])
        x_data_series = tf.stack(x_series, axis=0)
        y_data_series = tf.stack(y_series, axis=0)

        # num_test_values = 3
        search_args = tf.constant([[-5.2, 0.0, 5.3], [2.2, 1.8, 5.0]],
                                  dtype=tf.float64)
        x_test = tf.stack(search_args, axis=0)
        spline = cubic.build_spline(x_data_series, y_data_series)

        msg = ("Failed to catch that the test vector data lies outside of "
               "spline range")

        with self.assertRaises(tf.errors.InvalidArgumentError, msg=msg) as cm:
            self.evaluate(cubic.interpolate(x_test, spline,
                                            validate_args=True))
        print(cm.exception)
Esempio n. 2
0
    def test_error_calc(self):
        """Test that the deviation between the interpolated values and the actual values.

       This should be less than 0.02. This value was derived by running the
       same test with scipy cubic interpolation
    """
        sampling_points = 1000
        spline_x = np.linspace(0.0, 10.0, num=11, dtype=np.float64)
        spline_y = [1.0 / (1.0 + x * x) for x in spline_x]
        x_series = np.array([spline_x])
        y_series = np.array([spline_y])
        spline = cubic.build_spline(x_series, y_series)

        # There is an error if we go to 10.0
        test_range_x = np.linspace(0.0,
                                   9.99,
                                   num=sampling_points,
                                   dtype=np.float64)
        search_args = tf.constant(np.array([test_range_x]), dtype=tf.float64)
        projected_y = cubic.interpolate(search_args, spline)
        expected_y = tf.constant([[1.0 / (1.0 + x * x) for x in test_range_x]],
                                 dtype=tf.float64)
        errors = expected_y - projected_y
        deviation = self.evaluate(tfp.stats.stddev(errors[0], sample_axis=0))
        limit = 0.02
        self.assertLess(deviation, limit)
Esempio n. 3
0
    def test_validate_args_build(self):
        """Test that validation works as intended."""
        x_data = tf.constant([[1.0, 2.0, 2.0, 3.0, 4.0]], dtype=tf.float64)
        y_data = tf.constant([[1.0, 1.0, 1.0, 1.0, 1.0]], dtype=tf.float64)

        # this should not fail
        self.evaluate(
            cubic.build_spline(x_data, y_data, validate_args=False)[2])
Esempio n. 4
0
    def test_duplicate_x_points(self):
        """Test a spline where there are x_points of the same value."""
        x_data = tf.constant([[1.0, 2.0, 2.0, 3.0, 4.0]], dtype=tf.float64)
        y_data = tf.constant([[1.0, 1.0, 1.0, 1.0, 1.0]], dtype=tf.float64)

        msg = "Failed to detect duplicate x_data points"
        with self.assertRaises(tf.errors.InvalidArgumentError, msg=msg) as cm:
            self.evaluate(
                cubic.build_spline(x_data, y_data, validate_args=True)[2])
        print(cm.exception)
Esempio n. 5
0
    def test_invalid_spline_x_points(self):
        """Test a spline where the x_points are not strictly increasing."""
        x_data = tf.constant([[1.0, 2.0, 1.5, 3.0, 4.0]], dtype=tf.float64)
        y_data = tf.constant([[1.0, 1.0, 1.0, 1.0, 1.0]], dtype=tf.float64)

        msg = "Failed to detect invalid x_data sequence"
        with self.assertRaises(tf.errors.InvalidArgumentError, msg=msg) as cm:
            self.evaluate(
                cubic.build_spline(x_data, y_data, validate_args=True)[2])
        print(cm.exception)
Esempio n. 6
0
    def __init__(self,
                 x_data: types.RealTensor,
                 y_data: types.RealTensor,
                 z_data: types.RealTensor,
                 dtype: tf.DType = None,
                 name: str = None):
        """Initialize the 2d-interpolation object.

    Args:
      x_data: A `Tensor` of real `dtype` and shape
        `batch_shape + [num_x_data_points]`.
        Defines the x-coordinates of the input data. `num_x_data_points` should
        be >= 2. The elements of `x_data` should be in a non-decreasing order.
      y_data: A `Tensor` of the same `dtype` as `x_data` and shape
        `batch_shape + [num_x_data_points, num_y_data_points]`. Defines the
        y-coordinates of the input data. `num_y_data_points` should be >= 2.
        The elements of `y_data` should be in a non-decreasing order along last
        dimension.
      z_data: A `Tensor` of the same shape and `dtype` as `y_data`. Defines the
        z-coordinates of the input data (i.e., the function values).
      dtype: Optional dtype for the input `Tensor`s.
        Default value: `None` which maps to the default dtype inferred by
        TensorFlow.
      name: Python `str` name prefixed to ops created by this class.
        Default value: `None` which is mapped to the default name
        `interpolation_2d`.
    """

        name = name or "interpolation_2d"
        with tf.name_scope(name):
            self._xdata = tf.convert_to_tensor(x_data,
                                               dtype=dtype,
                                               name="x_data")
            self._dtype = dtype or self._xdata.dtype
            self._ydata = tf.convert_to_tensor(y_data,
                                               dtype=self._dtype,
                                               name="y_data")
            self._zdata = tf.convert_to_tensor(z_data,
                                               dtype=self._dtype,
                                               name="z_data")
            self._name = name

            # For each `x_data` point, build a spline in y-direction
            self._spline_yz = cubic.build_spline(self._ydata,
                                                 self._zdata,
                                                 name="spline_y_direction")
Esempio n. 7
0
    def test_dtype_conversion_float64_32(self):
        """Test specifying float64 data but requiring conversion to float32."""
        x_data1 = np.linspace(-5.0, 5.0, num=11, dtype=np.float64)
        x_data2 = np.linspace(0.0, 10.0, num=11, dtype=np.float64)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2], dtype=np.float64)
        search_args = tf.constant([[-1.2, 0.0, 0.3], [2.2, 1.8, 5.0]],
                                  dtype=tf.float64)

        spline2 = cubic.build_spline(x_series, y_series, dtype=tf.float32)
        msg = "Tensor conversion from float64 to float32 should fail here"
        with self.assertRaises(ValueError, msg=msg) as cm:
            self.evaluate(
                cubic.interpolate(search_args, spline2, dtype=tf.float32))
            print(cm)
Esempio n. 8
0
    def test_compare_shape_conformance_of_interpolate(self):
        """Test that the shape of the result of the interpolate method is correct.

    i.e.
    given
    x_points.shape (num_splines, spline_length)
    y_points.shape (num_splines, spline_length)
    x_test.shape (num_splines, num_test_values)

    then interpolate(x_test, spline ) -> shape(num_splines, num_test_values)
    """

        # num splines = 2
        # spline_length = 11
        x_data1 = np.linspace(-5.0, 5.0, num=11)
        x_data2 = np.linspace(0.0, 10.0, num=11)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2])

        # num_test_values = 3
        search_args = tf.constant([[-1.2, 0.0, 0.3], [2.2, 1.8, 5.0]],
                                  dtype=tf.float64)

        x_data_series = tf.stack(x_series, axis=0)
        y_data_series = tf.stack(y_series, axis=0)
        x_test = tf.stack(search_args, axis=0)

        spline = cubic.build_spline(x_data_series, y_data_series)
        predicted = cubic.interpolate(x_test, spline)

        self.assertAllEqual(tf.shape(x_test), tf.shape(predicted))

        # num_test_values = 13
        search_args11 = tf.constant(
            [[-1.2, 0.0, 0.3, 1.2, 2.1, 0.8, 0.0, 0.3, 1.2, 2.1, 0.8],
             [2.2, 1.8, 5.0, 2.2, 1.8, 5.0, 5.0, 2.2, 1.8, 5.0, 2.2]],
            dtype=tf.float64)

        x_test11 = tf.stack(search_args11, axis=0)
        predicted11 = cubic.interpolate(x_test11, spline)

        self.assertAllEqual(tf.shape(x_test11), tf.shape(predicted11))
Esempio n. 9
0
    def test_invalid_interpolate_parameter_shape(self):
        """Test shape(x_points)[0] != shape(test_x)[0]."""

        x_data1 = np.linspace(-5.0, 5.0, num=11)
        x_data2 = np.linspace(0.0, 10.0, num=11)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2])
        x_data_series = tf.stack(x_series, axis=0)
        y_data_series = tf.stack(y_series, axis=0)

        search_args = tf.constant([[-1.2, 0.0, 0.3]], dtype=tf.float64)
        x_test = tf.stack(search_args, axis=0)
        spline = cubic.build_spline(x_data_series, y_data_series)

        msg = "Failed to catch that the test vector has less rows than x_points"
        with self.assertRaises(ValueError, msg=msg):
            cubic.interpolate(x_test, spline)
Esempio n. 10
0
    def test_compare_spline_32(self):
        x_data1 = np.linspace(-5.0, 5.0, num=11, dtype=np.float32)
        x_data2 = np.linspace(0.0, 10.0, num=11, dtype=np.float32)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2], dtype=np.float32)
        search_args = tf.constant([[-1.2, 0.0, 0.3], [2.2, 1.8, 5.0]],
                                  dtype=tf.float32)

        spline2 = cubic.build_spline(x_series, y_series)
        result = tf.reshape(cubic.interpolate(search_args, spline2), [6])

        expected = tf.constant([
            0.401153371166, 1.0, 0.927547412565, 0.144129651521,
            0.194406085855, 0.037037037037
        ],
                               dtype=tf.float32)

        self.assertAllClose(expected, result)
Esempio n. 11
0
    def test_validate_args_interpolate(self):
        """Test that validation can be turned off in the interpolate call."""
        x_data1 = np.linspace(-5.0, 5.0, num=11)
        x_data2 = np.linspace(0.0, 10.0, num=11)
        x_series = np.array([x_data1, x_data2])
        y_data1 = [1.0 / (1.0 + x * x) for x in x_data1]
        y_data2 = [1.0 / (2.0 + x * x) for x in x_data2]
        y_series = np.array([y_data1, y_data2])
        x_data_series = tf.stack(x_series, axis=0)
        y_data_series = tf.stack(y_series, axis=0)

        # num_test_values = 3
        search_args = tf.constant([[-5.2, 0.0, 5.3], [2.2, 1.8, 5.0]],
                                  dtype=tf.float64)
        x_test = tf.stack(search_args, axis=0)
        spline = cubic.build_spline(x_data_series, y_data_series)

        # this should not fail with a validation error but a separate error
        # thrown by gather_nd
        msg = "The error should be an invalid argument"
        with self.assertRaises(tf.errors.InvalidArgumentError, msg=msg) as cm:
            self.evaluate(
                cubic.interpolate(x_test, spline, validate_args=False))
            print(cm.exception)