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(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(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 TupleType;
64  typedef typename T::elem_type eT;
65 
66  // The true type of the parameter is std::tuple.
67  const size_t dimensions = matrix.n_rows;
68  std::get<1>(IO::GetParam(identifier)) = std::move(matrix);
69  data::DatasetInfo& di = std::get<0>(IO::GetParam(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(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(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(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 TupleType;
122  return std::get<1>(IO::GetParam(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
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
bool backtrace
If true, on a fatal error, a backtrace will be printed if HAS_BFD_DL is defined.
Auxiliary information for a dataset, including mappings to/from strings (or other types) and the data...
void ResetTimers()
Reset the status of all timers.
Definition: io_util.hpp:84
Linear algebra utility functions, generally performed on matrices or vectors.
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
void DisableVerbose()
Turn verbose output off.
Definition: io_util.hpp:68
bool ignoreInput
Discards input, prints nothing if true.
Datatype Type(const size_t dimension) const
Return the type of a given dimension (numeric or categorical).
void DisableBacktrace()
Disable backtraces.
Definition: io_util.hpp:76
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
void EnableTimers()
Enable timing.
Definition: io_util.hpp:93
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
T * GetParamPtr(const std::string ¶mName)
Return a pointer.
Definition: io_util.hpp:52
void SetParam(const std::string &identifier, T &value)
Set the parameter to the given value.
Definition: io_util.hpp:29
DatasetMapper< data::IncrementPolicy > DatasetInfo
static IO & GetSingleton()
Retrieve the singleton.
static void EnableTiming()
Enable timing of mlpack programs.
T & GetParamWithInfo(const std::string ¶mName)
Return the matrix part of a matrix + dataset info parameter.
Definition: io_util.hpp:118
void EnableVerbose()
Turn verbose output on.
Definition: io_util.hpp:60
Timers timer
Holds the timer objects.
Definition: io.hpp:315
void Reset()
Reset the timers.
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