Ejemplo n.º 1
0
    def test_error_degree(self):

        with np.testing.assert_raises(ValueError):
            interpolator = SplineInterpolator(7)
            f = FDataGrid(self.data_matrix_1_1,
                          sample_points=np.arange(10),
                          interpolator=interpolator)
            f(1)

        with np.testing.assert_raises(ValueError):
            interpolator = SplineInterpolator(0)
            f = FDataGrid(self.data_matrix_1_1,
                          sample_points=np.arange(10),
                          interpolator=interpolator)
            f(1)
Ejemplo n.º 2
0
    def setUp(self):
        """Initialization of samples"""

        self.time = np.linspace(-1, 1, 50)
        interpolator = SplineInterpolator(3, monotone=True)
        self.polynomial = FDataGrid([self.time**3, self.time**5],
                                    self.time,
                                    interpolator=interpolator)
Ejemplo n.º 3
0
    def test_evaluation_cubic_derivative(self):
        """Test derivative"""
        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(3))

        # Derivate = [2*x, 2*(9-x)]
        np.testing.assert_array_almost_equal(
            f([0.5, 1.5, 2.5], derivative=1).round(3),
            np.array([[1., 3., 5.], [-17., -15., -13.]]))
Ejemplo n.º 4
0
    def test_evaluation_nodes(self):
        """Test interpolation in nodes for all dimensions"""

        for degree in range(1, 6):
            interpolator = SplineInterpolator(degree)

            f = FDataGrid(self.data_matrix_1_n,
                          sample_points=np.arange(10),
                          interpolator=interpolator)

            # Test interpolation in nodes
            np.testing.assert_array_almost_equal(f(np.arange(10)),
                                                 self.data_matrix_1_n)
Ejemplo n.º 5
0
    def test_evaluation_cubic_point(self):
        """Test the evaluation of a single point"""

        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(3))

        # Test a single point
        np.testing.assert_array_almost_equal(
            f(5.3).round(3), np.array([[28.09], [13.69]]))

        np.testing.assert_array_almost_equal(
            f([3]).round(3), np.array([[9.], [36.]]))
        np.testing.assert_array_almost_equal(
            f((2, )).round(3), np.array([[4.], [49.]]))
Ejemplo n.º 6
0
    def test_evaluation_cubic_simple(self):
        """Test basic usage of evaluation"""

        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(3))

        # Test interpolation in nodes
        np.testing.assert_array_almost_equal(
            f(np.arange(10)).round(1), self.data_matrix_1_1)

        # Test evaluation in a list of times
        np.testing.assert_array_almost_equal(
            f([0.5, 1.5, 2.5]).round(2),
            np.array([[0.25, 2.25, 6.25], [72.25, 56.25, 42.25]]))
Ejemplo n.º 7
0
    def setUp(self):
        # Data matrix of a datagrid with a dimension of domain and image equal
        # to 1.

        # Matrix of functions (x**2, (9-x)**2)

        self.t = np.arange(10)

        data_1 = np.array(
            [np.arange(10)**2,
             np.arange(start=9, stop=-1, step=-1)**2])
        data_2 = np.sin(np.pi / 81 * data_1)

        self.data_matrix_1_n = np.dstack((data_1, data_2))

        self.interpolator = SplineInterpolator(interpolation_order=2)
Ejemplo n.º 8
0
    def test_evaluation_grid(self):
        """Test grid evaluation. With domain dimension = 1"""

        f = FDataGrid(self.data_matrix_1_n,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(2))

        t = [1.5, 2.5, 3.5]
        res = np.array([[[2.25, 0.08721158], [6.25, 0.24020233],
                         [12.25, 0.4577302]],
                        [[56.25, 0.81614206], [42.25, 0.99758925],
                         [30.25, 0.92214607]]])

        # Test evaluation in a list of times
        np.testing.assert_array_almost_equal(f(t, grid=True), res)
        np.testing.assert_array_almost_equal(f((t, ), grid=True), res)
        np.testing.assert_array_almost_equal(f([t], grid=True), res)

        # Check erroneous axis
        with np.testing.assert_raises(ValueError):
            f((t, t), grid=True)
Ejemplo n.º 9
0
    def test_evaluation_cubic_composed(self):

        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(3))

        # Evaluate (x**2, (9-x)**2) in (1,8)
        np.testing.assert_array_almost_equal(
            f([[1], [8]], aligned_evaluation=False).round(3),
            np.array([[1.], [1.]]))

        t = np.linspace(4, 6, 4)
        np.testing.assert_array_almost_equal(
            f([t, 9 - t], aligned_evaluation=False).round(2),
            np.array([[16., 21.78, 28.44, 36.], [16., 21.78, 28.44, 36.]]))

        # Same length than nsample
        t = np.linspace(4, 6, 2)
        np.testing.assert_array_almost_equal(
            f([t, 9 - t], aligned_evaluation=False).round(3),
            np.array([[16., 36.], [16., 36.]]))
Ejemplo n.º 10
0
    def test_evaluation_cubic_grid(self):
        """Test grid evaluation. With domain dimension = 1"""

        f = FDataGrid(self.data_matrix_1_1,
                      sample_points=np.arange(10),
                      interpolator=SplineInterpolator(3))

        t = [0.5, 1.5, 2.5]
        res = np.array([[0.25, 2.25, 6.25], [72.25, 56.25, 42.25]])

        # Test evaluation in a list of times
        np.testing.assert_array_almost_equal(f(t, grid=True).round(3), res)
        np.testing.assert_array_almost_equal(f((t, ), grid=True).round(3), res)
        np.testing.assert_array_almost_equal(f([t], grid=True).round(3), res)
        # Single point with grid
        np.testing.assert_array_almost_equal(f(3, grid=True),
                                             np.array([[9.], [36.]]))

        # Check erroneous axis
        with np.testing.assert_raises(ValueError):
            f((t, t), grid=True)
Ejemplo n.º 11
0
import skfda
import numpy as np
import matplotlib.pyplot as plt
from skfda.datasets import fetch_growth
from skfda.representation.interpolation import SplineInterpolator
from skfda.preprocessing.registration import elastic_registration_warping, elastic_registration

data = fetch_growth()

fd = data['data']

fd = fd[data['target'] == 0]

fd = fd.derivative()
fd.interpolator = SplineInterpolator(3)
#fd = fd.to_grid(np.linspace(*fd.domain_range[0], 150))
fd.dataset_label = None
fd.axes_labels = [
    "age (year)", r"$\partial \, height \, / \, \partial \, age$ (cm/year)"
]

plt.figure("berkeley-males")
fd.plot()

plt.xlim(fd.domain_range[0])
a, b = plt.ylim()
plt.ylim(a, 20)

plt.tight_layout()

plt.figure("berkeley-warping")
Ejemplo n.º 12
0
import skfda
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

from skfda.datasets import make_sinusoidal_process
from skfda.representation.interpolation import SplineInterpolator

fd = make_sinusoidal_process(n_samples=1, n_features=6, random_state=3) + 1.2
fd.interpolator = SplineInterpolator(interpolation_order=3)

plt.figure("original-function")
fd.scatter(label='original values')
fd.plot(label='interpolation', color='C0', linestyle="--")
plt.legend()

x = fd.sample_points[0]
y = fd.data_matrix.squeeze()

ymin, ymax = plt.ylim()

ymin = 0

for i in range(len(x)):
    plt.plot([x[i], x[i]], [-1.2, y[i]], color='C0', linewidth=1)

plt.ylim(ymin, ymax)
plt.yticks([])

plt.tight_layout()
plt.figure("function-resampled")
Ejemplo n.º 13
0
# at the points of discretization.
#

fd.plot()
fd.scatter()

################################################################################
# The interpolation method of the FDataGrid could be changed setting the
# attribute `interpolator`. Once we have set an interpolator it is used for
# the evaluation of the object.
#
# Polynomial spline interpolation could be performed using the interpolator
# :class:`SplineInterpolator`. In the following example a cubic interpolator
# is set.

fd.interpolator = SplineInterpolator(interpolation_order=3)

fd.plot()
fd.scatter()

###############################################################################
# Smooth interpolation could be performed with the attribute
# `smoothness_parameter` of the spline interpolator.
#

# Sample with noise
fd_smooth = skfda.datasets.make_sinusoidal_process(n_samples=1,
                                                   n_features=30,
                                                   random_state=1,
                                                   error_std=.3)
Ejemplo n.º 14
0
# In this representation, the data can be arranged as a matrix.
print(fd.data_matrix)

###############################################################################
# By default, the data points are interpolated using a linear interpolation,
# but this is configurable.
dataset = skfda.datasets.fetch_medflies()
fd = dataset['data']

first_curve = fd[0]
first_curve.plot()

###############################################################################
# The interpolation used can however be changed. Here, we will use an
# interpolation with degree 3 splines.
first_curve.interpolator = SplineInterpolator(3)
first_curve.plot()

###############################################################################
# This representation allows also functions with arbitrary dimensions of the
# domain and codomain.
fd = skfda.datasets.make_multimodal_samples(n_samples=1, ndim_domain=2,
                                            ndim_image=2)

print(fd.ndim_domain)
print(fd.ndim_codomain)

fd.plot()

###############################################################################
# Another possible representation is a decomposition in a basis of functions.
Ejemplo n.º 15
0
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import axes3d

from skfda.datasets import make_sinusoidal_process
from skfda.representation.interpolation import SplineInterpolator

X, Y, Z = axes3d.get_test_data(1.2)
data_matrix = [Z.T]
sample_points = [X[0, :], Y[:, 0]]

fd = skfda.FDataGrid(data_matrix, sample_points)

plt.figure("surface-bilinear")
fig, ax = fd.plot(alpha=.9)
fd.scatter(ax=ax, color="maroon")

plt.tight_layout()

plt.figure("surface-bicubic")
fd.interpolator = SplineInterpolator(interpolation_order=3)

fig, ax = fd.plot(alpha=.9)
fd.scatter(ax=ax, color="maroon")
plt.tight_layout()

plt.figure("surface-bicubic-dx")
fig, ax = fd.plot(derivative=(1, 0), alpha=.9)
plt.tight_layout()

plt.show()