def test_error_degree(self): with np.testing.assert_raises(ValueError): interpolation = SplineInterpolation(7) f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=interpolation) f(1) with np.testing.assert_raises(ValueError): interpolation = SplineInterpolation(0) f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=interpolation) f(1)
def setUp(self): """Initialization of samples""" self.time = np.linspace(-1, 1, 50) interpolation = SplineInterpolation(3, monotone=True) self.polynomial = FDataGrid([self.time**3, self.time**5], self.time, interpolation=interpolation)
def test_evaluation_nodes(self): """Test interpolation in nodes for all dimensions""" for degree in range(1, 6): interpolation = SplineInterpolation(degree) f = FDataGrid(self.data_matrix_1_n, grid_points=np.arange(10), interpolation=interpolation) # Test interpolation in nodes np.testing.assert_array_almost_equal(f(np.arange(10)), self.data_matrix_1_n)
def test_evaluation_cubic_point(self): """Test the evaluation of a single point""" f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=SplineInterpolation(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.]]]))
def test_evaluation_cubic_simple(self): """Test basic usage of evaluation""" f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=SplineInterpolation(3)) # Test interpolation in nodes np.testing.assert_array_almost_equal(f(np.arange(10)).round(1)[..., 0], 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]]]))
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.interpolation = SplineInterpolation(interpolation_order=2)
def test_evaluation_cubic_composed(self): f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=SplineInterpolation(3)) # Evaluate (x**2, (9-x)**2) in (1,8) np.testing.assert_array_almost_equal( f([[1], [8]], aligned=False).round(3), np.array([[[1.]], [[1.]]])) t = np.linspace(4, 6, 4) np.testing.assert_array_almost_equal( f([t, 9 - t], aligned=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=False).round(3), np.array([[[16.], [36.]], [[16.], [36.]]]))
def test_evaluation_cubic_grid(self): """Test grid evaluation. With domain dimension = 1""" f = FDataGrid(self.data_matrix_1_1, grid_points=np.arange(10), interpolation=SplineInterpolation(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)
def test_evaluation_grid(self): """Test grid evaluation. With domain dimension = 1""" f = FDataGrid(self.data_matrix_1_n, grid_points=np.arange(10), interpolation=SplineInterpolation(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)
# differentiable at the points of discretization. # fig = fd.plot() fd.scatter(fig=fig) ############################################################################## # The interpolation method of the FDataGrid could be changed setting the # attribute ``interpolation``. Once we have set an interpolation it is used for # the evaluation of the object. # # Polynomial spline interpolation could be performed using the interpolation # :class:`~skfda.representation.interpolation.SplineInterpolation. In the # following example a cubic interpolation is set. fd.interpolation = SplineInterpolation(interpolation_order=3) fig = fd.plot() fd.scatter(fig=fig) ############################################################################## # Smooth interpolation could be performed with the attribute # ``smoothness_parameter`` of the spline interpolation. # # Sample with noise fd_smooth = skfda.datasets.make_sinusoidal_process(n_samples=1, n_features=30, random_state=1, error_std=.3)
# 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.interpolation = SplineInterpolation(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, dim_domain=2, dim_codomain=2) print(fd.dim_domain) print(fd.dim_codomain) fd.plot() ##############################################################################