![]() |
aGrUM 3.2.0
a C++ library for (probabilistic) graphical models
|
Generates a database of trajectories from a k-DBN (one CSV per trajectory). More...
#include <agrum/KTBN/database/KTBNDatabaseGenerator.h>
Classes | |
| struct | ParentRef |
| a parent of a template node, precompiled for fast sampling More... | |
| struct | NodeRef |
| a template node, precompiled for fast sampling More... | |
Public Types | |
| enum class | DiscretizedLabelMode : char { INTERVAL , MEDIAN , RANDOM } |
| rendering of discretized variables when labels are requested More... | |
| enum class | VarOrderMode : char { RANDOM , TOPOLOGICAL , ANTI_TOPOLOGICAL } |
| column order used for the exported CSV More... | |
Public Member Functions | |
Constructors / Destructors | |
| KTBNDatabaseGenerator (const KTBN< GUM_SCALAR > &kdbn) | |
| Constructor. | |
| ~KTBNDatabaseGenerator () | |
| destructor | |
Accessors / Modifiers | |
| 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::vector< double > | drawSamples (const std::vector< Size > &nbTimeSlices, std::string_view dirPath, std::string_view csvBaseName, VarOrderMode mode=VarOrderMode::RANDOM, bool useLabels=true, std::string csvSeparator=",") |
| Like drawSamples(), but every trajectory may have its own horizon. | |
| void | setDiscretizedLabelModeRandom () |
| set discretized-label rendering to a uniform random draw in the interval (this is the default; each labelled export then differs) | |
| void | setDiscretizedLabelModeMedian () |
| set discretized-label rendering to the (deterministic) interval median | |
| void | setDiscretizedLabelModeInterval () |
| set discretized-label rendering to the interval label "[min,max[" | |
| Size | nbVars () const |
| returns the number of base variable columns | |
Public Attributes | |
| Signaler< Size, double > | onProgress |
| Progression (percent) and time. | |
| Signaler< std::string_view > | onStop |
| with a possible explanation for stopping | |
Private Member Functions | |
| void | _build_ (const KTBN< GUM_SCALAR > &kdbn) |
| one-shot initialisation called by the constructor: fills the column index (baseCols, nbVars), the topological node/parent cache (nodes, kernel), the per-column representatives (vars), and the shared instantiation (inst). All cached pointers refer to template. | |
| 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 log2likelihood | |
| 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) | |
| 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 nbVars) in canonical column order; colOrder gives the output column order. | |
| void | setVarOrderRandomized (std::vector< Idx > &colOrder) const |
| builds a uniformly random column order | |
| void | setVarOrderTopological (std::vector< Idx > &colOrder) const |
| builds a topological column order (transition-kernel projection) | |
| void | setVarOrderAntiTopological (std::vector< Idx > &colOrder) const |
| builds the reverse of setVarOrderTopological() | |
| 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 (when non-null) else from fixedLen; samples each trajectory and writes it straight to its own CSV file. | |
| KTBNDatabaseGenerator (const KTBNDatabaseGenerator &)=delete | |
| KTBNDatabaseGenerator (KTBNDatabaseGenerator &&)=delete | |
| KTBNDatabaseGenerator & | operator= (const KTBNDatabaseGenerator &)=delete |
| KTBNDatabaseGenerator & | operator= (KTBNDatabaseGenerator &&)=delete |
Static Private Member Functions | |
| 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, t); anything else (bare or bracket-named atemporal node) -> (name, ATEMPORAL). | |
Private Attributes | |
| BayesNet< GUM_SCALAR > | _template_ |
| the \(k\)-slice template (a small copy, independent of the horizon) | |
| Size | _k_ |
| the order \(k\) of the k-DBN | |
| Size | _nbVars_ |
| number of base variable columns | |
| std::vector< std::string > | _baseCols_ |
| col index -> base name (canonical column numbering) | |
| std::vector< const DiscreteVariable * > | _vars_ |
| one representative variable per base column (same order as baseCols), pointing into template so it outlives the source k-DBN. Label rendering only. | |
| std::vector< NodeRef > | _nodes_ |
| all template nodes in topological order (drives Phase 1, the bootstrap) | |
| std::vector< Idx > | _kernel_ |
| indices, in nodes, of the slice-(k-1) nodes (the transition kernel, drives Phase 2); already in topological order | |
| Instantiation | _inst_ |
| a shared instantiation over all template variables, so we don't have to rebuild it for every draw | |
| DiscretizedLabelMode | _discretizedLabelMode_ = DiscretizedLabelMode::RANDOM |
| rendering of discretized variables when labels are requested | |
Generates a database of trajectories from a k-DBN (one CSV per trajectory).
Definition at line 108 of file KTBNDatabaseGenerator.h.
|
strong |
rendering of discretized variables when labels are requested
| Enumerator | |
|---|---|
| INTERVAL | |
| MEDIAN | |
| RANDOM | |
Definition at line 111 of file KTBNDatabaseGenerator.h.
|
strong |
column order used for the exported CSV
| Enumerator | |
|---|---|
| RANDOM | |
| TOPOLOGICAL | |
| ANTI_TOPOLOGICAL | |
Definition at line 114 of file KTBNDatabaseGenerator.h.
|
explicit |
Constructor.
| kdbn | The k-DBN to sample from (only its \(k\)-slice template is copied, via toBN(); the k-DBN itself is not retained). The horizon is not fixed here; it is passed to drawSamples(). |
Definition at line 77 of file KTBNDatabaseGenerator_tpl.h.
References KTBNDatabaseGenerator(), _build_(), _k_, and _template_.
Referenced by KTBNDatabaseGenerator(), KTBNDatabaseGenerator(), KTBNDatabaseGenerator(), ~KTBNDatabaseGenerator(), operator=(), and operator=().
| gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::~KTBNDatabaseGenerator | ( | ) |
destructor
Definition at line 84 of file KTBNDatabaseGenerator_tpl.h.
References KTBNDatabaseGenerator().
|
privatedelete |
|
privatedelete |
|
private |
one-shot initialisation called by the constructor: fills the column index (baseCols, nbVars), the topological node/parent cache (nodes, kernel), the per-column representatives (vars), and the shared instantiation (inst). All cached pointers refer to template.
Definition at line 108 of file KTBNDatabaseGenerator_tpl.h.
References _baseCols_, _decode_(), _inst_, _k_, _kernel_, _nbVars_, _nodes_, _template_, _vars_, gum::KTBN< GUM_SCALAR >::ATEMPORAL, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::col, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::col, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::cpt, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::isAtemporal, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::lag, gum::Variable::name(), gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::parents, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::slice, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::var, and gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::var.
Referenced by KTBNDatabaseGenerator().
|
staticprivate |
decodes a template node name into (base, slice): "B[t]" with B a known temporal process -> (B, t); anything else (bare or bracket-named atemporal node) -> (name, ATEMPORAL).
Definition at line 89 of file KTBNDatabaseGenerator_tpl.h.
References gum::KTBN< GUM_SCALAR >::ATEMPORAL.
Referenced by _build_().
|
private |
the single worker behind both drawSamples() overloads. Trajectory i's horizon is read from perTraj (when non-null) else from fixedLen; samples each trajectory and writes it straight to its own CSV file.
The single worker behind both public drawSamples() overloads. Trajectory i's horizon T is read from perTraj when given, else from the shared fixedLen. Each trajectory is sampled into a flat row-major buffer (T × nbVars, in canonical column order) in two phases and written straight to its own CSV:
Phase 1 — initial k slices (0..k-1): no history yet, so every node of the k-slice template is drawn from scratch in topological order by inverse-CDF.
Phase 2 — transition (slices k..T-1): only the slice-(k-1) nodes (the kernel, already topological) are drawn, each parent read from the row at time (t - lag).
Definition at line 229 of file KTBNDatabaseGenerator_tpl.h.
References _drawVar_(), _inst_, _k_, _kernel_, _nbVars_, _nodes_, _writeTrajectory_(), ANTI_TOPOLOGICAL, gum::KTBN< GUM_SCALAR >::ATEMPORAL, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::col, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::col, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::cpt, GUM_EMIT1, GUM_EMIT2, GUM_ERROR, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::isAtemporal, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::lag, gum::ProgressNotifier::onProgress, gum::ProgressNotifier::onStop, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::parents, RANDOM, setVarOrderAntiTopological(), setVarOrderRandomized(), setVarOrderTopological(), TOPOLOGICAL, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::NodeRef::var, and gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::var.
Referenced by drawSamples(), and drawSamples().
|
private |
inverse-CDF draw of var given the parents already set in inst; accumulates log2(P(drawn value)) into log2likelihood
Definition at line 164 of file KTBNDatabaseGenerator_tpl.h.
References _inst_, and gum::randomProba().
Referenced by _drawSamples_().
|
private |
renders the label of modality idx of base column col (taking the discretized-label mode into account)
Definition at line 346 of file KTBNDatabaseGenerator_tpl.h.
References _discretizedLabelMode_, _vars_, gum::DISCRETIZED, GUM_ERROR, INTERVAL, gum::DiscreteVariable::label(), MEDIAN, gum::DiscreteVariable::numerical(), RANDOM, and gum::DiscreteVariable::varType().
Referenced by _writeTrajectory_().
|
private |
writes one trajectory CSV (header + T rows) to csvFileURL. traj is the flat row-major buffer (T x nbVars) in canonical column order; colOrder gives the output column order.
Definition at line 361 of file KTBNDatabaseGenerator_tpl.h.
References _baseCols_, _label_(), _nbVars_, and GUM_ERROR.
Referenced by _drawSamples_().
| std::vector< double > gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::drawSamples | ( | const std::vector< Size > & | nbTimeSlices, |
| std::string_view | dirPath, | ||
| std::string_view | csvBaseName, | ||
| VarOrderMode | mode = VarOrderMode::RANDOM, | ||
| bool | useLabels = true, | ||
| std::string | csvSeparator = "," ) |
Like drawSamples(), but every trajectory may have its own horizon.
| nbTimeSlices | One horizon per trajectory; its size is the number of trajectories. Every entry must be \(\geq k\). |
| dirPath | Directory to write the CSV files into. |
| csvBaseName | Stem for each file name (index and .csv appended). |
| mode | The column order of the base variables. |
| useLabels | Render values as variable labels (else modality index). |
| csvSeparator | Column separator (must not contain a newline). |
| OperationNotAllowed | if some entry of nbTimeSlices is smaller than \(k\). |
Definition at line 200 of file KTBNDatabaseGenerator_tpl.h.
References _drawSamples_().
| std::vector< double > gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::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.
File names are csvBaseName followed by the 1-based trajectory index and ".csv" (e.g. "traj1.csv", "traj2.csv", …). Each file has one column per base variable in the order chosen by mode, and nbTimeSlices data rows.
| nbSamples | The number of trajectories to generate. |
| nbTimeSlices | The horizon \(T\) shared by every trajectory. Must be \(\geq k\). |
| dirPath | Directory to write the CSV files into. |
| csvBaseName | Stem for each file name (index and .csv appended). |
| mode | The column order of the base variables. |
| useLabels | Render values as variable labels (else modality index). |
| csvSeparator | Column separator (must not contain a newline). |
nbSamples). | OperationNotAllowed | if nbTimeSlices is smaller than \(k\). |
Definition at line 180 of file KTBNDatabaseGenerator_tpl.h.
References _drawSamples_().
| INLINE Size gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::nbVars | ( | ) | const |
returns the number of base variable columns
Definition at line 72 of file KTBNDatabaseGenerator_tpl.h.
References _nbVars_.
|
privatedelete |
|
privatedelete |
| void gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeInterval | ( | ) |
set discretized-label rendering to the interval label "[min,max["
Definition at line 341 of file KTBNDatabaseGenerator_tpl.h.
References _discretizedLabelMode_, and INTERVAL.
| void gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeMedian | ( | ) |
set discretized-label rendering to the (deterministic) interval median
Definition at line 336 of file KTBNDatabaseGenerator_tpl.h.
References _discretizedLabelMode_, and MEDIAN.
| void gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::setDiscretizedLabelModeRandom | ( | ) |
set discretized-label rendering to a uniform random draw in the interval (this is the default; each labelled export then differs)
Definition at line 331 of file KTBNDatabaseGenerator_tpl.h.
References _discretizedLabelMode_, and RANDOM.
|
private |
builds the reverse of setVarOrderTopological()
Definition at line 460 of file KTBNDatabaseGenerator_tpl.h.
References setVarOrderTopological().
Referenced by _drawSamples_().
|
private |
builds a uniformly random column order
Definition at line 393 of file KTBNDatabaseGenerator_tpl.h.
References _nbVars_, and gum::randomGenerator().
Referenced by _drawSamples_().
|
private |
builds a topological column order (transition-kernel projection)
Definition at line 401 of file KTBNDatabaseGenerator_tpl.h.
References _baseCols_, _k_, _nbVars_, _nodes_, gum::KTBN< GUM_SCALAR >::ATEMPORAL, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::col, GUM_ERROR, gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::isAtemporal, and gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::ParentRef::lag.
Referenced by _drawSamples_(), and setVarOrderAntiTopological().
|
private |
col index -> base name (canonical column numbering)
Definition at line 227 of file KTBNDatabaseGenerator.h.
Referenced by _build_(), _writeTrajectory_(), and setVarOrderTopological().
|
private |
rendering of discretized variables when labels are requested
Definition at line 245 of file KTBNDatabaseGenerator.h.
Referenced by _label_(), setDiscretizedLabelModeInterval(), setDiscretizedLabelModeMedian(), and setDiscretizedLabelModeRandom().
|
private |
a shared instantiation over all template variables, so we don't have to rebuild it for every draw
Definition at line 242 of file KTBNDatabaseGenerator.h.
Referenced by _build_(), _drawSamples_(), and _drawVar_().
|
private |
the order \(k\) of the k-DBN
Definition at line 221 of file KTBNDatabaseGenerator.h.
Referenced by KTBNDatabaseGenerator(), _build_(), _drawSamples_(), and setVarOrderTopological().
|
private |
indices, in nodes, of the slice-(k-1) nodes (the transition kernel, drives Phase 2); already in topological order
Definition at line 238 of file KTBNDatabaseGenerator.h.
Referenced by _build_(), and _drawSamples_().
|
private |
number of base variable columns
Definition at line 224 of file KTBNDatabaseGenerator.h.
Referenced by _build_(), _drawSamples_(), _writeTrajectory_(), nbVars(), setVarOrderRandomized(), and setVarOrderTopological().
|
private |
all template nodes in topological order (drives Phase 1, the bootstrap)
Definition at line 234 of file KTBNDatabaseGenerator.h.
Referenced by _build_(), _drawSamples_(), and setVarOrderTopological().
|
private |
the \(k\)-slice template (a small copy, independent of the horizon)
Definition at line 218 of file KTBNDatabaseGenerator.h.
Referenced by KTBNDatabaseGenerator(), and _build_().
|
private |
one representative variable per base column (same order as baseCols), pointing into template so it outlives the source k-DBN. Label rendering only.
Definition at line 231 of file KTBNDatabaseGenerator.h.
Progression (percent) and time.
Definition at line 69 of file progressNotification.h.
Referenced by gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::_drawSamples_(), and gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples().
|
inherited |
with a possible explanation for stopping
Definition at line 72 of file progressNotification.h.
Referenced by gum::learning::KTBNDatabaseGenerator< GUM_SCALAR >::_drawSamples_(), and gum::learning::BNDatabaseGenerator< GUM_SCALAR >::drawSamples().