57 template < GUM_Numeric GUM_SCALAR >
62 template < GUM_Numeric GUM_SCALAR >
67 template < GUM_Numeric GUM_SCALAR >
72 template < GUM_Numeric GUM_SCALAR >
78 template < GUM_Numeric GUM_SCALAR >
83 template < GUM_Numeric GUM_SCALAR >
88 template < GUM_Numeric GUM_SCALAR >
91 return std::string{base} +
'[' + std::to_string(slice) +
']';
94 template < GUM_Numeric GUM_SCALAR >
95 std::pair< std::string, int >
102 const std::size_t bracketPos = name.rfind(
'[');
105 const std::string_view bracketContent{name.c_str() + bracketPos + 1,
106 name.size() - bracketPos - 1};
107 if (bracketContent.empty() || bracketContent.back() !=
']')
110 const std::string_view digits = bracketContent.substr(0, bracketContent.size() - 1);
112 for (
const char c: digits)
113 if (std::isdigit(
static_cast< unsigned char >(c)) == 0)
118 slice = std::stoi(std::string{digits});
119 }
catch (
const std::out_of_range&) {
121 "Node name '" << name <<
"' has a slice index too large to represent as int.")
123 return {name.substr(0, bracketPos), slice};
126 template < GUM_Numeric GUM_SCALAR >
128 std::string_view head,
129 std::string_view action)
const {
134 "cannot " << action <<
" " << tail <<
" -> " << head
135 <<
": a temporal variable can never be a parent of an atemporal one; "
136 "this constraint is part of the k-TBN definition")
137 if (headSlice < tailSlice)
139 "cannot " << action <<
" " << tail <<
" -> " << head
140 <<
": its head is at an earlier time slice than its tail, which "
141 "violates temporal causality")
144 template < GUM_Numeric GUM_SCALAR >
146 std::string_view dirPath,
147 std::string_view csvBaseName,
149 const std::vector< std::string >& missingSymbols) {
150 namespace fs = std::filesystem;
151 const fs::path dir{dirPath};
152 const std::string stem{csvBaseName};
153 const std::unordered_set< std::string > missing(missingSymbols.begin(), missingSymbols.end());
155 std::vector< std::string > header;
156 std::vector< std::size_t > candidates;
157 std::vector< std::string > firstSeen;
161 for (
Size i = 0; i < nbSamples; ++i) {
166 if (i > 0 && candidates.empty())
break;
168 const fs::path file = dir / (stem + std::to_string(i + 1) +
".csv");
169 std::ifstream is(file, std::ifstream::in);
174 const auto& raw = parser.
current();
177 header.assign(raw.begin(), raw.end());
178 candidates.resize(header.size());
179 std::iota(candidates.begin(), candidates.end(), std::size_t{0});
181 bool same = (raw.size() == header.size());
182 for (std::size_t c = 0; same && c < header.size(); ++c)
183 same = (raw[c] == header[c]);
186 "Header of " << file.string() <<
" differs from trajectory 1");
189 haveFirstSeen.assign(header.size(),
false);
190 firstSeen.assign(header.size(), {});
192 while (parser.
next()) {
193 const auto& tokens = parser.
current();
194 if (tokens.size() != header.size())
196 "Trajectory " << (i + 1) <<
", row " << parser.
nbLine() <<
": expected "
197 << header.size() <<
" columns, got " << tokens.size());
200 for (std::size_t idx = 0; idx < candidates.size();) {
201 const std::size_t c = candidates[idx];
202 if (missing.contains(tokens[c])) {
206 if (!haveFirstSeen[c]) {
207 firstSeen[c] = tokens[c];
208 haveFirstSeen[c] =
true;
210 }
else if (tokens[c] != firstSeen[c]) {
211 candidates[idx] = candidates.back();
212 candidates.pop_back();
217 if (candidates.empty())
break;
221 std::unordered_set< std::string > atemporalVars;
222 for (
const std::size_t c: candidates)
223 atemporalVars.insert(header[c]);
224 return atemporalVars;
227 template < GUM_Numeric GUM_SCALAR >
229 std::string_view context)
const {
230 const std::string b{base};
233 "unknown base variable '" << base
234 <<
"': it is not one of this learner's variables")
237 "atemporal variable '" << base <<
"' cannot appear in " << context
238 <<
": it has no per-slice instance")
241 template < GUM_Numeric GUM_SCALAR >
245 "a k-TBN learner requires "
246 << label <<
" >= 2: k=1 is a static Bayesian network, use BNLearner instead")
Common configuration interface for k-TBN learners.
T typed(std::string_view section, std::string_view key) const
Value of section.key converted according to its declared type.
Exception : input/output problem.
Exception: at least one argument passed to a function is not what was expected.
static constexpr int ATEMPORAL
Conventional time-slice value denoting an atemporal (static) variable.
Class for fast parsing of CSV file (never more than one line in application memory).
bool next()
gets the next line of the csv stream and parses it
std::size_t nbLine() const
returns the current line number within the stream
const std::vector< std::string > & current() const
returns the current parsed line
Pure-virtual configuration interface shared by all k-TBN learners.
virtual bool _isKnownBase_(std::string_view base) const =0
Whether base is one of this learner's variables, temporal or atemporal. The second subclass-specific ...
virtual IKTBNLearner< GUM_SCALAR > & useSmoothingPrior(double weight)=0
smoothing prior with the default weight (key priorWeight of the section learning of gum::Configuratio...
void _checkBaseIsTemporal_(std::string_view base, std::string_view context) const
Throw InvalidArgument unless base is a known temporal base. context completes "cannot appear in <cont...
void _checkArcTemporallyFeasible_(std::string_view tail, std::string_view head, std::string_view action) const
Reject an arc the k-TBN definition can never contain, so eraseForbiddenArc and addMandatoryArc both f...
static std::unordered_set< std::string > _scanConstantColumns_(std::string_view dirPath, std::string_view csvBaseName, Size nbSamples, const std::vector< std::string > &missingSymbols)
Scans every trajectory and returns the base names classified atemporal: those whose value never chang...
virtual const std::unordered_set< std::string > & _atemporalVarNames_() const =0
The base names of the atemporal (static) variables. The only subclass-specific input to determineNode...
static void _checkMinimalOrder_(Size order, std::string_view label)
Throw InvalidArgument unless order is at least 2, label naming the offending parameter ("k" for the f...
static double defaultPriorWeight_()
IKTBNLearner< GUM_SCALAR > & useLocalSearchWithTabuList()
local search with tabu list and the default parameters (keys tabuListSize and tabuNbDecrease of the s...
static Size defaultTabuListSize_()
std::string _encode_(std::string_view base, int slice) const
(base, slice) -> engine name ("A[1]" / atemporal engine name). Pure function, shared by every learner...
IKTBNLearner< GUM_SCALAR > & useSmoothingPrior()
smoothing prior with the default weight (key priorWeight of the section learning of gum::Configuratio...
virtual IKTBNLearner< GUM_SCALAR > & useLocalSearchWithTabuList(Size tabu_size, Size nb_decrease)=0
local search with tabu list, with the default number of decreasing changes (key tabuNbDecrease of the...
std::pair< std::string, int > _determineNode_(const std::string &name) const
engine name -> (base, slice); atemporal names map to KTBN::ATEMPORAL. Shared by every learner; only t...
static Size defaultTabuNbDecrease_()
Typed, documented, sectioned configuration singleton read from an .ini content.
#define GUM_ERROR(type, msg)
std::size_t Size
In aGrUM, hashed values are unsigned long int.
GUM_SHARED_PUBLIC bool contains(std::string_view s, std::string_view needle)
true if needle in s
include the inlined functions if necessary
gum is the global namespace for all aGrUM entities
Configuration & config()
A short name for Configuration::instance() : gum::config().epsilonStandard(). It is not inline so tha...