aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4NumericalDiscreteVariable.cpp
Go to the documentation of this file.
1/****************************************************************************
2 * This file is part of the aGrUM/pyAgrum library. *
3 * *
4 * Copyright (c) 2005-2025 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-2025 *
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
47
49
50#ifndef DOXYGEN_SHOULD_SKIP_THIS
51
53# ifdef GUM_NO_INLINE
55# endif /* GUM_NO_INLINE */
56
57namespace gum {
58
59 namespace learning {
60
63 const NumericalDiscreteVariable& var,
64 const std::vector< std::string >& missing_symbols,
65 std::size_t max_dico_entries) :
67 true,
68 missing_symbols,
69 false,
70 max_dico_entries),
71 _variable_(var) {
72 // check that the variable has not too many entries
73 if (var.domainSize() > max_dico_entries) {
74 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
75 }
76
77 // remove all the missing symbols corresponding to a number belonging to the variable
78 for (auto iter = this->missing_symbols_.beginSafe(); iter != this->missing_symbols_.endSafe();
79 ++iter) {
80 if (DBCell::isReal(*iter)) {
81 char* endptr = nullptr;
82 const double missing_val = std::strtod(iter->c_str(), &endptr);
83 if (_variable_.isValue(missing_val)) { this->missing_symbols_.erase(iter); }
84 }
85 }
86
87 // add the content of the variable into the back dictionary
88 std::size_t size = 0;
89 for (const auto& label: var.labels()) {
90 // if the label corresponds to a missing value, then remove it from
91 // the set of missing symbols. If, in addition, it has already
92 // been entered into the back_dictionary, then, this has been done
93 // because the label corresponded to a missing value, so we should
94 // remove the label as well from the back_dictionary.
95 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
96
97 this->back_dico_.insert(size, label);
98 ++size;
99 }
100
101 // for debugging purposes
102 GUM_CONSTRUCTOR(DBTranslator4NumericalDiscreteVariable);
103 }
104
106 DBTranslator4NumericalDiscreteVariable::DBTranslator4NumericalDiscreteVariable(
107 const NumericalDiscreteVariable& var,
108 std::size_t max_dico_entries) :
109 DBTranslator(DBTranslatedValueType::DISCRETE, true, false, max_dico_entries),
110 _variable_(var) {
111 // check that the variable has not too many entries
112 if (var.domainSize() > max_dico_entries) {
113 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
114 }
115
116 // add the content of the variable into the back dictionary
117 std::size_t size = 0;
118 for (const auto& label: var.labels()) {
119 this->back_dico_.insert(size, label);
120 ++size;
121 }
122
123 // for debugging purposes
124 GUM_CONSTRUCTOR(DBTranslator4NumericalDiscreteVariable);
125 }
126
128 DBTranslator4NumericalDiscreteVariable::DBTranslator4NumericalDiscreteVariable(
129 const DBTranslator4NumericalDiscreteVariable& from) :
130 DBTranslator(from), _variable_(from._variable_) {
131 // for debugging purposes
132 GUM_CONS_CPY(DBTranslator4NumericalDiscreteVariable);
133 }
134
136 DBTranslator4NumericalDiscreteVariable::DBTranslator4NumericalDiscreteVariable(
137 DBTranslator4NumericalDiscreteVariable&& from) :
138 DBTranslator(std::move(from)), _variable_(std::move(from._variable_)) {
139 // for debugging purposes
140 GUM_CONS_MOV(DBTranslator4NumericalDiscreteVariable);
141 }
142
144 DBTranslator4NumericalDiscreteVariable* DBTranslator4NumericalDiscreteVariable::clone() const {
145 return new DBTranslator4NumericalDiscreteVariable(*this);
146 }
147
149 DBTranslator4NumericalDiscreteVariable& DBTranslator4NumericalDiscreteVariable::operator=(
150 const DBTranslator4NumericalDiscreteVariable& from) {
151 if (this != &from) {
152 DBTranslator::operator=(from);
153 _variable_ = from._variable_;
154 }
155
156 return *this;
157 }
158
160 DBTranslator4NumericalDiscreteVariable& DBTranslator4NumericalDiscreteVariable::operator=(
161 DBTranslator4NumericalDiscreteVariable&& from) {
162 if (this != &from) {
163 DBTranslator::operator=(std::move(from));
164 _variable_ = std::move(from._variable_);
165 }
166
167 return *this;
168 }
169
170 } /* namespace learning */
171
172} /* namespace gum */
173
174#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The databases' cell translators for numerical discrete variables.
The databases' cell translators for integer variables.
DBTranslator4NumericalDiscreteVariable(const NumericalDiscreteVariable &var, const std::vector< std::string > &missing_symbols, std::size_t max_dico_entries=std::numeric_limits< std::size_t >::max())
default constructor with a numerical discrete variable as translator
The base class for all the tabular database cell translators.
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
DBTranslatedValueType
The nature of the elements handled by translators (discrete, continuous).
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
STL namespace.