Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Basis - Python unit test #860

Merged
merged 8 commits into from
Aug 29, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/scripts/python.sh
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ make -j2 install
cd $GITHUB_WORKSPACE/build/python
$PYTHON setup.py install --user --prefix=
cd $GITHUB_WORKSPACE/python/gtsam/tests
$PYTHON -m unittest discover
$PYTHON -m unittest discover -v
10 changes: 7 additions & 3 deletions gtsam/basis/Fourier.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,9 +40,13 @@ class GTSAM_EXPORT FourierBasis : public Basis<FourierBasis> {
static Weights CalculateWeights(size_t N, double x) {
Weights b(N);
b[0] = 1;
for (size_t i = 1, n = 1; i < N; i += 2, n++) {
b[i] = cos(n * x);
b[i + 1] = sin(n * x);
for (size_t i = 1, n = 1; i < N; i++) {
if (i % 2 == 1) {
b[i] = cos(n * x);
} else {
b[i] = sin(n * x);
n++;
}
}
return b;
}
Expand Down
5 changes: 3 additions & 2 deletions python/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -166,5 +166,6 @@ add_custom_target(
COMMAND
${CMAKE_COMMAND} -E env # add package to python path so no need to install
"PYTHONPATH=${GTSAM_PYTHON_BUILD_DIRECTORY}/$ENV{PYTHONPATH}"
${PYTHON_EXECUTABLE} -m unittest discover -s "${GTSAM_PYTHON_BUILD_DIRECTORY}/gtsam/tests"
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES})
${PYTHON_EXECUTABLE} -m unittest discover -v -s .
DEPENDS ${GTSAM_PYTHON_DEPENDENCIES}
WORKING_DIRECTORY "${GTSAM_PYTHON_BUILD_DIRECTORY}/gtsam/tests")
2 changes: 2 additions & 0 deletions python/gtsam/preamble/basis.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,5 @@
* Without this they will be automatically converted to a Python object, and all
* mutations on Python side will not be reflected on C++.
*/

#include <pybind11/stl.h>
96 changes: 96 additions & 0 deletions python/gtsam/tests/test_basis.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
"""
GTSAM Copyright 2010-2019, Georgia Tech Research Corporation,
Atlanta, Georgia 30332-0415
All Rights Reserved

See LICENSE for the license information

Basis unit tests.
Author: Frank Dellaert & Gerry Chen (Python)
"""
import unittest

import numpy as np

import gtsam
from gtsam.utils.test_case import GtsamTestCase
from gtsam.symbol_shorthand import B


class TestBasis(GtsamTestCase):
gchenfc marked this conversation as resolved.
Show resolved Hide resolved
"""
Tests FitBasis python binding for FourierBasis, Chebyshev1Basis, Chebyshev2Basis, and Chebyshev2.

It tests FitBasis by fitting to a ground-truth function that can be represented exactly in
the basis, then checking that the regression (fit result) matches the function. For the
Chebyshev bases, the line y=x is used to generate the data while for Fourier, 0.7*cos(x) is
used.
"""
def setUp(self):
self.N = 2
self.x = [0., 0.5, 0.75]
self.interpx = np.linspace(0., 1., 10)
self.noise = gtsam.noiseModel.Unit.Create(1)

def evaluate(self, basis, fitparams, x):
"""
Until wrapper for Basis functors are ready,
this is how to evaluate a basis function.
"""
return basis.WeightMatrix(self.N, x) @ fitparams

def fit_basis_helper(self, fitter, basis, f=lambda x: x):
"""Helper method to fit data to a specified fitter using a specified basis."""
data = {x: f(x) for x in self.x}
fit = fitter(data, self.noise, self.N)
coeff = fit.parameters()
interpy = self.evaluate(basis, coeff, self.interpx)
return interpy

def test_fit_basis_fourier(self):
"""Fit a Fourier basis."""

f = lambda x: 0.7 * np.cos(x)
interpy = self.fit_basis_helper(gtsam.FitBasisFourierBasis,
gtsam.FourierBasis, f)
# test a basis by checking that the fit result matches the function at x-values interpx.
np.testing.assert_almost_equal(interpy,
np.array([f(x) for x in self.interpx]),
decimal=7)

def test_fit_basis_chebyshev1basis(self):
"""Fit a Chebyshev1 basis."""

f = lambda x: x
interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev1Basis,
gtsam.Chebyshev1Basis, f)
# test a basis by checking that the fit result matches the function at x-values interpx.
np.testing.assert_almost_equal(interpy,
np.array([f(x) for x in self.interpx]),
decimal=7)

def test_fit_basis_chebyshev2basis(self):
"""Fit a Chebyshev2 basis."""

f = lambda x: x
interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev2Basis,
gtsam.Chebyshev2Basis)
# test a basis by checking that the fit result matches the function at x-values interpx.
np.testing.assert_almost_equal(interpy,
np.array([f(x) for x in self.interpx]),
decimal=7)

def test_fit_basis_chebyshev2(self):
"""Fit a Chebyshev2 pseudospectral basis."""

f = lambda x: x
interpy = self.fit_basis_helper(gtsam.FitBasisChebyshev2,
gtsam.Chebyshev2)
# test a basis by checking that the fit result matches the function at x-values interpx.
np.testing.assert_almost_equal(interpy,
np.array([f(x) for x in self.interpx]),
decimal=7)


if __name__ == "__main__":
unittest.main()