Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

markdown for graphs #987

Merged
merged 4 commits into from
Dec 25, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion gtsam/discrete/DecisionTreeFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -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;

/// @}

Expand Down
12 changes: 11 additions & 1 deletion gtsam/discrete/DiscreteBayesNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ namespace gtsam {
double DiscreteBayesNet::evaluate(const DiscreteValues & values) const {
// evaluate all conditionals and multiply
double result = 1.0;
for(DiscreteConditional::shared_ptr conditional: *this)
for(const DiscreteConditional::shared_ptr& conditional: *this)
result *= (*conditional)(values);
return result;
}
Expand All @@ -61,5 +61,15 @@ namespace gtsam {
return result;
}

/* ************************************************************************* */
std::string DiscreteBayesNet::_repr_markdown_(
const KeyFormatter& keyFormatter) const {
using std::endl;
std::stringstream ss;
ss << "`DiscreteBayesNet` of size " << size() << endl << endl;
for(const DiscreteConditional::shared_ptr& conditional: *this)
ss << conditional->_repr_markdown_(keyFormatter) << endl;
return ss.str();
}
/* ************************************************************************* */
} // namespace
9 changes: 9 additions & 0 deletions gtsam/discrete/DiscreteBayesNet.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* @file DiscreteBayesNet.h
* @date Feb 15, 2011
* @author Duy-Nguyen Ta
* @author Frank dellaert
*/

#pragma once
Expand Down Expand Up @@ -97,6 +98,14 @@ namespace gtsam {
DiscreteValues sample() const;

///@}
/// @name Wrapper support
/// @{

/// Render as markdown table.
std::string _repr_markdown_(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;

/// @}

private:
/** Serialization function */
Expand Down
21 changes: 17 additions & 4 deletions gtsam/discrete/DiscreteBayesTree.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,8 +55,21 @@ namespace gtsam {
return result;
}

} // \namespace gtsam



/* **************************************************************************/
std::string DiscreteBayesTree::_repr_markdown_(
const KeyFormatter& keyFormatter) 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()->_repr_markdown_(keyFormatter);
return indent + 1;
};
size_t indent;
treeTraversal::DepthFirstForest(*this, indent, visitor);
return ss.str();
}

/* **************************************************************************/
} // namespace gtsam
13 changes: 12 additions & 1 deletion gtsam/discrete/DiscreteBayesTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,8 @@ class GTSAM_EXPORT DiscreteBayesTree
typedef DiscreteBayesTree This;
typedef boost::shared_ptr<This> shared_ptr;

/// @name Standard interface
/// @{
/** Default constructor, creates an empty Bayes tree */
DiscreteBayesTree() {}

Expand All @@ -82,10 +84,19 @@ class GTSAM_EXPORT DiscreteBayesTree
double evaluate(const DiscreteValues& values) const;

//** (Preferred) sugar for the above for given DiscreteValues */
double operator()(const DiscreteValues & values) const {
double operator()(const DiscreteValues& values) const {
return evaluate(values);
}

/// @}
/// @name Wrapper support
/// @{

/// Render as markdown table.
std::string _repr_markdown_(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;

/// @}
};

} // namespace gtsam
24 changes: 17 additions & 7 deletions gtsam/discrete/DiscreteConditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -229,11 +229,22 @@ std::string DiscreteConditional::_repr_markdown_(

// Print out signature.
ss << " $P(";
for(Key key: frontals())
ss << keyFormatter(key);
if (nrParents() > 0)
ss << "|";
bool first = true;
for (Key key : frontals()) {
if (!first) ss << ",";
ss << keyFormatter(key);
first = false;
}
if (nrParents() == 0) {
// We have no parents, call factor method.
ss << ")$:" << std::endl;
ss << DecisionTreeFactor::_repr_markdown_();
return ss.str();
}

// We have parents, continue signature and do custom print.
ss << "|";
first = true;
for (Key parent : parents()) {
if (!first) ss << ",";
ss << keyFormatter(parent);
Expand All @@ -256,9 +267,8 @@ std::string DiscreteConditional::_repr_markdown_(
pairs.emplace_back(key, k);
n *= k;
}
size_t nrParents = size() - nrFrontals_;
std::vector<std::pair<Key, size_t>> slatnorf(pairs.rbegin(),
pairs.rend() - nrParents);
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);
Expand All @@ -268,7 +278,7 @@ std::string DiscreteConditional::_repr_markdown_(

// Print out separator with alignment hints.
ss << "|";
for (size_t j = 0; j < nrParents + n; j++) ss << ":-:|";
for (size_t j = 0; j < nrParents() + n; j++) ss << ":-:|";
ss << "\n";

// Print out all rows.
Expand Down
2 changes: 1 addition & 1 deletion gtsam/discrete/DiscreteConditional.h
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ class GTSAM_EXPORT DiscreteConditional: public DecisionTreeFactor,

/// Render as markdown table.
std::string _repr_markdown_(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const override;

/// @}
};
Expand Down
8 changes: 8 additions & 0 deletions gtsam/discrete/DiscreteFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,14 @@ class GTSAM_EXPORT DiscreteFactor: public Factor {

virtual DecisionTreeFactor toDecisionTreeFactor() const = 0;

/// @}
/// @name Wrapper support
/// @{

/// Render as markdown table.
virtual std::string _repr_markdown_(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const = 0;

/// @}
};
// DiscreteFactor
Expand Down
16 changes: 14 additions & 2 deletions gtsam/discrete/DiscreteFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
8 changes: 8 additions & 0 deletions gtsam/discrete/DiscreteFactorGraph.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,14 @@ public EliminateableFactorGraph<DiscreteFactorGraph> {
// /** 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
Expand Down
8 changes: 8 additions & 0 deletions gtsam/discrete/discrete.i
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,8 @@ class DiscreteBayesNet {
double operator()(const gtsam::DiscreteValues& values) const;
gtsam::DiscreteValues optimize() const;
gtsam::DiscreteValues sample() const;
string _repr_markdown_(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
};

#include <gtsam/discrete/DiscreteBayesTree.h>
Expand Down Expand Up @@ -124,6 +126,9 @@ class DiscreteBayesTree {
const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
double operator()(const gtsam::DiscreteValues& values) const;

string _repr_markdown_(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
};

#include <gtsam/inference/DotWriter.h>
Expand Down Expand Up @@ -164,6 +169,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
34 changes: 29 additions & 5 deletions gtsam/discrete/tests/testDiscreteBayesNet.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ using namespace boost::assign;
using namespace std;
using namespace gtsam;

static const DiscreteKey Asia(0, 2), Smoking(4, 2), Tuberculosis(3, 2),
LungCancer(6, 2), Bronchitis(7, 2), Either(5, 2), XRay(2, 2), Dyspnea(1, 2);

/* ************************************************************************* */
TEST(DiscreteBayesNet, bayesNet) {
DiscreteBayesNet bayesNet;
Expand Down Expand Up @@ -71,8 +74,6 @@ TEST(DiscreteBayesNet, bayesNet) {
/* ************************************************************************* */
TEST(DiscreteBayesNet, Asia) {
DiscreteBayesNet asia;
DiscreteKey Asia(0, 2), Smoking(4, 2), Tuberculosis(3, 2), LungCancer(6, 2),
Bronchitis(7, 2), Either(5, 2), XRay(2, 2), Dyspnea(1, 2);

asia.add(Asia % "99/1");
asia.add(Smoking % "50/50");
Expand Down Expand Up @@ -151,9 +152,6 @@ TEST(DiscreteBayesNet, Sugar) {

/* ************************************************************************* */
TEST(DiscreteBayesNet, Dot) {
DiscreteKey Asia(0, 2), Smoking(4, 2), Tuberculosis(3, 2), LungCancer(6, 2),
Either(5, 2);

DiscreteBayesNet fragment;
fragment.add(Asia % "99/1");
fragment.add(Smoking % "50/50");
Expand All @@ -172,6 +170,32 @@ TEST(DiscreteBayesNet, Dot) {
"}");
}

/* ************************************************************************* */
// Check markdown representation looks as expected.
TEST(DiscreteBayesNet, markdown) {
DiscreteBayesNet fragment;
fragment.add(Asia % "99/1");
fragment.add(Smoking | Asia = "8/2 7/3");

string expected =
"`DiscreteBayesNet` of size 2\n"
"\n"
" $P(Asia)$:\n"
"|0|value|\n"
"|:-:|:-:|\n"
"|0|0.99|\n"
"|1|0.01|\n"
"\n"
" $P(Smoking|Asia)$:\n"
"|Asia|0|1|\n"
"|:-:|:-:|:-:|\n"
"|0|0.8|0.2|\n"
"|1|0.7|0.3|\n\n";
auto formatter = [](Key key) { return key == 0 ? "Asia" : "Smoking"; };
string actual = fragment._repr_markdown_(formatter);
EXPECT(actual == expected);
}

/* ************************************************************************* */
int main() {
TestResult tr;
Expand Down
10 changes: 6 additions & 4 deletions gtsam/discrete/tests/testDiscreteConditional.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -110,13 +110,15 @@ TEST(DiscreteConditional, Combine) {
/* ************************************************************************* */
// Check markdown representation looks as expected, no parents.
TEST(DiscreteConditional, markdown_prior) {
DiscreteKey A(Symbol('x', 1), 2);
DiscreteConditional conditional(A % "1/3");
DiscreteKey A(Symbol('x', 1), 3);
DiscreteConditional conditional(A % "1/2/2");
string expected =
" $P(x1)$:\n"
"|0|1|\n"
"|x1|value|\n"
"|:-:|:-:|\n"
"|0.25|0.75|\n";
"|0|0.2|\n"
"|1|0.4|\n"
"|2|0.4|\n";
string actual = conditional._repr_markdown_();
EXPECT(actual == expected);
}
Expand Down
42 changes: 39 additions & 3 deletions gtsam/discrete/tests/testDiscreteFactorGraph.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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");

Expand All @@ -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<string> 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;
Expand Down
Loading