test_basis.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
3 Atlanta, Georgia 30332-0415
4 All Rights Reserved
5 
6 See LICENSE for the license information
7 
8 Basis unit tests.
9 Author: Frank Dellaert & Gerry Chen (Python)
10 """
11 import unittest
12 
13 import numpy as np
14 
15 import gtsam
16 from gtsam.utils.test_case import GtsamTestCase
17 from gtsam.symbol_shorthand import B
18 
19 
21  """
22  Tests FitBasis python binding for FourierBasis, Chebyshev1Basis, Chebyshev2Basis, and Chebyshev2.
23 
24  It tests FitBasis by fitting to a ground-truth function that can be represented exactly in
25  the basis, then checking that the regression (fit result) matches the function. For the
26  Chebyshev bases, the line y=x is used to generate the data while for Fourier, 0.7*cos(x) is
27  used.
28  """
29  def setUp(self):
30  self.N = 2
31  self.x = [0., 0.5, 0.75]
32  self.interpx = np.linspace(0., 1., 10)
34 
35  def evaluate(self, basis, fitparams, x):
36  """
37  Until wrapper for Basis functors are ready,
38  this is how to evaluate a basis function.
39  """
40  return basis.WeightMatrix(self.N, x) @ fitparams
41 
42  def fit_basis_helper(self, fitter, basis, f=lambda x: x):
43  """Helper method to fit data to a specified fitter using a specified basis."""
44  data = {x: f(x) for x in self.x}
45  fit = fitter(data, self.noise, self.N)
46  coeff = fit.parameters()
47  interpy = self.evaluate(basis, coeff, self.interpx)
48  return interpy
49 
51  """Fit a Fourier basis."""
52 
53  f = lambda x: 0.7 * np.cos(x)
54  interpy = self.fit_basis_helper(gtsam.FitBasisFourierBasis,
56  # test a basis by checking that the fit result matches the function at x-values interpx.
57  np.testing.assert_almost_equal(interpy,
58  np.array([f(x) for x in self.interpx]),
59  decimal=7)
60 
62  """Fit a Chebyshev1 basis."""
63 
64  f = lambda x: x
65  interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev1Basis,
67  # test a basis by checking that the fit result matches the function at x-values interpx.
68  np.testing.assert_almost_equal(interpy,
69  np.array([f(x) for x in self.interpx]),
70  decimal=7)
71 
73  """Fit a Chebyshev2 basis."""
74 
75  f = lambda x: x
76  interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev2Basis,
78  # test a basis by checking that the fit result matches the function at x-values interpx.
79  np.testing.assert_almost_equal(interpy,
80  np.array([f(x) for x in self.interpx]),
81  decimal=7)
82 
84  """Fit a Chebyshev2 pseudospectral basis."""
85 
86  f = lambda x: x
87  interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev2,
89  # test a basis by checking that the fit result matches the function at x-values interpx.
90  np.testing.assert_almost_equal(interpy,
91  np.array([f(x) for x in self.interpx]),
92  decimal=7)
93 
94 
95 if __name__ == "__main__":
96  unittest.main()
def test_fit_basis_chebyshev2basis(self)
Definition: test_basis.py:72
def test_fit_basis_chebyshev2(self)
Definition: test_basis.py:83
def evaluate(self, basis, fitparams, x)
Definition: test_basis.py:35
static shared_ptr Create(size_t dim)
Definition: NoiseModel.h:610
def fit_basis_helper(self, fitter, basis, f=lambda x:x)
Definition: test_basis.py:42
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Fourier basis.
Definition: Fourier.h:27
def test_fit_basis_fourier(self)
Definition: test_basis.py:50
def test_fit_basis_chebyshev1basis(self)
Definition: test_basis.py:61


gtsam
Author(s):
autogenerated on Tue Jul 4 2023 02:37:45