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(util::Params& params,
33  const std::string& identifier,
34  T& value)
35 {
36  params.Get<T>(identifier) = std::move(value);
37 }
38 
49 template<typename T>
50 inline void SetParamPtr(util::Params& params,
51  const std::string& identifier,
52  T* value,
53  const bool copy)
54 {
55  params.Get<T*>(identifier) = copy ? new T(*value) : value;
56 }
57 
61 template<typename T>
62 inline void SetParamWithInfo(util::Params& params,
63  const std::string& identifier,
64  T& matrix,
65  const bool* dims)
66 {
67  typedef typename std::tuple<data::DatasetInfo, T> TupleType;
68  typedef typename T::elem_type eT;
69 
70  // The true type of the parameter is std::tuple<T, DatasetInfo>.
71  const size_t dimensions = matrix.n_rows;
72  std::get<1>(params.Get<TupleType>(identifier)) = std::move(matrix);
73  data::DatasetInfo& di = std::get<0>(params.Get<TupleType>(identifier));
74  di = data::DatasetInfo(dimensions);
75 
76  bool hasCategoricals = false;
77  for (size_t i = 0; i < dimensions; ++i)
78  {
79  if (dims[i])
80  {
82  hasCategoricals = true;
83  }
84  }
85 
86  // Do we need to find how many categories we have?
87  if (hasCategoricals)
88  {
89  arma::vec maxs = arma::max(
90  std::get<1>(params.Get<TupleType>(identifier)), 1);
91 
92  for (size_t i = 0; i < dimensions; ++i)
93  {
94  if (dims[i])
95  {
96  // Map the right number of objects.
97  for (size_t j = 0; j < (size_t) maxs[i]; ++j)
98  {
99  std::ostringstream oss;
100  oss << j;
101  di.MapString<eT>(oss.str(), i);
102  }
103  }
104  }
105  }
106 }
107 
112 template<typename T>
113 T* GetParamPtr(util::Params& params,
114  const std::string& paramName)
115 {
116  return params.Get<T*>(paramName);
117 }
118 
122 template<typename T>
124  const std::string& paramName)
125 {
126  // T will be the Armadillo type.
127  typedef std::tuple<data::DatasetInfo, T> TupleType;
128  return std::get<1>(params.Get<TupleType>(paramName));
129 }
130 
134 inline void EnableVerbose()
135 {
136  Log::Info.ignoreInput = false;
137 }
138 
142 inline void DisableVerbose()
143 {
144  Log::Info.ignoreInput = true;
145 }
146 
150 inline void DisableBacktrace()
151 {
152  Log::Fatal.backtrace = false;
153 }
154 
158 inline void ResetTimers()
159 {
160  // Just get a new object---removes all old timers.
161  Timer::ResetAll();
162 }
163 
167 inline void EnableTimers()
168 {
170 }
171 
172 } // namespace util
173 } // namespace mlpack
174 
175 #endif
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
void SetParamPtr(util::Params &p, const std::string &identifier, T *value)
Set the parameter to the given value, given that the type is a pointer.
Definition: io_util.hpp:43
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 DisableVerbose()
Turn verbose output off.
Definition: io_util.hpp:71
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:79
void SetParam(util::Params &p, const std::string &identifier, T &value)
Set the parameter to the given value.
Definition: io_util.hpp:29
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:92
static MLPACK_EXPORT util::PrefixedOutStream Info
Prints informational messages if –verbose is specified, prefixed with [INFO ].
Definition: log.hpp:84
DatasetMapper< data::IncrementPolicy > DatasetInfo
T & Get(const std::string &identifier)
Get the value of type T found for the parameter specified by identifier.
static void EnableTiming()
Enable timing of mlpack programs.
static void ResetAll()
Stop and reset all running timers.
void EnableVerbose()
Turn verbose output on.
Definition: io_util.hpp:63
The Params class holds all information about the parameters passed to a specific binding.
Definition: params.hpp:20
T & GetParamWithInfo(util::Params &params, const std::string &paramName)
Return the matrix part of a matrix + dataset info parameter.
Definition: io_util.hpp:123
void SetParamWithInfo(util::Params &params, 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:62
T * GetParamPtr(util::Params &p, const std::string &paramName)
Return a pointer.
Definition: io_util.hpp:55