print_input_processing.hpp
Go to the documentation of this file.
1 
12 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
13 #define MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
14 
15 #include <mlpack/prereqs.hpp>
16 #include "get_arma_type.hpp"
17 #include "get_numpy_type.hpp"
18 #include "get_numpy_type_char.hpp"
19 #include "get_cython_type.hpp"
20 #include "strip_type.hpp"
21 
22 namespace mlpack {
23 namespace bindings {
24 namespace python {
25 
29 template<typename T>
31  const util::ParamData& d,
32  const size_t indent,
33  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
34  const typename boost::disable_if<data::HasSerialize<T>>::type* = 0,
35  const typename boost::disable_if<std::is_same<T,
36  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
37 {
38  // The copy_all_inputs parameter must be handled first, and therefore is
39  // outside the scope of this code.
40  if (d.name == "copy_all_inputs")
41  return;
42 
43  const std::string prefix(indent, ' ');
44 
45  std::string def = "None";
46  if (std::is_same<T, bool>::value)
47  def = "False";
48 
49  // Make sure that we don't use names that are Python keywords.
50  std::string name = (d.name == "lambda") ? "lambda_" : d.name;
51 
60  std::cout << prefix << "# Detect if the parameter was passed; set if so."
61  << std::endl;
62  if (!d.required)
63  {
64  std::cout << prefix << "if " << name << " is not " << def << ":"
65  << std::endl;
66 
67  std::cout << prefix << " SetParam[" << GetCythonType<T>(d) << "](<const "
68  << "string> '" << d.name << "', ";
69  if (GetCythonType<T>(d) == "string")
70  std::cout << name << ".encode(\"UTF-8\")";
71  else if (GetCythonType<T>(d) == "vector[string]")
72  std::cout << "[i.encode(\"UTF-8\") for i in " << name << "]";
73  else
74  std::cout << name;
75  std::cout << ")" << std::endl;
76  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name
77  << "')" << std::endl;
78 
79  // If this parameter is "verbose", then enable verbose output.
80  if (d.name == "verbose")
81  std::cout << prefix << " EnableVerbose()" << std::endl;
82  }
83  else
84  {
85  std::cout << prefix << "SetParam[" << GetCythonType<T>(d) << "](<const "
86  << "string> '" << d.name << "', ";
87  if (GetCythonType<T>(d) == "string")
88  std::cout << name << ".encode(\"UTF-8\")";
89  else if (GetCythonType<T>(d) == "vector[string]")
90  std::cout << "[i.encode(\"UTF-8\") for i in " << name << "]";
91  else
92  std::cout << name;
93  std::cout << ")" << std::endl;
94  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
95  << std::endl;
96  }
97  std::cout << std::endl; // Extra line is to clear up the code a bit.
98 }
99 
103 template<typename T>
105  const util::ParamData& d,
106  const size_t indent,
107  const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
108 {
109  const std::string prefix(indent, ' ');
110 
122  std::cout << prefix << "# Detect if the parameter was passed; set if so."
123  << std::endl;
124  if (!d.required)
125  {
126  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
127 
128  std::cout << prefix << " " << d.name << "_tuple = to_matrix(" << d.name
129  << ", dtype=" << GetNumpyType<typename T::elem_type>() << ", "
130  << "copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
131  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_"
132  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
133  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
134  std::cout << prefix << " SetParam[" << GetCythonType<T>(d) << "](<const "
135  << "string> '" << d.name << "', dereference(" << d.name << "_mat))"
136  << std::endl;
137  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name << "')"
138  << std::endl;
139  std::cout << prefix << " del " << d.name << "_mat";
140  }
141  else
142  {
143  std::cout << prefix << d.name << "_tuple = to_matrix(" << d.name
144  << ", dtype=" << GetNumpyType<typename T::elem_type>() << ", "
145  << "copy=CLI.HasParam('copy_all_inputs'))" << std::endl;
146  std::cout << prefix << d.name << "_mat = arma_numpy.numpy_to_"
147  << GetArmaType<T>() << "_" << GetNumpyTypeChar<T>() << "(" << d.name
148  << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
149  std::cout << prefix << "SetParam[" << GetCythonType<T>(d) << "](<const "
150  << "string> '" << d.name << "', dereference(" << d.name << "_mat))"
151  << std::endl;
152  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
153  << std::endl;
154  std::cout << prefix << "del " << d.name << "_mat";
155  }
156  std::cout << std::endl;
157 }
158 
162 template<typename T>
164  const util::ParamData& d,
165  const size_t indent,
166  const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
167  const typename boost::enable_if<data::HasSerialize<T>>::type* = 0)
168 {
169  // First, get the correct class name if needed.
170  std::string strippedType, printedType, defaultsType;
171  StripType(d.cppType, strippedType, printedType, defaultsType);
172 
173  const std::string prefix(indent, ' ');
174 
191  std::cout << prefix << "# Detect if the parameter was passed; set if so."
192  << std::endl;
193  if (!d.required)
194  {
195  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
196  std::cout << prefix << " try:" << std::endl;
197  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
198  << "', (<" << strippedType << "Type?> " << d.name << ").modelptr, "
199  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
200  std::cout << prefix << " except TypeError as e:" << std::endl;
201  std::cout << prefix << " if type(" << d.name << ").__name__ == '"
202  << strippedType << "Type':" << std::endl;
203  std::cout << prefix << " SetParamPtr[" << strippedType << "]('"
204  << d.name << "', (<" << strippedType << "Type> " << d.name
205  << ").modelptr, CLI.HasParam('copy_all_inputs'))" << std::endl;
206  std::cout << prefix << " else:" << std::endl;
207  std::cout << prefix << " raise e" << std::endl;
208  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name << "')"
209  << std::endl;
210  }
211  else
212  {
213  std::cout << prefix << "try:" << std::endl;
214  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
215  << "', (<" << strippedType << "Type?> " << d.name << ").modelptr, "
216  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
217  std::cout << prefix << "except TypeError as e:" << std::endl;
218  std::cout << prefix << " if type(" << d.name << ").__name__ == '"
219  << strippedType << "Type':" << std::endl;
220  std::cout << prefix << " SetParamPtr[" << strippedType << "]('" << d.name
221  << "', (<" << strippedType << "Type> " << d.name << ").modelptr, "
222  << "CLI.HasParam('copy_all_inputs'))" << std::endl;
223  std::cout << prefix << " else:" << std::endl;
224  std::cout << prefix << " raise e" << std::endl;
225  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
226  << std::endl;
227  }
228  std::cout << std::endl;
229 }
230 
234 template<typename T>
236  const util::ParamData& d,
237  const size_t indent,
238  const typename boost::enable_if<std::is_same<T,
239  std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
240 {
241  // The user should pass in a matrix type of some sort.
242  const std::string prefix(indent, ' ');
243 
253  std::cout << prefix << "cdef np.ndarray " << d.name << "_dims" << std::endl;
254  std::cout << prefix << "# Detect if the parameter was passed; set if so."
255  << std::endl;
256  if (!d.required)
257  {
258  std::cout << prefix << "if " << d.name << " is not None:" << std::endl;
259  std::cout << prefix << " " << d.name << "_tuple = to_matrix_with_info("
260  << d.name << ", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))"
261  << std::endl;
262  std::cout << prefix << " " << d.name << "_mat = arma_numpy.numpy_to_mat_d("
263  << d.name << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
264  std::cout << prefix << " " << d.name << "_dims = " << d.name << "_tuple[2]"
265  << std::endl;
266  std::cout << prefix << " SetParamWithInfo[arma.Mat[double]](<const string>"
267  << " '" << d.name << "', dereference(" << d.name << "_mat), <const "
268  << "bool*> " << d.name << "_dims.data)" << std::endl;
269  std::cout << prefix << " CLI.SetPassed(<const string> '" << d.name << "')"
270  << std::endl;
271  std::cout << prefix << " del " << d.name << "_mat" << std::endl;
272  }
273  else
274  {
275  std::cout << prefix << d.name << "_tuple = to_matrix_with_info(" << d.name
276  << ", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))"
277  << std::endl;
278  std::cout << prefix << d.name << "_mat = arma_numpy.numpy_to_mat_d("
279  << d.name << "_tuple[0], " << d.name << "_tuple[1])" << std::endl;
280  std::cout << prefix << d.name << "_dims = " << d.name << "_tuple[2]"
281  << std::endl;
282  std::cout << prefix << "SetParamWithInfo[arma.Mat[double]](<const string>"
283  << " '" << d.name << "', dereference(" << d.name << "_mat), <const "
284  << "bool*> " << d.name << "_dims.data)" << std::endl;
285  std::cout << prefix << "CLI.SetPassed(<const string> '" << d.name << "')"
286  << std::endl;
287  std::cout << prefix << "del " << d.name << "_mat" << std::endl;
288  }
289  std::cout << std::endl;
290 }
291 
303 template<typename T>
305  const void* input,
306  void* /* output */)
307 {
308  PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
309  *((size_t*) input));
310 }
311 
312 } // namespace python
313 } // namespace bindings
314 } // namespace mlpack
315 
316 #endif
void PrintInputProcessing(const util::ParamData &d, const size_t indent, 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.
.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
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