aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4DiscretizedVariable_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-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#pragma once
41
42
48
49#include <sstream>
50#include <utility>
51#include <vector>
52
55
56#ifndef DOXYGEN_SHOULD_SKIP_THIS
57
58namespace gum {
59
60 namespace learning {
61
62
64 template < typename GUM_SCALAR >
66 const DiscretizedVariable< GUM_SCALAR >& var,
67 const std::vector< std::string >& missing_symbols,
68 std::size_t max_dico_entries) :
70 false,
71 missing_symbols,
72 false,
73 max_dico_entries),
74 _variable_(var.name(), var.description()) {
75 // check that the variable has not too many entries
76 if (var.domainSize() > max_dico_entries) {
77 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
78 }
79
80 // copy the ticks of var into our internal variable
81 const auto& ticks = var.ticks();
82 for (const auto tick: ticks) {
83 _variable_.addTick((float)tick);
84 }
85
86 // the 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 template < typename GUM_SCALAR >
124 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
125 const DiscretizedVariable< GUM_SCALAR >& var,
126 std::size_t max_dico_entries) :
127 DBTranslator(DBTranslatedValueType::DISCRETE, false, false, max_dico_entries),
128 _variable_(var.name(), var.description()) {
129 // check that the variable has not too many entries
130 if (var.domainSize() > max_dico_entries) {
131 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
132 }
133
134 // copy the ticks of var into our internal variable
135 const auto& ticks = var.ticks();
136 for (const auto tick: ticks) {
137 _variable_.addTick((float)tick);
138 }
139
140 // add the content of the variable into the back dictionary
141 std::size_t size = 0;
142 for (const auto& label: var.labels()) {
143 this->back_dico_.insert(size, label);
144 ++size;
145 }
146
147 // store a copy of the variable, that should be used by method variable ()
148 _real_variable_ = var.clone();
149
150 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
151 }
152
153
154 } /* namespace learning */
155
156} /* namespace gum */
157
158
159#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The class representing the original values of the cells of databases.
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