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

html renderers in discrete #1022

Merged
merged 10 commits into from
Jan 10, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Next Next commit
Initial attempt at html
  • Loading branch information
dellaert committed Jan 9, 2022
commit 9087d3d81b12ce0557198cbdbdd89e1674e58a94
55 changes: 54 additions & 1 deletion gtsam/discrete/DecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,13 @@ namespace gtsam {
return ADT::dot(keyFormatter, valueFormatter, showZero);
}

// Print out header.
/* ************************************************************************* */
string DecisionTreeFactor::markdown(const KeyFormatter& keyFormatter,
const Names& names) const {
stringstream ss;

// Print out header and construct argument for `cartesianProduct`.
// Print out header.
ss << "|";
for (auto& key : keys()) {
ss << keyFormatter(key) << "|";
Expand All @@ -218,11 +219,63 @@ namespace gtsam {
return ss.str();
}

/* ************************************************************************ */
string DecisionTreeFactor::html(const KeyFormatter& keyFormatter,
const Names& names) const {
const string style =
"<style scoped=\'\'>\n"
" .dataframe tbody tr th:only-of-type {\n"
" vertical-align: middle;\n"
" }\n"
" .dataframe tbody tr th {\n"
" vertical-align: top;\n"
" }\n"
" .dataframe thead th {\n"
" text-align: right;\n"
" }\n"
"</style>\n";

stringstream ss;

// Print out preamble.
ss << "<div>\n"
<< style
<< "<table border=\'1\' class=\'dataframe\'>\n"
" <thead>\n";

// Print out header row.
ss << " <tr style=\'text-align: right;\'>";
for (auto& key : keys()) {
ss << "<th>" << keyFormatter(key) << "</th>";
}
ss << "<th>value</th></tr>\n";

// Finish header and start body.
ss << " </thead>\n <tbody>\n";

// Print out all rows.
auto rows = enumerate();
for (const auto& kv : rows) {
ss << " <tr>";
auto assignment = kv.first;
for (auto& key : keys()) {
size_t index = assignment.at(key);
ss << "<th>" << Translate(names, key, index) << "</th>";
}
ss << "<td>" << kv.second << "</td>"; // value
ss << "</tr>\n";
}
ss << " </tbody>\n</table>\n</div>";
return ss.str();
}

/* ************************************************************************* */
DecisionTreeFactor::DecisionTreeFactor(const DiscreteKeys &keys, const vector<double> &table) :
DiscreteFactor(keys.indices()), AlgebraicDecisionTree<Key>(keys, table),
cardinalities_(keys.cardinalities()) {
}

/* ************************************************************************* */
DecisionTreeFactor::DecisionTreeFactor(const DiscreteKeys &keys, const string &table) :
DiscreteFactor(keys.indices()), AlgebraicDecisionTree<Key>(keys, table),
cardinalities_(keys.cardinalities()) {
Expand Down
10 changes: 10 additions & 0 deletions gtsam/discrete/DecisionTreeFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -211,6 +211,16 @@ namespace gtsam {
std::string markdown(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override;

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

/// @}

};
Expand Down
11 changes: 11 additions & 0 deletions gtsam/discrete/DiscreteFactor.h
Original file line number Diff line number Diff line change
Expand Up @@ -106,6 +106,17 @@ class GTSAM_EXPORT DiscreteFactor: public Factor {
const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const = 0;

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

/// @}
};
// DiscreteFactor
Expand Down
8 changes: 8 additions & 0 deletions gtsam/discrete/discrete.i
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,10 @@ virtual class DecisionTreeFactor : gtsam::DiscreteFactor {
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
string html(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string html(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/discrete/DiscreteConditional.h>
Expand Down Expand Up @@ -93,6 +97,10 @@ virtual class DiscreteConditional : gtsam::DecisionTreeFactor {
gtsam::DefaultKeyFormatter) const;
string markdown(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
string html(const gtsam::KeyFormatter& keyFormatter =
gtsam::DefaultKeyFormatter) const;
string html(const gtsam::KeyFormatter& keyFormatter,
std::map<gtsam::Key, std::vector<std::string>> names) const;
};

#include <gtsam/discrete/DiscretePrior.h>
Expand Down
45 changes: 45 additions & 0 deletions gtsam/discrete/tests/testDecisionTreeFactor.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,51 @@ TEST(DecisionTreeFactor, markdownWithValueFormatter) {
EXPECT(actual == expected);
}

/* ************************************************************************* */
// Check html representation with a value formatter.
TEST(DecisionTreeFactor, htmlWithValueFormatter) {
DiscreteKey A(12, 3), B(5, 2);
DecisionTreeFactor f(A & B, "1 2 3 4 5 6");
string expected =
"<div>\n"
"<style scoped=''>\n"
" .dataframe tbody tr th:only-of-type {\n"
" vertical-align: middle;\n"
" }\n"
" .dataframe tbody tr th {\n"
" vertical-align: top;\n"
" }\n"
" .dataframe thead th {\n"
" text-align: right;\n"
" }\n"
"</style>\n"
"<table border='1' class='dataframe'>\n"
" <thead>\n"
" <tr style='text-align: "
"right;'><th>A</th><th>B</th><th>value</th></tr>\n"
" </thead>\n"
" <tbody>\n"
" <tr><th>Zero</th><th>-</th><td>1</td></tr>\n"
" <tr><th>Zero</th><th>+</th><td>2</td></tr>\n"
" <tr><th>One</th><th>-</th><td>3</td></tr>\n"
" <tr><th>One</th><th>+</th><td>4</td></tr>\n"
" <tr><th>Two</th><th>-</th><td>5</td></tr>\n"
" <tr><th>Two</th><th>+</th><td>6</td></tr>\n"
" </tbody>\n"
"</table>\n"
"</div>";
auto keyFormatter = [](Key key) { return key == 12 ? "A" : "B"; };
DecisionTreeFactor::Names names{{12, {"Zero", "One", "Two"}},
{5, {"-", "+"}}};
string actual = f.html(keyFormatter, names);
cout << expected << endl;
cout << actual << endl;
ofstream ef("expected=html.txt"), af("actual-html.txt");
ef << expected << endl;
af << actual << endl;
EXPECT(actual == expected);
}

/* ************************************************************************* */
int main() {
TestResult tr;
Expand Down
6 changes: 6 additions & 0 deletions gtsam_unstable/discrete/Constraint.h
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,12 @@ class GTSAM_EXPORT Constraint : public DiscreteFactor {
return (boost::format("`Constraint` on %1% variables\n") % (size())).str();
}

/// Render as html table.
std::string html(const KeyFormatter& keyFormatter = DefaultKeyFormatter,
const Names& names = {}) const override {
return (boost::format("<p>Constraint on %1% variables</p>") % (size())).str();
}

/// @}
};
// DiscreteFactor
Expand Down