aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
KTBNDatabaseGenerator.h
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2026 by *
5 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
6 * - Christophe GONZALES(_at_AMU) *
7 * *
8 * The aGrUM/pyAgrum library is free software; you can redistribute it *
9 * and/or modify it under the terms of either : *
10 * *
11 * - the GNU Lesser General Public License as published by *
12 * the Free Software Foundation, either version 3 of the License, *
13 * or (at your option) any later version, *
14 * - the MIT license (MIT), *
15 * - or both in dual license, as here. *
16 * *
17 * (see https://agrum.gitlab.io/articles/dual-licenses-lgplv3mit.html) *
18 * *
19 * This aGrUM/pyAgrum library is distributed in the hope that it will be *
20 * useful, but WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, *
21 * INCLUDING BUT NOT LIMITED TO THE WARRANTIES MERCHANTABILITY or FITNESS *
22 * FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE *
23 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER *
24 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, *
25 * ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR *
26 * OTHER DEALINGS IN THE SOFTWARE. *
27 * *
28 * See LICENCES for more details. *
29 * *
30 * SPDX-FileCopyrightText: Copyright 2005-2026 *
31 * - Pierre-Henri WUILLEMIN(_at_LIP6) *
32 * - Christophe GONZALES(_at_AMU) *
33 * SPDX-License-Identifier: LGPL-3.0-or-later OR MIT *
34 * *
35 * Contact : info_at_agrum_dot_org *
36 * homepage : http://agrum.gitlab.io *
37 * gitlab : https://gitlab.com/agrumery/agrum *
38 * *
39 ****************************************************************************/
40
41
79
80#ifndef GUM_KTBN_DATABASE_GENERATOR_H
81#define GUM_KTBN_DATABASE_GENERATOR_H
82
83#include <cstddef>
84#include <string>
85#include <vector>
86
88#include <agrum/KTBN/KTBN.h>
89
90#include <string_view>
91#include <unordered_set>
92
93namespace gum {
94
95 namespace learning {
107 template < GUM_Numeric GUM_SCALAR >
109 public:
111 enum class DiscretizedLabelMode : char { INTERVAL, MEDIAN, RANDOM };
112
114 enum class VarOrderMode : char { RANDOM, TOPOLOGICAL, ANTI_TOPOLOGICAL };
115
116 // #######################################################################
118 // #######################################################################
120
127 explicit KTBNDatabaseGenerator(const KTBN< GUM_SCALAR >& kdbn);
128
131
133 // #######################################################################
135 // #######################################################################
137
157 std::vector< double > drawSamples(Size nbSamples,
158 Size nbTimeSlices,
159 std::string_view dirPath,
160 std::string_view csvBaseName,
162 bool useLabels = true,
163 std::string csvSeparator = ",");
164
178 std::vector< double > drawSamples(const std::vector< Size >& nbTimeSlices,
179 std::string_view dirPath,
180 std::string_view csvBaseName,
182 bool useLabels = true,
183 std::string csvSeparator = ",");
184
192
193
195 Size nbVars() const;
196
198
199 private:
207
209 struct NodeRef {
211 const Tensor< GUM_SCALAR >* cpt;
212 int slice;
214 std::vector< ParentRef > parents;
215 };
216
218 BayesNet< GUM_SCALAR > _template_;
219
222
225
227 std::vector< std::string > _baseCols_;
228
231 std::vector< const DiscreteVariable* > _vars_;
232
234 std::vector< NodeRef > _nodes_;
235
238 std::vector< Idx > _kernel_;
239
243
246
247 // ----- constructor helper -----
248
252 static std::pair< std::string, int >
253 _decode_(const std::string& name, const std::unordered_set< std::string >& temporalSet);
254
259 void _build_(const KTBN< GUM_SCALAR >& kdbn);
260
261 // ----- sampling -----
262
265 Idx _drawVar_(const DiscreteVariable& var,
266 const Tensor< GUM_SCALAR >& cpt,
267 double& log2likelihood);
268
269 // ----- export -----
270
273 std::string _label_(Idx col, Idx idx) const;
274
278 void _writeTrajectory_(std::string_view csvFileURL,
279 const std::vector< Idx >& traj,
280 Size nbTimeSlices,
281 bool useLabels,
282 const std::string& csvSeparator,
283 const std::vector< Idx >& colOrder) const;
284
285 // ----- column order -----
286 // each fills @p colOrder: output position -> canonical column index
287
289 void setVarOrderRandomized(std::vector< Idx >& colOrder) const;
290
292 void setVarOrderTopological(std::vector< Idx >& colOrder) const;
293
295 void setVarOrderAntiTopological(std::vector< Idx >& colOrder) const;
296
300 std::vector< double > _drawSamples_(Size nbSamples,
301 Size fixedLen,
302 const std::vector< Size >* perTraj,
303 std::string_view dirPath,
304 std::string_view csvBaseName,
305 VarOrderMode mode,
306 bool useLabels,
307 const std::string& csvSeparator);
308
309 // forbidden copies / moves
314 };
315
316
317#ifndef GUM_NO_EXTERN_TEMPLATE_CLASS
318 extern template class GUM_PUBLIC_KTBN KTBNDatabaseGenerator< double >;
319#endif
320
321 } /* namespace learning */
322} /* namespace gum */
323
325
326#endif /* GUM_KTBN_DATABASE_GENERATOR_H */
Implementation of the KTBNDatabaseGenerator class.
Class representing k-order dynamic Bayesian networks (k-DBN).
Base class for discrete random variable.
Class for assigning/browsing values to tuples of discrete variables.
Notification for progress using listener.
Generates a database of trajectories from a k-DBN (one CSV per trajectory).
Idx _drawVar_(const DiscreteVariable &var, const Tensor< GUM_SCALAR > &cpt, double &log2likelihood)
inverse-CDF draw of var given the parents already set in inst; accumulates log2(P(drawn value)) into ...
Size _nbVars_
number of base variable columns
std::vector< const DiscreteVariable * > _vars_
one representative variable per base column (same order as baseCols), pointing into template so it ou...
KTBNDatabaseGenerator(const KTBNDatabaseGenerator &)=delete
void setVarOrderAntiTopological(std::vector< Idx > &colOrder) const
builds the reverse of setVarOrderTopological()
void setVarOrderTopological(std::vector< Idx > &colOrder) const
builds a topological column order (transition-kernel projection)
void setDiscretizedLabelModeInterval()
set discretized-label rendering to the interval label "[min,max["
std::vector< std::string > _baseCols_
col index -> base name (canonical column numbering)
static std::pair< std::string, int > _decode_(const std::string &name, const std::unordered_set< std::string > &temporalSet)
decodes a template node name into (base, slice): "B[t]" with B a known temporal process -> (B,...
void _writeTrajectory_(std::string_view csvFileURL, const std::vector< Idx > &traj, Size nbTimeSlices, bool useLabels, const std::string &csvSeparator, const std::vector< Idx > &colOrder) const
writes one trajectory CSV (header + T rows) to csvFileURL. traj is the flat row-major buffer (T x nbV...
KTBNDatabaseGenerator & operator=(KTBNDatabaseGenerator &&)=delete
void setDiscretizedLabelModeRandom()
set discretized-label rendering to a uniform random draw in the interval (this is the default; each l...
KTBNDatabaseGenerator & operator=(const KTBNDatabaseGenerator &)=delete
std::vector< Idx > _kernel_
indices, in nodes, of the slice-(k-1) nodes (the transition kernel, drives Phase 2); already in topol...
std::vector< NodeRef > _nodes_
all template nodes in topological order (drives Phase 1, the bootstrap)
void setDiscretizedLabelModeMedian()
set discretized-label rendering to the (deterministic) interval median
std::vector< double > _drawSamples_(Size nbSamples, Size fixedLen, const std::vector< Size > *perTraj, std::string_view dirPath, std::string_view csvBaseName, VarOrderMode mode, bool useLabels, const std::string &csvSeparator)
the single worker behind both drawSamples() overloads. Trajectory i's horizon is read from perTraj (w...
KTBNDatabaseGenerator(KTBNDatabaseGenerator &&)=delete
DiscretizedLabelMode
rendering of discretized variables when labels are requested
void _build_(const KTBN< GUM_SCALAR > &kdbn)
one-shot initialisation called by the constructor: fills the column index (baseCols,...
Instantiation _inst_
a shared instantiation over all template variables, so we don't have to rebuild it for every draw
KTBNDatabaseGenerator(const KTBN< GUM_SCALAR > &kdbn)
Constructor.
std::string _label_(Idx col, Idx idx) const
renders the label of modality idx of base column col (taking the discretized-label mode into account)
BayesNet< GUM_SCALAR > _template_
the -slice template (a small copy, independent of the horizon)
VarOrderMode
column order used for the exported CSV
void setVarOrderRandomized(std::vector< Idx > &colOrder) const
builds a uniformly random column order
Size nbVars() const
returns the number of base variable columns
DiscretizedLabelMode _discretizedLabelMode_
rendering of discretized variables when labels are requested
std::vector< double > drawSamples(Size nbSamples, Size nbTimeSlices, std::string_view dirPath, std::string_view csvBaseName, VarOrderMode mode=VarOrderMode::RANDOM, bool useLabels=true, std::string csvSeparator=",")
Generates nbSamples independent trajectories, writing one CSV file per trajectory into dirPath.
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Definition types.h:74
Size Idx
Type for indexes.
Definition types.h:79
include the inlined functions if necessary
Definition CSVParser.h:55
template class GUM_PUBLIC_KTBN KTBNDatabaseGenerator< double >
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
priority queues (in which an element cannot appear more than once)
a template node, precompiled for fast sampling
int slice
its template slice (ATEMPORAL if static)
const DiscreteVariable * var
the node variable (in template)
std::vector< ParentRef > parents
its parents (drives sampling and topology)
const Tensor< GUM_SCALAR > * cpt
its CPT (in template)
a parent of a template node, precompiled for fast sampling
bool isAtemporal
whether the parent is atemporal
const DiscreteVariable * var
the parent variable (in template)
int lag
time steps back: parentTime = nodeTime - lag