mlpack
io_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_UTIL_IO_IMPL_HPP
13 #define MLPACK_CORE_UTIL_IO_IMPL_HPP
14 
15 // In case it has not already been included.
16 #include "io.hpp"
17 #include "prefixedoutstream.hpp"
18 
19 namespace mlpack {
20 
32 template<typename T>
33 T& IO::GetParam(const std::string& identifier)
34 {
35  // Only use the alias if the parameter does not exist as given.
36  std::string key =
37  (GetSingleton().parameters.count(identifier) == 0 &&
38  identifier.length() == 1 && GetSingleton().aliases.count(identifier[0]))
39  ? GetSingleton().aliases[identifier[0]] : identifier;
40 
41  if (GetSingleton().parameters.count(key) == 0)
42  Log::Fatal << "Parameter --" << key << " does not exist in this program!"
43  << std::endl;
44 
45  util::ParamData& d = GetSingleton().parameters[key];
46 
47  // Make sure the types are correct.
48  if (TYPENAME(T) != d.tname)
49  Log::Fatal << "Attempted to access parameter --" << key << " as type "
50  << TYPENAME(T) << ", but its true type is " << d.tname << "!"
51  << std::endl;
52 
53  // Do we have a special mapped function?
54  if (IO::GetSingleton().functionMap[d.tname].count("GetParam") != 0)
55  {
56  T* output = NULL;
57  IO::GetSingleton().functionMap[d.tname]["GetParam"](d, NULL,
58  (void*) &output);
59  return *output;
60  }
61  else
62  {
63  return *boost::any_cast<T>(&d.value);
64  }
65 }
66 
74 template<typename T>
75 std::string IO::GetPrintableParam(const std::string& identifier)
76 {
77  // Only use the alias if the parameter does not exist as given.
78  std::string key = ((GetSingleton().parameters.count(identifier) == 0) &&
79  (identifier.length() == 1) &&
80  (GetSingleton().aliases.count(identifier[0]) > 0)) ?
81  GetSingleton().aliases[identifier[0]] : identifier;
82 
83  if (GetSingleton().parameters.count(key) == 0)
84  Log::Fatal << "Parameter --" << key << " does not exist in this program!"
85  << std::endl;
86 
87  util::ParamData& d = GetSingleton().parameters[key];
88 
89  // Make sure the types are correct.
90  if (TYPENAME(T) != d.tname)
91  Log::Fatal << "Attempted to access parameter --" << key << " as type "
92  << TYPENAME(T) << ", but its true type is " << d.tname << "!"
93  << std::endl;
94 
95  // Do we have a special mapped function?
96  if (IO::GetSingleton().functionMap[d.tname].count("GetPrintableParam") != 0)
97  {
98  std::string output;
99  IO::GetSingleton().functionMap[d.tname]["GetPrintableParam"](d, NULL,
100  (void*) &output);
101  return output;
102  }
103  else
104  {
105  std::ostringstream oss;
106  oss << "no GetPrintableParam function handler registered for type "
107  << d.cppType;
108  throw std::runtime_error(oss.str());
109  }
110 }
111 
112 template<typename T>
113 T& IO::GetRawParam(const std::string& identifier)
114 {
115  // Only use the alias if the parameter does not exist as given.
116  std::string key =
117  (GetSingleton().parameters.count(identifier) == 0 &&
118  identifier.length() == 1 && GetSingleton().aliases.count(identifier[0]))
119  ? GetSingleton().aliases[identifier[0]] : identifier;
120 
121  if (GetSingleton().parameters.count(key) == 0)
122  Log::Fatal << "Parameter --" << key << " does not exist in this program!"
123  << std::endl;
124 
125  util::ParamData& d = GetSingleton().parameters[key];
126 
127  // Make sure the types are correct.
128  if (TYPENAME(T) != d.tname)
129  Log::Fatal << "Attempted to access parameter --" << key << " as type "
130  << TYPENAME(T) << ", but its true type is " << d.tname << "!"
131  << std::endl;
132 
133  // Do we have a special mapped function?
134  if (IO::GetSingleton().functionMap[d.tname].count("GetRawParam") != 0)
135  {
136  T* output = NULL;
137  IO::GetSingleton().functionMap[d.tname]["GetRawParam"](d, NULL,
138  (void*) &output);
139  return *output;
140  }
141  else
142  {
143  // Use the regular GetParam().
144  return GetParam<T>(identifier);
145  }
146 }
147 
148 template<typename T>
149 void IO::CheckInputMatrix(const T& matrix, const std::string& identifier)
150 {
151  std::string errMsg1 = "The input " + identifier + " has NaN values.";
152  std::string errMsg2 = "The input " + identifier + " has inf values.";
153 
154  if (matrix.has_nan())
155  Log::Fatal << errMsg1 << std::endl;
156  if (matrix.has_inf())
157  Log::Fatal << errMsg2 << std::endl;
158 }
159 
160 } // namespace mlpack
161 
162 #endif
std::string tname
Type information of this parameter.
Definition: param_data.hpp:61
static MLPACK_EXPORT util::PrefixedOutStream Fatal
Prints fatal messages prefixed with [FATAL], then terminates the program.
Definition: log.hpp:90
Linear algebra utility functions, generally performed on matrices or vectors.
Definition: cv.hpp:1
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84
static T & GetRawParam(const std::string &identifier)
Get the raw value of the parameter before any processing that GetParam() might normally do...
Definition: io_impl.hpp:113
static std::string GetPrintableParam(const std::string &identifier)
Cast the given parameter of the given type to a short, printable std::string, for use in status messa...
Definition: io_impl.hpp:75
static IO & GetSingleton()
Retrieve the singleton.
Definition: io.cpp:147
static T & GetParam(const std::string &identifier)
Get the value of type T found while parsing.
Definition: io_impl.hpp:33
This structure holds all of the information about a single parameter, including its value (which is s...
Definition: param_data.hpp:52
static void CheckInputMatrix(const T &matrix, const std::string &identifier)
Utility function for CheckInputMatrices().
Definition: io_impl.hpp:149
#define TYPENAME(x)
The TYPENAME macro is used internally to convert a type into a string.
Definition: param_data.hpp:22
boost::any value
The actual value that is held.
Definition: param_data.hpp:82