mlpack 3.4.2
io_util.hpp
Go to the documentation of this file.
1
13#ifndef MLPACK_BINDINGS_PYTHON_CYTHON_IO_UTIL_HPP
14#define MLPACK_BINDINGS_PYTHON_CYTHON_IO_UTIL_HPP
15
18
19namespace mlpack {
20namespace util {
21
31template<typename T>
32inline void SetParam(const std::string& identifier, T& value)
33{
34 IO::GetParam<T>(identifier) = std::move(value);
35}
36
47template<typename T>
48inline void SetParamPtr(const std::string& identifier,
49 T* value,
50 const bool copy)
51{
52 IO::GetParam<T*>(identifier) = copy ? new T(*value) : value;
53}
54
58template<typename T>
59inline void SetParamWithInfo(const std::string& identifier,
60 T& matrix,
61 const bool* dims)
62{
63 typedef typename std::tuple<data::DatasetInfo, T> TupleType;
64 typedef typename T::elem_type eT;
65
66 // The true type of the parameter is std::tuple<T, DatasetInfo>.
67 const size_t dimensions = matrix.n_rows;
68 std::get<1>(IO::GetParam<TupleType>(identifier)) = std::move(matrix);
69 data::DatasetInfo& di = std::get<0>(IO::GetParam<TupleType>(identifier));
70 di = data::DatasetInfo(dimensions);
71
72 bool hasCategoricals = false;
73 for (size_t i = 0; i < dimensions; ++i)
74 {
75 if (dims[i])
76 {
78 hasCategoricals = true;
79 }
80 }
81
82 // Do we need to find how many categories we have?
83 if (hasCategoricals)
84 {
85 arma::vec maxs = arma::max(
86 std::get<1>(IO::GetParam<TupleType>(identifier)), 1);
87
88 for (size_t i = 0; i < dimensions; ++i)
89 {
90 if (dims[i])
91 {
92 // Map the right number of objects.
93 for (size_t j = 0; j < (size_t) maxs[i]; ++j)
94 {
95 std::ostringstream oss;
96 oss << j;
97 di.MapString<eT>(oss.str(), i);
98 }
99 }
100 }
101 }
102}
103
108template<typename T>
109T* GetParamPtr(const std::string& paramName)
110{
111 return IO::GetParam<T*>(paramName);
112}
113
117template<typename T>
118T& GetParamWithInfo(const std::string& paramName)
119{
120 // T will be the Armadillo type.
121 typedef std::tuple<data::DatasetInfo, T> TupleType;
122 return std::get<1>(IO::GetParam<TupleType>(paramName));
123}
124
128inline void EnableVerbose()
129{
130 Log::Info.ignoreInput = false;
131}
132
136inline void DisableVerbose()
137{
138 Log::Info.ignoreInput = true;
139}
140
144inline void DisableBacktrace()
145{
146 Log::Fatal.backtrace = false;
147}
148
152inline void ResetTimers()
153{
154 // Just get a new object---removes all old timers.
156}
157
161inline void EnableTimers()
162{
164}
165
166} // namespace util
167} // namespace mlpack
168
169#endif
static IO & GetSingleton()
Retrieve the singleton.
Timers timer
Holds the timer objects.
Definition: io.hpp:315
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
static void EnableTiming()
Enable timing of mlpack programs.
void Reset()
Reset the timers.
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
Datatype Type(const size_t dimension) const
Return the type of a given dimension (numeric or categorical).
bool backtrace
If true, on a fatal error, a backtrace will be printed if HAS_BFD_DL is defined.
bool ignoreInput
Discards input, prints nothing if true.
DatasetMapper< data::IncrementPolicy > DatasetInfo
T & GetParamWithInfo(const std::string &paramName)
Return the matrix part of a matrix + dataset info parameter.
Definition: io_util.hpp:118
void SetParam(const std::string &identifier, T &value)
Set the parameter to the given value.
Definition: io_util.hpp:29
void DisableVerbose()
Turn verbose output off.
Definition: io_util.hpp:68
void EnableTimers()
Enable timing.
Definition: io_util.hpp:93
void EnableVerbose()
Turn verbose output on.
Definition: io_util.hpp:60
void SetParamWithInfo(const std::string &identifier, T &matrix, const bool *dims)
Set the parameter (which is a matrix/DatasetInfo tuple) to the given value.
Definition: io_util.hpp:59
void ResetTimers()
Reset the status of all timers.
Definition: io_util.hpp:84
void DisableBacktrace()
Disable backtraces.
Definition: io_util.hpp:76
T * GetParamPtr(const std::string &paramName)
Return a pointer.
Definition: io_util.hpp:52
void SetParamPtr(const std::string &identifier, T *value)
Set the parameter to the given value, given that the type is a pointer.
Definition: io_util.hpp:41
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1