From 042cb9d902593930e479dea4172884d0f56d0e5e Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Fri, 24 Dec 2021 13:27:02 -0500 Subject: [PATCH] markdown for DiscreteFactorGraph --- gtsam/discrete/DecisionTreeFactor.h | 2 +- gtsam/discrete/DiscreteConditional.h | 2 +- gtsam/discrete/DiscreteFactor.h | 8 ++++ gtsam/discrete/DiscreteFactorGraph.cpp | 16 ++++++- gtsam/discrete/DiscreteFactorGraph.h | 8 ++++ gtsam/discrete/discrete.i | 3 ++ .../tests/testDiscreteFactorGraph.cpp | 42 +++++++++++++++++-- gtsam_unstable/discrete/Constraint.h | 11 +++++ 8 files changed, 85 insertions(+), 7 deletions(-) diff --git a/gtsam/discrete/DecisionTreeFactor.h b/gtsam/discrete/DecisionTreeFactor.h index 308b2f9ca..841f90fe2 100644 --- a/gtsam/discrete/DecisionTreeFactor.h +++ b/gtsam/discrete/DecisionTreeFactor.h @@ -168,7 +168,7 @@ namespace gtsam { /// Render as markdown table. std::string _repr_markdown_( - const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; + const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override; /// @} diff --git a/gtsam/discrete/DiscreteConditional.h b/gtsam/discrete/DiscreteConditional.h index ad21151a8..b76e4f65f 100644 --- a/gtsam/discrete/DiscreteConditional.h +++ b/gtsam/discrete/DiscreteConditional.h @@ -172,7 +172,7 @@ public: /// Render as markdown table. std::string _repr_markdown_( - const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; + const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override; /// @} }; diff --git a/gtsam/discrete/DiscreteFactor.h b/gtsam/discrete/DiscreteFactor.h index e2be94b94..f046e5e44 100644 --- a/gtsam/discrete/DiscreteFactor.h +++ b/gtsam/discrete/DiscreteFactor.h @@ -88,6 +88,14 @@ public: virtual DecisionTreeFactor toDecisionTreeFactor() const = 0; + /// @} + /// @name Wrapper support + /// @{ + + /// Render as markdown table. + virtual std::string _repr_markdown_( + const KeyFormatter& keyFormatter = DefaultKeyFormatter) const = 0; + /// @} }; // DiscreteFactor diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index 77127ac30..129ab4dae 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -129,6 +129,18 @@ namespace gtsam { return std::make_pair(cond, sum); } -/* ************************************************************************* */ -} // namespace + /* ************************************************************************* */ + std::string DiscreteFactorGraph::_repr_markdown_( + const KeyFormatter& keyFormatter) const { + using std::endl; + std::stringstream ss; + ss << "`DiscreteFactorGraph` of size " << size() << endl << endl; + for (size_t i = 0; i < factors_.size(); i++) { + ss << "factor " << i << ":\n"; + ss << factors_[i]->_repr_markdown_(keyFormatter) << endl; + } + return ss.str(); + } + /* ************************************************************************* */ + } // namespace gtsam diff --git a/gtsam/discrete/DiscreteFactorGraph.h b/gtsam/discrete/DiscreteFactorGraph.h index ff0aaef19..616d7c7d2 100644 --- a/gtsam/discrete/DiscreteFactorGraph.h +++ b/gtsam/discrete/DiscreteFactorGraph.h @@ -154,6 +154,14 @@ public: // /** Apply a reduction, which is a remapping of variable indices. */ // GTSAM_EXPORT void reduceWithInverse(const internal::Reduction& inverseReduction); + /// @name Wrapper support + /// @{ + + /// Render as markdown table. + std::string _repr_markdown_( + const KeyFormatter& keyFormatter = DefaultKeyFormatter) const; + + /// @} }; // \ DiscreteFactorGraph /// traits diff --git a/gtsam/discrete/discrete.i b/gtsam/discrete/discrete.i index 19d1b8cd9..07334cf9b 100644 --- a/gtsam/discrete/discrete.i +++ b/gtsam/discrete/discrete.i @@ -166,6 +166,9 @@ class DiscreteFactorGraph { gtsam::DiscreteBayesNet eliminateSequential(const gtsam::Ordering& ordering); gtsam::DiscreteBayesTree eliminateMultifrontal(); gtsam::DiscreteBayesTree eliminateMultifrontal(const gtsam::Ordering& ordering); + + string _repr_markdown_(const gtsam::KeyFormatter& keyFormatter = + gtsam::DefaultKeyFormatter) const; }; } // namespace gtsam diff --git a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp index 32117bd25..f1fd26af4 100644 --- a/gtsam/discrete/tests/testDiscreteFactorGraph.cpp +++ b/gtsam/discrete/tests/testDiscreteFactorGraph.cpp @@ -361,11 +361,9 @@ cout << unicorns; /* ************************************************************************* */ TEST(DiscreteFactorGraph, Dot) { - // Declare a bunch of keys - DiscreteKey C(0, 2), A(1, 2), B(2, 2); - // Create Factor graph DiscreteFactorGraph graph; + DiscreteKey C(0, 2), A(1, 2), B(2, 2); graph.add(C & A, "0.2 0.8 0.3 0.7"); graph.add(C & B, "0.1 0.9 0.4 0.6"); @@ -384,6 +382,44 @@ TEST(DiscreteFactorGraph, Dot) { EXPECT(actual == expected); } +/* ************************************************************************* */ +// Check markdown representation looks as expected. +TEST(DiscreteFactorGraph, markdown) { + // Create Factor graph + DiscreteFactorGraph graph; + DiscreteKey C(0, 2), A(1, 2), B(2, 2); + graph.add(C & A, "0.2 0.8 0.3 0.7"); + graph.add(C & B, "0.1 0.9 0.4 0.6"); + + string expected = + "`DiscreteFactorGraph` of size 2\n" + "\n" + "factor 0:\n" + "|C|A|value|\n" + "|:-:|:-:|:-:|\n" + "|0|0|0.2|\n" + "|0|1|0.8|\n" + "|1|0|0.3|\n" + "|1|1|0.7|\n" + "\n" + "factor 1:\n" + "|C|B|value|\n" + "|:-:|:-:|:-:|\n" + "|0|0|0.1|\n" + "|0|1|0.9|\n" + "|1|0|0.4|\n" + "|1|1|0.6|\n\n"; + vector names{"C", "A", "B"}; + auto formatter = [names](Key key) { return names[key]; }; + string actual = graph._repr_markdown_(formatter); + EXPECT(actual == expected); + + // Make sure values are correctly displayed. + DiscreteValues values; + values[0] = 1; + values[1] = 0; + EXPECT_DOUBLES_EQUAL(0.3, graph[0]->operator()(values), 1e-9); +} /* ************************************************************************* */ int main() { TestResult tr; diff --git a/gtsam_unstable/discrete/Constraint.h b/gtsam_unstable/discrete/Constraint.h index 0a05bbfd2..e772c54df 100644 --- a/gtsam_unstable/discrete/Constraint.h +++ b/gtsam_unstable/discrete/Constraint.h @@ -22,6 +22,7 @@ #include #include +#include #include namespace gtsam { @@ -81,6 +82,16 @@ class GTSAM_EXPORT Constraint : public DiscreteFactor { /// Partially apply known values, domain version virtual shared_ptr partiallyApply(const Domains&) const = 0; /// @} + /// @name Wrapper support + /// @{ + + /// Render as markdown table. + std::string _repr_markdown_( + const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override { + return (boost::format("`Constraint` on %1% variables\n") % (size())).str(); + } + + /// @} }; // DiscreteFactor