Skip to content

Commit

Permalink
Merge pull request #1001 from borglab/feature/markdown_values
Browse files Browse the repository at this point in the history
  • Loading branch information
dellaert committed Jan 4, 2022
2 parents 1574227 + 9d6f9f6 commit 53b4053
Show file tree
Hide file tree
Showing 24 changed files with 172 additions and 62 deletions.
11 changes: 7 additions & 4 deletions gtsam/discrete/DecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -179,9 +179,9 @@ namespace gtsam {
}

/* ************************************************************************* */
std::string DecisionTreeFactor::markdown(
const KeyFormatter& keyFormatter) const {
std::stringstream ss;
string DecisionTreeFactor::markdown(const KeyFormatter& keyFormatter,
const Names& names) const {
stringstream ss;

// Print out header and construct argument for `cartesianProduct`.
ss << "|";
Expand All @@ -200,7 +200,10 @@ namespace gtsam {
for (const auto& kv : rows) {
ss << "|";
auto assignment = kv.first;
for (auto& key : keys()) ss << assignment.at(key) << "|";
for (auto& key : keys()) {
size_t index = assignment.at(key);
ss << Translate(names, key, index) << "|";
}
ss << kv.second << "|\n";
}
return ss.str();
Expand Down
12 changes: 9 additions & 3 deletions gtsam/discrete/DecisionTreeFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -192,9 +192,15 @@ namespace gtsam {
std::string dot(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
bool showZero = true) const;

/// Render as markdown table.
std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override;
/**
* @brief Render as markdown table
*
* @param keyFormatter GTSAM-style Key formatter.
* @param names optional, category names corresponding to choices.
* @return std::string a markdown string.
*/
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;

/// @}

Expand Down
5 changes: 3 additions & 2 deletions gtsam/discrete/DiscreteBayesNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,13 @@ namespace gtsam {

/* ************************************************************************* */
std::string DiscreteBayesNet::markdown(
const KeyFormatter& keyFormatter) const {
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)
ss << conditional->markdown(keyFormatter) << endl;
ss << conditional->markdown(keyFormatter, names) << endl;
return ss.str();
}
/* ************************************************************************* */
Expand Down
4 changes: 2 additions & 2 deletions gtsam/discrete/DiscreteBayesNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,8 +108,8 @@ namespace gtsam {
/// @{

/// Render as markdown table.
std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const DiscreteFactor::Names& names = {}) const;

/// @}

Expand Down
5 changes: 3 additions & 2 deletions gtsam/discrete/DiscreteBayesTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -57,13 +57,14 @@ namespace gtsam {

/* **************************************************************************/
std::string DiscreteBayesTree::markdown(
const KeyFormatter& keyFormatter) const {
const KeyFormatter& keyFormatter,
const DiscreteFactor::Names& names) const {
using std::endl;
std::stringstream ss;
ss << "`DiscreteBayesTree` of size " << nodes_.size() << endl << endl;
auto visitor = [&](const DiscreteBayesTreeClique::shared_ptr& clique,
size_t& indent) {
ss << "\n" << clique->conditional()->markdown(keyFormatter);
ss << "\n" << clique->conditional()->markdown(keyFormatter, names);
return indent + 1;
};
size_t indent;
Expand Down
4 changes: 2 additions & 2 deletions gtsam/discrete/DiscreteBayesTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,8 +93,8 @@ class GTSAM_EXPORT DiscreteBayesTree
/// @{

/// Render as markdown table.
std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const DiscreteFactor::Names& names = {}) const;

/// @}
};
Expand Down
26 changes: 20 additions & 6 deletions gtsam/discrete/DiscreteConditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -282,9 +282,18 @@ size_t DiscreteConditional::sample(size_t parent_value) const {
return sample(values);
}

/* ******************************************************************************** */
size_t DiscreteConditional::sample() const {
if (nrParents() != 0)
throw std::invalid_argument(
"sample() can only be invoked on no-parent prior");
DiscreteValues values;
return sample(values);
}

/* ************************************************************************* */
std::string DiscreteConditional::markdown(
const KeyFormatter& keyFormatter) const {
std::string DiscreteConditional::markdown(const KeyFormatter& keyFormatter,
const Names& names) const {
std::stringstream ss;

// Print out signature.
Expand Down Expand Up @@ -317,7 +326,7 @@ std::string DiscreteConditional::markdown(
ss << "|";
const_iterator it;
for(Key parent: parents()) {
ss << keyFormatter(parent) << "|";
ss << "*" << keyFormatter(parent) << "*|";
pairs.emplace_back(parent, cardinalities_.at(parent));
}

Expand All @@ -331,7 +340,10 @@ std::string DiscreteConditional::markdown(
pairs.rend() - nrParents());
const auto frontal_assignments = cartesianProduct(slatnorf);
for (const auto& a : frontal_assignments) {
for (it = beginFrontals(); it != endFrontals(); ++it) ss << a.at(*it);
for (it = beginFrontals(); it != endFrontals(); ++it) {
size_t index = a.at(*it);
ss << Translate(names, *it, index);
}
ss << "|";
}
ss << "\n";
Expand All @@ -348,8 +360,10 @@ std::string DiscreteConditional::markdown(
for (const auto& a : assignments) {
if (count == 0) {
ss << "|";
for (it = beginParents(); it != endParents(); ++it)
ss << a.at(*it) << "|";
for (it = beginParents(); it != endParents(); ++it) {
size_t index = a.at(*it);
ss << Translate(names, *it, index) << "|";
}
}
ss << operator()(a) << "|";
count = (count + 1) % n;
Expand Down
9 changes: 6 additions & 3 deletions gtsam/discrete/DiscreteConditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -162,9 +162,12 @@ class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
size_t sample(const DiscreteValues& parentsValues) const;


/// Single value version.
/// Single parent version.
size_t sample(size_t parent_value) const;

/// Zero parent version.
size_t sample() const;

/// @}
/// @name Advanced Interface
/// @{
Expand All @@ -180,8 +183,8 @@ class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,
/// @{

/// Render as markdown table.
std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override;
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;

/// @}
};
Expand Down
15 changes: 13 additions & 2 deletions gtsam/discrete/DiscreteFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,20 @@

#include <gtsam/discrete/DiscreteFactor.h>

#include <sstream>

using namespace std;

namespace gtsam {

/* ************************************************************************* */
} // namespace gtsam
string DiscreteFactor::Translate(const Names& names, Key key, size_t index) {
if (names.empty()) {
stringstream ss;
ss << index;
return ss.str();
} else {
return names.at(key)[index];
}
}

} // namespace gtsam
17 changes: 15 additions & 2 deletions gtsam/discrete/DiscreteFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -89,9 +89,22 @@ class GTSAM_EXPORT DiscreteFactor: public Factor {
/// @name Wrapper support
/// @{

/// Render as markdown table.
/// Translation table from values to strings.
using Names = std::map<Key, std::vector<std::string>>;

/// Translate an integer index value for given key to a string.
static std::string Translate(const Names& names, Key key, size_t index);

/**
* @brief Render as markdown table
*
* @param keyFormatter GTSAM-style Key formatter.
* @param names optional, category names corresponding to choices.
* @return std::string a markdown string.
*/
virtual std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const = 0;
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const = 0;

/// @}
};
Expand Down
21 changes: 12 additions & 9 deletions gtsam/discrete/DiscreteFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,17 @@
* @author Frank Dellaert
*/

//#define ENABLE_TIMING
#include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteConditional.h>
#include <gtsam/discrete/DiscreteBayesTree.h>
#include <gtsam/discrete/DiscreteConditional.h>
#include <gtsam/discrete/DiscreteEliminationTree.h>
#include <gtsam/discrete/DiscreteFactorGraph.h>
#include <gtsam/discrete/DiscreteJunctionTree.h>
#include <gtsam/inference/FactorGraph-inst.h>
#include <gtsam/inference/EliminateableFactorGraph-inst.h>
#include <boost/make_shared.hpp>
#include <gtsam/inference/FactorGraph-inst.h>

using std::vector;
using std::string;
using std::map;

namespace gtsam {

Expand Down Expand Up @@ -64,7 +66,7 @@ namespace gtsam {
}

/* ************************************************************************* */
void DiscreteFactorGraph::print(const std::string& s,
void DiscreteFactorGraph::print(const string& s,
const KeyFormatter& formatter) const {
std::cout << s << std::endl;
std::cout << "size: " << size() << std::endl;
Expand Down Expand Up @@ -130,14 +132,15 @@ namespace gtsam {
}

/* ************************************************************************* */
std::string DiscreteFactorGraph::markdown(
const KeyFormatter& keyFormatter) const {
string DiscreteFactorGraph::markdown(
const KeyFormatter& keyFormatter,
const DiscreteFactor::Names& names) 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]->markdown(keyFormatter) << endl;
ss << factors_[i]->markdown(keyFormatter, names) << endl;
}
return ss.str();
}
Expand Down
15 changes: 12 additions & 3 deletions gtsam/discrete/DiscreteFactorGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,10 @@
#include <gtsam/discrete/DecisionTreeFactor.h>
#include <gtsam/discrete/DiscreteBayesNet.h>
#include <gtsam/base/FastSet.h>

#include <boost/make_shared.hpp>
#include <string>
#include <vector>

namespace gtsam {

Expand Down Expand Up @@ -140,9 +143,15 @@ public EliminateableFactorGraph<DiscreteFactorGraph> {
/// @name Wrapper support
/// @{

/// Render as markdown table.
std::string markdown(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/**
* @brief Render as markdown table
*
* @param keyFormatter GTSAM-style Key formatter.
* @param names optional, a map from Key to category names.
* @return std::string a (potentially long) markdown string.
*/
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const DiscreteFactor::Names& names = {}) const;

/// @}
}; // \ DiscreteFactorGraph
Expand Down
2 changes: 1 addition & 1 deletion gtsam/discrete/DiscretePrior.h
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,7 @@ class GTSAM_EXPORT DiscretePrior : public DiscreteConditional {
* sample
* @return sample from conditional
*/
size_t sample() const { return Base::sample({}); }
size_t sample() const { return Base::sample(); }

/// @}
};
Expand Down
12 changes: 11 additions & 1 deletion gtsam/discrete/discrete.i
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,8 @@ virtual class DecisionTreeFactor : gtsam::DiscreteFactor {
std::vector<std::pair<DiscreteValues, double>> enumerate() const;
string markdown(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/discrete/DiscreteConditional.h>
Expand Down Expand Up @@ -84,10 +86,13 @@ virtual class DiscreteConditional : gtsam::DecisionTreeFactor {
size_t solve(const gtsam::DiscreteValues& parentsValues) const;
size_t sample(const gtsam::DiscreteValues& parentsValues) const;
size_t sample(size_t value) const;
size_t sample() const;
void solveInPlace(gtsam::DiscreteValues @parentsValues) const;
void sampleInPlace(gtsam::DiscreteValues @parentsValues) const;
string markdown(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/discrete/DiscretePrior.h>
Expand All @@ -101,7 +106,6 @@ virtual class DiscretePrior : gtsam::DiscreteConditional {
double operator()(size_t value) const;
std::vector<double> pmf() const;
size_t solve() const;
size_t sample() const;
};

#include <gtsam/discrete/DiscreteBayesNet.h>
Expand Down Expand Up @@ -130,6 +134,8 @@ class DiscreteBayesNet {
gtsam::DiscreteValues sample() const;
string markdown(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/discrete/DiscreteBayesTree.h>
Expand Down Expand Up @@ -164,6 +170,8 @@ class DiscreteBayesTree {

string markdown(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/inference/DotWriter.h>
Expand Down Expand Up @@ -211,6 +219,8 @@ class DiscreteFactorGraph {

string markdown(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

} // namespace gtsam
Loading

0 comments on commit 53b4053

Please sign in to comment.