test_GaussianBayesNet.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 Unit tests for Gaussian Bayes Nets.
9 Author: Frank Dellaert
10 """
11 # pylint: disable=invalid-name, no-name-in-module, no-member
12 
13 import unittest
14 
15 import numpy as np
16 from gtsam.utils.test_case import GtsamTestCase
17 
18 import gtsam
19 from gtsam import GaussianBayesNet, GaussianConditional
20 
21 # some keys
22 _x_ = 11
23 _y_ = 22
24 _z_ = 33
25 
26 
28  """Create a small Bayes Net for testing"""
29  bayesNet = GaussianBayesNet()
30  I_1x1 = np.eye(1, dtype=float)
31  bayesNet.push_back(GaussianConditional(_x_, [9.0], I_1x1, _y_, I_1x1))
32  bayesNet.push_back(GaussianConditional(_y_, [5.0], I_1x1))
33  return bayesNet
34 
35 
37  """Tests for Gaussian Bayes nets."""
38 
39  def test_matrix(self):
40  """Test matrix method"""
41  R, d = smallBayesNet().matrix() # get matrix and RHS
42  R1 = np.array([[1.0, 1.0], [0.0, 1.0]])
43  d1 = np.array([9.0, 5.0])
44  np.testing.assert_equal(R, R1)
45  np.testing.assert_equal(d, d1)
46 
47  def test_evaluate(self):
48  """Test evaluate method"""
49  bayesNet = smallBayesNet()
50  values = gtsam.VectorValues()
51  values.insert(_x_, np.array([9.0]))
52  values.insert(_y_, np.array([5.0]))
53  for i in [0, 1]:
54  self.assertAlmostEqual(bayesNet.at(i).logProbability(values),
55  np.log(bayesNet.at(i).evaluate(values)))
56  self.assertAlmostEqual(bayesNet.logProbability(values),
57  np.log(bayesNet.evaluate(values)))
58 
59  def test_sample(self):
60  """Test sample method"""
61  bayesNet = smallBayesNet()
62  sample = bayesNet.sample()
63  self.assertIsInstance(sample, gtsam.VectorValues)
64 
65  # standard deviation is 1.0 for both, so we set tolerance to 3*sigma
66  mean = bayesNet.optimize()
67  self.gtsamAssertEquals(sample, mean, tol=3.0)
68 
69 
70 if __name__ == "__main__":
71  unittest.main()
test_GaussianBayesNet.TestGaussianBayesNet.test_matrix
def test_matrix(self)
Definition: test_GaussianBayesNet.py:39
gtsam::utils.test_case.GtsamTestCase.gtsamAssertEquals
def gtsamAssertEquals(self, actual, expected, tol=1e-9)
Definition: test_case.py:18
gtsam::VectorValues
Definition: VectorValues.h:74
gtsam::GaussianConditional
Definition: GaussianConditional.h:40
gtsam::utils.test_case
Definition: test_case.py:1
test_GaussianBayesNet.TestGaussianBayesNet
Definition: test_GaussianBayesNet.py:36
test_GaussianBayesNet.TestGaussianBayesNet.test_evaluate
def test_evaluate(self)
Definition: test_GaussianBayesNet.py:47
matrix
Map< Matrix< T, Dynamic, Dynamic, ColMajor >, 0, OuterStride<> > matrix(T *data, int rows, int cols, int stride)
Definition: gtsam/3rdparty/Eigen/blas/common.h:110
test_GaussianBayesNet.TestGaussianBayesNet.test_sample
def test_sample(self)
Definition: test_GaussianBayesNet.py:59
gtsam::utils.test_case.GtsamTestCase
Definition: test_case.py:15
gtsam::GaussianBayesNet
Definition: GaussianBayesNet.h:35
test_GaussianBayesNet.smallBayesNet
def smallBayesNet()
Definition: test_GaussianBayesNet.py:27


gtsam
Author(s):
autogenerated on Sat Nov 16 2024 04:07:05