Added more flexible print interface with optional key formatter

release/4.3a0
Alex Cunningham 2012-06-27 19:16:18 +00:00
parent b8c346b60d
commit b98f60ddb5
18 changed files with 81 additions and 47 deletions

View File

@ -63,9 +63,11 @@ namespace gtsam {
/// @{
/** GTSAM-style print */
void print(const std::string& s = "Discrete Conditional: ") const {
void print(const std::string& s = "Discrete Conditional: ",
const boost::function<std::string(Index)>& formatter
= &(boost::lexical_cast<std::string, Index>)) const {
std::cout << s << std::endl;
IndexConditional::print(s);
IndexConditional::print(s, formatter);
Potentials::print(s);
}

View File

@ -82,8 +82,10 @@ namespace gtsam {
/// @{
// print
virtual void print(const std::string& s = "DiscreteFactor\n") const {
IndexFactor::print(s);
virtual void print(const std::string& s = "DiscreteFactor\n",
const boost::function<std::string(Index)>& formatter
= &(boost::lexical_cast<std::string, Index>)) const {
IndexFactor::print(s,formatter);
}
/// @}

View File

@ -63,6 +63,18 @@ namespace gtsam {
return product;
}
/* ************************************************************************* */
void DiscreteFactorGraph::print(const std::string& s,
const IndexFormatter& formatter) const {
std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss;
ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str(), formatter);
}
}
/* ************************************************************************* */
std::pair<DiscreteConditional::shared_ptr, DecisionTreeFactor::shared_ptr> //
EliminateDiscrete(const FactorGraph<DiscreteFactor>& factors, size_t num) {
@ -90,6 +102,7 @@ namespace gtsam {
return std::make_pair(cond, sum);
}
/* ************************************************************************* */
} // namespace

View File

@ -77,15 +77,8 @@ public:
double operator()(const DiscreteFactor::Values & values) const;
/// print
void print(const std::string& s = "DiscreteFactorGraph") const {
std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss;
ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str());
}
}
void print(const std::string& s = "DiscreteFactorGraph",
const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
};
// DiscreteFactorGraph

View File

@ -34,10 +34,11 @@ namespace gtsam {
/* ************************************************************************* */
template<class CONDITIONAL>
void BayesNet<CONDITIONAL>::print(const std::string& s) const {
void BayesNet<CONDITIONAL>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s;
BOOST_REVERSE_FOREACH(sharedConditional conditional, conditionals_)
conditional->print();
conditional->print("Conditional", formatter);
}
/* ************************************************************************* */

View File

@ -23,6 +23,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/serialization/nvp.hpp>
#include <boost/assign/list_inserter.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/types.h>
#include <gtsam/base/FastList.h>
@ -48,6 +49,7 @@ public:
typedef typename boost::shared_ptr<BayesNet<CONDITIONAL> > shared_ptr;
/** We store shared pointers to Conditional densities */
typedef typename CONDITIONAL::KeyType KeyType;
typedef typename boost::shared_ptr<CONDITIONAL> sharedConditional;
typedef typename boost::shared_ptr<const CONDITIONAL> const_sharedConditional;
typedef typename std::list<sharedConditional> Conditionals;
@ -88,7 +90,8 @@ public:
/// @{
/** print */
void print(const std::string& s = "") const;
void print(const std::string& s = "",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** print statistics */
void printStats(const std::string& s = "") const;

View File

@ -71,10 +71,11 @@ namespace gtsam {
/* ************************************************************************* */
template<class FG>
void ClusterTree<FG>::Cluster::print(const std::string& indent) const {
void ClusterTree<FG>::Cluster::print(const std::string& indent,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << indent;
BOOST_FOREACH(const Index key, frontal)
std::cout << key << " ";
std::cout << formatter(key) << " ";
std::cout << ": ";
BOOST_FOREACH(const Index key, separator)
std::cout << key << " ";
@ -83,19 +84,21 @@ namespace gtsam {
/* ************************************************************************* */
template<class FG>
void ClusterTree<FG>::Cluster::printTree(const std::string& indent) const {
print(indent);
void ClusterTree<FG>::Cluster::printTree(const std::string& indent,
const boost::function<std::string(KeyType)>& formatter) const {
print(indent, formatter);
BOOST_FOREACH(const shared_ptr& child, children_)
child->printTree(indent + " ");
child->printTree(indent + " ", formatter);
}
/* ************************************************************************* *
* ClusterTree
* ************************************************************************* */
template<class FG>
void ClusterTree<FG>::print(const std::string& str) const {
void ClusterTree<FG>::print(const std::string& str,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << str << std::endl;
if (root_) root_->printTree("");
if (root_) root_->printTree("", formatter);
}
/* ************************************************************************* */

View File

@ -25,6 +25,7 @@
#include <boost/shared_ptr.hpp>
#include <boost/weak_ptr.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/types.h>
@ -38,6 +39,9 @@ namespace gtsam {
*/
template <class FG>
class ClusterTree {
public:
// Access to factor types
typedef typename FG::KeyType KeyType;
protected:
@ -74,10 +78,10 @@ namespace gtsam {
Cluster(FRONTALIT firstFrontal, FRONTALIT lastFrontal, SEPARATORIT firstSeparator, SEPARATORIT lastSeparator);
/// print
void print(const std::string& indent) const;
void print(const std::string& indent, const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// print the enire tree
void printTree(const std::string& indent) const;
void printTree(const std::string& indent, const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// check equality
bool equals(const Cluster& other) const;
@ -123,7 +127,7 @@ namespace gtsam {
/// @{
/// print the object
void print(const std::string& str="") const;
void print(const std::string& str="", const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** check equality */
bool equals(const ClusterTree<FG>& other, double tol = 1e-9) const;

View File

@ -166,12 +166,13 @@ EliminationTree<FACTOR>::Create(const FactorGraph<DERIVEDFACTOR>& factorGraph) {
/* ************************************************************************* */
template<class FACTORGRAPH>
void EliminationTree<FACTORGRAPH>::print(const std::string& name) const {
std::cout << name << " (" << key_ << ")" << std::endl;
void EliminationTree<FACTORGRAPH>::print(const std::string& name,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << name << " (" << formatter(key_) << ")" << std::endl;
BOOST_FOREACH(const sharedFactor& factor, factors_) {
factor->print(name + " "); }
factor->print(name + " ", formatter); }
BOOST_FOREACH(const shared_ptr& child, subTrees_) {
child->print(name + " "); }
child->print(name + " ", formatter); }
}
/* ************************************************************************* */

View File

@ -54,6 +54,8 @@ public:
typedef boost::shared_ptr<This> shared_ptr; ///< Shared pointer to this class
typedef typename boost::shared_ptr<FACTOR> sharedFactor; ///< Shared pointer to a factor
typedef gtsam::BayesNet<typename FACTOR::ConditionalType> BayesNet; ///< The BayesNet corresponding to FACTOR
typedef FACTOR Factor;
typedef typename FACTOR::KeyType KeyType;
/** Typedef for an eliminate subroutine */
typedef typename FactorGraph<FACTOR>::Eliminate Eliminate;
@ -67,7 +69,7 @@ private:
typedef FastList<shared_ptr> SubTrees;
typedef std::vector<typename FACTOR::ConditionalType::shared_ptr> Conditionals;
Index key_; ///< index associated with root
Index key_; ///< index associated with root // FIXME: doesn't this require that "Index" is the type of keys in the generic factor?
Factors factors_; ///< factors associated with root
SubTrees subTrees_; ///< sub-trees
@ -141,7 +143,8 @@ public:
/// @{
/** Print the tree to cout */
void print(const std::string& name = "EliminationTree: ") const;
void print(const std::string& name = "EliminationTree: ",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** Test whether the tree is equal to another */
bool equals(const EliminationTree& other, double tol = 1e-9) const;

View File

@ -56,9 +56,10 @@ namespace gtsam {
/* ************************************************************************* */
template<typename KEY>
void Factor<KEY>::print(const std::string& s) const {
void Factor<KEY>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s << " ";
BOOST_FOREACH(KEY key, keys_) std::cout << " " << key;
BOOST_FOREACH(KEY key, keys_) std::cout << " " << formatter(key);
std::cout << std::endl;
}

View File

@ -25,6 +25,8 @@
#include <vector>
#include <boost/serialization/nvp.hpp>
#include <boost/foreach.hpp>
#include <boost/function/function1.hpp>
#include <boost/lexical_cast.hpp>
#include <gtsam/base/FastMap.h>
namespace gtsam {
@ -190,7 +192,8 @@ public:
/// @{
/// print
void print(const std::string& s = "Factor") const;
void print(const std::string& s = "Factor",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/// check equality
bool equals(const This& other, double tol = 1e-9) const;

View File

@ -48,13 +48,14 @@ namespace gtsam {
/* ************************************************************************* */
template<class FACTOR>
void FactorGraph<FACTOR>::print(const std::string& s) const {
void FactorGraph<FACTOR>::print(const std::string& s,
const boost::function<std::string(KeyType)>& formatter) const {
std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl;
for (size_t i = 0; i < factors_.size(); i++) {
std::stringstream ss;
ss << "factor " << i << ": ";
if (factors_[i] != NULL) factors_[i]->print(ss.str());
if (factors_[i] != NULL) factors_[i]->print(ss.str(), formatter);
}
}

View File

@ -138,7 +138,8 @@ template<class CONDITIONAL, class CLIQUE> class BayesTree;
/// @{
/** print out graph */
void print(const std::string& s = "FactorGraph") const;
void print(const std::string& s = "FactorGraph",
const boost::function<std::string(KeyType)>& formatter = &(boost::lexical_cast<std::string, KeyType>)) const;
/** Check equality */
bool equals(const This& fg, double tol = 1e-9) const;

View File

@ -23,11 +23,11 @@ using namespace std;
namespace gtsam {
/* ************************************************************************* */
void GaussianDensity::print(const string &s) const
void GaussianDensity::print(const string &s, const IndexFormatter& formatter) const
{
cout << s << ": density on ";
for(const_iterator it = beginFrontals(); it != endFrontals(); ++it)
cout << (boost::format("[%1%]")%(*it)).str() << " ";
cout << (boost::format("[%1%]")%(formatter(*it))).str() << " ";
cout << endl;
gtsam::print(Matrix(get_R()),"R");
gtsam::print(Vector(get_d()),"d");

View File

@ -54,7 +54,8 @@ namespace gtsam {
}
/// print
void print(const std::string& = "GaussianDensity") const;
void print(const std::string& = "GaussianDensity",
const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
/// Mean \f$ \mu = R^{-1} d \f$
Vector mean() const;

View File

@ -77,10 +77,10 @@ void VectorValues::insert(Index j, const Vector& value) {
}
/* ************************************************************************* */
void VectorValues::print(const std::string& str) const {
void VectorValues::print(const std::string& str, const IndexFormatter& formatter) const {
std::cout << str << ": " << size() << " elements\n";
for (Index var = 0; var < size(); ++var)
std::cout << " " << var << ": \n" << operator[](var) << "\n";
std::cout << " " << formatter(var) << ": \n" << operator[](var) << "\n";
std::cout.flush();
}

View File

@ -21,6 +21,7 @@
#include <gtsam/base/types.h>
#include <gtsam/inference/Permutation.h>
#include <boost/lexical_cast.hpp>
#include <boost/foreach.hpp>
#include <boost/shared_ptr.hpp>
#include <numeric>
@ -167,7 +168,8 @@ namespace gtsam {
const_reverse_iterator rend() const { chk(); return maps_.rend(); } ///< Reverse iterator over variables
/** print required by Testable for unit testing */
void print(const std::string& str = "VectorValues: ") const;
void print(const std::string& str = "VectorValues: ",
const IndexFormatter& formatter = &(boost::lexical_cast<std::string, Index>)) const;
/** equals required by Testable for unit testing */
bool equals(const VectorValues& x, double tol = 1e-9) const;