13 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP 14 #define MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP 36 const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
38 const typename std::enable_if<!std::is_same<T,
39 std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0)
43 if (d.
name ==
"copy_all_inputs")
46 const std::string prefix(indent,
' ');
48 std::string def =
"None";
49 if (std::is_same<T, bool>::value)
66 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 70 if (GetPrintableType<T>(d) ==
"bool")
72 std::cout << prefix <<
"if isinstance(" << name <<
", " 73 << GetPrintableType<T>(d) <<
"):" << std::endl;
74 std::cout << prefix <<
" if " << name <<
" is not " << def <<
":" 79 std::cout << prefix <<
"if " << name <<
" is not " << def <<
":" 81 std::cout << prefix <<
" if isinstance(" << name <<
", " 82 << GetPrintableType<T>(d) <<
"):" << std::endl;
85 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d)
86 <<
"](p, <const string> '" << d.
name <<
"', ";
87 if (GetCythonType<T>(d) ==
"string")
88 std::cout << name <<
".encode(\"UTF-8\")";
91 std::cout <<
")" << std::endl;
92 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 96 if (d.
name ==
"verbose")
97 std::cout << prefix <<
" EnableVerbose()" << std::endl;
99 if (GetPrintableType<T>(d) ==
"bool")
101 std::cout <<
" else:" << std::endl;
102 std::cout <<
" raise TypeError(" <<
"\"'"<< name
103 <<
"' must have type \'" << GetPrintableType<T>(d)
104 <<
"'!\")" << std::endl;
108 std::cout <<
" else:" << std::endl;
109 std::cout <<
" raise TypeError(" <<
"\"'"<< name
110 <<
"' must have type \'" << GetPrintableType<T>(d)
111 <<
"'!\")" << std::endl;
116 if (GetPrintableType<T>(d) ==
"bool")
118 std::cout << prefix <<
"if isinstance(" << name <<
", " 119 << GetPrintableType<T>(d) <<
"):" << std::endl;
120 std::cout << prefix <<
" if " << name <<
" is not " << def <<
":" 125 std::cout << prefix <<
"if " << name <<
" is not " << def <<
":" 127 std::cout << prefix <<
" if isinstance(" << name <<
", " 128 << GetPrintableType<T>(d) <<
"):" << std::endl;
131 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d) <<
"](p, <const " 132 <<
"string> '" << d.
name <<
"', ";
133 if (GetCythonType<T>(d) ==
"string")
134 std::cout << name <<
".encode(\"UTF-8\")";
135 else if (GetCythonType<T>(d) ==
"vector[string]")
136 std::cout <<
"[i.encode(\"UTF-8\") for i in " << name <<
"]";
139 std::cout <<
")" << std::endl;
140 std::cout << prefix <<
" p.SetPassed(<const string> '" 141 << d.
name <<
"')" << std::endl;
143 if (GetPrintableType<T>(d) ==
"bool")
145 std::cout <<
" else:" << std::endl;
146 std::cout <<
" raise TypeError(" <<
"\"'"<< name
147 <<
"' must have type \'" << GetPrintableType<T>(d)
148 <<
"'!\")" << std::endl;
152 std::cout <<
" else:" << std::endl;
153 std::cout <<
" raise TypeError(" <<
"\"'"<< name
154 <<
"' must have type \'" << GetPrintableType<T>(d)
155 <<
"'!\")" << std::endl;
158 std::cout << std::endl;
168 const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
170 const typename std::enable_if<!std::is_same<T,
171 std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0,
174 const std::string prefix(indent,
' ');
190 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 197 std::cout << prefix <<
"if " << name <<
" is not None:" 199 std::cout << prefix <<
" if isinstance(" << name <<
", list):" 201 std::cout << prefix <<
" if len(" << name <<
") > 0:" 203 std::cout << prefix <<
" if isinstance(" << name <<
"[0], " 204 << GetPrintableType<typename T::value_type>(d) <<
"):" << std::endl;
205 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d)
206 <<
"](p, <const string> '" << d.
name <<
"', ";
208 if (GetCythonType<T>(d) ==
"vector[string]")
209 std::cout <<
"[i.encode(\"UTF-8\") for i in " << name <<
"]";
212 std::cout <<
")" << std::endl;
213 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 214 <<
"')" << std::endl;
215 std::cout << prefix <<
" else:" << std::endl;
216 std::cout << prefix <<
" raise TypeError(" <<
"\"'"<< d.
name 217 <<
"' must have type \'" << GetPrintableType<T>(d)
218 <<
"'!\")" << std::endl;
219 std::cout << prefix <<
" else:" << std::endl;
220 std::cout << prefix <<
" raise TypeError(" <<
"\"'"<< d.
name 221 <<
"' must have type \'list'!\")" << std::endl;
225 std::cout << prefix <<
"if isinstance(" << name <<
", list):" 227 std::cout << prefix <<
" if len(" << name <<
") > 0:" 229 std::cout << prefix <<
" if isinstance(" << name <<
"[0], " 230 << GetPrintableType<typename T::value_type>(d) <<
"):" << std::endl;
231 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d)
232 <<
"](p, <const string> '" << d.
name <<
"', ";
234 if (GetCythonType<T>(d) ==
"vector[string]")
235 std::cout <<
"[i.encode(\"UTF-8\") for i in " << name <<
"]";
238 std::cout <<
")" << std::endl;
239 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 240 <<
"')" << std::endl;
241 std::cout << prefix <<
" else:" << std::endl;
242 std::cout << prefix <<
" raise TypeError(" <<
"\"'"<< d.
name 243 <<
"' must have type \'" << GetPrintableType<T>(d)
244 <<
"'!\")" << std::endl;
245 std::cout << prefix <<
"else:" << std::endl;
246 std::cout << prefix <<
" raise TypeError(" <<
"\"'"<< d.
name 247 <<
"' must have type \'list'!\")" << std::endl;
259 const typename std::enable_if<arma::is_arma_type<T>::value>::type* = 0)
261 const std::string prefix(indent,
' ');
278 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 284 if (T::is_row || T::is_col)
286 std::cout << prefix <<
"if " << name <<
" is not None:" << std::endl;
287 std::cout << prefix <<
" " << name <<
"_tuple = to_matrix(" 288 << name <<
", dtype=" << GetNumpyType<typename T::elem_type>()
289 <<
", copy=p.Has('copy_all_inputs'))" << std::endl;
290 std::cout << prefix <<
" if len(" << name <<
"_tuple[0].shape) > 1:" 292 std::cout << prefix <<
" if " << name <<
"_tuple[0]" 293 <<
".shape[0] == 1 or " << name <<
"_tuple[0].shape[1] == 1:" 295 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" 296 << d.
name <<
"_tuple[0].size,)" << std::endl;
297 std::cout << prefix <<
" " << name <<
"_mat = arma_numpy.numpy_to_" 298 << GetArmaType<T>() <<
"_" << GetNumpyTypeChar<T>() <<
"(" << name
299 <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
300 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d)
301 <<
"](p, <const string> '" << d.
name <<
"', dereference(" 302 << name <<
"_mat))"<< std::endl;
303 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 304 <<
"')" << std::endl;
305 std::cout << prefix <<
" del " << name <<
"_mat" << std::endl;
309 std::cout << prefix <<
"if " << name <<
" is not None:" << std::endl;
310 std::cout << prefix <<
" " << name <<
"_tuple = to_matrix(" 311 << name <<
", dtype=" << GetNumpyType<typename T::elem_type>()
312 <<
", copy=p.Has('copy_all_inputs'))" << std::endl;
313 std::cout << prefix <<
" if len(" << name <<
"_tuple[0].shape" 314 <<
") < 2:" << std::endl;
315 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" << name
316 <<
"_tuple[0].shape[0], 1)" << std::endl;
317 std::cout << prefix <<
" " << name <<
"_mat = arma_numpy.numpy_to_" 318 << GetArmaType<T>() <<
"_" << GetNumpyTypeChar<T>() <<
"(" << name
319 <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
320 std::cout << prefix <<
" SetParam[" << GetCythonType<T>(d)
321 <<
"](p, <const string> '" << d.
name <<
"', dereference(" 322 << name <<
"_mat))"<< std::endl;
323 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 324 <<
"')" << std::endl;
325 std::cout << prefix <<
" del " << name <<
"_mat" << std::endl;
330 if (T::is_row || T::is_col)
332 std::cout << prefix << name <<
"_tuple = to_matrix(" << name
333 <<
", dtype=" << GetNumpyType<typename T::elem_type>()
334 <<
", copy=p.Has('copy_all_inputs'))" << std::endl;
335 std::cout << prefix <<
"if len(" << name <<
"_tuple[0].shape) > 1:" 337 std::cout << prefix <<
" if " << name <<
"_tuple[0].shape[0] == 1 or " 338 << name <<
"_tuple[0].shape[1] == 1:" << std::endl;
339 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" 340 << name <<
"_tuple[0].size,)" << std::endl;
341 std::cout << prefix << name <<
"_mat = arma_numpy.numpy_to_" 342 << GetArmaType<T>() <<
"_" << GetNumpyTypeChar<T>() <<
"(" << name
343 <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
344 std::cout << prefix <<
"SetParam[" << GetCythonType<T>(d)
345 <<
"](p, <const string> '" << d.
name <<
"', dereference(" 346 << name <<
"_mat))"<< std::endl;
347 std::cout << prefix <<
"p.SetPassed(<const string> '" << d.
name <<
"')" 349 std::cout << prefix <<
"del " << name <<
"_mat" << std::endl;
353 std::cout << prefix << name <<
"_tuple = to_matrix(" << name
354 <<
", dtype=" << GetNumpyType<typename T::elem_type>()
355 <<
", copy=p.Has('copy_all_inputs'))" << std::endl;
356 std::cout << prefix <<
"if len(" << name <<
"_tuple[0].shape) < 2:" 358 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" << name
359 <<
"_tuple[0].shape[0], 1)" << std::endl;
360 std::cout << prefix << name <<
"_mat = arma_numpy.numpy_to_" 361 << GetArmaType<T>() <<
"_" << GetNumpyTypeChar<T>() <<
"(" << name
362 <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
363 std::cout << prefix <<
"SetParam[" << GetCythonType<T>(d)
364 <<
"](p, <const string> '" << d.
name <<
"', dereference(" << name
365 <<
"_mat))" << std::endl;
366 std::cout << prefix <<
"p.SetPassed(<const string> '" << d.
name <<
"')" 368 std::cout << prefix <<
"del " << name <<
"_mat" << std::endl;
371 std::cout << std::endl;
382 const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
386 std::string strippedType, printedType, defaultsType;
391 const std::string prefix(indent,
' ');
409 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 413 std::cout << prefix <<
"if " << name <<
" is not None:" << std::endl;
414 std::cout << prefix <<
" try:" << std::endl;
415 std::cout << prefix <<
" SetParamPtr[" << strippedType <<
"](p, '" << d.
name 416 <<
"', (<" << strippedType <<
"Type?> " << name <<
").modelptr, " 417 <<
"p.Has('copy_all_inputs'))" << std::endl;
418 std::cout << prefix <<
" except TypeError as e:" << std::endl;
419 std::cout << prefix <<
" if type(" << name <<
").__name__ == '" 420 << strippedType <<
"Type':" << std::endl;
421 std::cout << prefix <<
" SetParamPtr[" << strippedType <<
"](p, '" 422 << d.
name <<
"', (<" << strippedType <<
"Type> " << name
423 <<
").modelptr, p.Has('copy_all_inputs'))" << std::endl;
424 std::cout << prefix <<
" else:" << std::endl;
425 std::cout << prefix <<
" raise e" << std::endl;
426 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name <<
"')" 431 std::cout << prefix <<
"try:" << std::endl;
432 std::cout << prefix <<
" SetParamPtr[" << strippedType <<
"](p, '" << d.
name 433 <<
"', (<" << strippedType <<
"Type?> " << name <<
").modelptr, " 434 <<
"p.Has('copy_all_inputs'))" << std::endl;
435 std::cout << prefix <<
"except TypeError as e:" << std::endl;
436 std::cout << prefix <<
" if type(" << name <<
").__name__ == '" 437 << strippedType <<
"Type':" << std::endl;
438 std::cout << prefix <<
" SetParamPtr[" << strippedType <<
"](p,'" << d.
name 439 <<
"', (<" << strippedType <<
"Type> " << name <<
").modelptr, " 440 <<
"p.Has('copy_all_inputs'))" << std::endl;
441 std::cout << prefix <<
" else:" << std::endl;
442 std::cout << prefix <<
" raise e" << std::endl;
443 std::cout << prefix <<
"p.SetPassed(<const string> '" << d.
name <<
"')" 446 std::cout << std::endl;
457 const typename std::enable_if<std::is_same<T,
458 std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0)
463 const std::string prefix(indent,
' ');
476 std::cout << prefix <<
"cdef np.ndarray " << name <<
"_dims" << std::endl;
477 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 481 std::cout << prefix <<
"if " << name <<
" is not None:" << std::endl;
482 std::cout << prefix <<
" " << name <<
"_tuple = to_matrix_with_info(" 483 << name <<
", dtype=np.double, copy=p.Has('copy_all_inputs'))" 485 std::cout << prefix <<
" if len(" << name <<
"_tuple[0].shape" 486 <<
") < 2:" << std::endl;
487 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" << name
488 <<
"_tuple[0].shape[0], 1)" << std::endl;
489 std::cout << prefix <<
" " << name <<
"_mat = arma_numpy.numpy_to_mat_d(" 490 << name <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
491 std::cout << prefix <<
" " << name <<
"_dims = " << name
492 <<
"_tuple[2]" << std::endl;
493 std::cout << prefix <<
" SetParamWithInfo[arma.Mat[double]](p, <const " 494 <<
"string> '" << d.
name <<
"', dereference(" << name <<
"_mat), " 495 <<
"<const cbool*> " << name <<
"_dims.data)" << std::endl;
496 std::cout << prefix <<
" p.SetPassed(<const string> '" << d.
name 497 <<
"')" << std::endl;
498 std::cout << prefix <<
" del " << name <<
"_mat" << std::endl;
502 std::cout << prefix << name <<
"_tuple = to_matrix_with_info(" << name
503 <<
", dtype=np.double, copy=p.Has('copy_all_inputs'))" 505 std::cout << prefix <<
"if len(" << name <<
"_tuple[0].shape" 506 <<
") < 2:" << std::endl;
507 std::cout << prefix <<
" " << name <<
"_tuple[0].shape = (" << name
508 <<
"_tuple[0].shape[0], 1)" << std::endl;
509 std::cout << prefix << name <<
"_mat = arma_numpy.numpy_to_mat_d(" 510 << name <<
"_tuple[0], " << name <<
"_tuple[1])" << std::endl;
511 std::cout << prefix << name <<
"_dims = " << name <<
"_tuple[2]" 513 std::cout << prefix <<
"SetParamWithInfo[arma.Mat[double]](p, <const " 514 <<
"string> '" << d.
name <<
"', dereference(" << name <<
"_mat), " 515 <<
"<const cbool*> " << name <<
"_dims.data)" << std::endl;
516 std::cout << prefix <<
"p.SetPassed(<const string> '" << d.
name <<
"')" 518 std::cout << prefix <<
"del " << name <<
"_mat" << std::endl;
520 std::cout << std::endl;
539 PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
Linear algebra utility functions, generally performed on matrices or vectors.
The core includes that mlpack expects; standard C++ includes and Armadillo.
This structure holds all of the information about a single parameter, including its value (which is s...
Metaprogramming structure for vector detection.
void PrintInputProcessing(util::ParamData &d, const size_t indent, const typename std::enable_if<!util::IsStdVector< T >::value >::type *=0, const typename std::enable_if<!arma::is_arma_type< T >::value >::type *=0, const typename std::enable_if<!data::HasSerialize< T >::value >::type *=0, const typename std::enable_if<!std::is_same< T, std::tuple< data::DatasetInfo, arma::mat >>::value >::type *=0)
Print input processing for a standard option type.
std::string name
Name of this parameter.
bool required
True if this option is required.
void StripType(const std::string &inputType, std::string &strippedType, std::string &printedType, std::string &defaultsType)
Given an input type like, e.g., "LogisticRegression<>", return three types that can be used in Python...
std::string cppType
The true name of the type, as it would be written in C++.
if(NOT BUILD_GO_SHLIB) macro(add_go_binding name) endmacro() return() endif() endmacro() macro(post_go_setup) if(BUILD_GO_BINDINGS) file(APPEND "$
std::string GetValidName(const std::string ¶mName)