test_DecisionTreeFactor.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 DecisionTreeFactors.
9 Author: Frank Dellaert
10 """
11 
12 # pylint: disable=no-name-in-module, invalid-name
13 
14 import unittest
15 
16 from gtsam import (DecisionTreeFactor, DiscreteDistribution, DiscreteValues,
17  Ordering)
18 from gtsam.utils.test_case import GtsamTestCase
19 
20 
22  """Tests for DecisionTreeFactors."""
23 
24  def setUp(self):
25  self.A = (12, 3)
26  self.B = (5, 2)
27  self.factor = DecisionTreeFactor([self.A, self.B], "1 2 3 4 5 6")
28 
29  def test_from_floats(self):
30  """Test whether we can construct a factor from floats."""
31  actual = DecisionTreeFactor([self.A, self.B], [1., 2., 3., 4., 5., 6.])
32  self.gtsamAssertEquals(actual, self.factor)
33 
34  def test_enumerate(self):
35  """Test whether we can enumerate the factor."""
36  actual = self.factor.enumerate()
37  _, values = zip(*actual)
38  self.assertEqual(list(values), [1.0, 2.0, 3.0, 4.0, 5.0, 6.0])
39 
41  """Test whether multiplication works with overloading."""
42  v0 = (0, 2)
43  v1 = (1, 2)
44  v2 = (2, 2)
45 
46  # Multiply with a DiscreteDistribution, i.e., Bayes Law!
47  prior = DiscreteDistribution(v1, [1, 3])
48  f1 = DecisionTreeFactor([v0, v1], "1 2 3 4")
49  expected = DecisionTreeFactor([v0, v1], "0.25 1.5 0.75 3")
50  self.gtsamAssertEquals(DecisionTreeFactor(prior) * f1, expected)
51  self.gtsamAssertEquals(f1 * prior, expected)
52 
53  # Multiply two factors
54  f2 = DecisionTreeFactor([v1, v2], "5 6 7 8")
55  actual = f1 * f2
56  expected2 = DecisionTreeFactor([v0, v1, v2], "5 6 14 16 15 18 28 32")
57  self.gtsamAssertEquals(actual, expected2)
58 
59  def test_methods(self):
60  """Test whether we can call methods in python."""
61  # double operator()(const DiscreteValues& values) const;
62  values = DiscreteValues()
63  values[self.A[0]] = 0
64  values[self.B[0]] = 0
65  self.assertIsInstance(self.factor(values), float)
66 
67  # size_t cardinality(Key j) const;
68  self.assertIsInstance(self.factor.cardinality(self.A[0]), int)
69 
70  # DecisionTreeFactor operator/(const DecisionTreeFactor& f) const;
71  self.assertIsInstance(self.factor / self.factor, DecisionTreeFactor)
72 
73  # DecisionTreeFactor* sum(size_t nrFrontals) const;
74  self.assertIsInstance(self.factor.sum(1), DecisionTreeFactor)
75 
76  # DecisionTreeFactor* sum(const Ordering& keys) const;
77  ordering = Ordering()
78  ordering.push_back(self.A[0])
79  self.assertIsInstance(self.factor.sum(ordering), DecisionTreeFactor)
80 
81  # DecisionTreeFactor* max(size_t nrFrontals) const;
82  self.assertIsInstance(self.factor.max(1), DecisionTreeFactor)
83 
84  def test_markdown(self):
85  """Test whether the _repr_markdown_ method."""
86 
87  expected = \
88  "|A|B|value|\n" \
89  "|:-:|:-:|:-:|\n" \
90  "|0|0|1|\n" \
91  "|0|1|2|\n" \
92  "|1|0|3|\n" \
93  "|1|1|4|\n" \
94  "|2|0|5|\n" \
95  "|2|1|6|\n"
96 
97  def formatter(x: int):
98  return "A" if x == 12 else "B"
99 
100  actual = self.factor._repr_markdown_(formatter)
101  self.assertEqual(actual, expected)
102 
103 
104 if __name__ == "__main__":
105  unittest.main()
test_DecisionTreeFactor.TestDecisionTreeFactor.test_enumerate
def test_enumerate(self)
Definition: test_DecisionTreeFactor.py:34
gtsam::DecisionTreeFactor
Definition: DecisionTreeFactor.h:44
test_DecisionTreeFactor.TestDecisionTreeFactor.B
B
Definition: test_DecisionTreeFactor.py:26
list
Definition: pytypes.h:2166
gtsam::DiscreteDistribution
Definition: DiscreteDistribution.h:33
test_DecisionTreeFactor.TestDecisionTreeFactor.factor
factor
Definition: test_DecisionTreeFactor.py:27
formatter
const KeyFormatter & formatter
Definition: treeTraversal-inst.h:204
gtsam::utils.test_case.GtsamTestCase.gtsamAssertEquals
def gtsamAssertEquals(self, actual, expected, tol=1e-9)
Definition: test_case.py:18
test_DecisionTreeFactor.TestDecisionTreeFactor.setUp
def setUp(self)
Definition: test_DecisionTreeFactor.py:24
test_DecisionTreeFactor.TestDecisionTreeFactor.test_multiplication
def test_multiplication(self)
Definition: test_DecisionTreeFactor.py:40
gtsam::utils.test_case
Definition: test_case.py:1
test_DecisionTreeFactor.TestDecisionTreeFactor
Definition: test_DecisionTreeFactor.py:21
test_DecisionTreeFactor.TestDecisionTreeFactor.test_methods
def test_methods(self)
Definition: test_DecisionTreeFactor.py:59
test_DecisionTreeFactor.TestDecisionTreeFactor.test_from_floats
def test_from_floats(self)
Definition: test_DecisionTreeFactor.py:29
gtsam::DiscreteValues
Definition: DiscreteValues.h:34
gtsam::utils.test_case.GtsamTestCase
Definition: test_case.py:15
test_DecisionTreeFactor.TestDecisionTreeFactor.A
A
Definition: test_DecisionTreeFactor.py:25
max
#define max(a, b)
Definition: datatypes.h:20
gtsam::Ordering
Definition: inference/Ordering.h:33
test_DecisionTreeFactor.TestDecisionTreeFactor.test_markdown
def test_markdown(self)
Definition: test_DecisionTreeFactor.py:84


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