test_DiscreteDistribution.py
Go to the documentation of this file.
1 """
2 GTSAM Copyright 2010-2021, 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 Discrete Priors.
9 Author: Frank Dellaert
10 """
11 
12 # pylint: disable=no-name-in-module, invalid-name
13 
14 import unittest
15 
16 import numpy as np
17 from gtsam import DecisionTreeFactor, DiscreteKeys, DiscreteDistribution
18 from gtsam.utils.test_case import GtsamTestCase
19 
20 X = 0, 2
21 
22 
24  """Tests for Discrete Priors."""
25 
26  def test_constructor(self):
27  """Test various constructors."""
28  keys = DiscreteKeys()
29  keys.push_back(X)
30  f = DecisionTreeFactor(keys, "0.4 0.6")
31  expected = DiscreteDistribution(f)
32 
33  actual = DiscreteDistribution(X, "2/3")
34  self.gtsamAssertEquals(actual, expected)
35 
36  actual2 = DiscreteDistribution(X, [0.4, 0.6])
37  self.gtsamAssertEquals(actual2, expected)
38 
39  def test_operator(self):
40  prior = DiscreteDistribution(X, "2/3")
41  self.assertAlmostEqual(prior(0), 0.4)
42  self.assertAlmostEqual(prior(1), 0.6)
43 
44  def test_pmf(self):
45  prior = DiscreteDistribution(X, "2/3")
46  expected = np.array([0.4, 0.6])
47  np.testing.assert_allclose(expected, prior.pmf())
48 
49  def test_sample(self):
50  prior = DiscreteDistribution(X, "2/3")
51  actual = prior.sample()
52  self.assertIsInstance(actual, int)
53 
54  def test_markdown(self):
55  """Test the _repr_markdown_ method."""
56 
57  prior = DiscreteDistribution(X, "2/3")
58  expected = " *P(0):*\n\n" \
59  "|0|value|\n" \
60  "|:-:|:-:|\n" \
61  "|0|0.4|\n" \
62  "|1|0.6|\n" \
63 
64  actual = prior._repr_markdown_()
65  self.assertEqual(actual, expected)
66 
67 
68 if __name__ == "__main__":
69  unittest.main()
Point2 prior(const Point2 &x)
Prior on a single pose.
Definition: simulated2D.h:88
def gtsamAssertEquals(self, actual, expected, tol=1e-9)
Definition: test_case.py:18
DiscreteKeys is a set of keys that can be assembled using the & operator.
Definition: DiscreteKey.h:41


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