aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4DiscretizedVariable.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 IDiscretizedVariable& var,
64 const std::vector< std::string >& missing_symbols,
65 std::size_t max_dico_entries) :
67 false,
68 missing_symbols,
69 false,
70 max_dico_entries),
71 _variable_(var.name(), var.description()) {
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 // copy the ticks of var into our internal variable
78 const auto ticks = var.ticksAsDoubles();
79 for (const auto tick: ticks) {
80 _variable_.addTick((float)tick);
81 }
82 // copy the isEmpirical
83 // (why not using clone ?)
84 _variable_.setEmpirical(var.isEmpirical());
85
86 // the bounds of the discretized variable
87 const auto lower_bound = float(ticks[0]);
88 const auto upper_bound = float(ticks.back());
89
90 // remove all the missing symbols corresponding to a number between
91 // lower_bound and upper_bound
92 for (auto iter = this->missing_symbols_.beginSafe(); iter != this->missing_symbols_.endSafe();
93 ++iter) {
94 if (DBCell::isReal(*iter)) {
95 const float missing_val = std::stof(*iter);
96 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
97 this->missing_symbols_.erase(iter);
98 }
99 }
100 }
101
102 // add the content of the variable into the back dictionary
103 std::size_t size = 0;
104 for (const auto& label: var.labels()) {
105 // if the label corresponds to a missing value, then remove it from
106 // the set of missing symbols. If, in addition, it has already
107 // been entered into the back_dictionary, then, this has been done
108 // because the label corresponded to a missing value, so we should
109 // remove the label as well from the back_dictionary.
110 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
111
112 this->back_dico_.insert(size, label);
113 ++size;
114 }
115
116 // store a copy of the variable, that should be used by method variable ()
117 _real_variable_ = var.clone();
118
119 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
120 }
121
123 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
124 const IDiscretizedVariable& var,
125 std::size_t max_dico_entries) :
126 DBTranslator(DBTranslatedValueType::DISCRETE, false, false, max_dico_entries),
127 _variable_(var.name(), var.description()) {
128 // check that the variable has not too many entries
129 if (var.domainSize() > max_dico_entries) {
130 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
131 }
132
133 // copy the ticks of var into our internal variable
134 const auto ticks = var.ticksAsDoubles();
135 for (const auto tick: ticks) {
136 _variable_.addTick((float)tick);
137 }
138
139 // add the content of the variable into the back dictionary
140 std::size_t size = 0;
141 for (const auto& label: var.labels()) {
142 this->back_dico_.insert(size, label);
143 ++size;
144 }
145
146 // store a copy of the variable, that should be used by method variable ()
147 _real_variable_ = var.clone();
148
149 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
150 }
151
153 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
154 const DBTranslator4DiscretizedVariable& from) :
155 DBTranslator(from), _variable_(from._variable_) {
156 // store a copy of the variable, that should be used by method variable ()
157 _real_variable_ = from._real_variable_->clone();
158
159 GUM_CONS_CPY(DBTranslator4DiscretizedVariable);
160 }
161
163 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
164 DBTranslator4DiscretizedVariable&& from) :
165 DBTranslator(std::move(from)), _variable_(std::move(from._variable_)) {
166 // moves the copy of the variable, that should be used by method variable ()
167 _real_variable_ = from._real_variable_;
168 from._real_variable_ = nullptr;
169
170 GUM_CONS_MOV(DBTranslator4DiscretizedVariable);
171 }
172
174 DBTranslator4DiscretizedVariable* DBTranslator4DiscretizedVariable::clone() const {
175 return new DBTranslator4DiscretizedVariable(*this);
176 }
177
179 DBTranslator4DiscretizedVariable&
180 DBTranslator4DiscretizedVariable::operator=(const DBTranslator4DiscretizedVariable& from) {
181 if (this != &from) {
182 DBTranslator::operator=(from);
183 _variable_ = from._variable_;
184
185 if (_real_variable_ != nullptr) delete _real_variable_;
186 _real_variable_ = from._real_variable_->clone();
187 }
188
189 return *this;
190 }
191
193 DBTranslator4DiscretizedVariable&
194 DBTranslator4DiscretizedVariable::operator=(DBTranslator4DiscretizedVariable&& from) {
195 if (this != &from) {
196 DBTranslator::operator=(std::move(from));
197 _variable_ = std::move(from._variable_);
198
199 if (_real_variable_ != nullptr) delete _real_variable_;
200 _real_variable_ = from._real_variable_;
201 from._real_variable_ = nullptr;
202 }
203
204 return *this;
205 }
206
207 } /* namespace learning */
208
209} /* namespace gum */
210
211#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The databases' cell translators for discretized variables.
DBTranslator4DiscretizedVariable(const DiscretizedVariable< GUM_SCALAR > &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 discretized 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.