aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
gum::learning::BNDatabaseGenerator< GUM_SCALAR > Class Template Reference

#include <BNDatabaseGenerator.h>

Inheritance diagram for gum::learning::BNDatabaseGenerator< GUM_SCALAR >:
Collaboration diagram for gum::learning::BNDatabaseGenerator< GUM_SCALAR >:

Public Types

enum class  DiscretizedLabelMode : char { INTERVAL , MEDIAN , RANDOM }

Public Member Functions

Constructors / Destructors
 BNDatabaseGenerator (const BayesNet< GUM_SCALAR > &bn)
 default constructor
 ~BNDatabaseGenerator ()
 destructor
Accessors / Modifiers
double drawSamples (Size nbSamples)
 generate and stock database, returns log2likelihood using ProgressNotifier as notification
double drawSamples (Size nbSamples, const gum::Instantiation &evs, int timeout=300)
 Generate and stock the part of the database compatible with the evidence, returns log2likelihood using ProgressNotifier as notification.
void setDiscretizedLabelModeRandom ()
 set the behaviour of sampling for discretized variable to uniformly draw double value
void setDiscretizedLabelModeMedian ()
 set the behaviour of sampling for discretized variable to deterministic select double median of intervalls
void setDiscretizedLabelModeInterval ()
 set the behaviour of sampling for discretized variable to select the label : "[min,max["
void toCSV (std::string_view csvFileURL, bool useLabels=true, bool append=false, std::string csvSeparator=",", bool checkOnAppend=false) const
 generates csv representing the generated database
DatabaseTable toDatabaseTable (bool useLabels=true) const
 generates a DatabaseVectInRAM
std::vector< std::vector< Idx > > database () const
 generates database according to bn into a std::vector
Size samplesNbRows () const
 generate and stock database, returns log2likelihood using ProgressNotifier as notification
Size samplesNbCols () const
 generate and stock database, returns log2likelihood using ProgressNotifier as notification
Idx samplesAt (Idx row, Idx col) const
 generate and stock database, returns log2likelihood using ProgressNotifier as notification
std::string samplesLabelAt (Idx row, Idx col) const
 generate and stock database, returns log2likelihood using ProgressNotifier as notification
void setVarOrder (const std::vector< Idx > &varOrder)
 change columns order
void setVarOrder (const std::vector< std::string > &varOrder)
 change columns order using variable names
void setVarOrderFromCSV (std::string_view csvFileURL, std::string_view csvSeparator=",")
 change columns order according to a csv file
void setTopologicalVarOrder ()
 set columns in topoligical order
void setAntiTopologicalVarOrder ()
 set columns in antiTopoligical order
void setRandomVarOrder ()
 set columns in random order
std::vector< IdxvarOrder () const
 returns variable order indexes
std::vector< std::string > varOrderNames () const
 returns variable order.
double log2likelihood () const
 returns log2Likelihood of generated samples
const BayesNet< GUM_SCALAR > & bn (void)
 return const ref to the Bayes Net

Public Attributes

Signaler< Size, doubleonProgress
 Progression (percent) and time.
Signaler< std::string_view > onStop
 with a possible explanation for stopping

Private Member Functions

std::string _label_ (const std::vector< Idx > &row, const DiscreteVariable &v, Idx i) const
 return the final string for a label (taking into account the behavior for DiscretizedVariable) from a row
std::vector< Idx_varOrderFromCSV_ (std::string_view csvFileURL, std::string_view csvSeparator=",") const
 returns varOrder from a csv file
std::vector< Idx_varOrderFromCSV_ (std::ifstream &csvFile, std::string_view csvSeparator=",") const
 returns varOrder from a csv file
 BNDatabaseGenerator (const BNDatabaseGenerator &)=delete
 BNDatabaseGenerator (BNDatabaseGenerator &&)=delete
BNDatabaseGeneratoroperator= (const BNDatabaseGenerator &)=delete
BNDatabaseGeneratoroperator= (BNDatabaseGenerator &&)=delete

Private Attributes

DiscretizedLabelMode _discretizedLabelMode_
const BayesNet< GUM_SCALAR > & _bn_
 Bayesian network.
Bijection< std::string, NodeId_names2ids_
 bijection nodes names
Size _nbVars_
 number of variables
std::vector< std::vector< Idx > > _database_
 generated database
std::vector< Idx_varOrder_
 variable order in generated database
bool _drawnSamples_ = false
 whether drawSamples has been already called.
double _log2likelihood_ = 0
 log2Likelihood of generated samples

Detailed Description

template<GUM_Numeric GUM_SCALAR>
class gum::learning::BNDatabaseGenerator< GUM_SCALAR >

Definition at line 103 of file BNDatabaseGenerator.h.

Member Enumeration Documentation

◆ DiscretizedLabelMode

template<GUM_Numeric GUM_SCALAR>
enum class gum::learning::BNDatabaseGenerator::DiscretizedLabelMode : char
strong
Enumerator
INTERVAL 
MEDIAN 
RANDOM 

Definition at line 105 of file BNDatabaseGenerator.h.

105: char { INTERVAL, MEDIAN, RANDOM };

Constructor & Destructor Documentation

◆ BNDatabaseGenerator() [1/3]

template<GUM_Numeric GUM_SCALAR>
gum::learning::BNDatabaseGenerator< GUM_SCALAR >::BNDatabaseGenerator ( const BayesNet< GUM_SCALAR > & bn)
explicit

default constructor

Definition at line 58 of file BNDatabaseGenerator_tpl.h.

58 :
59 _bn_(bn) {
61
62 // get the node names => they will serve as ids
63 NodeId id = 0;
64 for (const auto& var: _bn_.internalDag()) {
65 auto name = _bn_.variable(var).name();
66 _names2ids_.insert(name, var);
67 ++id;
68 }
69 _nbVars_ = id;
70 _varOrder_.resize(_nbVars_);
72 std::iota(_varOrder_.begin(), _varOrder_.end(), (Idx)0);
73 }
const BayesNet< GUM_SCALAR > & bn(void)
return const ref to the Bayes Net
BNDatabaseGenerator(const BayesNet< GUM_SCALAR > &bn)
default constructor
Bijection< std::string, NodeId > _names2ids_
bijection nodes names
const BayesNet< GUM_SCALAR > & _bn_
Bayesian network.
std::vector< Idx > _varOrder_
variable order in generated database

References BNDatabaseGenerator(), _bn_, _discretizedLabelMode_, _names2ids_, _nbVars_, _varOrder_, bn(), and RANDOM.

Referenced by BNDatabaseGenerator(), BNDatabaseGenerator(), BNDatabaseGenerator(), ~BNDatabaseGenerator(), operator=(), and operator=().

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

◆ ~BNDatabaseGenerator()

template<GUM_Numeric GUM_SCALAR>
gum::learning::BNDatabaseGenerator< GUM_SCALAR >::~BNDatabaseGenerator ( )

destructor

Definition at line 77 of file BNDatabaseGenerator_tpl.h.

References BNDatabaseGenerator().

Here is the call graph for this function:

◆ BNDatabaseGenerator() [2/3]

template<GUM_Numeric GUM_SCALAR>
gum::learning::BNDatabaseGenerator< GUM_SCALAR >::BNDatabaseGenerator ( const BNDatabaseGenerator< GUM_SCALAR > & )
privatedelete

References BNDatabaseGenerator().

Here is the call graph for this function:

◆ BNDatabaseGenerator() [3/3]

template<GUM_Numeric GUM_SCALAR>
gum::learning::BNDatabaseGenerator< GUM_SCALAR >::BNDatabaseGenerator ( BNDatabaseGenerator< GUM_SCALAR > && )
privatedelete

References BNDatabaseGenerator().

Here is the call graph for this function:

Member Function Documentation

◆ _label_()

template<GUM_Numeric GUM_SCALAR>
std::string gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_label_ ( const std::vector< Idx > & row,
const DiscreteVariable & v,
Idx i ) const
private

return the final string for a label (taking into account the behavior for DiscretizedVariable) from a row

Definition at line 296 of file BNDatabaseGenerator_tpl.h.

298 {
299 if (v.varType() == VarType::DISCRETIZED) {
300 switch (_discretizedLabelMode_) {
301 case DiscretizedLabelMode::MEDIAN : return std::to_string(v.numerical(row.at(i)));
303 return std::to_string(static_cast< const IDiscretizedVariable& >(v).draw(row.at(i)));
304 case DiscretizedLabelMode::INTERVAL : return v.label(row.at(i));
305 }
306 }
307
308 return v.label(row.at(i));
309 }

References _discretizedLabelMode_, gum::DISCRETIZED, INTERVAL, gum::DiscreteVariable::label(), MEDIAN, gum::DiscreteVariable::numerical(), RANDOM, and gum::DiscreteVariable::varType().

Referenced by samplesLabelAt(), and toDatabaseTable().

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

◆ _varOrderFromCSV_() [1/2]

template<GUM_Numeric GUM_SCALAR>
std::vector< Idx > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_varOrderFromCSV_ ( std::ifstream & csvFile,
std::string_view csvSeparator = "," ) const
private

returns varOrder from a csv file

Definition at line 491 of file BNDatabaseGenerator_tpl.h.

492 {
495 header_found.reserve(_nbVars_);
496 while (std::getline(csvFile, line)) {
497 std::size_t i = 0;
498 auto pos = line.find(csvSeparator);
499 while (pos != std::string::npos) {
500 header_found.push_back(line.substr(i, pos - i));
501 pos += csvSeparator.length();
502 i = pos;
503 pos = line.find(csvSeparator, pos);
504
505 if (pos == std::string::npos) header_found.push_back(line.substr(i, line.length()));
506 }
507 break;
508 }
509
511 varOrder.reserve(_nbVars_);
512
513 for (const auto& hf: header_found) {
514 varOrder.push_back(_names2ids_.second(hf));
515 }
516
517 return varOrder;
518 }
std::vector< Idx > varOrder() const
returns variable order indexes

References _names2ids_, _nbVars_, and varOrder().

Here is the call graph for this function:

◆ _varOrderFromCSV_() [2/2]

template<GUM_Numeric GUM_SCALAR>
std::vector< Idx > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_varOrderFromCSV_ ( std::string_view csvFileURL,
std::string_view csvSeparator = "," ) const
private

returns varOrder from a csv file

Definition at line 474 of file BNDatabaseGenerator_tpl.h.

475 {
478 if (csvFile) {
480 csvFile.close();
481 } else {
482 GUM_ERROR(NotFound, "csvFileURL does not exist")
483 }
484
485 return varOrder;
486 }
std::vector< Idx > _varOrderFromCSV_(std::string_view csvFileURL, std::string_view csvSeparator=",") const
returns varOrder from a csv file
#define GUM_ERROR(type, msg)
Definition exceptions.h:76

References _varOrderFromCSV_(), GUM_ERROR, and varOrder().

Referenced by _varOrderFromCSV_(), setVarOrderFromCSV(), and toCSV().

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

◆ bn()

template<GUM_Numeric GUM_SCALAR>
const BayesNet< GUM_SCALAR > & gum::learning::BNDatabaseGenerator< GUM_SCALAR >::bn ( void )

return const ref to the Bayes Net

Definition at line 521 of file BNDatabaseGenerator_tpl.h.

521 {
522 return _bn_;
523 }

References _bn_.

Referenced by BNDatabaseGenerator().

Here is the caller graph for this function:

◆ database()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< Idx > > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::database ( ) const

generates database according to bn into a std::vector

returns database using specified data order

Warning
: makes a copy of the whole database

Definition at line 361 of file BNDatabaseGenerator_tpl.h.

361 {
362 if (!_drawnSamples_) GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.")
363
365 for (Idx i = 0; i < _database_.size(); ++i) {
366 for (Idx j = 0; j < _nbVars_; ++j) {
367 db.at(i).at(j) = (Idx)_database_.at(i).at(_varOrder_.at(j));
368 }
369 }
370 return db;
371 }
bool _drawnSamples_
whether drawSamples has been already called.
std::vector< std::vector< Idx > > _database_
generated database

References _database_, _drawnSamples_, _nbVars_, _varOrder_, and GUM_ERROR.

◆ drawSamples() [1/2]

template<GUM_Numeric GUM_SCALAR>
double gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples ( Size nbSamples)

generate and stock database, returns log2likelihood using ProgressNotifier as notification

draw instances from bn

Definition at line 83 of file BNDatabaseGenerator_tpl.h.

83 {
84 const Instantiation inst;
85 return drawSamples(nbSamples, inst);
86 }
double drawSamples(Size nbSamples)
generate and stock database, returns log2likelihood using ProgressNotifier as notification

References drawSamples().

Referenced by drawSamples().

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

◆ drawSamples() [2/2]

template<GUM_Numeric GUM_SCALAR>
double gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples ( Size nbSamples,
const gum::Instantiation & evs,
int timeout = 300 )

Generate and stock the part of the database compatible with the evidence, returns log2likelihood using ProgressNotifier as notification.

draw instances from bn

Warning
nbSamples is not the number of generated samples but the size of the filtered database. It may happen that the evidence is very rare (or even impossible). In that case, the rejection sampling process may be very slow (or even infinite). In that case, the timeout is mandatory.
Parameters
nbSamplesthe size of the filtered database.
evsthe evidence.
timeoutthe maximum time in seconds to wait for the generation of the samples. If the timeout is reached, the function returns the log2likelihood of the generated samples. if timeout=0, no timeout are watched and the function may run indefinitely.

Definition at line 90 of file BNDatabaseGenerator_tpl.h.

92 {
93 int progress = 0;
94
95 if (onProgress.hasListener()) { GUM_EMIT2(onProgress, progress, 0.0); }
96
97 _database_.clear();
98 _database_.resize(nbSamples);
99 for (auto& row: _database_) {
100 row.resize(_nbVars_);
101 }
102 // get the order in which the nodes will be sampled
103 const auto topOrder = _bn_.topologicalOrder();
105
106 // create instantiations in advance
107 for (NodeId node = 0; node < _nbVars_; ++node)
108 particule.add(_bn_.variable(node));
109
111 timer.reset();
112
113 // perform the sampling
115 Idx idSample = 0;
116 while (idSample < nbSamples) {
117 if (onProgress.hasListener()) {
118 auto p = int((idSample * 100) / nbSamples);
119 if (p != progress) {
120 progress = p;
122 }
123 }
125 bool reject = false;
126 for (Idx rank = 0; rank < _nbVars_; ++rank) {
127 const NodeId node = topOrder[rank];
128 const auto& var = _bn_.variable(node);
129 const auto& cpt = _bn_.cpt(node);
130
131 const double nb = gum::randomProba();
132 double cumul = 0.0;
133 for (particule.setFirstVar(var); !particule.end(); particule.incVar(var)) {
134 cumul += cpt[particule];
135 if (cumul >= nb) break;
136 }
137 if (particule.end()) particule.setLastVar(var);
138
139 if ((!evs.empty()) && evs.contains(var) && (evs.val(var) != particule.val(var))) {
140 reject = true;
141 break;
142 }
143
144 sample.at(node) = particule.val(var);
146 }
147 if (timeout > 0 && timer.step() > timeout) { break; }
148 if (reject) { continue; }
149 idSample++;
150 }
151
152 if (idSample > 0) {
153 if (idSample < nbSamples) _database_.resize(idSample);
154 } else {
155 _database_.clear();
156 }
157 _drawnSamples_ = true;
158
159 if (onProgress.hasListener()) {
161 std::format("Database of size {}({}) generated in {} seconds. Log2likelihood : {}",
162 idSample,
163 nbSamples,
164 timer.step(),
166 }
167
168 return _log2likelihood_;
169 }
Signaler< std::string_view > onStop
with a possible explanation for stopping
Signaler< Size, double > onProgress
Progression (percent) and time.
double _log2likelihood_
log2Likelihood of generated samples
double randomProba()
Returns a random double between 0 and 1 included (i.e.
#define GUM_EMIT2(signal, arg1, arg2)
Definition signaler.h:290
#define GUM_EMIT1(signal, arg1)
Definition signaler.h:289

References _bn_, _database_, _drawnSamples_, _log2likelihood_, _nbVars_, gum::Instantiation::add(), gum::Instantiation::contains(), gum::Instantiation::empty(), gum::Instantiation::end(), GUM_EMIT1, GUM_EMIT2, gum::Instantiation::incVar(), gum::ProgressNotifier::onProgress, gum::ProgressNotifier::onStop, gum::randomProba(), gum::Timer::reset(), gum::Instantiation::setFirstVar(), gum::Instantiation::setLastVar(), gum::Timer::step(), and gum::Instantiation::val().

Here is the call graph for this function:

◆ log2likelihood()

template<GUM_Numeric GUM_SCALAR>
double gum::learning::BNDatabaseGenerator< GUM_SCALAR >::log2likelihood ( ) const

returns log2Likelihood of generated samples

Definition at line 466 of file BNDatabaseGenerator_tpl.h.

466 {
467 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
468 return _log2likelihood_;
469 }

References _drawnSamples_, _log2likelihood_, and GUM_ERROR.

◆ operator=() [1/2]

template<GUM_Numeric GUM_SCALAR>
BNDatabaseGenerator & gum::learning::BNDatabaseGenerator< GUM_SCALAR >::operator= ( BNDatabaseGenerator< GUM_SCALAR > && )
privatedelete

References BNDatabaseGenerator().

Here is the call graph for this function:

◆ operator=() [2/2]

template<GUM_Numeric GUM_SCALAR>
BNDatabaseGenerator & gum::learning::BNDatabaseGenerator< GUM_SCALAR >::operator= ( const BNDatabaseGenerator< GUM_SCALAR > & )
privatedelete

References BNDatabaseGenerator().

Here is the call graph for this function:

◆ samplesAt()

template<GUM_Numeric GUM_SCALAR>
Idx gum::learning::BNDatabaseGenerator< GUM_SCALAR >::samplesAt ( Idx row,
Idx col ) const

generate and stock database, returns log2likelihood using ProgressNotifier as notification

draw instances from bn

Definition at line 186 of file BNDatabaseGenerator_tpl.h.

186 {
187 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
188 return _database_.at(row).at(_varOrder_.at(col));
189 }

References _database_, _drawnSamples_, _varOrder_, and GUM_ERROR.

◆ samplesLabelAt()

template<GUM_Numeric GUM_SCALAR>
std::string gum::learning::BNDatabaseGenerator< GUM_SCALAR >::samplesLabelAt ( Idx row,
Idx col ) const

generate and stock database, returns log2likelihood using ProgressNotifier as notification

draw instances from bn

Definition at line 192 of file BNDatabaseGenerator_tpl.h.

192 {
193 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
194 const auto j = _varOrder_.at(col);
195 return _label_(_database_.at(row), _bn_.variable(j), j);
196 }
std::string _label_(const std::vector< Idx > &row, const DiscreteVariable &v, Idx i) const
return the final string for a label (taking into account the behavior for DiscretizedVariable) from a...

References _bn_, _database_, _drawnSamples_, _label_(), _varOrder_, and GUM_ERROR.

Here is the call graph for this function:

◆ samplesNbCols()

template<GUM_Numeric GUM_SCALAR>
Size gum::learning::BNDatabaseGenerator< GUM_SCALAR >::samplesNbCols ( ) const

generate and stock database, returns log2likelihood using ProgressNotifier as notification

draw instances from bn

Definition at line 179 of file BNDatabaseGenerator_tpl.h.

179 {
180 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
181
182 return _nbVars_;
183 }

References _drawnSamples_, _nbVars_, and GUM_ERROR.

◆ samplesNbRows()

template<GUM_Numeric GUM_SCALAR>
Size gum::learning::BNDatabaseGenerator< GUM_SCALAR >::samplesNbRows ( ) const

generate and stock database, returns log2likelihood using ProgressNotifier as notification

draw instances from bn

Definition at line 172 of file BNDatabaseGenerator_tpl.h.

172 {
173 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
174
175 return _database_.size();
176 }

References _database_, _drawnSamples_, and GUM_ERROR.

◆ setAntiTopologicalVarOrder()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setAntiTopologicalVarOrder ( )

set columns in antiTopoligical order

set columns in antiTopological order

Definition at line 424 of file BNDatabaseGenerator_tpl.h.

424 {
426 varOrder.reserve(_nbVars_);
427 for (const auto& v: _bn_.topologicalOrder()) {
428 varOrder.push_back(v);
429 }
430 std::reverse(varOrder.begin(), varOrder.end());
432 }
void setVarOrder(const std::vector< Idx > &varOrder)
change columns order

References _bn_, _nbVars_, setVarOrder(), and varOrder().

Here is the call graph for this function:

◆ setDiscretizedLabelModeInterval()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeInterval ( )

set the behaviour of sampling for discretized variable to select the label : "[min,max["

Definition at line 209 of file BNDatabaseGenerator_tpl.h.

References _discretizedLabelMode_, and INTERVAL.

◆ setDiscretizedLabelModeMedian()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeMedian ( )

set the behaviour of sampling for discretized variable to deterministic select double median of intervalls

Definition at line 204 of file BNDatabaseGenerator_tpl.h.

References _discretizedLabelMode_, and MEDIAN.

◆ setDiscretizedLabelModeRandom()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeRandom ( )

set the behaviour of sampling for discretized variable to uniformly draw double value

Warning
: each call to toCSV or toDatabase that use labels will then generate different values
: this is the default behaviour

Definition at line 199 of file BNDatabaseGenerator_tpl.h.

References _discretizedLabelMode_, and RANDOM.

◆ setRandomVarOrder()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setRandomVarOrder ( )

set columns in random order

Definition at line 436 of file BNDatabaseGenerator_tpl.h.

436 {
438 varOrder.reserve(_bn_.size());
439 for (const auto& var: _bn_.internalDag()) {
440 varOrder.push_back(_bn_.variable(var).name());
441 }
444 }

References _bn_, gum::randomGenerator(), setVarOrder(), and varOrder().

Here is the call graph for this function:

◆ setTopologicalVarOrder()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setTopologicalVarOrder ( )

set columns in topoligical order

set columns in Topological order

Definition at line 413 of file BNDatabaseGenerator_tpl.h.

413 {
415 varOrder.reserve(_nbVars_);
416 for (const auto& v: _bn_.topologicalOrder()) {
417 varOrder.push_back(v);
418 }
420 }

References _bn_, _nbVars_, setVarOrder(), and varOrder().

Here is the call graph for this function:

◆ setVarOrder() [1/2]

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setVarOrder ( const std::vector< Idx > & varOrder)

change columns order

Definition at line 375 of file BNDatabaseGenerator_tpl.h.

375 {
376 if (varOrder.size() != _nbVars_)
377 GUM_ERROR(FatalError, "varOrder's size must be equal to the number of variables")
378
379 std::vector< bool > usedVars(_nbVars_, false);
380 for (const auto& i: varOrder) {
381 if (i >= _nbVars_) GUM_ERROR(FatalError, "varOrder contains invalid variables")
382 if (usedVars.at(i)) GUM_ERROR(FatalError, "varOrder must not have repeated variables")
383 usedVars.at(i) = true;
384 }
385
386 if (std::find(usedVars.begin(), usedVars.end(), false) != usedVars.end()) {
387 GUM_ERROR(FatalError, "varOrder must contain all variables")
388 }
389
391 }

References _nbVars_, _varOrder_, GUM_ERROR, and varOrder().

Referenced by setAntiTopologicalVarOrder(), setRandomVarOrder(), setTopologicalVarOrder(), setVarOrder(), and setVarOrderFromCSV().

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

◆ setVarOrder() [2/2]

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setVarOrder ( const std::vector< std::string > & varOrder)

change columns order using variable names

Definition at line 395 of file BNDatabaseGenerator_tpl.h.

395 {
397 varOrderIdx.reserve(varOrder.size());
398 for (const auto& vname: varOrder) {
399 varOrderIdx.push_back(_names2ids_.second(vname));
400 }
402 }

References _names2ids_, setVarOrder(), and varOrder().

Here is the call graph for this function:

◆ setVarOrderFromCSV()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::setVarOrderFromCSV ( std::string_view csvFileURL,
std::string_view csvSeparator = "," )

change columns order according to a csv file

Definition at line 406 of file BNDatabaseGenerator_tpl.h.

References _varOrderFromCSV_(), and setVarOrder().

Here is the call graph for this function:

◆ toCSV()

template<GUM_Numeric GUM_SCALAR>
void gum::learning::BNDatabaseGenerator< GUM_SCALAR >::toCSV ( std::string_view csvFileURL,
bool useLabels = true,
bool append = false,
std::string csvSeparator = ",",
bool checkOnAppend = false ) const

generates csv representing the generated database

generates database, and writes csv file

Definition at line 215 of file BNDatabaseGenerator_tpl.h.

219 {
220 if (!_drawnSamples_) { GUM_ERROR(OperationNotAllowed, "drawSamples() must be called first.") }
221
222 if (csvSeparator.find('\n') != std::string::npos) {
223 GUM_ERROR(InvalidArgument, "csvSeparator must not contain end-line characters")
224 }
225
226 bool includeHeader = true;
227 if (append) {
229 if (csvFile) {
233 "Inconsistent variable order in csvFile when appending. You "
234 "can use setVarOrderFromCSV(url) function to get the right "
235 "order. You could also set parameter checkOnAppend=false if you "
236 "know what you are doing.")
237 includeHeader = false;
238 }
239 csvFile.close();
240 }
241
242
244
246 bool firstCol = true;
247 if (includeHeader) {
248 for (const auto& i: _varOrder_) {
249 if (firstCol) {
250 firstCol = false;
251 } else {
252 os << csvSeparator;
253 }
254 os << _bn_.variable(i).name();
255 }
256 }
257 os << std::endl;
258
259 bool firstRow = true;
260 for (const auto& row: _database_) {
261 if (firstRow) {
262 firstRow = false;
263 } else {
264 os << std::endl;
265 }
266 firstCol = true;
267 for (const auto& i: _varOrder_) {
268 if (firstCol) {
269 firstCol = false;
270 } else {
271 os << csvSeparator;
272 }
273 if (useLabels) {
274 const auto& v = _bn_.variable(i);
275 if (v.varType() == VarType::DISCRETIZED) {
276 switch (_discretizedLabelMode_) {
277 case DiscretizedLabelMode::MEDIAN : os << v.numerical(row.at(i)); break;
280 break;
281 case DiscretizedLabelMode::INTERVAL : os << v.label(row.at(i)); break;
282 }
283 } else {
284 os << v.label(row.at(i));
285 }
286 } else {
287 os << row[i];
288 }
289 }
290 }
291
292 os.close();
293 }

References _bn_, _database_, _discretizedLabelMode_, _drawnSamples_, _varOrder_, _varOrderFromCSV_(), gum::DISCRETIZED, GUM_ERROR, INTERVAL, MEDIAN, RANDOM, and varOrder().

Here is the call graph for this function:

◆ toDatabaseTable()

template<GUM_Numeric GUM_SCALAR>
DatabaseTable gum::learning::BNDatabaseGenerator< GUM_SCALAR >::toDatabaseTable ( bool useLabels = true) const

generates a DatabaseVectInRAM

Definition at line 313 of file BNDatabaseGenerator_tpl.h.

313 {
314 if (!_drawnSamples_) GUM_ERROR(OperationNotAllowed, "proceed() must be called first.")
315
317 std::vector< std::string > varNames;
320 varNames.push_back(_names2ids_.first(i));
321 }
322
323 // create the translators
324 for (std::size_t i = 0; i < _nbVars_; ++i) {
325 const Variable& var = _bn_.variable(_varOrder_[i]);
326 db.insertTranslator(var, i);
327 }
328
329 if (useLabels) {
331 for (const auto& row: _database_) {
332 for (Idx i = 0; i < _nbVars_; ++i) {
333 const Idx j = _varOrder_.at(i);
334 xrow[i] = _label_(row, _bn_.variable(j), j);
335 }
336 db.insertRow(xrow);
337 }
338 } else {
340 for (std::size_t i = 0; i < _nbVars_; ++i) {
341 translatorType[i] = db.translator(i).getValType();
342 }
345 for (const auto& row: _database_) {
346 for (Idx i = 0; i < _nbVars_; ++i) {
347 const Idx j = _varOrder_.at(i);
349 xrow[i].discr_val = std::size_t(row.at(j));
350 else xrow[i].cont_val = float(row.at(j));
351 }
352 }
353 db.insertRow(xrow, xmiss);
354 }
355
356 return db;
357 }

References _bn_, _database_, _drawnSamples_, _label_(), _names2ids_, _nbVars_, _varOrder_, gum::learning::DISCRETE, gum::learning::DBTranslator::getValType(), GUM_ERROR, gum::learning::DatabaseTable::insertRow(), gum::learning::DatabaseTable::insertTranslator(), and gum::learning::DatabaseTable::translator().

Here is the call graph for this function:

◆ varOrder()

template<GUM_Numeric GUM_SCALAR>
std::vector< Idx > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::varOrder ( ) const

returns variable order indexes

Definition at line 448 of file BNDatabaseGenerator_tpl.h.

448 {
449 return _varOrder_;
450 }

References _varOrder_.

Referenced by _varOrderFromCSV_(), _varOrderFromCSV_(), setAntiTopologicalVarOrder(), setRandomVarOrder(), setTopologicalVarOrder(), setVarOrder(), setVarOrder(), and toCSV().

Here is the caller graph for this function:

◆ varOrderNames()

template<GUM_Numeric GUM_SCALAR>
std::vector< std::string > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::varOrderNames ( ) const

returns variable order.

Definition at line 454 of file BNDatabaseGenerator_tpl.h.

454 {
456 varNames.reserve(_nbVars_);
457 for (const auto& i: _varOrder_) {
458 varNames.push_back(_names2ids_.first(i));
459 }
460
461 return varNames;
462 }

References _names2ids_, _nbVars_, and _varOrder_.

Member Data Documentation

◆ _bn_

template<GUM_Numeric GUM_SCALAR>
const BayesNet< GUM_SCALAR >& gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_bn_
private

◆ _database_

template<GUM_Numeric GUM_SCALAR>
std::vector< std::vector< Idx > > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_database_
private

generated database

Definition at line 228 of file BNDatabaseGenerator.h.

Referenced by database(), drawSamples(), samplesAt(), samplesLabelAt(), samplesNbRows(), toCSV(), and toDatabaseTable().

◆ _discretizedLabelMode_

template<GUM_Numeric GUM_SCALAR>
DiscretizedLabelMode gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_discretizedLabelMode_
private

◆ _drawnSamples_

template<GUM_Numeric GUM_SCALAR>
bool gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_drawnSamples_ = false
private

whether drawSamples has been already called.

Definition at line 234 of file BNDatabaseGenerator.h.

Referenced by database(), drawSamples(), log2likelihood(), samplesAt(), samplesLabelAt(), samplesNbCols(), samplesNbRows(), toCSV(), and toDatabaseTable().

◆ _log2likelihood_

template<GUM_Numeric GUM_SCALAR>
double gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_log2likelihood_ = 0
private

log2Likelihood of generated samples

Definition at line 237 of file BNDatabaseGenerator.h.

Referenced by drawSamples(), and log2likelihood().

◆ _names2ids_

template<GUM_Numeric GUM_SCALAR>
Bijection< std::string, NodeId > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_names2ids_
private

bijection nodes names

Definition at line 222 of file BNDatabaseGenerator.h.

Referenced by BNDatabaseGenerator(), _varOrderFromCSV_(), setVarOrder(), toDatabaseTable(), and varOrderNames().

◆ _nbVars_

◆ _varOrder_

template<GUM_Numeric GUM_SCALAR>
std::vector< Idx > gum::learning::BNDatabaseGenerator< GUM_SCALAR >::_varOrder_
private

variable order in generated database

Definition at line 231 of file BNDatabaseGenerator.h.

Referenced by BNDatabaseGenerator(), database(), samplesAt(), samplesLabelAt(), setVarOrder(), toCSV(), toDatabaseTable(), varOrder(), and varOrderNames().

◆ onProgress

Signaler< Size, double > gum::ProgressNotifier::onProgress
inherited

Progression (percent) and time.

Definition at line 69 of file progressNotification.h.

Referenced by gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples().

◆ onStop

Signaler< std::string_view > gum::ProgressNotifier::onStop
inherited

with a possible explanation for stopping

Definition at line 72 of file progressNotification.h.

Referenced by gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples().


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