From 80d335ed774fbe62b218e7454e9528f89cbb7b1f Mon Sep 17 00:00:00 2001 From: Manohar Paluri Date: Fri, 22 Jan 2010 23:52:56 +0000 Subject: [PATCH] added saveGraph function to BayesTree which writes out a dot file which can be visualized using graphviz dot command --- cpp/BayesTree-inl.h | 43 +++++++++++++++++++++++++++++++++++++++++++ cpp/BayesTree.h | 8 ++++++++ 2 files changed, 51 insertions(+) diff --git a/cpp/BayesTree-inl.h b/cpp/BayesTree-inl.h index 4572858ca..db4ed8e3d 100644 --- a/cpp/BayesTree-inl.h +++ b/cpp/BayesTree-inl.h @@ -10,12 +10,14 @@ #include #include // for operator += +#include using namespace boost::assign; #include "Conditional.h" #include "BayesTree.h" #include "Ordering.h" #include "inference-inl.h" +#include "Key.h" namespace gtsam { @@ -86,6 +88,47 @@ namespace gtsam { } } + /* ************************************************************************* */ + template + void BayesTree::saveGraph(const std::string &s) const { + ofstream of(s.c_str()); + of<< "digraph G{\n"; + saveGraph(of, root_); + of<<"}"; + of.close(); + } + + template + void BayesTree::saveGraph(ostream &s, + BayesTree::sharedClique clique, + int parentnum, int num) const { + bool first = true; + std::stringstream out; + out << num; + string parent = out.str(); + parent += "[label=\""; + BOOST_FOREACH(boost::shared_ptr c, clique->conditionals_) { + if(!first) parent += ","; first = false; + parent += (string(c->key())).c_str(); + } + if( clique != root_){ + parent += " : "; + s << parentnum << "->" << num << "\n"; + } + first = true; + BOOST_FOREACH(const Symbol& sep, clique->separator_) { + if(!first) parent += ","; first = false; + parent += ((string)sep).c_str(); + } + parent += "\"];\n"; + s << parent; + parentnum = num; + BOOST_FOREACH(sharedClique c, clique->children_) { + saveGraph(s, c, parentnum, ++num); + } + } + + template typename BayesTree::CliqueStats BayesTree::CliqueData::getStats() const { diff --git a/cpp/BayesTree.h b/cpp/BayesTree.h index 8745ccac5..ba0f2adc2 100644 --- a/cpp/BayesTree.h +++ b/cpp/BayesTree.h @@ -127,6 +127,13 @@ namespace gtsam { /** print */ void print(const std::string& s = "") const; + /** saves the Tree to a text file in GraphViz format */ + void saveGraph(const std::string& s) const; + private: + void saveGraph(std::ostream &s, sharedClique clique, + int parentnum = 0, int num = 0) const; + public: + /** check equality */ bool equals(const BayesTree& other, double tol = 1e-9) const; @@ -170,6 +177,7 @@ namespace gtsam { CliqueStats getStats() const; }; CliqueData getCliqueData() const; + private: void getCliqueData(CliqueData& stats, sharedClique clique) const; public: