From 3ea5aed26e3e5d220e1de7be5d8e738940649304 Mon Sep 17 00:00:00 2001 From: Frank Dellaert Date: Sun, 9 Jan 2022 11:42:56 -0500 Subject: [PATCH] html for all graphs --- gtsam/discrete/DiscreteBayesNet.cpp | 17 +++++++++++++++-- gtsam/discrete/DiscreteBayesNet.h | 21 ++++++++++++++------- gtsam/discrete/DiscreteBayesTree.cpp | 18 ++++++++++++++++++ gtsam/discrete/DiscreteBayesTree.h | 6 +++++- gtsam/discrete/DiscreteConditional.cpp | 5 ++++- gtsam/discrete/DiscreteFactorGraph.cpp | 17 +++++++++++++++-- gtsam/discrete/DiscreteFactorGraph.h | 17 +++++++++++++---- gtsam/discrete/discrete.i | 12 ++++++++++++ 8 files changed, 96 insertions(+), 17 deletions(-) diff --git a/gtsam/discrete/DiscreteBayesNet.cpp b/gtsam/discrete/DiscreteBayesNet.cpp index 510fb5638..c0dfd747c 100644 --- a/gtsam/discrete/DiscreteBayesNet.cpp +++ b/gtsam/discrete/DiscreteBayesNet.cpp @@ -61,16 +61,29 @@ namespace gtsam { return result; } - /* ************************************************************************* */ + /* *********************************************************************** */ std::string DiscreteBayesNet::markdown( const KeyFormatter& keyFormatter, const DiscreteFactor::Names& names) const { using std::endl; std::stringstream ss; ss << "`DiscreteBayesNet` of size " << size() << endl << endl; - for(const DiscreteConditional::shared_ptr& conditional: *this) + for (const DiscreteConditional::shared_ptr& conditional : *this) ss << conditional->markdown(keyFormatter, names) << endl; return ss.str(); } + + /* *********************************************************************** */ + std::string DiscreteBayesNet::html( + const KeyFormatter& keyFormatter, + const DiscreteFactor::Names& names) const { + using std::endl; + std::stringstream ss; + ss << "

DiscreteBayesNet of size " << size() << "

"; + for (const DiscreteConditional::shared_ptr& conditional : *this) + ss << conditional->html(keyFormatter, names) << endl; + return ss.str(); + } + /* ************************************************************************* */ } // namespace diff --git a/gtsam/discrete/DiscreteBayesNet.h b/gtsam/discrete/DiscreteBayesNet.h index 5332b51dd..17dfe2c5f 100644 --- a/gtsam/discrete/DiscreteBayesNet.h +++ b/gtsam/discrete/DiscreteBayesNet.h @@ -18,13 +18,16 @@ #pragma once -#include -#include -#include +#include +#include #include #include -#include -#include + +#include +#include +#include +#include +#include namespace gtsam { @@ -107,13 +110,17 @@ namespace gtsam { /// @name Wrapper support /// @{ - /// Render as markdown table. + /// Render as markdown tables. std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter, const DiscreteFactor::Names& names = {}) const; + /// Render as html tables. + std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter, + const DiscreteFactor::Names& names = {}) const; + /// @} - private: + private: /** Serialization function */ friend class boost::serialization::access; template diff --git a/gtsam/discrete/DiscreteBayesTree.cpp b/gtsam/discrete/DiscreteBayesTree.cpp index 07d6e0f0e..139292eee 100644 --- a/gtsam/discrete/DiscreteBayesTree.cpp +++ b/gtsam/discrete/DiscreteBayesTree.cpp @@ -72,5 +72,23 @@ namespace gtsam { return ss.str(); } + /* **************************************************************************/ + std::string DiscreteBayesTree::html( + const KeyFormatter& keyFormatter, + const DiscreteFactor::Names& names) const { + using std::endl; + std::stringstream ss; + ss << "

DiscreteBayesTree of size " << nodes_.size() + << "

"; + auto visitor = [&](const DiscreteBayesTreeClique::shared_ptr& clique, + size_t& indent) { + ss << clique->conditional()->html(keyFormatter, names); + return indent + 1; + }; + size_t indent; + treeTraversal::DepthFirstForest(*this, indent, visitor); + return ss.str(); + } + /* **************************************************************************/ } // namespace gtsam diff --git a/gtsam/discrete/DiscreteBayesTree.h b/gtsam/discrete/DiscreteBayesTree.h index 6189f25d5..809ce9c83 100644 --- a/gtsam/discrete/DiscreteBayesTree.h +++ b/gtsam/discrete/DiscreteBayesTree.h @@ -92,10 +92,14 @@ class GTSAM_EXPORT DiscreteBayesTree /// @name Wrapper support /// @{ - /// Render as markdown table. + /// Render as markdown tables. std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter, const DiscreteFactor::Names& names = {}) const; + /// Render as html tables. + std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter, + const DiscreteFactor::Names& names = {}) const; + /// @} }; diff --git a/gtsam/discrete/DiscreteConditional.cpp b/gtsam/discrete/DiscreteConditional.cpp index 48f8fd322..512ff88b4 100644 --- a/gtsam/discrete/DiscreteConditional.cpp +++ b/gtsam/discrete/DiscreteConditional.cpp @@ -410,16 +410,19 @@ string DiscreteConditional::html(const KeyFormatter& keyFormatter, } auto frontalAssignments = this->frontalAssignments(); for (const auto& a : frontalAssignments) { + ss << ""; for (auto&& it = beginFrontals(); it != endFrontals(); ++it) { size_t index = a.at(*it); - ss << "" << Translate(names, *it, index) << ""; + ss << Translate(names, *it, index); } + ss << ""; } ss << "\n"; // Finish header and start body. ss << " \n \n"; + // Output all rows, one per assignment: size_t count = 0, n = frontalAssignments.size(); for (const auto& a : allAssignments()) { if (count == 0) { diff --git a/gtsam/discrete/DiscreteFactorGraph.cpp b/gtsam/discrete/DiscreteFactorGraph.cpp index be046d290..c1248c60b 100644 --- a/gtsam/discrete/DiscreteFactorGraph.cpp +++ b/gtsam/discrete/DiscreteFactorGraph.cpp @@ -131,7 +131,7 @@ namespace gtsam { return std::make_pair(cond, sum); } - /* ************************************************************************* */ + /* ************************************************************************ */ string DiscreteFactorGraph::markdown( const KeyFormatter& keyFormatter, const DiscreteFactor::Names& names) const { @@ -145,5 +145,18 @@ namespace gtsam { return ss.str(); } - /* ************************************************************************* */ + /* ************************************************************************ */ + string DiscreteFactorGraph::html(const KeyFormatter& keyFormatter, + const DiscreteFactor::Names& names) const { + using std::endl; + std::stringstream ss; + ss << "

DiscreteFactorGraph of size " << size() << "

"; + for (size_t i = 0; i < factors_.size(); i++) { + ss << "

factor " << i << ":

"; + ss << factors_[i]->html(keyFormatter, names) << endl; + } + return ss.str(); + } + + /* ************************************************************************ */ } // namespace gtsam diff --git a/gtsam/discrete/DiscreteFactorGraph.h b/gtsam/discrete/DiscreteFactorGraph.h index 9aa04d649..08c3d893d 100644 --- a/gtsam/discrete/DiscreteFactorGraph.h +++ b/gtsam/discrete/DiscreteFactorGraph.h @@ -22,18 +22,17 @@ #include #include #include -#include #include #include #include +#include #include namespace gtsam { // Forward declarations class DiscreteFactorGraph; -class DiscreteFactor; class DiscreteConditional; class DiscreteBayesNet; class DiscreteEliminationTree; @@ -144,8 +143,8 @@ public: /// @{ /** - * @brief Render as markdown table - * + * @brief Render as markdown tables + * * @param keyFormatter GTSAM-style Key formatter. * @param names optional, a map from Key to category names. * @return std::string a (potentially long) markdown string. @@ -153,6 +152,16 @@ public: std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter, const DiscreteFactor::Names& names = {}) const; + /** + * @brief Render as html tables + * + * @param keyFormatter GTSAM-style Key formatter. + * @param names optional, a map from Key to category names. + * @return std::string a (potentially long) html string. + */ + std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter, + const DiscreteFactor::Names& names = {}) const; + /// @} }; // \ DiscreteFactorGraph diff --git a/gtsam/discrete/discrete.i b/gtsam/discrete/discrete.i index 3e8013ce8..9bf324fd3 100644 --- a/gtsam/discrete/discrete.i +++ b/gtsam/discrete/discrete.i @@ -144,6 +144,10 @@ class DiscreteBayesNet { gtsam::DefaultKeyFormatter) const; string markdown(const gtsam::KeyFormatter& keyFormatter, std::map> names) const; + string html(const gtsam::KeyFormatter& keyFormatter = + gtsam::DefaultKeyFormatter) const; + string html(const gtsam::KeyFormatter& keyFormatter, + std::map> names) const; }; #include @@ -180,6 +184,10 @@ class DiscreteBayesTree { gtsam::DefaultKeyFormatter) const; string markdown(const gtsam::KeyFormatter& keyFormatter, std::map> names) const; + string html(const gtsam::KeyFormatter& keyFormatter = + gtsam::DefaultKeyFormatter) const; + string html(const gtsam::KeyFormatter& keyFormatter, + std::map> names) const; }; #include @@ -229,6 +237,10 @@ class DiscreteFactorGraph { gtsam::DefaultKeyFormatter) const; string markdown(const gtsam::KeyFormatter& keyFormatter, std::map> names) const; + string html(const gtsam::KeyFormatter& keyFormatter = + gtsam::DefaultKeyFormatter) const; + string html(const gtsam::KeyFormatter& keyFormatter, + std::map> names) const; }; } // namespace gtsam