Initial attempt at html
parent
4dafcc50e8
commit
9087d3d81b
|
@ -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) << "|";
|
||||
|
@ -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()) {
|
||||
|
|
|
@ -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;
|
||||
|
||||
/// @}
|
||||
|
||||
};
|
||||
|
|
|
@ -106,6 +106,17 @@ public:
|
|||
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
|
||||
|
|
|
@ -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>
|
||||
|
@ -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>
|
||||
|
|
|
@ -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;
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue