print_input_processing.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
14 #define MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include "get_arma_type.hpp"
18 #include "get_numpy_type.hpp"
19 #include "get_numpy_type_char.hpp"
20 #include "get_cython_type.hpp"
21 #include "strip_type.hpp"
22 
23 namespace mlpack {
24 namespace bindings {
25 namespace python {
26 
30 template<typename T>
32  const util::ParamData& d,
33  const size_t indent,
34  const typename boost::disable_if<util::IsStdVector<T>>::type* = 0,
35  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
36  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
37  const typename boost::disable_if<std::is_same<T,
38  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
39 {
40  // The copy_all_inputs parameter must be handled first, and therefore is
41  // outside the scope of this code.
42  if (d.name == "copy_all_inputs")
43  return;
44 
45  const std::string prefix(indent, ' ');
46 
47  std::string def = "None";
48  if (std::is_same<T, bool>::value)
49  def = "False";
50 
51  // Make sure that we don't use names that are Python keywords.
52  std::string name = (d.name == "lambda") ? "lambda_" : d.name;
53 
65  std::cout << prefix << "# Detect if the parameter was passed; set if so."
66  << std::endl;
67  if (!d.required)
68  {
69  if (GetPrintableType<T>(d) == "bool")
70  {
71  std::cout << prefix << "if isinstance(" << name << ", "
72  << GetPrintableType<T>(d) << "):" << std::endl;
73  std::cout << prefix << " if " << name << " is not " << def << ":"
74  << std::endl;
75  }
76  else
77  {
78  std::cout << prefix << "if " << name << " is not " << def << ":"
79  << std::endl;
80  std::cout << prefix << " if isinstance(" << name << ", "
81  << GetPrintableType<T>(d) << "):" << std::endl;
82  }
83 
84  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
85  << "](<const string> '" << d.name << "', ";
86  if (GetCythonType<T>(d) == "string")
87  std::cout << name << ".encode(\"UTF-8\")";
88  else
89  std::cout << name;
90  std::cout << ")" << std::endl;
91  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
92  << "')" << std::endl;
93 
94  // If this parameter is "verbose", then enable verbose output.
95  if (d.name == "verbose")
96  std::cout << prefix << " EnableVerbose()" << std::endl;
97 
98  if (GetPrintableType<T>(d) == "bool")
99  {
100  std::cout << " else:" << std::endl;
101  std::cout << " raise TypeError(" <<"\"'"<< name
102  << "' must have type \'" << GetPrintableType<T>(d)
103  << "'!\")" << std::endl;
104  }
105  else
106  {
107  std::cout << " else:" << std::endl;
108  std::cout << " raise TypeError(" <<"\"'"<< name
109  << "' must have type \'" << GetPrintableType<T>(d)
110  << "'!\")" << std::endl;
111  }
112  }
113  else
114  {
115  if (GetPrintableType<T>(d) == "bool")
116  {
117  std::cout << prefix << "if isinstance(" << name << ", "
118  << GetPrintableType<T>(d) << "):" << std::endl;
119  std::cout << prefix << " if " << name << " is not " << def << ":"
120  << std::endl;
121  }
122  else
123  {
124  std::cout << prefix << "if " << name << " is not " << def << ":"
125  << std::endl;
126  std::cout << prefix << " if isinstance(" << name << ", "
127  << GetPrintableType<T>(d) << "):" << std::endl;
128  }
129 
130  std::cout << prefix << " SetParam[" << GetCythonType<T>(d) << "](<const "
131  << "string> '" << d.name << "', ";
132  if (GetCythonType<T>(d) == "string")
133  std::cout << name << ".encode(\"UTF-8\")";
134  else if (GetCythonType<T>(d) == "vector[string]")
135  std::cout << "[i.encode(\"UTF-8\") for i in " << name << "]";
136  else
137  std::cout << name;
138  std::cout << ")" << std::endl;
139  std::cout << prefix << " CLI.SetPassed(<const string> '"
140  << d.name << "')" << std::endl;
141 
142  if (GetPrintableType<T>(d) == "bool")
143  {
144  std::cout << " else:" << std::endl;
145  std::cout << " raise TypeError(" <<"\"'"<< name
146  << "' must have type \'" << GetPrintableType<T>(d)
147  << "'!\")" << std::endl;
148  }
149  else
150  {
151  std::cout << " else:" << std::endl;
152  std::cout << " raise TypeError(" <<"\"'"<< name
153  << "' must have type \'" << GetPrintableType<T>(d)
154  << "'!\")" << std::endl;
155  }
156  }
157  std::cout << std::endl; // Extra line is to clear up the code a bit.
158 }
159 
163 template<typename T>
165  const util::ParamData& d,
166  const size_t indent,
167  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
168  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
169  const typename boost::disable_if<std::is_same<T,
170  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0,
171  const typename boost::enable_if<util::IsStdVector<T>>::type* = 0)
172 {
173  const std::string prefix(indent, ' ');
174 
189  std::cout << prefix << "# Detect if the parameter was passed; set if so."
190  << std::endl;
191  if (!d.required)
192  {
193  std::cout << prefix << "if " << d.name << " is not None:"
194  << std::endl;
195  std::cout << prefix << " if isinstance(" << d.name << ", list):"
196  << std::endl;
197  std::cout << prefix << " if len(" << d.name << ") > 0:"
198  << std::endl;
199  std::cout << prefix << " if isinstance(" << d.name << "[0], "
200  << GetPrintableType<typename T::value_type>(d) << "):" << std::endl;
201  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
202  << "](<const string> '" << d.name << "', ";
203  // Strings need special handling.
204  if (GetCythonType<T>(d) == "vector[string]")
205  std::cout << "[i.encode(\"UTF-8\") for i in " << d.name << "]";
206  else
207  std::cout << d.name;
208  std::cout << ")" << std::endl;
209  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
210  << "')" << std::endl;
211  std::cout << prefix << " else:" << std::endl;
212  std::cout << prefix << " raise TypeError(" <<"\"'"<< d.name
213  << "' must have type \'" << GetPrintableType<T>(d)
214  << "'!\")" << std::endl;
215  std::cout << prefix << " else:" << std::endl;
216  std::cout << prefix << " raise TypeError(" <<"\"'"<< d.name
217  << "' must have type \'list'!\")" << std::endl;
218  }
219  else
220  {
221  std::cout << prefix << "if isinstance(" << d.name << ", list):"
222  << std::endl;
223  std::cout << prefix << " if len(" << d.name << ") > 0:"
224  << std::endl;
225  std::cout << prefix << " if isinstance(" << d.name << "[0], "
226  << GetPrintableType<typename T::value_type>(d) << "):" << std::endl;
227  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
228  << "](<const string> '" << d.name << "', ";
229  // Strings need special handling.
230  if (GetCythonType<T>(d) == "vector[string]")
231  std::cout << "[i.encode(\"UTF-8\") for i in " << d.name << "]";
232  else
233  std::cout << d.name;
234  std::cout << ")" << std::endl;
235  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
236  << "')" << std::endl;
237  std::cout << prefix << " else:" << std::endl;
238  std::cout << prefix << " raise TypeError(" <<"\"'"<< d.name
239  << "' must have type \'" << GetPrintableType<T>(d)
240  << "'!\")" << std::endl;
241  std::cout << prefix << "else:" << std::endl;
242  std::cout << prefix << " raise TypeError(" <<"\"'"<< d.name
243  << "' must have type \'list'!\")" << std::endl;
244  }
245 }
246 
250 template<typename T>
252  const util::ParamData& d,
253  const size_t indent,
254  const typename boost::disable_if<util::IsStdVector<T>>::type* = 0,
255  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
256 {
257  const std::string prefix(indent, ' ');
258 
274  std::cout << prefix << "# Detect if the parameter was passed; set if so."
275  << std::endl;
276  if (!d.required)
277  {
278  if (T::is_row || T::is_col)
279  {
280  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
281  std::cout << prefix << " " << d.name << "_tuple = to_matrix("
282  << d.name << ", dtype=" << GetNumpyType<typename T::elem_type>()
283  << ", copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
284  std::cout << prefix << " if len(" << d.name << "_tuple[0].shape) > 1:"
285  << std::endl;
286  std::cout << prefix << " if " << d.name << "_tuple[0]"
287  << ".shape[0] == 1 or " << d.name << "_tuple[0].shape[1] == 1:"
288  << std::endl;
289  std::cout << prefix << " " << d.name << "_tuple[0].shape = ("
290  << d.name << "_tuple[0].size,)" << std::endl;
291  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_"
292  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
293  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
294  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
295  << "](<const string> '" << d.name << "', dereference("
296  << d.name << "_mat))"<< std::endl;
297  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
298  << "')" << std::endl;
299  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
300  }
301  else
302  {
303  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
304  std::cout << prefix << " " << d.name << "_tuple = to_matrix("
305  << d.name << ", dtype=" << GetNumpyType<typename T::elem_type>()
306  << ", copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
307  std::cout << prefix << " if len(" << d.name << "_tuple[0].shape"
308  << ") < 2:" << std::endl;
309  std::cout << prefix << " " << d.name << "_tuple[0].shape = (" << d.name
310  << "_tuple[0].shape[0], 1)" << std::endl;
311  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_"
312  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
313  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
314  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
315  << "](<const string> '" << d.name << "', dereference("
316  << d.name << "_mat))"<< std::endl;
317  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
318  << "')" << std::endl;
319  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
320  }
321  }
322  else
323  {
324  if (T::is_row || T::is_col)
325  {
326  std::cout << prefix << " " << d.name << "_tuple = to_matrix("
327  << d.name << ", dtype=" << GetNumpyType<typename T::elem_type>()
328  << ", copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
329  std::cout << prefix << " if len(" << d.name << "_tuple[0].shape) > 1:"
330  << std::endl;
331  std::cout << prefix << " if " << d.name << "_tuple[0]"
332  << ".shape[0] == 1 or " << d.name << "_tuple[0].shape[1] == 1:"
333  << std::endl;
334  std::cout << prefix << " " << d.name << "_tuple[0].shape = ("
335  << d.name << "_tuple[0].size,)" << std::endl;
336  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_"
337  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
338  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
339  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
340  << "](<const string> '" << d.name << "', dereference("
341  << d.name << "_mat))"<< std::endl;
342  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
343  << "')" << std::endl;
344  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
345  }
346  else
347  {
348  std::cout << prefix << " " << d.name << "_tuple = to_matrix("
349  << d.name << ", dtype=" << GetNumpyType<typename T::elem_type>()
350  << ", copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
351  std::cout << prefix << " if len(" << d.name << "_tuple[0].shape) > 2:"
352  << std::endl;
353  std::cout << prefix << " " << d.name << "_tuple[0].shape = (" << d.name
354  << "_tuple[0].shape[0], 1)" << std::endl;
355  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_"
356  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
357  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
358  std::cout << prefix << " SetParam[" << GetCythonType<T>(d)
359  << "](<const string> '" << d.name << "', dereference("
360  << d.name << "_mat))"<< std::endl;
361  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
362  << "')" << std::endl;
363  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
364  }
365  }
366  std::cout << std::endl;
367 }
368 
372 template<typename T>
374  const util::ParamData& d,
375  const size_t indent,
376  const typename boost::disable_if<util::IsStdVector<T>>::type* = 0,
377  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
378  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
379 {
380  // First, get the correct class name if needed.
381  std::string strippedType, printedType, defaultsType;
382  StripType(d.cppType, strippedType, printedType, defaultsType);
383 
384  const std::string prefix(indent, ' ');
385 
402  std::cout << prefix << "# Detect if the parameter was passed; set if so."
403  << std::endl;
404  if (!d.required)
405  {
406  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
407  std::cout << prefix << " try:" << std::endl;
408  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
409  << "', (<" << strippedType << "Type?> " << d.name << ").modelptr, "
410  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
411  std::cout << prefix << " except TypeError as e:" << std::endl;
412  std::cout << prefix << " if type(" << d.name << ").__name__ == '"
413  << strippedType << "Type':" << std::endl;
414  std::cout << prefix << " SetParamPtr[" << strippedType << "]('"
415  << d.name << "', (<" << strippedType << "Type> " << d.name
416  << ").modelptr, CLI.HasParam('copy_all_inputs'))" << std::endl;
417  std::cout << prefix << " else:" << std::endl;
418  std::cout << prefix << " raise e" << std::endl;
419  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name << "')"
420  << std::endl;
421  }
422  else
423  {
424  std::cout << prefix << "try:" << std::endl;
425  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
426  << "', (<" << strippedType << "Type?> " << d.name << ").modelptr, "
427  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
428  std::cout << prefix << "except TypeError as e:" << std::endl;
429  std::cout << prefix << " if type(" << d.name << ").__name__ == '"
430  << strippedType << "Type':" << std::endl;
431  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
432  << "', (<" << strippedType << "Type> " << d.name << ").modelptr, "
433  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
434  std::cout << prefix << " else:" << std::endl;
435  std::cout << prefix << " raise e" << std::endl;
436  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
437  << std::endl;
438  }
439  std::cout << std::endl;
440 }
441 
445 template<typename T>
447  const util::ParamData& d,
448  const size_t indent,
449  const typename boost::disable_if<util::IsStdVector<T>>::type* = 0,
450  const typename boost::enable_if<std::is_same<T,
451  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
452 {
453  // The user should pass in a matrix type of some sort.
454  const std::string prefix(indent, ' ');
455 
467  std::cout << prefix << "cdef np.ndarray " << d.name << "_dims" << std::endl;
468  std::cout << prefix << "# Detect if the parameter was passed; set if so."
469  << std::endl;
470  if (!d.required)
471  {
472  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
473  std::cout << prefix << " " << d.name << "_tuple = to_matrix_with_info("
474  << d.name << ", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))"
475  << std::endl;
476  std::cout << prefix << " if len(" << d.name << "_tuple[0].shape"
477  << ") < 2:" << std::endl;
478  std::cout << prefix << " " << d.name << "_tuple[0].shape = (" << d.name
479  << "_tuple[0].shape[0], 1)" << std::endl;
480  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_mat_d("
481  << d.name << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
482  std::cout << prefix << " " << d.name << "_dims = " << d.name
483  << "_tuple[2]" << std::endl;
484  std::cout << prefix << " SetParamWithInfo[arma.Mat[double]](<const "
485  << "string> '" << d.name << "', dereference(" << d.name << "_mat), "
486  << "<const cbool*> " << d.name << "_dims.data)" << std::endl;
487  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
488  << "')" << std::endl;
489  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
490  }
491  else
492  {
493  std::cout << prefix << d.name << "_tuple = to_matrix_with_info(" << d.name
494  << ", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))"
495  << std::endl;
496  std::cout << prefix << "if len(" << d.name << "_tuple[0].shape"
497  << ") < 2:" << std::endl;
498  std::cout << prefix << " " << d.name << "_tuple[0].shape = (" << d.name
499  << "_tuple[0].shape[0], 1)" << std::endl;
500  std::cout << prefix << d.name << "_mat = arma_numpy.numpy_to_mat_d("
501  << d.name << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
502  std::cout << prefix << d.name << "_dims = " << d.name << "_tuple[2]"
503  << std::endl;
504  std::cout << prefix << "SetParamWithInfo[arma.Mat[double]](<const "
505  << "string> '" << d.name << "', dereference(" << d.name << "_mat), "
506  << "<const cbool*> " << d.name << "_dims.data)" << std::endl;
507  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
508  << std::endl;
509  std::cout << prefix << "del " << d.name << "_mat" << std::endl;
510  }
511  std::cout << std::endl;
512 }
513 
525 template<typename T>
527  const void* input,
528  void* /* output */)
529 {
530  PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
531  *((size_t*) input));
532 }
533 
534 } // namespace python
535 } // namespace bindings
536 } // namespace mlpack
537 
538 #endif
.hpp
Definition: add_to_po.hpp:21
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...
Definition: param_data.hpp:52
Metaprogramming structure for vector detection.
std::string name
Name of this parameter.
Definition: param_data.hpp:56
bool required
True if this option is required.
Definition: param_data.hpp:71
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...
Definition: strip_type.hpp:28
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:84
void PrintInputProcessing(const util::ParamData &d, const size_t indent, const typename boost::disable_if< util::IsStdVector< T >>::type *=0, const typename boost::disable_if< arma::is_arma_type< T >>::type *=0, const typename boost::disable_if< data::HasSerialize< T >>::type *=0, const typename boost::disable_if< std::is_same< T, std::tuple< data::DatasetInfo, arma::mat >>>::type *=0)
Print input processing for a standard option type.