mlpack
param_checks_impl.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_CORE_UTIL_PARAM_CHECKS_IMPL_HPP
13 #define MLPACK_CORE_UTIL_PARAM_CHECKS_IMPL_HPP
14 
15 #include "param_checks.hpp"
16 
17 namespace mlpack {
18 namespace util {
19 
20 // Check that the arguments are given.
22  const std::vector<std::string>& constraints,
23  const bool fatal,
24  const std::string& errorMessage,
25  const bool allowNone)
26 {
27  if (BINDING_IGNORE_CHECK(constraints))
28  return;
29 
30  size_t set = 0;
31  for (size_t i = 0; i < constraints.size(); ++i)
32  {
33  if (IO::HasParam(constraints[i]))
34  ++set;
35  }
36 
37  util::PrefixedOutStream& stream = fatal ? Log::Fatal : Log::Warn;
38  if (set > 1)
39  {
40  // Give different output depending on whether 2 or more parameters are
41  // given.
42  if (constraints.size() == 2)
43  {
44  stream << "Can only pass one of " << PRINT_PARAM_STRING(constraints[0])
45  << " or " << PRINT_PARAM_STRING(constraints[1]);
46  }
47  else
48  {
49  stream << "Can only pass one of ";
50  for (size_t i = 0 ; i < constraints.size() - 1; ++i)
51  stream << PRINT_PARAM_STRING(constraints[i]) << ", ";
52  stream << "or "
53  << PRINT_PARAM_STRING(constraints[constraints.size() - 1]);
54  }
55 
56  // Append a custom message.
57  if (!errorMessage.empty())
58  stream << "; " << errorMessage;
59  stream << "!" << std::endl;
60  }
61  else if (set == 0 && !allowNone)
62  {
63  stream << (fatal ? "Must " : "Should ");
64 
65  // Give different output depending on whether 1, 2, or more parameters are
66  // given.
67  if (constraints.size() == 1)
68  {
69  stream << "specify " << PRINT_PARAM_STRING(constraints[0]);
70  }
71  else if (constraints.size() == 2)
72  {
73  stream << "specify one of " << PRINT_PARAM_STRING(constraints[0])
74  << " or " << PRINT_PARAM_STRING(constraints[1]);
75  }
76  else
77  {
78  stream << "specify one of ";
79  for (size_t i = 0; i < constraints.size() - 1; ++i)
80  stream << PRINT_PARAM_STRING(constraints[i]) << ", ";
81  stream << "or "
82  << PRINT_PARAM_STRING(constraints[constraints.size() - 1]);
83  }
84 
85  // Append a custom message.
86  if (!errorMessage.empty())
87  stream << "; " << errorMessage;
88  stream << "!" << std::endl;
89  }
90 }
91 
93  const std::vector<std::string>& constraints,
94  const bool fatal,
95  const std::string& errorMessage)
96 {
97  if (BINDING_IGNORE_CHECK(constraints))
98  return;
99 
100  size_t set = 0;
101  for (size_t i = 0; i < constraints.size(); ++i)
102  {
103  if (IO::HasParam(constraints[i]))
104  ++set;
105  }
106 
107  if (set == 0)
108  {
109  util::PrefixedOutStream& stream = fatal ? Log::Fatal : Log::Warn;
110  stream << (fatal ? "Must " : "Should ");
111  if (constraints.size() == 1)
112  {
113  // This shouldn't happen... just use PARAM_*_REQ()...
114  stream << "pass " << PRINT_PARAM_STRING(constraints[0]);
115  }
116  else if (constraints.size() == 2)
117  {
118  stream << "pass either " << PRINT_PARAM_STRING(constraints[0])
119  << " or " << PRINT_PARAM_STRING(constraints[1]) << " or both";
120  }
121  else
122  {
123  stream << "pass one of ";
124  for (size_t i = 0; i < constraints.size() - 1; ++i)
125  stream << PRINT_PARAM_STRING(constraints[i]) << ", ";
126  stream << "or "
127  << PRINT_PARAM_STRING(constraints[constraints.size() - 1]);
128  }
129 
130  // Append custom error message.
131  if (!errorMessage.empty())
132  stream << "; " << errorMessage << "!" << std::endl;
133  else
134  stream << "!" << std::endl;
135  }
136 }
137 
139  const std::vector<std::string>& constraints,
140  const bool fatal,
141  const std::string& errorMessage)
142 {
143  if (BINDING_IGNORE_CHECK(constraints))
144  return;
145 
146  size_t set = 0;
147  for (size_t i = 0; i < constraints.size(); ++i)
148  {
149  if (IO::HasParam(constraints[i]))
150  ++set;
151  }
152 
153  if (set != 0 && set < constraints.size())
154  {
155  util::PrefixedOutStream& stream = fatal ? Log::Fatal : Log::Warn;
156  stream << (fatal ? "Must " : "Should ");
157  if (constraints.size() == 2)
158  {
159  stream << "pass none or both of " << PRINT_PARAM_STRING(constraints[0])
160  << " and " << PRINT_PARAM_STRING(constraints[1]);
161  }
162  else
163  {
164  // constraints.size() > 2.
165  stream << "pass none or all of ";
166  for (size_t i = 0; i < constraints.size() - 1; ++i)
167  stream << PRINT_PARAM_STRING(constraints[i]) << ", ";
168  stream << "and "
169  << PRINT_PARAM_STRING(constraints[constraints.size() - 1]);
170  }
171 
172  // Append custom error message.
173  if (!errorMessage.empty())
174  stream << "; " << errorMessage << "!" << std::endl;
175  else
176  stream << "!" << std::endl;
177  }
178 }
179 
180 template<typename T>
181 void RequireParamInSet(const std::string& name,
182  const std::vector<T>& set,
183  const bool fatal,
184  const std::string& errorMessage)
185 {
186  if (BINDING_IGNORE_CHECK(name))
187  return;
188 
189  if (std::find(set.begin(), set.end(), IO::GetParam<T>(name)) == set.end())
190  {
191  // The item was not found in the set.
192  util::PrefixedOutStream& stream = fatal ? Log::Fatal : Log::Warn;
193  stream << "Invalid value of " << PRINT_PARAM_STRING(name) << " specified ("
194  << PRINT_PARAM_VALUE(IO::GetParam<T>(name), true) << "); ";
195  if (!errorMessage.empty())
196  stream << errorMessage << "; ";
197  stream << "must be one of ";
198  for (size_t i = 0; i < set.size() - 1; ++i)
199  stream << PRINT_PARAM_VALUE(set[i], true) << ", ";
200  stream << "or " << PRINT_PARAM_VALUE(set[set.size() - 1], true) << "!"
201  << std::endl;
202  }
203 }
204 
205 template<typename T>
206 void RequireParamValue(const std::string& name,
207  const std::function<bool(T)>& conditional,
208  const bool fatal,
209  const std::string& errorMessage)
210 {
211  if (BINDING_IGNORE_CHECK(name))
212  return;
213 
214  // We need to make sure that the condition holds.
215  bool condition = conditional(IO::GetParam<T>(name));
216  if (!condition)
217  {
218  // The condition failed.
219  util::PrefixedOutStream& stream = fatal ? Log::Fatal : Log::Warn;
220  stream << "Invalid value of " << PRINT_PARAM_STRING(name) << " specified ("
221  << PRINT_PARAM_VALUE(IO::GetParam<T>(name), false) << "); "
222  << errorMessage << "!" << std::endl;
223  }
224 }
225 
226 inline void ReportIgnoredParam(
227  const std::vector<std::pair<std::string, bool>>& constraints,
228  const std::string& paramName)
229 {
230  if (BINDING_IGNORE_CHECK(paramName))
231  return;
232 
233  // Determine whether or not the condition is true.
234  bool condition = true;
235  for (size_t i = 0; i < constraints.size(); ++i)
236  {
237  if (IO::HasParam(constraints[i].first) != constraints[i].second)
238  {
239  condition = false;
240  break;
241  }
242  }
243 
244  // If the condition is satisfied, then report that the parameter is ignored
245  // (if the user passed it).
246  if (condition && IO::HasParam(paramName))
247  {
248  // The output will be different depending on whether there are 1, 2, or more
249  // constraints.
250  Log::Warn << PRINT_PARAM_STRING(paramName) << " ignored because ";
251  if (constraints.size() == 1)
252  {
253  Log::Warn << PRINT_PARAM_STRING(constraints[0].first)
254  << ((constraints[0].second) ? " is " : " is not ")
255  << "specified!" << std::endl;
256  }
257  else if (constraints.size() == 2)
258  {
259  if (constraints[0].second == constraints[1].second)
260  {
261  Log::Warn << ((constraints[0].second) ? "both " : "neither ")
262  << PRINT_PARAM_STRING(constraints[0].first)
263  << ((constraints[0].second) ? "or " : "nor ")
264  << PRINT_PARAM_STRING(constraints[1].first)
265  << " are specified!" << std::endl;
266  }
267  else
268  {
269  Log::Warn << PRINT_PARAM_STRING(constraints[0].first)
270  << ((constraints[0].second) ? " is " : " is not ")
271  << "specified and "
272  << ((constraints[1].second) ? " is " : " is not ")
273  << "specified!" << std::endl;
274  }
275  }
276  else
277  {
278  // List each constraint and whether or not it was or wasn't specified.
279  for (size_t i = 0; i < constraints.size(); ++i)
280  {
281  Log::Warn << PRINT_PARAM_STRING(constraints[i].first)
282  << ((constraints[i].second) ? " is " : " is not ")
283  << ((i == constraints.size() - 1) ? "specified!"
284  : "specified and ");
285  }
286  Log::Warn << std::endl;
287  }
288  }
289 }
290 
291 inline void ReportIgnoredParam(const std::string& paramName,
292  const std::string& reason)
293 {
294  // If the argument was passed, we need to print the reason.
295  if (IO::HasParam(paramName))
296  {
297  Log::Warn << PRINT_PARAM_STRING(paramName) << " ignored because "
298  << reason << "!" << std::endl;
299  }
300 }
301 
302 } // namespace util
303 } // namespace mlpack
304 
305 #endif
static bool HasParam(const std::string &identifier)
See if the specified flag was found while parsing.
Definition: io.cpp:85
void RequireParamInSet(const std::string &paramName, const std::vector< T > &set, const bool fatal, const std::string &errorMessage)
Require that a given parameter is in a set of allowable parameters.
Definition: param_checks_impl.hpp:181
void RequireAtLeastOnePassed(const std::vector< std::string > &constraints, const bool fatal=true, const std::string &customErrorMessage="")
Require that at least one of the given parameters in the constraints set was passed to the IO object;...
Definition: param_checks_impl.hpp:92
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
void RequireOnlyOnePassed(const std::vector< std::string > &constraints, const bool fatal=true, const std::string &customErrorMessage="", const bool allowNone=false)
Require that only one of the given parameters in the constraints set was passed to the IO object; oth...
Definition: param_checks_impl.hpp:21
void RequireNoneOrAllPassed(const std::vector< std::string > &constraints, const bool fatal=true, const std::string &customErrorMessage="")
Require that either none or all of the given parameters in the constraints set were passed to the IO ...
Definition: param_checks_impl.hpp:138
static MLPACK_EXPORT util::PrefixedOutStream Warn
Prints warning messages prefixed with [WARN ].
Definition: log.hpp:87
void RequireParamValue(const std::string &paramName, const std::function< bool(T)> &conditional, const bool fatal, const std::string &errorMessage)
Require that a given parameter satisfies the given conditional function.
Definition: param_checks_impl.hpp:206
void ReportIgnoredParam(const std::vector< std::pair< std::string, bool >> &constraints, const std::string &paramName)
Report that a parameter is ignored, if each of the constraints given are satisfied.
Definition: param_checks_impl.hpp:226
Allows us to output to an ostream with a prefix at the beginning of each line, in the same way we wou...
Definition: prefixedoutstream.hpp:46