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 
16 #include <mlpack/core/util/io.hpp>
18 
19 namespace mlpack {
20 namespace util {
21 
31 template<typename T>
32 inline void SetParam(const std::string& identifier, T& value)
33 {
34  IO::GetParam<T>(identifier) = std::move(value);
35 }
36 
47 template<typename T>
48 inline 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 
58 template<typename T>
59 inline 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 
108 template<typename T>
109 T* GetParamPtr(const std::string& paramName)
110 {
111  return IO::GetParam<T*>(paramName);
112 }
113 
117 template<typename T>
118 T& 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 
128 inline void EnableVerbose()
129 {
130  Log::Info.ignoreInput = false;
131 }
132 
136 inline void DisableVerbose()
137 {
138  Log::Info.ignoreInput = true;
139 }
140 
144 inline void DisableBacktrace()
145 {
146  Log::Fatal.backtrace = false;
147 }
148 
152 inline void ResetTimers()
153 {
154  // Just get a new object---removes all old timers.
156 }
157 
161 inline void EnableTimers()
162 {
164 }
165 
166 } // namespace util
167 } // namespace mlpack
168 
169 #endif
mlpack::IO::timer
Timers timer
Holds the timer objects.
Definition: io.hpp:315
mlpack::util::SetParam
void SetParam(const std::string &identifier, T &value)
Set the parameter to the given value.
Definition: io_util.hpp:29
mlpack::util::EnableTimers
void EnableTimers()
Enable timing.
Definition: io_util.hpp:93
mlpack::util::SetParamWithInfo
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
mlpack::data::DatasetMapper::MapString
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
mlpack::util::PrefixedOutStream::ignoreInput
bool ignoreInput
Discards input, prints nothing if true.
Definition: prefixedoutstream.hpp:119
mlpack::util::DisableBacktrace
void DisableBacktrace()
Disable backtraces.
Definition: io_util.hpp:76
io.hpp
mlpack
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: add_to_cli11.hpp:21
mlpack::util::ResetTimers
void ResetTimers()
Reset the status of all timers.
Definition: io_util.hpp:84
mlpack::util::DisableVerbose
void DisableVerbose()
Turn verbose output off.
Definition: io_util.hpp:68
mlpack::util::PrefixedOutStream::backtrace
bool backtrace
If true, on a fatal error, a backtrace will be printed if HAS_BFD_DL is defined.
Definition: prefixedoutstream.hpp:123
mlpack::util::SetParamPtr
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
mlpack::data::DatasetMapper::Type
Datatype Type(const size_t dimension) const
Return the type of a given dimension (numeric or categorical).
mlpack::util::GetParamPtr
T * GetParamPtr(const std::string &paramName)
Return a pointer.
Definition: io_util.hpp:52
mlpack::util::EnableVerbose
void EnableVerbose()
Turn verbose output on.
Definition: io_util.hpp:60
mlpack::Log::Fatal
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
mlpack::Timer::EnableTiming
static void EnableTiming()
Enable timing of mlpack programs.
mlpack::Timers::Reset
void Reset()
Reset the timers.
dataset_mapper.hpp
mlpack::Log::Info
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
mlpack::IO::GetSingleton
static IO & GetSingleton()
Retrieve the singleton.
mlpack::data::categorical
@ categorical
Definition: datatype.hpp:27
mlpack::util::GetParamWithInfo
T & GetParamWithInfo(const std::string &paramName)
Return the matrix part of a matrix + dataset info parameter.
Definition: io_util.hpp:118
mlpack::data::DatasetInfo
DatasetMapper< data::IncrementPolicy > DatasetInfo
Definition: dataset_mapper.hpp:196
mlpack::data::DatasetMapper
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
Definition: dataset_mapper.hpp:42