57 template < GUM_Numeric GUM_SCALAR >
64 for (
const auto& var:
_bn_.internalDag()) {
65 auto name =
_bn_.variable(var).name();
76 template < GUM_Numeric GUM_SCALAR >
82 template < GUM_Numeric GUM_SCALAR >
89 template < GUM_Numeric GUM_SCALAR >
103 const auto topOrder =
_bn_.topologicalOrder();
108 particule.
add(
_bn_.variable(node));
116 while (idSample < nbSamples) {
118 auto p = int((idSample * 100) / nbSamples);
124 std::vector< Idx >& sample =
_database_.at(idSample);
127 const NodeId node = topOrder[rank];
128 const auto& var =
_bn_.variable(node);
129 const auto& cpt =
_bn_.cpt(node);
134 cumul += cpt[particule];
135 if (cumul >= nb)
break;
144 sample.at(node) = particule.
val(var);
147 if (timeout > 0 && timer.
step() > timeout) {
break; }
148 if (reject) {
continue; }
153 if (idSample < nbSamples)
_database_.resize(idSample);
161 std::format(
"Database of size {}({}) generated in {} seconds. Log2likelihood : {}",
171 template < GUM_Numeric GUM_SCALAR >
178 template < GUM_Numeric GUM_SCALAR >
185 template < GUM_Numeric GUM_SCALAR >
191 template < GUM_Numeric GUM_SCALAR >
198 template < GUM_Numeric GUM_SCALAR >
203 template < GUM_Numeric GUM_SCALAR >
208 template < GUM_Numeric GUM_SCALAR >
214 template < GUM_Numeric GUM_SCALAR >
218 std::string csvSeparator,
219 bool checkOnAppend)
const {
222 if (csvSeparator.find(
'\n') != std::string::npos) {
226 bool includeHeader =
true;
228 std::ifstream csvFile(std::filesystem::path{csvFileURL});
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;
243 auto ofstreamFlag = append ? std::ofstream::app : std::ofstream::out;
245 std::ofstream os(std::filesystem::path{csvFileURL}, ofstreamFlag);
246 bool firstCol =
true;
254 os <<
_bn_.variable(i).name();
259 bool firstRow =
true;
274 const auto& v =
_bn_.variable(i);
279 os << static_cast< const IDiscretizedVariable& >(v).draw(row.at(i));
284 os << v.label(row.at(i));
295 template < GUM_Numeric GUM_SCALAR >
308 return v.
label(row.at(i));
312 template < GUM_Numeric GUM_SCALAR >
317 std::vector< std::string > varNames;
324 for (std::size_t i = 0; i <
_nbVars_; ++i) {
330 std::vector< std::string > xrow(
_nbVars_);
339 std::vector< DBTranslatedValueType > translatorType(
_nbVars_);
340 for (std::size_t i = 0; i <
_nbVars_; ++i) {
344 const auto xmiss = gum::learning::DatabaseTable::IsMissing::False;
349 xrow[i].discr_val = std::size_t(row.at(j));
350 else xrow[i].cont_val = float(row.at(j));
360 template < GUM_Numeric GUM_SCALAR >
374 template < GUM_Numeric GUM_SCALAR >
379 std::vector< bool > usedVars(
_nbVars_,
false);
383 usedVars.at(i) =
true;
386 if (std::find(usedVars.begin(), usedVars.end(),
false) != usedVars.end()) {
394 template < GUM_Numeric GUM_SCALAR >
396 std::vector< Idx > varOrderIdx;
397 varOrderIdx.reserve(
varOrder.size());
405 template < GUM_Numeric GUM_SCALAR >
407 std::string_view csvSeparator) {
412 template < GUM_Numeric GUM_SCALAR >
416 for (
const auto& v:
_bn_.topologicalOrder()) {
423 template < GUM_Numeric GUM_SCALAR >
427 for (
const auto& v:
_bn_.topologicalOrder()) {
435 template < GUM_Numeric GUM_SCALAR >
437 std::vector< std::string >
varOrder;
439 for (
const auto& var:
_bn_.internalDag()) {
447 template < GUM_Numeric GUM_SCALAR >
453 template < GUM_Numeric GUM_SCALAR >
455 std::vector< std::string > varNames;
465 template < GUM_Numeric GUM_SCALAR >
472 template < GUM_Numeric GUM_SCALAR >
475 std::string_view csvSeparator)
const {
476 std::ifstream csvFile(std::filesystem::path{csvFileURL});
489 template < GUM_Numeric GUM_SCALAR >
492 std::string_view csvSeparator)
const {
494 std::vector< std::string > header_found;
496 while (std::getline(csvFile, line)) {
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();
503 pos = line.find(csvSeparator, pos);
505 if (pos == std::string::npos) header_found.push_back(line.substr(i, line.length()));
513 for (
const auto& hf: header_found) {
520 template < GUM_Numeric GUM_SCALAR >
Base class for discrete random variable.
virtual double numerical(Idx indice) const =0
get a numerical representation of the indice-th value.
VarType varType() const override=0
returns the varType of variable
virtual std::string label(Idx i) const =0
get the indice-th label. This method is pure virtual.
Exception : fatal (unknown ?) error.
A base class for discretized variables, independent of the ticks type.
Class for assigning/browsing values to tuples of discrete variables.
bool end() const
Returns true if the Instantiation reached the end.
void incVar(const DiscreteVariable &v)
Operator increment for variable v only.
void add(const DiscreteVariable &v) final
Adds a new variable in the Instantiation.
bool empty() const final
Returns true if the instantiation is empty.
bool contains(const DiscreteVariable &v) const final
Indicates whether a given variable belongs to the Instantiation.
void setFirstVar(const DiscreteVariable &v)
Assign the first value in the Instantiation for var v.
Idx val(Idx i) const
Returns the current value of the variable at position i.
void setLastVar(const DiscreteVariable &v)
Assign the last value in the Instantiation for var v.
Exception: at least one argument passed to a function is not what was expected.
Exception : the element we looked for cannot be found.
Exception : operation not allowed.
Signaler< std::string_view > onStop
with a possible explanation for stopping
Signaler< Size, double > onProgress
Progression (percent) and time.
Class used to compute response times for benchmark purposes.
void reset()
Reset the timer.
double step() const
Returns the delta time between now and the last reset() call (or the constructor).
Base class for every random variable.
bool _drawnSamples_
whether drawSamples has been already called.
std::vector< Idx > varOrder() const
returns variable order indexes
DatabaseTable toDatabaseTable(bool useLabels=true) const
generates a DatabaseVectInRAM
std::vector< Idx > _varOrderFromCSV_(std::string_view csvFileURL, std::string_view csvSeparator=",") const
returns varOrder from a csv file
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...
std::vector< std::vector< Idx > > database() const
generates database according to bn into a std::vector
void setDiscretizedLabelModeRandom()
set the behaviour of sampling for discretized variable to uniformly draw double value
double _log2likelihood_
log2Likelihood of generated samples
const BayesNet< GUM_SCALAR > & bn(void)
return const ref to the Bayes Net
BNDatabaseGenerator(const BayesNet< GUM_SCALAR > &bn)
default constructor
Size samplesNbCols() 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 setDiscretizedLabelModeInterval()
set the behaviour of sampling for discretized variable to select the label : "[min,...
Bijection< std::string, NodeId > _names2ids_
bijection nodes names
std::vector< std::vector< Idx > > _database_
generated database
DiscretizedLabelMode _discretizedLabelMode_
const BayesNet< GUM_SCALAR > & _bn_
Bayesian network.
void setAntiTopologicalVarOrder()
set columns in antiTopoligical order
Size samplesNbRows() const
generate and stock database, returns log2likelihood using ProgressNotifier as notification
double log2likelihood() const
returns log2Likelihood of generated samples
void setTopologicalVarOrder()
set columns in topoligical order
void setDiscretizedLabelModeMedian()
set the behaviour of sampling for discretized variable to deterministic select double median of inter...
double drawSamples(Size nbSamples)
generate and stock database, returns log2likelihood using ProgressNotifier as notification
std::vector< Idx > _varOrder_
variable order in generated database
void setVarOrderFromCSV(std::string_view csvFileURL, std::string_view csvSeparator=",")
change columns order according to a csv file
std::vector< std::string > varOrderNames() const
returns variable order.
Size _nbVars_
number of variables
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
void setVarOrder(const std::vector< Idx > &varOrder)
change columns order
Idx samplesAt(Idx row, Idx col) const
generate and stock database, returns log2likelihood using ProgressNotifier as notification
void setRandomVarOrder()
set columns in random order
~BNDatabaseGenerator()
destructor
The class for storing a record in a database.
DBTranslatedValueType getValType() const
returns the type of values handled by the translator
The class representing a tabular database as used by learning tasks.
std::size_t insertTranslator(const DBTranslator &translator, const std::size_t input_column, const bool unique_column=true)
insert a new translator into the database table
const DBTranslator & translator(const std::size_t k, const bool k_is_input_col=false) const
returns either the kth translator of the database table or the first one reading the kth column of th...
void insertRow(const std::vector< std::string > &new_row) override
insert a new row at the end of the database
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
Size Idx
Type for indexes.
Size NodeId
Type for node ids.
std::mt19937 & randomGenerator()
define a random_engine with correct seed
double randomProba()
Returns a random double between 0 and 1 included (i.e.
include the inlined functions if necessary
#define GUM_EMIT2(signal, arg1, arg2)
#define GUM_EMIT1(signal, arg1)
Class used to compute response times for benchmark purposes.