aGrUM  0.17.2
a C++ library for (probabilistic) graphical models
O3prmBNWriter_tpl.h
Go to the documentation of this file.
1 
23 #ifndef DOXYGEN_SHOULD_SKIP_THIS
24 
26 
27 # define O3PRM_INDENT " "
28 
29 namespace gum {
30  /*
31  * Default constructor.
32  */
33  template < typename GUM_SCALAR >
35  GUM_CONSTRUCTOR(O3prmBNWriter);
36  }
37 
38  /*
39  * Destructor.
40  */
41  template < typename GUM_SCALAR >
43  GUM_DESTRUCTOR(O3prmBNWriter);
44  }
45 
46  /*
47  * Writes a bayes net in the given ouput stream.
48  *
49  * @param output The output stream.
50  * @param bn The bayes net writen in the stream.
51  * @throws IOError Raised if an I/O error occurs.
52  */
53  template < typename GUM_SCALAR >
54  INLINE void
55  O3prmBNWriter< GUM_SCALAR >::write(std::ostream& output,
56  const IBayesNet< GUM_SCALAR >& bn) {
57  if (!output.good()) {
58  GUM_ERROR(IOError, "Stream states flags are not all unset.");
59  }
60  std::string bnName = bn.propertyWithDefault("name", "");
61  if (bnName == "") bnName = "bayesnet";
62 
63  output << "class " << bnName << " {" << std::endl;
64 
65  for (auto node: bn.nodes()) {
66  output << __extractAttribute(bn, node) << std::endl;
67  }
68 
69  output << "}" << std::endl;
70 
71  output << std::endl;
72 
73  output.flush();
74 
75  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
76  }
77 
78  template < typename GUM_SCALAR >
80  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
81  std::stringstream str;
82  str << O3PRM_INDENT;
83  str << __extractType(bn, node) << " ";
84  str << __extractName(bn, node) << " ";
85  if (bn.parents(node).size() > 0) {
86  str << "dependson " << __extractParents(bn, node) << " ";
87  }
88  str << " {" << __extractCPT(bn, node) << "};" << std::endl;
89  return str.str();
90  }
91 
92  template < typename GUM_SCALAR >
94  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
95  std::stringstream str;
96  auto var = &(bn.variable(node));
97  for (auto parent: bn.cpt(node).variablesSequence()) {
98  if (var != parent) { str << parent->name() << ", "; }
99  }
100  return str.str().substr(0, str.str().size() - 2);
101  }
102 
103  template < typename GUM_SCALAR >
104  INLINE std::string
105  O3prmBNWriter< GUM_SCALAR >::__extractCPT(const IBayesNet< GUM_SCALAR >& bn,
106  NodeId node) {
107  std::stringstream str;
108  bool first = true;
109  Instantiation inst(bn.cpt(node));
110 
111  str << "[";
112  if (inst.nbrDim() == 1) {
113  // 1D potential
114  for (inst.setFirst(); !inst.end(); inst.inc()) {
115  if (!first) {
116  str << ", ";
117  } else {
118  first = false;
119  }
120  str << bn.cpt(node)[inst];
121  }
122  } else {
123  // (>1)D potential (with parents)
124  Instantiation jnst;
125  for (auto var = inst.variablesSequence().rbegin();
126  var != inst.variablesSequence().rend();
127  --var) {
128  jnst.add(**var);
129  }
130  inst.setFirst();
131  auto currentval = inst.val(0) + 1;
132  for (jnst.setFirst(); !jnst.end(); jnst.inc()) {
133  inst.setVals(jnst);
134  if (!first) {
135  str << ", ";
136  } else {
137  first = false;
138  }
139  if (currentval != inst.val(0)) { // begining line
140  str << std::endl << O3PRM_INDENT << O3PRM_INDENT;
141  currentval = inst.val(0);
142  }
143  str << bn.cpt(node)[inst];
144  }
145  str << std::endl << O3PRM_INDENT;
146  }
147 
148  str << "]";
149  return str.str();
150  }
151 
152  template < typename GUM_SCALAR >
153  INLINE std::string
154  O3prmBNWriter< GUM_SCALAR >::__extractType(const IBayesNet< GUM_SCALAR >& bn,
155  NodeId node) {
156  switch (bn.variable(node).varType()) {
158  auto double_var = dynamic_cast< const DiscretizedVariable< double >* >(
159  &(bn.variable(node)));
160  if (double_var != nullptr) {
161  return __extractDiscretizedType< DiscretizedVariable< double > >(
162  double_var);
163  } else {
164  auto float_var = dynamic_cast< const DiscretizedVariable< float >* >(
165  &(bn.variable(node)));
166  if (float_var != nullptr) {
167  return __extractDiscretizedType< DiscretizedVariable< float > >(
168  float_var);
169  }
170  }
171  GUM_ERROR(InvalidArgument,
172  "DiscretizedVariable ticks are neither doubles or floats");
173  }
174  case gum::VarType::Range: {
175  return __extractRangeType(bn, node);
176  }
177  default: {
178  return __extractLabelizedType(bn, node);
179  }
180  }
181  }
182 
183  template < typename GUM_SCALAR >
185  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
186  const auto& var = static_cast< const RangeVariable& >(bn.variable(node));
187  std::stringstream str;
188  str << "int (" << var.minVal() << ", " << var.maxVal() << ")";
189  return str.str();
190  }
191 
192  template < typename GUM_SCALAR >
194  const IBayesNet< GUM_SCALAR >& bn, NodeId node) {
195  std::stringstream str;
196  str << "labels(";
197  for (auto l: bn.variable(node).labels()) {
198  str << l << ", ";
199  }
200  return str.str().substr(0, str.str().size() - 2) + ")";
201  }
202 
203  template < typename GUM_SCALAR >
204  template < typename VARTYPE >
205  INLINE std::string
207  std::stringstream str;
208  if (var->ticks().size() >= 3) {
209  str << "real(" << var->ticks()[0];
210  for (size_t i = 1; i < var->ticks().size(); ++i) {
211  str << ", " << var->ticks()[i];
212  }
213  str << ")";
214  return str.str();
215  }
216  GUM_ERROR(InvalidArgument, "discretized variable does not have enough ticks");
217  }
218 
219  template < typename GUM_SCALAR >
220  INLINE std::string
221  O3prmBNWriter< GUM_SCALAR >::__extractName(const IBayesNet< GUM_SCALAR >& bn,
222  NodeId node) {
223  if (!bn.variable(node).name().empty()) {
224  return bn.variable(node).name();
225  } else {
226  std::stringstream str;
227  str << node;
228  return str.str();
229  }
230  }
231 
232  /*
233  * Writes a bayes net in the file referenced by filePath.
234  * If the file doesn't exists, it is created.
235  * If the file exists, it's content will be erased.
236  *
237  * @param filePath The path to the file used to write the bayes net.
238  * @param bn The bayes net writen in the file.
239  * @throw IOError Raised if an I/O error occurs.
240  */
241  template < typename GUM_SCALAR >
242  INLINE void
243  O3prmBNWriter< GUM_SCALAR >::write(const std::string& filePath,
244  const IBayesNet< GUM_SCALAR >& bn) {
245  std::ofstream output(filePath.c_str(), std::ios_base::trunc);
246 
247  write(output, bn);
248 
249  output.close();
250 
251  if (output.fail()) { GUM_ERROR(IOError, "Writing in the ostream failed."); }
252  }
253 
254 } /* namespace gum */
255 
256 #endif // DOXYGEN_SHOULD_SKIP_THIS
std::string __extractType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
Definition: agrum.h:25
Copyright 2005-2020 Pierre-Henri WUILLEMIN () et Christophe GONZALES () info_at_agrum_dot_org.
std::string __extractCPT(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
O3prmBNWriter()
Default constructor.
std::string __extractRangeType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractLabelizedType(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractName(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractAttribute(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
std::string __extractDiscretizedType(const VARTYPE *var)
virtual ~O3prmBNWriter()
Destructor.
Size NodeId
Type for node ids.
Definition: graphElements.h:98
std::string __extractParents(const IBayesNet< GUM_SCALAR > &bn, NodeId node)
#define GUM_ERROR(type, msg)
Definition: exceptions.h:55
virtual void write(std::ostream &output, const IBayesNet< GUM_SCALAR > &bn) final
Writes an bayes net in the given ouput stream.