Made parent-less vertical, like a factor

release/4.3a0
Frank Dellaert 2021-12-24 14:30:42 -05:00
parent 042cb9d902
commit 1ab8a23792
3 changed files with 26 additions and 13 deletions

View File

@ -229,11 +229,22 @@ std::string DiscreteConditional::_repr_markdown_(
// Print out signature. // Print out signature.
ss << " $P("; ss << " $P(";
for(Key key: frontals())
ss << keyFormatter(key);
if (nrParents() > 0)
ss << "|";
bool first = true; 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()) { for (Key parent : parents()) {
if (!first) ss << ","; if (!first) ss << ",";
ss << keyFormatter(parent); ss << keyFormatter(parent);
@ -256,9 +267,8 @@ std::string DiscreteConditional::_repr_markdown_(
pairs.emplace_back(key, k); pairs.emplace_back(key, k);
n *= k; n *= k;
} }
size_t nrParents = size() - nrFrontals_;
std::vector<std::pair<Key, size_t>> slatnorf(pairs.rbegin(), std::vector<std::pair<Key, size_t>> slatnorf(pairs.rbegin(),
pairs.rend() - nrParents); pairs.rend() - nrParents());
const auto frontal_assignments = cartesianProduct(slatnorf); const auto frontal_assignments = cartesianProduct(slatnorf);
for (const auto& a : frontal_assignments) { for (const auto& a : frontal_assignments) {
for (it = beginFrontals(); it != endFrontals(); ++it) ss << a.at(*it); for (it = beginFrontals(); it != endFrontals(); ++it) ss << a.at(*it);
@ -268,7 +278,7 @@ std::string DiscreteConditional::_repr_markdown_(
// Print out separator with alignment hints. // Print out separator with alignment hints.
ss << "|"; ss << "|";
for (size_t j = 0; j < nrParents + n; j++) ss << ":-:|"; for (size_t j = 0; j < nrParents() + n; j++) ss << ":-:|";
ss << "\n"; ss << "\n";
// Print out all rows. // Print out all rows.

View File

@ -181,9 +181,10 @@ TEST(DiscreteBayesNet, markdown) {
"`DiscreteBayesNet` of size 2\n" "`DiscreteBayesNet` of size 2\n"
"\n" "\n"
" $P(Asia)$:\n" " $P(Asia)$:\n"
"|0|1|\n" "|0|value|\n"
"|:-:|:-:|\n" "|:-:|:-:|\n"
"|0.99|0.01|\n" "|0|0.99|\n"
"|1|0.01|\n"
"\n" "\n"
" $P(Smoking|Asia)$:\n" " $P(Smoking|Asia)$:\n"
"|Asia|0|1|\n" "|Asia|0|1|\n"

View File

@ -110,13 +110,15 @@ TEST(DiscreteConditional, Combine) {
/* ************************************************************************* */ /* ************************************************************************* */
// Check markdown representation looks as expected, no parents. // Check markdown representation looks as expected, no parents.
TEST(DiscreteConditional, markdown_prior) { TEST(DiscreteConditional, markdown_prior) {
DiscreteKey A(Symbol('x', 1), 2); DiscreteKey A(Symbol('x', 1), 3);
DiscreteConditional conditional(A % "1/3"); DiscreteConditional conditional(A % "1/2/2");
string expected = string expected =
" $P(x1)$:\n" " $P(x1)$:\n"
"|0|1|\n" "|x1|value|\n"
"|:-:|:-:|\n" "|:-:|:-:|\n"
"|0.25|0.75|\n"; "|0|0.2|\n"
"|1|0.4|\n"
"|2|0.4|\n";
string actual = conditional._repr_markdown_(); string actual = conditional._repr_markdown_();
EXPECT(actual == expected); EXPECT(actual == expected);
} }