From 13092f621860198a8cfea71195920b22ec3c00a9 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 5 Feb 2022 18:31:12 -0500 Subject: [PATCH 1/3] Add test for dot --- python/gtsam/tests/test_DiscreteBayesNet.py | 35 +++++++++++++++++++++ 1 file changed, 35 insertions(+) diff --git a/python/gtsam/tests/test_DiscreteBayesNet.py b/python/gtsam/tests/test_DiscreteBayesNet.py index 3ae3b625cd..74191dcc7a 100644 --- a/python/gtsam/tests/test_DiscreteBayesNet.py +++ b/python/gtsam/tests/test_DiscreteBayesNet.py @@ -12,7 +12,9 @@ # pylint: disable=no-name-in-module, invalid-name import unittest +import textwrap +import gtsam from gtsam import (DiscreteBayesNet, DiscreteConditional, DiscreteFactorGraph, DiscreteKeys, DiscreteDistribution, DiscreteValues, Ordering) from gtsam.utils.test_case import GtsamTestCase @@ -126,6 +128,39 @@ def test_fragment(self): actual = fragment.sample(given) self.assertEqual(len(actual), 5) + def test_dot(self): + """Check that dot works with position hints.""" + fragment = DiscreteBayesNet() + fragment.add(Either, [Tuberculosis, LungCancer], "F T T T") + MyAsia = gtsam.symbol('a', 0), 2 # use a symbol! + fragment.add(Tuberculosis, [MyAsia], "99/1 95/5") + fragment.add(LungCancer, [Smoking], "99/1 90/10") + + # Make sure we can *update* position hints + writer = gtsam.DotWriter() + ph: dict = writer.positionHints + ph.update({'a': 2}) # hint at symbol position + writer.positionHints = ph + + # Check the output of dot + actual = fragment.dot(writer=writer) + expected_result = """\ + digraph { + size="5,5"; + + var3[label="3"]; + var4[label="4"]; + var5[label="5"]; + var6[label="6"]; + vara0[label="a0", pos="0,2!"]; + + var4->var6 + vara0->var3 + var3->var5 + var6->var5 + }""" + self.assertEqual(actual, textwrap.dedent(expected_result)) + if __name__ == "__main__": unittest.main() From c7975bebeaed3369f9365b485de4b3e2448244ca Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 5 Feb 2022 18:31:23 -0500 Subject: [PATCH 2/3] remove stray output --- gtsam/discrete/tests/testDiscreteBayesNet.cpp | 1 - 1 file changed, 1 deletion(-) diff --git a/gtsam/discrete/tests/testDiscreteBayesNet.cpp b/gtsam/discrete/tests/testDiscreteBayesNet.cpp index cfc9c1bb50..19af676f7d 100644 --- a/gtsam/discrete/tests/testDiscreteBayesNet.cpp +++ b/gtsam/discrete/tests/testDiscreteBayesNet.cpp @@ -150,7 +150,6 @@ TEST(DiscreteBayesNet, Dot) { fragment.add((Either | Tuberculosis, LungCancer) = "F T T T"); string actual = fragment.dot(); - cout << actual << endl; EXPECT(actual == "digraph {\n" " size=\"5,5\";\n" From 72772b1f4105a7576f6db3a88222161b27942ffb Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sat, 5 Feb 2022 18:41:35 -0500 Subject: [PATCH 3/3] Add header back in --- python/gtsam/preamble/inference.h | 2 ++ 1 file changed, 2 insertions(+) diff --git a/python/gtsam/preamble/inference.h b/python/gtsam/preamble/inference.h index d07a75f6fb..4106c794ac 100644 --- a/python/gtsam/preamble/inference.h +++ b/python/gtsam/preamble/inference.h @@ -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 \ No newline at end of file