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