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 from gtsam.utils.test_case import GtsamTestCase
15 
16 import gtsam
17 
18 
20  """
21  Tests FitBasis python binding for FourierBasis, Chebyshev1Basis, Chebyshev2Basis, and Chebyshev2.
22 
23  It tests FitBasis by fitting to a ground-truth function that can be represented exactly in
24  the basis, then checking that the regression (fit result) matches the function. For the
25  Chebyshev bases, the line y=x is used to generate the data while for Fourier, 0.7*cos(x) is
26  used.
27  """
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()
test_basis.TestBasis.test_fit_basis_fourier
def test_fit_basis_fourier(self)
Definition: test_basis.py:50
test_basis.TestBasis.test_fit_basis_chebyshev2
def test_fit_basis_chebyshev2(self)
Definition: test_basis.py:83
test_basis.TestBasis.evaluate
def evaluate(self, basis, fitparams, x)
Definition: test_basis.py:35
gtsam::Chebyshev1Basis
Definition: Chebyshev.h:32
gtsam::Chebyshev2
Definition: Chebyshev2.h:46
test_basis.TestBasis.interpx
interpx
Definition: test_basis.py:32
gtsam::noiseModel::Unit::Create
static shared_ptr Create(size_t dim)
Definition: NoiseModel.h:631
gtsam::FourierBasis
Fourier basis.
Definition: Fourier.h:27
gtsam::utils.test_case
Definition: test_case.py:1
test_basis.TestBasis.noise
noise
Definition: test_basis.py:33
gtsam::Chebyshev2Basis
Definition: Chebyshev.h:80
tree::f
Point2(* f)(const Point3 &, OptionalJacobian< 2, 3 >)
Definition: testExpression.cpp:218
test_basis.TestBasis
Definition: test_basis.py:19
test_basis.TestBasis.setUp
def setUp(self)
Definition: test_basis.py:29
test_basis.TestBasis.test_fit_basis_chebyshev2basis
def test_fit_basis_chebyshev2basis(self)
Definition: test_basis.py:72
gtsam::utils.test_case.GtsamTestCase
Definition: test_case.py:15
test_basis.TestBasis.fit_basis_helper
def fit_basis_helper(self, fitter, basis, f=lambda x:x)
Definition: test_basis.py:42
test_basis.TestBasis.x
x
Definition: test_basis.py:31
test_basis.TestBasis.N
N
Definition: test_basis.py:30
test_basis.TestBasis.test_fit_basis_chebyshev1basis
def test_fit_basis_chebyshev1basis(self)
Definition: test_basis.py:61


gtsam
Author(s):
autogenerated on Fri Nov 1 2024 03:39:26