Add formatter

release/4.3a0
Frank Dellaert 2021-12-23 15:19:38 -05:00
parent c6925987e1
commit c5e6650d67
3 changed files with 23 additions and 22 deletions

View File

@ -135,33 +135,31 @@ namespace gtsam {
} }
/* ************************************************************************* */ /* ************************************************************************* */
// Check markdown representation looks as expected. std::string DecisionTreeFactor::_repr_markdown_(
std::string DecisionTreeFactor::_repr_markdown_() const { const KeyFormatter& keyFormatter) const {
std::stringstream ss; std::stringstream ss;
// Print out header and calculate number of rows. // Print out header and construct argument for `cartesianProduct`.
std::vector<std::pair<Key, size_t>> pairs;
ss << "|"; ss << "|";
for (auto& key : cardinalities_) { for (auto& key : keys()) {
size_t k = key.second; ss << keyFormatter(key) << "|";
ss << key.first << "(" << k << ")|"; pairs.emplace_back(key, cardinalities_.at(key));
} }
ss << "value|\n"; ss << "value|\n";
// Print out separator with alignment hints. // Print out separator with alignment hints.
size_t n = cardinalities_.size();
ss << "|"; ss << "|";
for (size_t j = 0; j < n; j++) ss << ":-:|"; for (size_t j = 0; j < size(); j++) ss << ":-:|";
ss << ":-:|\n"; ss << ":-:|\n";
// Print out all rows. // Print out all rows.
std::vector<std::pair<Key, size_t>> keys(cardinalities_.begin(), std::vector<std::pair<Key, size_t>> rpairs(pairs.rbegin(), pairs.rend());
cardinalities_.end()); const auto assignments = cartesianProduct(rpairs);
const auto assignments = cartesianProduct(keys); for (const auto& assignment : assignments) {
for (auto &&assignment : assignments) {
ss << "|"; ss << "|";
for (auto& kv : assignment) ss << kv.second << "|"; for (auto& key : keys()) ss << assignment.at(key) << "|";
const double value = operator()(assignment); ss << operator()(assignment) << "|\n";
ss << value << "|\n";
} }
return ss.str(); return ss.str();
} }

View File

@ -167,7 +167,8 @@ namespace gtsam {
/// @{ /// @{
/// Render as markdown table. /// Render as markdown table.
std::string _repr_markdown_() const; std::string _repr_markdown_(
const KeyFormatter& keyFormatter = DefaultKeyFormatter) const;
/// @} /// @}

View File

@ -83,19 +83,21 @@ TEST( DecisionTreeFactor, sum_max)
} }
/* ************************************************************************* */ /* ************************************************************************* */
// Check markdown representation looks as expected.
TEST(DecisionTreeFactor, markdown) { TEST(DecisionTreeFactor, markdown) {
DiscreteKey v0(0, 3), v1(1, 2); DiscreteKey A(12, 3), B(5, 2);
DecisionTreeFactor f1(v0 & v1, "1 2 3 4 5 6"); DecisionTreeFactor f1(A & B, "1 2 3 4 5 6");
string expected = string expected =
"|0(3)|1(2)|value|\n" "|A|B|value|\n"
"|:-:|:-:|:-:|\n" "|:-:|:-:|:-:|\n"
"|0|0|1|\n" "|0|0|1|\n"
"|1|0|3|\n"
"|2|0|5|\n"
"|0|1|2|\n" "|0|1|2|\n"
"|1|0|3|\n"
"|1|1|4|\n" "|1|1|4|\n"
"|2|0|5|\n"
"|2|1|6|\n"; "|2|1|6|\n";
string actual = f1._repr_markdown_(); auto formatter = [](Key key) { return key == 12 ? "A" : "B"; };
string actual = f1._repr_markdown_(formatter);
EXPECT(actual == expected); EXPECT(actual == expected);
} }