aGrUM 3.1.1
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-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
49
50#include <sstream>
51#include <utility>
52#include <vector>
53
56
57#ifndef DOXYGEN_SHOULD_SKIP_THIS
58
59namespace gum {
60
61 namespace learning {
62
63
65 template < GUM_Numeric GUM_SCALAR >
67 const DiscretizedVariable< GUM_SCALAR >& var,
68 const std::vector< std::string >& missing_symbols,
69 std::size_t max_dico_entries) :
71 false,
72 missing_symbols,
73 false,
74 max_dico_entries),
75 _variable_(var.name(), var.description()) {
76 // check that the variable has not too many entries
77 if (var.domainSize() > max_dico_entries) {
78 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
79 }
80
81 // copy the ticks of var into our internal variable
82 const auto& ticks = var.ticks();
83 for (const auto tick: ticks) {
84 _variable_.addTick((float)tick);
85 }
86
87 // the the bounds of the discretized variable
88 const auto lower_bound = (float)ticks[0];
89 const auto upper_bound = (float)ticks.back();
90
91 // remove all the missing symbols corresponding to a number between
92 // lower_bound and upper_bound
93 for (auto iter = this->missing_symbols_.beginSafe(); iter != this->missing_symbols_.endSafe();
94 ++iter) {
95 if (DBCell::isReal(*iter)) {
96 const float missing_val = std::stof(*iter);
97 if ((missing_val >= lower_bound) && (missing_val <= upper_bound)) {
98 this->missing_symbols_.erase(iter);
99 }
100 }
101 }
102
103 // add the content of the variable into the back dictionary
104 std::size_t size = 0;
105 for (const auto& label: var.labels()) {
106 // if the label corresponds to a missing value, then remove it from
107 // the set of missing symbols. If, in addition, it has already
108 // been entered into the back_dictionary, then, this has been done
109 // because the label corresponded to a missing value, so we should
110 // remove the label as well from the back_dictionary.
111 if (this->missing_symbols_.exists(label)) { this->missing_symbols_.erase(label); }
112
113 this->back_dico_.insert(size, label);
114 ++size;
115 }
116
117 // store a copy of the variable, that should be used by method variable ()
118 _real_variable_ = var.clone();
119
120 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
121 }
122
124 template < GUM_Numeric GUM_SCALAR >
125 DBTranslator4DiscretizedVariable::DBTranslator4DiscretizedVariable(
126 const DiscretizedVariable< GUM_SCALAR >& var,
127 std::size_t max_dico_entries) :
128 DBTranslator(DBTranslatedValueType::DISCRETE, false, false, max_dico_entries),
129 _variable_(var.name(), var.description()) {
130 // check that the variable has not too many entries
131 if (var.domainSize() > max_dico_entries) {
132 GUM_ERROR(SizeError, "the dictionary induced by the variable is too large")
133 }
134
135 // copy the ticks of var into our internal variable
136 const auto& ticks = var.ticks();
137 for (const auto tick: ticks) {
138 _variable_.addTick((float)tick);
139 }
140
141 // add the content of the variable into the back dictionary
142 std::size_t size = 0;
143 for (const auto& label: var.labels()) {
144 this->back_dico_.insert(size, label);
145 ++size;
146 }
147
148 // store a copy of the variable, that should be used by method variable ()
149 _real_variable_ = var.clone();
150
151 GUM_CONSTRUCTOR(DBTranslator4DiscretizedVariable);
152 }
153
154
155 } /* namespace learning */
156
157} /* namespace gum */
158
159
160#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:55
gum is the global namespace for all aGrUM entities
Definition agrum.h:46