aGrUM 3.1.1
a C++ library for (probabilistic) graphical models
IBNLearner_tpl.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#pragma once
42
43
44#include <algorithm>
45
47
48namespace gum::learning {
49
50 template < GUM_Numeric GUM_SCALAR >
51 IBNLearner::Database::Database(std::string_view filename,
52 const BayesNet< GUM_SCALAR >& bn,
53 const std::vector< std::string >& missing_symbols) {
54 // assign to each column name in the database its position
56 DBInitializerFromCSV initializer{std::string(filename)};
57 const auto& xvar_names = initializer.variableNames();
58 std::size_t nb_vars = xvar_names.size();
60 for (std::size_t i = std::size_t(0); i < nb_vars; ++i)
61 var_names.insert(xvar_names[i], i);
62
63 // we use the bn to insert the translators into the database table
64 std::vector< NodeId > nodes;
65 nodes.reserve(bn.internalDag().sizeNodes());
66 for (const auto node: bn.internalDag())
67 nodes.push_back(node);
68 std::sort(nodes.begin(), nodes.end());
69 std::size_t i = std::size_t(0);
70 for (auto node: nodes) {
71 const Variable& var = bn.variable(node);
72 auto p = var_names.tryGet(var.name());
73 if (!p) { GUM_ERROR(MissingVariableInDatabase, "Variable '" << var.name() << "' is missing") }
74 _database_.insertTranslator(var, *p, missing_symbols);
75 _nodeId2cols_.insert(NodeId(node), i++);
76 }
77
78 // fill the database
79 initializer.fillDatabase(_database_);
80
81 // get the domain sizes of the variables
82 for (auto dom: _database_.domainSizes())
83 _domain_sizes_.push_back(dom);
84
85 // create the parser
87 }
88
89 template < GUM_Numeric GUM_SCALAR >
90 BayesNet< GUM_SCALAR > IBNLearner::Database::_BNVars_() const {
91 BayesNet< GUM_SCALAR > bn;
92 const std::size_t nb_vars = _database_.nbVariables();
93 for (std::size_t i = 0; i < nb_vars; ++i) {
94 const DiscreteVariable& var = dynamic_cast< const DiscreteVariable& >(_database_.variable(i));
95 bn.add(var);
96 }
97 return bn;
98 }
99
100 template < GUM_Numeric GUM_SCALAR >
101 IBNLearner::IBNLearner(std::string_view filename,
103 const std::vector< std::string >& missing_symbols) :
104 scoreDatabase_(filename, bn, missing_symbols) {
105 filename_ = filename;
106 noPrior_ = new NoPrior(scoreDatabase_.databaseTable());
107 inducedTypes_ = false;
108 GUM_CONSTRUCTOR(IBNLearner);
109 }
110
111
112} // namespace gum::learning
A class for generic framework of learning algorithms that can easily be used.
Class representing a Bayesian network.
Definition BayesNet.h:99
Base class for discrete random variable.
The class for generic Hash Tables.
Definition hashTable.h:640
value_type & insert(const Key &key, const Val &val)
Adds a new element (actually a copy of this element) into the hash table.
optional_ref< Val > tryGet(const Key &key)
Returns a pointer to the value associated with a given key, or nullptr if the key does not exist.
Error: A name of variable is not found in the database.
Base class for every random variable.
Definition variable.h:81
const std::string & name() const
returns the name of the variable
The class for initializing DatabaseTable and RawDatabaseTable instances from CSV files.
the class used to read a row in the database and to transform it into a set of DBRow instances that c...
The class used to pack sets of generators.
std::vector< std::size_t > _domain_sizes_
the domain sizes of the variables (useful to speed-up computations)
Definition IBNLearner.h:280
DatabaseTable _database_
the database itself
Definition IBNLearner.h:274
Bijection< NodeId, std::size_t > _nodeId2cols_
a bijection assigning to each variable name its NodeId
Definition IBNLearner.h:283
Database(std::string_view file, const std::vector< std::string > &missing_symbols, const bool induceTypes=false)
default constructor
DBRowGeneratorParser * _parser_
the parser used for reading the database
Definition IBNLearner.h:277
BayesNet< GUM_SCALAR > _BNVars_() const
Database scoreDatabase_
the database to be used by the scores and parameter estimators
static void isCSVFileName_(std::string_view filename)
checks whether the extension of a CSV filename is correct
std::string filename_
the filename database
IBNLearner(std::string_view filename, const std::vector< std::string > &missingSymbols, bool induceTypes=true)
read the database file for the score / parameter estimation and var names
bool inducedTypes_
the policy for typing variables
void fillDatabase(DATABASE &database, const bool retry_insertion=false)
fills the rows of the database table
const std::vector< std::string > & variableNames()
returns the names of the variables in the input dataset
the no a priorclass: corresponds to 0 weight-sample
Definition noPrior.h:65
#define GUM_ERROR(type, msg)
Definition exceptions.h:76
Size NodeId
Type for node ids.
include the inlined functions if necessary
Definition CSVParser.h:55