示例#1
0
    def test_regression_mixed(self):

        multivariate = np.array([[0, 0], [2, 7], [1, 7], [3, 9], [4, 16],
                                 [2, 14], [3, 5]])

        X = [
            multivariate,
            FDataBasis(Monomial(n_basis=3),
                       [[1, 0, 0], [0, 1, 0], [0, 0, 1], [1, 0, 1], [1, 0, 0],
                        [0, 1, 0], [0, 0, 1]])
        ]

        # y = 2 + sum([3, 1] * array) + int(3 * function)
        intercept = 2
        coefs_multivariate = np.array([3, 1])
        coefs_functions = FDataBasis(Monomial(n_basis=3), [[3, 0, 0]])
        y_integral = np.array([3, 3 / 2, 1, 4, 3, 3 / 2, 1])
        y_sum = multivariate @ coefs_multivariate
        y = 2 + y_sum + y_integral

        scalar = LinearRegression()
        scalar.fit(X, y)

        np.testing.assert_allclose(scalar.intercept_, intercept, atol=0.01)

        np.testing.assert_allclose(scalar.coef_[0],
                                   coefs_multivariate,
                                   atol=0.01)

        np.testing.assert_allclose(scalar.coef_[1].coefficients,
                                   coefs_functions.coefficients,
                                   atol=0.01)

        y_pred = scalar.predict(X)
        np.testing.assert_allclose(y_pred, y, atol=0.01)
示例#2
0
    def test_evaluation_composed_keepdims_bspline(self):
        """Test behaviour of keepdims with composed evaluation"""
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        f_keepdims = FDataBasis(bspline, coefficients, keepdims=True)

        t = [[0, 0.5, 0.6], [0.2, 0.7, 0.1]]

        res = np.array([[0.001, 0.57, 0.506], [0.524, 0.399, 0.359]])

        res_keepdims = res.reshape((2, 3, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False, keepdims=True).round(3),
            res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False).round(3), res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=False).round(3),
            res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=True).round(3),
            res_keepdims)
示例#3
0
    def test_regression_single_explanatory(self):

        x_basis = Monomial(n_basis=7)
        x_fd = FDataBasis(x_basis, np.identity(7))

        beta_basis = Fourier(n_basis=5)
        beta_fd = FDataBasis(beta_basis, [1, 1, 1, 1, 1])
        y = [
            0.9999999999999993, 0.162381381441085, 0.08527083481359901,
            0.08519946930844623, 0.09532291032042489, 0.10550022969639987,
            0.11382675064746171
        ]

        scalar = LinearRegression(coef_basis=[beta_basis])
        scalar.fit(x_fd, y)
        np.testing.assert_allclose(scalar.coef_[0].coefficients,
                                   beta_fd.coefficients)
        np.testing.assert_allclose(scalar.intercept_, 0.0, atol=1e-6)

        y_pred = scalar.predict(x_fd)
        np.testing.assert_allclose(y_pred, y)

        scalar = LinearRegression(coef_basis=[beta_basis], fit_intercept=False)
        scalar.fit(x_fd, y)
        np.testing.assert_allclose(scalar.coef_[0].coefficients,
                                   beta_fd.coefficients)
        np.testing.assert_equal(scalar.intercept_, 0.0)

        y_pred = scalar.predict(x_fd)
        np.testing.assert_allclose(y_pred, y)
示例#4
0
    def test_init_integer(self):
        """Tests initializations which only specify the order."""

        # Checks for a zero order Lfd object
        lfd_0 = LinearDifferentialOperator(order=0)
        weightfd = [FDataBasis(Constant(domain_range=(0, 1)), 1)]

        self._assert_equal_weights(
            lfd_0.weights, weightfd,
            "Wrong list of weight functions of the linear operator")

        # Checks for a non zero order Lfd object
        lfd_3 = LinearDifferentialOperator(3)
        consfd = FDataBasis(
            Constant(domain_range=(0, 1)),
            [[0], [0], [0], [1]],
        )
        bwtlist3 = list(consfd)

        self._assert_equal_weights(
            lfd_3.weights, bwtlist3,
            "Wrong list of weight functions of the linear operator")

        # Negative order must fail
        with np.testing.assert_raises(ValueError):
            LinearDifferentialOperator(-1)
示例#5
0
    def test_evaluation_keepdims_monomial(self):
        """Test behaviour of keepdims """
        monomial = Monomial(domain_range=(0, 1), nbasis=3)

        coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]]

        f = FDataBasis(monomial, coefficients)
        f_keepdims = FDataBasis(monomial, coefficients, keepdims=True)

        np.testing.assert_equal(f.keepdims, False)
        np.testing.assert_equal(f_keepdims.keepdims, True)

        t = np.linspace(0, 1, 4)

        res = np.array([[1., 2., 3.667, 6.],
                        [0.5, 1.111, 2.011, 3.2]])

        res_keepdims = res.reshape((2, 4, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(f(t).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(f(t, keepdims=True).round(3),
                                             res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t).round(3), res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=True).round(3), res_keepdims)
示例#6
0
    def test_evaluation_composed_keepdims_monomial(self):
        """Test behaviour of keepdims with composed evaluation"""
        monomial = Monomial(domain_range=(0, 1), nbasis=3)

        coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]]

        f = FDataBasis(monomial, coefficients)
        f_keepdims = FDataBasis(monomial, coefficients, keepdims=True)

        t = [[0, 0.5, 0.6], [0.2, 0.7, 0.1]]

        res = np.array([[1., 2.75, 3.28],
                        [0.832, 2.117, 0.653]])

        res_keepdims = res.reshape((2, 3, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(
            f(t, aligned_evaluation=False).round(3), res)
        np.testing.assert_array_almost_equal(f(t, aligned_evaluation=False,
                                               keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(f(t, aligned_evaluation=False,
                                               keepdims=True).round(3),
                                             res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False).round(3),
            res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=False).round(3),
            res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, aligned_evaluation=False, keepdims=True).round(3),
            res_keepdims)
示例#7
0
    def test_evaluation_keepdims_bspline(self):
        """Test behaviour of keepdims """
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        f_keepdims = FDataBasis(bspline, coefficients, keepdims=True)

        np.testing.assert_equal(f.keepdims, False)
        np.testing.assert_equal(f_keepdims.keepdims, True)

        t = np.linspace(0, 1, 4)

        res = np.array([[0.001, 0.564, 0.435, 0.33],
                        [0.018, 0.468, 0.371, 0.12]])

        res_keepdims = res.reshape((2, 4, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(f(t).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f(t, keepdims=True).round(3), res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(
            f_keepdims(t).round(3), res_keepdims)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=False).round(3), res)
        np.testing.assert_array_almost_equal(
            f_keepdims(t, keepdims=True).round(3), res_keepdims)
示例#8
0
    def test_fdatabasis_derivative_bspline(self):
        bspline = FDataBasis(BSpline(n_basis=8), [1, 5, 8, 9, 7, 8, 4, 5])
        bspline2 = FDataBasis(
            BSpline(n_basis=5),
            [[4, 9, 7, 4, 3], [1, 7, 9, 8, 5], [4, 6, 6, 6, 8]])

        bs0 = bspline.derivative(order=0)
        bs1 = bspline.derivative()
        bs2 = bspline.derivative(order=2)
        np.testing.assert_equal(bs1.basis, BSpline(n_basis=7, order=3))
        np.testing.assert_almost_equal(
            bs1.coefficients, np.atleast_2d([60, 22.5, 5, -10, 5, -30, 15]))
        np.testing.assert_equal(bs0, bspline)
        np.testing.assert_equal(bs2.basis, BSpline(n_basis=6, order=2))
        np.testing.assert_almost_equal(
            bs2.coefficients, np.atleast_2d([-375, -87.5, -75, 75, -175, 450]))

        bs0 = bspline2.derivative(order=0)
        bs1 = bspline2.derivative()
        bs2 = bspline2.derivative(order=2)

        np.testing.assert_equal(bs1.basis, BSpline(n_basis=4, order=3))
        np.testing.assert_almost_equal(
            bs1.coefficients,
            [[30, -6, -9, -6], [36, 6, -3, -18], [12, 0, 0, 12]])
        np.testing.assert_equal(bs0, bspline2)
        np.testing.assert_equal(bs2.basis, BSpline(n_basis=3, order=2))
        np.testing.assert_almost_equal(
            bs2.coefficients, [[-144, -6, 12], [-120, -18, -60], [-48, 0, 48]])
示例#9
0
    def test_fdatabasis_derivative_constant(self):
        constant = FDataBasis(Constant(), [[1], [2], [3], [4]])

        self.assertTrue(constant.derivative().equals(
            FDataBasis(Constant(), [[0], [0], [0], [0]])))
        self.assertTrue(
            constant.derivative(order=0).equals(
                FDataBasis(Constant(), [[1], [2], [3], [4]])))
示例#10
0
    def test_error_y_is_FData(self):
        """Tests that none of the explained variables is an FData object
        """
        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = list(FDataBasis(Monomial(n_basis=7), np.identity(7)))

        scalar = LinearScalarRegression([Fourier(n_basis=5)])

        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
示例#11
0
    def test_error_beta_not_basis(self):
        """ Test that all beta are Basis objects. """

        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = [1 for _ in range(7)]
        beta = FDataBasis(Monomial(n_basis=7), np.identity(7))

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
示例#12
0
    def test_fdatabasis_times_fdatabasis_fdatabasis(self):
        monomial = FDataBasis(Monomial(nbasis=3), [1, 2, 3])
        bspline = FDataBasis(BSpline(nbasis=6, order=4), [1, 2, 4, 1, 0, 1])
        times_fdar = monomial.times(bspline)

        prod_basis = BSpline(nbasis=9, order=6, knots=[0, 0.25, 0.5, 0.75, 1])
        prod_coefs = np.array([[0.9788352,  1.6289955,  2.7004969,  6.2678739,
                      8.7636441,  4.0069960,  0.7126961,  2.8826708,
                      6.0052311]])

        self.assertEqual(prod_basis, times_fdar.basis)
        np.testing.assert_array_almost_equal(prod_coefs, times_fdar.coefficients)
示例#13
0
    def test_fdatabasis__div__(self):
        monomial1 = FDataBasis(Monomial(n_basis=3), [1, 2, 3])
        monomial2 = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3, 4, 5]])

        self.assertTrue((monomial1 / 2).equals(
            FDataBasis(Monomial(n_basis=3), [[1 / 2, 1, 3 / 2]])))
        self.assertTrue((monomial2 / 2).equals(
            FDataBasis(Monomial(n_basis=3),
                       [[1 / 2, 1, 3 / 2], [3 / 2, 2, 5 / 2]])))

        self.assertTrue((monomial2 / [1, 2]).equals(
            FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3 / 2, 2, 5 / 2]])))
示例#14
0
    def test_fdatabasis__mul__2(self):
        monomial1 = FDataBasis(Monomial(n_basis=3), [1, 2, 3])
        monomial2 = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3, 4, 5]])

        np.testing.assert_equal(monomial1 / 2,
                                FDataBasis(Monomial(n_basis=3),
                                           [[1 / 2, 1, 3 / 2]]))
        np.testing.assert_equal(monomial2 / 2,
                                FDataBasis(Monomial(n_basis=3),
                                           [[1 / 2, 1, 3 / 2], [3 / 2, 2, 5 / 2]]))

        np.testing.assert_equal(monomial2 / [1, 2],
                                FDataBasis(Monomial(n_basis=3),
                                           [[1, 2, 3], [3 / 2, 2, 5 / 2]]))
示例#15
0
    def test_evaluation_composed_monomial(self):
        """Test the evaluation of FDataBasis the a matrix of times instead of
        a list of times """
        monomial = Monomial(domain_range=(0, 1), nbasis=3)

        coefficients = [[1, 2, 3], [0.5, 1.4, 1.3]]

        f = FDataBasis(monomial, coefficients)
        t = np.linspace(0, 1, 4)

        res_test = f(t)

        # Test same result than evaluation standart
        np.testing.assert_array_almost_equal(f([1]), f([[1], [1]],
                                                       aligned_evaluation=False))
        np.testing.assert_array_almost_equal(f(t), f(np.vstack((t, t)),
                                                     aligned_evaluation=False))

        # Different evaluation times
        t_multiple = [[0, 0.5], [0.2, 0.7]]
        np.testing.assert_array_almost_equal(f(t_multiple[0])[0],
                                             f(t_multiple,
                                               aligned_evaluation=False)[0])
        np.testing.assert_array_almost_equal(f(t_multiple[1])[1],
                                             f(t_multiple,
                                               aligned_evaluation=False)[1])
示例#16
0
    def test_domain_in_list_bspline(self):
        """Test the evaluation of FDataBasis"""

        for bspline in (BSpline(domain_range=[(0, 1)], nbasis=5, order=3),
                        BSpline(domain_range=((0, 1),), nbasis=5, order=3),
                        BSpline(domain_range=np.array((0, 1)), nbasis=5,
                                order=3),
                        BSpline(domain_range=np.array([(0, 1)]), nbasis=5,
                                order=3)
                        ):

            coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                            [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

            f = FDataBasis(bspline, coefficients)

            t = np.linspace(0, 1, 4)

            res = np.array([[0.001, 0.564, 0.435, 0.33],
                            [0.018, 0.468, 0.371, 0.12]])

            np.testing.assert_array_almost_equal(f(t).round(3), res)
            np.testing.assert_array_almost_equal(f.evaluate(t).round(3), res)

        # Check error
        with np.testing.assert_raises(ValueError):
            BSpline(domain_range=[(0, 1), (0, 1)])
    def test_evaluation_composed_bspline(self):
        """Test the evaluation of FDataBasis the a matrix of times instead of
        a list of times """
        bspline = BSpline(domain_range=(0, 1), n_basis=5, order=3)

        coefficients = [[0.00078238, 0.48857741, 0.63971985, 0.23, 0.33],
                        [0.01778079, 0.73440271, 0.20148638, 0.54, 0.12]]

        f = FDataBasis(bspline, coefficients)
        t = np.linspace(0, 1, 4)

        # Test same result than evaluation standart
        np.testing.assert_array_almost_equal(f([1]),
                                             f([[1], [1]], aligned=False))
        np.testing.assert_array_almost_equal(
            f(t), f(np.vstack((t, t)), aligned=False))

        # Different evaluation times
        t_multiple = [[0, 0.5], [0.2, 0.7]]
        np.testing.assert_array_almost_equal(
            f(t_multiple[0])[0],
            f(t_multiple, aligned=False)[0])
        np.testing.assert_array_almost_equal(
            f(t_multiple[1])[1],
            f(t_multiple, aligned=False)[1])
示例#18
0
    def fit(self, X, y=None, sample_weight=None):

        y, X, weights = self._argcheck(y, X, sample_weight)

        nbeta = len(self.beta_basis)
        n_samples = X[0].n_samples

        y = np.asarray(y).reshape((n_samples, 1))

        for j in range(nbeta):
            xcoef = X[j].coefficients
            inner_basis_x_beta_j = X[j].basis.inner_product(self.beta_basis[j])
            inner_x_beta = (
                xcoef @ inner_basis_x_beta_j if j == 0 else np.concatenate(
                    (inner_x_beta, xcoef @ inner_basis_x_beta_j), axis=1))

        if any(w != 1 for w in weights):
            inner_x_beta = inner_x_beta * np.sqrt(weights)
            y = y * np.sqrt(weights)

        gram_inner_x_beta = inner_x_beta.T @ inner_x_beta
        inner_x_beta_y = inner_x_beta.T @ y

        gram_inner_x_beta_inv = np.linalg.inv(gram_inner_x_beta)
        betacoefs = gram_inner_x_beta_inv @ inner_x_beta_y

        idx = 0
        for j in range(0, nbeta):
            self.beta_basis[j] = FDataBasis(
                self.beta_basis[j],
                betacoefs[idx:idx + self.beta_basis[j].n_basis].T)
            idx = idx + self.beta_basis[j].n_basis

        self.beta_ = self.beta_basis
        return self
示例#19
0
    def test_fdatabasis_fdatabasis_inprod(self):
        monomial = Monomial(n_basis=4)
        monomialfd = FDataBasis(monomial,
                                [[5, 4, 1, 0], [4, 2, 1, 0], [4, 1, 6, 4],
                                 [4, 5, 0, 1], [5, 6, 2, 0]])
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_allclose(
            inner_product_matrix(monomialfd, bsplinefd),
            np.array([[16.14797697, 52.81464364, 89.4813103],
                      [11.55565285, 38.22211951, 64.88878618],
                      [18.14698361, 55.64698361, 93.14698361],
                      [15.2495976, 48.9995976, 82.7495976],
                      [19.70392982, 63.03676315, 106.37009648]]),
            rtol=1e-4)
示例#20
0
    def test_evaluation_composed_fourier(self):
        """Test the evaluation of FDataBasis the a matrix of times instead of
        a list of times """
        fourier = Fourier(domain_range=(0, 1), nbasis=3)

        coefficients = np.array([[0.00078238, 0.48857741, 0.63971985],
                                 [0.01778079, 0.73440271, 0.20148638]])

        f = FDataBasis(fourier, coefficients)
        t = np.linspace(0, 1, 4)

        res_test = f(t)

        # Test same result than evaluation standart
        np.testing.assert_array_almost_equal(f([1]), f([[1], [1]],
                                                       aligned_evaluation=False))
        np.testing.assert_array_almost_equal(f(t), f(np.vstack((t, t)),
                                                     aligned_evaluation=False))

        # Different evaluation times
        t_multiple = [[0, 0.5], [0.2, 0.7]]
        np.testing.assert_array_almost_equal(f(t_multiple[0])[0],
                                             f(t_multiple,
                                               aligned_evaluation=False)[0])
        np.testing.assert_array_almost_equal(f(t_multiple[1])[1],
                                             f(t_multiple,
                                               aligned_evaluation=False)[1])
示例#21
0
    def test_regression_fit(self):

        x_basis = Monomial(n_basis=7)
        x_fd = FDataBasis(x_basis, np.identity(7))

        beta_basis = Fourier(n_basis=5)
        beta_fd = FDataBasis(beta_basis, [1, 1, 1, 1, 1])
        y = [
            1.0000684777229512, 0.1623672257830915, 0.08521053851548224,
            0.08514200869281137, 0.09529138749665378, 0.10549625973303875,
            0.11384314859153018
        ]

        scalar = LinearScalarRegression([beta_basis])
        scalar.fit([x_fd], y)
        np.testing.assert_array_almost_equal(scalar.beta_[0].coefficients,
                                             beta_fd.coefficients)
示例#22
0
    def test_init_default(self):
        """Tests default initialization (do not penalize)."""
        lfd = LinearDifferentialOperator()
        weightfd = [FDataBasis(Constant((0, 1)), 0)]

        np.testing.assert_equal(
            lfd.weights, weightfd,
            "Wrong list of weight functions of the linear operator")
示例#23
0
    def test_comutativity_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_allclose(
            inner_product_matrix(bsplinefd, monomial),
            np.transpose(inner_product_matrix(monomial, bsplinefd)))
示例#24
0
    def test_error_y_X_samples_different(self):
        """ Test that the number of response samples and explanatory samples
        are not different """

        x_fd = FDataBasis(Monomial(n_basis=7), np.identity(7))
        y = [1 for _ in range(8)]
        beta = Fourier(n_basis=5)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)

        x_fd = FDataBasis(Monomial(n_basis=8), np.identity(8))
        y = [1 for _ in range(7)]
        beta = Fourier(n_basis=5)

        scalar = LinearScalarRegression([beta])
        np.testing.assert_raises(ValueError, scalar.fit, [x_fd], y)
示例#25
0
    def test_comutativity_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_array_almost_equal(
            bsplinefd.inner_product(monomial).round(3),
            np.transpose(monomial.inner_product(bsplinefd).round(3)))
示例#26
0
    def test_fdatabasis__mul__(self):
        monomial1 = FDataBasis(Monomial(n_basis=3), [1, 2, 3])
        monomial2 = FDataBasis(Monomial(n_basis=3), [[1, 2, 3], [3, 4, 5]])

        np.testing.assert_equal(monomial1 * 2,
                                FDataBasis(Monomial(n_basis=3),
                                           [[2, 4, 6]]))
        np.testing.assert_equal(3 * monomial2,
                                FDataBasis(Monomial(n_basis=3),
                                           [[3, 6, 9], [9, 12, 15]]))
        np.testing.assert_equal(3 * monomial2,
                                monomial2 * 3)

        np.testing.assert_equal(monomial2 * [1, 2],
                                FDataBasis(Monomial(n_basis=3),
                                           [[1, 2, 3], [6, 8, 10]]))
        np.testing.assert_equal([1, 2] * monomial2,
                                FDataBasis(Monomial(n_basis=3),
                                           [[1, 2, 3], [6, 8, 10]]))

        with np.testing.assert_raises(TypeError):
            monomial2 * FDataBasis(Fourier(n_basis=3),
                                   [[2, 2, 3], [5, 4, 5]])

        with np.testing.assert_raises(TypeError):
            monomial2 * monomial2
示例#27
0
    def test_evaluation_grid_keepdims_fourier(self):
        """Test behaviour of keepdims with grid evaluation"""

        fourier = Fourier(domain_range=(0, 1), nbasis=3)

        coefficients = np.array([[0.00078238, 0.48857741, 0.63971985],
                                 [0.01778079, 0.73440271, 0.20148638]])

        f = FDataBasis(fourier, coefficients)
        f_keepdims = FDataBasis(fourier, coefficients, keepdims=True)

        np.testing.assert_equal(f.keepdims, False)
        np.testing.assert_equal(f_keepdims.keepdims, True)

        t = np.linspace(0, 1, 4)

        res = np.array([0.905482867989282, 0.146814813180645, -1.04995054116993,
                        0.905482867989282, 0.302725561229459,
                        0.774764356993855, -1.02414754822331, 0.302725561229459]
                       ).reshape((2, 4)).round(3)

        res_keepdims = res.reshape((2, 4, 1))

        # Case default behaviour keepdims=False
        np.testing.assert_array_almost_equal(f(t, grid=True).round(3), res)
        np.testing.assert_array_almost_equal(f(t, grid=True, keepdims=False
                                               ).round(3),
                                             res)

        np.testing.assert_array_almost_equal(f(t,  grid=True, keepdims=True
                                               ).round(3),
                                             res_keepdims)

        # Case default behaviour keepdims=True
        np.testing.assert_array_almost_equal(f_keepdims(t, grid=True
                                                        ).round(3),
                                             res_keepdims)
        np.testing.assert_array_almost_equal(f_keepdims(t, grid=True,
                                                        keepdims=False
                                                        ).round(3), res)
        np.testing.assert_array_almost_equal(f_keepdims(t, grid=True,
                                                        keepdims=True).round(3),
                                             res_keepdims)
示例#28
0
    def test_fdatabasis_derivative_fourier(self):
        fourier = FDataBasis(Fourier(n_basis=7), [1, 5, 8, 9, 8, 4, 5])
        fourier2 = FDataBasis(
            Fourier(n_basis=5),
            [[4, 9, 7, 4, 3], [1, 7, 9, 8, 5], [4, 6, 6, 6, 8]])

        fou0 = fourier.derivative(order=0)
        fou1 = fourier.derivative()
        fou2 = fourier.derivative(order=2)

        np.testing.assert_equal(fou1.basis, fourier.basis)
        np.testing.assert_almost_equal(
            fou1.coefficients.round(5),
            np.atleast_2d([
                0, -50.26548, 31.41593, -100.53096, 113.09734, -94.24778,
                75.39822
            ]))
        np.testing.assert_equal(fou0, fourier)
        np.testing.assert_equal(fou2.basis, fourier.basis)
        np.testing.assert_almost_equal(
            fou2.coefficients.round(5),
            np.atleast_2d([
                0, -197.39209, -315.82734, -1421.22303, -1263.30936,
                -1421.22303, -1776.52879
            ]))

        fou0 = fourier2.derivative(order=0)
        fou1 = fourier2.derivative()
        fou2 = fourier2.derivative(order=2)

        np.testing.assert_equal(fou1.basis, fourier2.basis)
        np.testing.assert_almost_equal(
            fou1.coefficients.round(5),
            [[0, -43.98230, 56.54867, -37.69911, 50.26548],
             [0, -56.54867, 43.98230, -62.83185, 100.53096],
             [0, -37.69911, 37.69911, -100.53096, 75.39822]])
        np.testing.assert_equal(fou0, fourier2)
        np.testing.assert_equal(fou2.basis, fourier2.basis)
        np.testing.assert_almost_equal(
            fou2.coefficients.round(5),
            [[0, -355.30576, -276.34892, -631.65468, -473.74101],
             [0, -276.34892, -355.30576, -1263.30936, -789.56835],
             [0, -236.87051, -236.87051, -947.48202, -1263.30936]])
示例#29
0
    def test_fdatabasis_times_fdatabasis_int(self):
        monomial = FDataBasis(Monomial(n_basis=3),
                              [[1, 2, 3], [4, 5, 6], [7, 8, 9]])
        result = monomial.times(3)

        expec_basis = Monomial(n_basis=3)
        expec_coefs = np.array([[3, 6, 9], [12, 15, 18], [21, 24, 27]])

        self.assertEqual(expec_basis, result.basis)
        np.testing.assert_array_almost_equal(expec_coefs, result.coefficients)
示例#30
0
    def test_basis_fdatabasis_inprod(self):
        monomial = Monomial(n_basis=4)
        bspline = BSpline(n_basis=5, order=3)
        bsplinefd = FDataBasis(bspline, np.arange(0, 15).reshape(3, 5))

        np.testing.assert_array_almost_equal(
            monomial.inner_product(bsplinefd).round(3),
            np.array([[2., 7., 12.], [1.29626206, 3.79626206, 6.29626206],
                      [0.96292873, 2.62959539, 4.29626206],
                      [0.7682873, 2.0182873, 3.2682873]]).round(3))