aGrUM 2.3.2
a C++ library for (probabilistic) graphical models
DBTranslator4DiscretizedVariable_inl.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
43#ifndef DOXYGEN_SHOULD_SKIP_THIS
44
46
47namespace gum {
48
49 namespace learning {
50
53 if (_real_variable_ != nullptr) delete _real_variable_;
54
56 }
57
60 // try to get the index of str within the discretized variable.
61 try {
62 return DBTranslatedValue{std::size_t(_variable_[str])};
63 } catch (gum::Exception& e) {
64 // check for a missing symbol
65 if (this->isMissingSymbol(str))
66 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
67
68 // check if the back_dictionary does not contain str. This enables
69 // to execute translate ( translateBack ( translate ( str ) ) )
70 // without raising an exception
71 try {
72 return DBTranslatedValue{this->back_dico_.first(str)};
73 } catch (const gum::Exception&) {
74 if (!DBCell::isReal(str)) {
75 GUM_ERROR(TypeError,
76 "String \"" << str << "\" cannot be translated because it is not a value for "
77 << _variable_);
78 } else {
79 GUM_ERROR(UnknownLabelInDatabase,
80 "The translation of \"" << str << "\" could not be found : " << e.what()
81 << " for variable " << _variable_);
82 }
83 }
84 }
85 }
86
89 const DBTranslatedValue translated_val) const {
90 try {
91 return this->back_dico_.second(translated_val.discr_val);
92 } catch (Exception const&) {
93 // check if this is a missing value
94 if ((translated_val.discr_val == std::numeric_limits< std::size_t >::max())
95 && !this->missing_symbols_.empty())
96 return *(this->missing_symbols_.begin());
97 else
98 GUM_ERROR(UnknownLabelInDatabase,
99 "The back translation of \"" << translated_val.discr_val
100 << "\" could not be found");
101 }
102 }
103
105 INLINE bool DBTranslator4DiscretizedVariable::hasEditableDictionary() const { return false; }
106
109
111 INLINE bool DBTranslator4DiscretizedVariable::needsReordering() const { return false; }
112
114 INLINE HashTable< std::size_t, std::size_t > DBTranslator4DiscretizedVariable::reorder() {
115 return HashTable< std::size_t, std::size_t >();
116 }
117
119 INLINE std::size_t DBTranslator4DiscretizedVariable::domainSize() const {
120 return _variable_.domainSize();
121 }
122
124 INLINE const IDiscretizedVariable* DBTranslator4DiscretizedVariable::variable() const {
125 return _real_variable_;
126 }
127
130 return DBTranslatedValue{std::numeric_limits< std::size_t >::max()};
131 }
132
133
134 } /* namespace learning */
135
136} /* namespace gum */
137
138#endif /* DOXYGEN_SHOULD_SKIP_THIS */
The databases' cell translators for discretized variables.
GUM_NODISCARD const char * what() const noexcept override
Definition exceptions.h:138
static bool isReal(const std::string &str)
determine whether a string corresponds precisely to a real number
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
virtual bool hasEditableDictionary() const final
indicates that the translator is never in editable dictionary mode
virtual HashTable< std::size_t, std::size_t > reorder() final
returns an empty HashTable to indicate that no reordering is needed.
virtual std::size_t domainSize() const final
returns the number of discretization intervals used for translations
virtual DBTranslatedValue missingValue() const final
returns the translation of a missing value
virtual void setEditableDictionaryMode(bool new_mode) final
sets/unset the editable dictionary mode
virtual bool needsReordering() const final
indicates that the translations should never be reordered
virtual DBTranslatedValue translate(const std::string &str) final
returns the translation of a string
virtual const IDiscretizedVariable * variable() const final
returns the variable stored into the translator
virtual std::string translateBack(const DBTranslatedValue translated_val) const final
returns the original value for a given translation
Bijection< std::size_t, std::string > back_dico_
the bijection relating back translated values and their original strings.
Set< std::string > missing_symbols_
the set of missing symbols
bool isMissingSymbol(const std::string &str) const
indicates whether a string corresponds to a missing symbol
#define GUM_ERROR(type, msg)
Definition exceptions.h:72
include the inlined functions if necessary
Definition CSVParser.h:54
gum is the global namespace for all aGrUM entities
Definition agrum.h:46
The union class for storing the translated values in learning databases.