aGrUM 3.3.2
a C++ library for (probabilistic) graphical models
gum::MarkovBlanket Class Reference

Class building the markov Blanket from a BN and a nodeName. More...

#include <agrum/BN/algorithms/MarkovBlanket.h>

Collaboration diagram for gum::MarkovBlanket:
[legend]

Public Member Functions

 MarkovBlanket (const DAGmodel &m, NodeId n, int level=1)
 MarkovBlanket (const DAGmodel &m, std::string_view name, int level=1)
 ~MarkovBlanket ()
DAG dag () const
std::string toDot () const
std::string toDot (const dot::DotOptions &options) const
const NodeSet & parents (NodeId id) const
 wrapping DAG::parents(id)
const NodeSet & children (NodeId id) const
 wrapping DAG::children(id)
NodeSet parents (const NodeSet &ids) const
 wrapping DAG::parents(ids)
NodeSet children (const NodeSet &ids) const
 wrapping DAG::children(ids)
NodeSet descendants (NodeId id) const
 wrapping DAG::descendants(id)
NodeSet ancestors (NodeId id) const
 wrapping DAG::ancestors(id)
Size sizeArcs () const
 wrapping DAG::sizeArcs()
const ArcSet & arcs () const
 wrapping DAG::arcs()
Size sizeNodes () const
 wrapping DAG::sizeNodes()
Size size () const
 wrapping DAG::size()
const NodeGraphPart & nodes () const
 wrapping DAG::nodes()
bool hasSameStructure (const DAGmodel &other) const

Private Attributes

const DAGmodel & _model_
DAG _mb_
const NodeId _node_
ArcSet _specialArcs_

Detailed Description

Class building the markov Blanket from a BN and a nodeName.

The main goal of this class is to build and to encapsulate the DiGraph which represents the Markov Blanket.

Definition at line 71 of file MarkovBlanket.h.

Constructor & Destructor Documentation

◆ MarkovBlanket() [1/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel & m,
NodeId n,
int level = 1 )

Definition at line 57 of file MarkovBlanket.cpp.

57 : _model_(m), _node_(id) {
58 if (level < 1) GUM_ERROR(InvalidArgument, "Argument level(=" << level << ") must be >0.")
59
60 _mb_ = graph::markovBlanket(m.internalDag(), id, level);
61
62 // Add arcs that exist in the model between MB nodes but are not part of
63 // the canonical MB definition (e.g. arc between a parent and a co-parent).
64 for (const auto node: _mb_.nodes()) {
65 for (const auto child: _model_.children(node)) {
66 if (_mb_.existsNode(child) && !_mb_.existsArc(Arc(node, child))) {
67 _mb_.addArc(node, child);
68 _specialArcs_.insert(Arc(node, child));
69 }
70 }
71 }
72
73 for (auto node: _mb_)
74 _mb_.setName(node, _model_.variable(node).name());
75 }
const NodeSet & children(const NodeId id) const
returns the set of nodes with arc outgoing from a given node
const DiscreteVariable & variable(NodeId id) const override
Returns a constant reference over a variable given its node id.
const DAGmodel & _model_
const NodeId _node_
const NodeGraphPart & nodes() const
wrapping DAG::nodes()
void setName(NodeId id, const std::string &name)
sets the name of node id
const std::string & name() const
returns the name of the variable
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _mb_, _model_, _node_, GUM_ERROR, gum::DAGmodel::internalDag(), gum::graph::markovBlanket(), and gum::NodeGraphPart::nodes().

Referenced by MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ MarkovBlanket() [2/2]

gum::MarkovBlanket::MarkovBlanket ( const DAGmodel & m,
std::string_view name,
int level = 1 )

Definition at line 77 of file MarkovBlanket.cpp.

77 :
78 MarkovBlanket(m, m.idFromName(name), level) {}
MarkovBlanket(const DAGmodel &m, NodeId n, int level=1)

References MarkovBlanket().

Here is the call graph for this function:

◆ ~MarkovBlanket()

gum::MarkovBlanket::~MarkovBlanket ( )
default

References ancestors(), arcs(), children(), dag(), descendants(), hasSameStructure(), nodes(), parents(), size(), sizeArcs(), sizeNodes(), and toDot().

Here is the call graph for this function:

Member Function Documentation

◆ ancestors()

INLINE NodeSet gum::MarkovBlanket::ancestors ( NodeId id) const

wrapping DAG::ancestors(id)

Definition at line 68 of file MarkovBlanket_inl.h.

68{ return _mb_.ancestors(id); }
NodeSet ancestors(NodeId id) const
returns the set of all ancestors of id (nodes from which id is reachable)

References _mb_, and gum::DiGraph::ancestors().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ arcs()

INLINE const ArcSet & gum::MarkovBlanket::arcs ( ) const

wrapping DAG::arcs()

Definition at line 72 of file MarkovBlanket_inl.h.

72{ return _mb_.arcs(); }
const ArcSet & arcs() const
returns the set of arcs stored within the ArcGraphPart

References _mb_, and gum::ArcGraphPart::arcs().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ children() [1/2]

INLINE NodeSet gum::MarkovBlanket::children ( const NodeSet & ids) const

wrapping DAG::children(ids)

Definition at line 64 of file MarkovBlanket_inl.h.

64{ return _mb_.children(ids); }
NodeSet children(const NodeSet &ids) const
returns the set of nodes which consists in the node and its parents returns the set of children of a ...

References _mb_, and gum::ArcGraphPart::children().

Here is the call graph for this function:

◆ children() [2/2]

INLINE const NodeSet & gum::MarkovBlanket::children ( NodeId id) const

wrapping DAG::children(id)

Definition at line 60 of file MarkovBlanket_inl.h.

60{ return _mb_.children(id); }

References _mb_, and gum::ArcGraphPart::children().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ dag()

INLINE DAG gum::MarkovBlanket::dag ( ) const
Returns
a copy of the graph

Definition at line 56 of file MarkovBlanket_inl.h.

56{ return _mb_; }

References _mb_.

Referenced by ~MarkovBlanket().

Here is the caller graph for this function:

◆ descendants()

INLINE NodeSet gum::MarkovBlanket::descendants ( NodeId id) const

wrapping DAG::descendants(id)

Definition at line 66 of file MarkovBlanket_inl.h.

66{ return _mb_.descendants(id); }
NodeSet descendants(NodeId id) const
returns the set of all descendants of id (nodes reachable from id)

References _mb_, and gum::DiGraph::descendants().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ hasSameStructure()

bool gum::MarkovBlanket::hasSameStructure ( const DAGmodel & other) const
Returns
true if all the named node are the same and all the named arcs are the same

Definition at line 82 of file MarkovBlanket.cpp.

82 {
83 if (size() != other.size()) return false;
84
85 if (sizeArcs() != other.sizeArcs()) return false;
86
87 for (const auto& nid: nodes()) {
88 if (!other.exists(_model_.variable(nid).name())) { return false; }
89 }
90
91 for (const auto& arc: arcs()) {
92 if (!other.arcs().exists(Arc(other.idFromName(_model_.variable(arc.tail()).name()),
93 other.idFromName(_model_.variable(arc.head()).name()))))
94 return false;
95 }
96
97 return true;
98 }
Size sizeArcs() const
wrapping DAG::sizeArcs()
const ArcSet & arcs() const
wrapping DAG::arcs()
Size size() const
wrapping DAG::size()

References _model_, gum::DAGmodel::arcs(), arcs(), gum::DAGmodel::exists(), gum::Set< Key >::exists(), gum::DiscreteGraphicalModel::idFromName(), gum::Variable::name(), nodes(), gum::DAGmodel::size(), size(), gum::DAGmodel::sizeArcs(), sizeArcs(), and gum::DiscreteGraphicalModel::variable().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ nodes()

INLINE const NodeGraphPart & gum::MarkovBlanket::nodes ( ) const

wrapping DAG::nodes()

Definition at line 78 of file MarkovBlanket_inl.h.

78{ return _mb_.nodes(); }
const NodeGraphPart & nodes() const
return *this as a NodeGraphPart

References _mb_, and gum::NodeGraphPart::nodes().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ parents() [1/2]

INLINE NodeSet gum::MarkovBlanket::parents ( const NodeSet & ids) const

wrapping DAG::parents(ids)

Definition at line 62 of file MarkovBlanket_inl.h.

62{ return _mb_.parents(ids); }
const NodeSet & parents(NodeId id) const
returns the set of nodes with arc ingoing to a given node

References _mb_, and gum::ArcGraphPart::parents().

Here is the call graph for this function:

◆ parents() [2/2]

INLINE const NodeSet & gum::MarkovBlanket::parents ( NodeId id) const

wrapping DAG::parents(id)

Definition at line 58 of file MarkovBlanket_inl.h.

58{ return _mb_.parents(id); }

References _mb_, and gum::ArcGraphPart::parents().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ size()

INLINE Size gum::MarkovBlanket::size ( ) const

wrapping DAG::size()

Definition at line 76 of file MarkovBlanket_inl.h.

76{ return _mb_.size(); }
Size size() const
alias for sizeNodes

References _mb_, and gum::NodeGraphPart::size().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeArcs()

INLINE Size gum::MarkovBlanket::sizeArcs ( ) const

wrapping DAG::sizeArcs()

Definition at line 70 of file MarkovBlanket_inl.h.

70{ return _mb_.sizeArcs(); }
Size sizeArcs() const
indicates the number of arcs stored within the ArcGraphPart

References _mb_, and gum::ArcGraphPart::sizeArcs().

Referenced by ~MarkovBlanket(), and hasSameStructure().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ sizeNodes()

INLINE Size gum::MarkovBlanket::sizeNodes ( ) const

wrapping DAG::sizeNodes()

Definition at line 74 of file MarkovBlanket_inl.h.

74{ return _mb_.sizeNodes(); }
Size sizeNodes() const
returns the number of nodes in the NodeGraphPart

References _mb_, and gum::NodeGraphPart::sizeNodes().

Referenced by ~MarkovBlanket().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toDot() [1/2]

std::string gum::MarkovBlanket::toDot ( ) const

Definition at line 100 of file MarkovBlanket.cpp.

100{ return toDot(dot::DotOptions()); }
std::string toDot() const

References toDot().

Referenced by ~MarkovBlanket(), and toDot().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ toDot() [2/2]

std::string gum::MarkovBlanket::toDot ( const dot::DotOptions & options) const

Definition at line 102 of file MarkovBlanket.cpp.

102 {
103 const auto style = dot::DotStyle::read();
104 dot::DotWriter writer(true);
105 dot::writePreamble(writer, style, options);
106
107 for (const auto node: _mb_.nodes()) {
108 const auto& name = _model_.variable(node).name();
109 auto attributes = dot::nodeAttributes(style,
110 options.node(node),
111 name,
112 "(" + std::to_string(node) + ") " + name);
113 if (node == _node_) dot::addDefaultAttribute(attributes, "color", "red");
114 writer.node(node, attributes);
115 }
116
117 for (const auto node: _mb_.nodes()) {
118 for (const auto chi: _mb_.children(node)) {
119 auto attributes = dot::arcAttributes(options.arc(node, chi));
120 if (_specialArcs_.exists(Arc(node, chi)))
121 dot::addDefaultAttribute(attributes, "color", "grey");
122 writer.edge(node, chi, attributes);
123 }
124 }
125
126 return writer.str();
127 }
bool exists(const Key &k) const
Indicates whether a given elements belong to the set.
Definition set_tpl.h:504
DotAttributes nodeAttributes(const DotStyle &style, const DotNodeOptions *options, std::string_view defaultLabel, std::string_view defaultTooltip)
The attributes of a node : its label and tooltip, and its colors if the options give some.
void writePreamble(DotWriter &writer, const DotStyle &style, const DotOptions &options, std::string_view defaultLabel)
Writes the beginning of a graph, as pyAgrum displays it.
void addDefaultAttribute(DotAttributes &attributes, std::string_view key, std::string_view value)
Adds an attribute at the beginning of the list, unless the list already gives a value for this key : ...
DotAttributes arcAttributes(const DotArcOptions *options)
The attributes of an arc or an edge given by the options : color, penwidth, label,...
static DotStyle read()
the style of the graphs of the section notebook
Definition dotStyle.cpp:89

References _mb_, _model_, _node_, _specialArcs_, gum::dot::addDefaultAttribute(), gum::dot::DotOptions::arc(), gum::dot::arcAttributes(), gum::ArcGraphPart::children(), gum::dot::DotWriter::edge(), gum::Set< Key >::exists(), gum::Variable::name(), gum::dot::DotOptions::node(), gum::dot::DotWriter::node(), gum::dot::nodeAttributes(), gum::NodeGraphPart::nodes(), gum::dot::DotStyle::read(), gum::dot::DotWriter::str(), gum::DiscreteGraphicalModel::variable(), and gum::dot::writePreamble().

Here is the call graph for this function:

Member Data Documentation

◆ _mb_

DAG gum::MarkovBlanket::_mb_
private

◆ _model_

const DAGmodel& gum::MarkovBlanket::_model_
private

Definition at line 128 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), hasSameStructure(), and toDot().

◆ _node_

const NodeId gum::MarkovBlanket::_node_
private

Definition at line 130 of file MarkovBlanket.h.

Referenced by MarkovBlanket(), and toDot().

◆ _specialArcs_

ArcSet gum::MarkovBlanket::_specialArcs_
private

Definition at line 131 of file MarkovBlanket.h.

Referenced by toDot().


The documentation for this class was generated from the following files: