cli_util.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_PYTHON_CYTHON_CLI_UTIL_HPP
14 #define MLPACK_BINDINGS_PYTHON_CYTHON_CLI_UTIL_HPP
15 
16 #include <mlpack/core/util/cli.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  CLI::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  CLI::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>(CLI::GetParam<TupleType>(identifier)) = std::move(matrix);
69  data::DatasetInfo& di = std::get<0>(CLI::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>(CLI::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 CLI::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>(CLI::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
T MapString(const InputType &input, const size_t dimension)
Given the input and the dimension to which it belongs, return its numeric mapping.
void EnableTimers()
Enable timing.
Definition: cli_util.hpp:161
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...
.hpp
Definition: add_to_po.hpp:21
static CLI & GetSingleton()
Retrieve the singleton.
void ResetTimers()
Reset the status of all timers.
Definition: cli_util.hpp:152
Timers timer
Holds the timer objects.
Definition: cli.hpp:306
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).
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
DatasetMapper< data::IncrementPolicy > DatasetInfo
static void EnableTiming()
Enable timing of mlpack programs.
void SetParam(const std::string &identifier, T &value)
Set the parameter to the given value.
Definition: cli_util.hpp:32
T & GetParamWithInfo(const std::string &paramName)
Return the matrix part of a matrix + dataset info parameter.
Definition: cli_util.hpp:118
void DisableBacktrace()
Disable backtraces.
Definition: cli_util.hpp:144
void Reset()
Reset the timers.
void EnableVerbose()
Turn verbose output on.
Definition: cli_util.hpp:128
void DisableVerbose()
Turn verbose output off.
Definition: cli_util.hpp:136
T * GetParamPtr(const std::string &paramName)
Return a pointer.
Definition: cli_util.hpp:109
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: cli_util.hpp:59
void SetParamPtr(const std::string &identifier, T *value, const bool copy)
Set the parameter to the given value, given that the type is a pointer.
Definition: cli_util.hpp:48