print_output_processing.hpp
Go to the documentation of this file.
1 
13 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_OUTPUT_PROCESSING_HPP
14 #define MLPACK_BINDINGS_PYTHON_PRINT_OUTPUT_PROCESSING_HPP
15 
16 #include <mlpack/prereqs.hpp>
17 #include "get_arma_type.hpp"
18 #include "get_numpy_type_char.hpp"
19 #include "get_cython_type.hpp"
20 
21 namespace mlpack {
22 namespace bindings {
23 namespace python {
24 
28 template<typename T>
30  util::Params& /* params */,
31  util::ParamData& d,
32  const size_t indent,
33  const bool onlyOutput,
34  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
35  const typename std::enable_if<!data::HasSerialize<T>::value>::type* = 0,
36  const typename std::enable_if<!std::is_same<T,
37  std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0)
38 {
39  const std::string prefix(indent, ' ');
40 
41  if (onlyOutput)
42  {
48  std::cout << prefix << "result = " << "p.Get[" << GetCythonType<T>(d)
49  << "](\"" << d.name << "\")";
50  if (GetCythonType<T>(d) == "string")
51  {
52  std::cout << std::endl << prefix << "result = result.decode(\"UTF-8\")";
53  }
54  else if (GetCythonType<T>(d) == "vector[string]")
55  {
56  std::cout << std::endl << prefix
57  << "result = [x.decode(\"UTF-8\") for x in result]";
58  }
59  }
60  else
61  {
67  std::cout << prefix << "result['" << d.name << "'] = p.Get["
68  << GetCythonType<T>(d) << "](\"" << d.name << "\")" << std::endl;
69  if (GetCythonType<T>(d) == "string")
70  {
71  std::cout << prefix << "result['" << d.name << "'] = result['" << d.name
72  << "'].decode(\"UTF-8\")" << std::endl;
73  }
74  else if (GetCythonType<T>(d) == "vector[string]")
75  {
76  std::cout << prefix << "result['" << d.name << "'] = [x.decode(\"UTF-8\")"
77  << " for x in result['" << d.name << "']]" << std::endl;
78  }
79  }
80 }
81 
85 template<typename T>
87  util::Params& /* params */,
88  util::ParamData& d,
89  const size_t indent,
90  const bool onlyOutput,
91  const typename std::enable_if<arma::is_arma_type<T>::value>::type* = 0)
92 {
93  const std::string prefix(indent, ' ');
94 
95  if (onlyOutput)
96  {
104  std::cout << prefix << "result = arma_numpy." << GetArmaType<T>()
105  << "_to_numpy_" << GetNumpyTypeChar<T>() << "(p.Get["
106  << GetCythonType<T>(d) << "](\"" << d.name << "\"))" << std::endl;
107  }
108  else
109  {
118  std::cout << prefix << "result['" << d.name
119  << "'] = arma_numpy." << GetArmaType<T>() << "_to_numpy_"
120  << GetNumpyTypeChar<T>() << "(p.Get[" << GetCythonType<T>(d)
121  << "]('" << d.name << "'))" << std::endl;
122  }
123 }
124 
128 template<typename T>
130  util::Params& /* params */,
131  util::ParamData& d,
132  const size_t indent,
133  const bool onlyOutput,
134  const typename std::enable_if<std::is_same<T,
135  std::tuple<data::DatasetInfo, arma::mat>>::value>::type* = 0)
136 {
137  const std::string prefix(indent, ' ');
138 
139  // Print the output with the matrix type. The dimension information doesn't
140  // need to go back.
141  if (onlyOutput)
142  {
148  std::cout << prefix << "result = arma_numpy.mat_to_numpy_"
149  << GetNumpyTypeChar<arma::mat>()
150  << "(GetParamWithInfo[arma.Mat[double]](p, '" << d.name << "'))"
151  << std::endl;
152  }
153  else
154  {
161  std::cout << prefix << "result['" << d.name
162  << "'] = arma_numpy.mat_to_numpy_" << GetNumpyTypeChar<arma::mat>()
163  << "(GetParamWithInfo[arma.Mat[double]](p, '" << d.name << "'))"
164  << std::endl;
165  }
166 }
167 
171 template<typename T>
173  util::Params& params,
174  util::ParamData& d,
175  const size_t indent,
176  const bool onlyOutput,
177  const typename std::enable_if<!arma::is_arma_type<T>::value>::type* = 0,
178  const typename std::enable_if<data::HasSerialize<T>::value>::type* = 0)
179 {
180  // Get the type names we need to use.
181  std::string strippedType, printedType, defaultsType;
182  StripType(d.cppType, strippedType, printedType, defaultsType);
183 
184  const std::string prefix(indent, ' ');
185 
186  if (onlyOutput)
187  {
194  std::cout << prefix << "result = " << strippedType << "Type()" << std::endl;
195  std::cout << prefix << "(<" << strippedType << "Type?> result).modelptr = "
196  << "GetParamPtr[" << strippedType << "](p, '" << d.name << "')"
197  << std::endl;
198 
205  std::map<std::string, util::ParamData>& parameters = params.Parameters();
206  for (auto it = parameters.begin(); it != parameters.end(); ++it)
207  {
208  // Is it an input parameter of the same type?
209  util::ParamData& data = it->second;
210  if (data.input && data.cppType == d.cppType && data.required)
211  {
212  std::cout << prefix << "if (<" << strippedType
213  << "Type> result).modelptr" << d.name << " == (<" << strippedType
214  << "Type> " << data.name << ").modelptr:" << std::endl;
215  std::cout << prefix << " (<" << strippedType
216  << "Type> result).modelptr = <" << strippedType << "*> 0"
217  << std::endl;
218  std::cout << prefix << " result = " << data.name << std::endl;
219  }
220  else if (data.input && data.cppType == d.cppType)
221  {
222  std::cout << prefix << "if " << data.name << " is not None:"
223  << std::endl;
224  std::cout << prefix << " if (<" << strippedType
225  << "Type> result).modelptr" << d.name << " == (<" << strippedType
226  << "Type> " << data.name << ").modelptr:" << std::endl;
227  std::cout << prefix << " (<" << strippedType
228  << "Type> result).modelptr = <" << strippedType << "*> 0"
229  << std::endl;
230  std::cout << prefix << " result = " << data.name << std::endl;
231  }
232  }
233  }
234  else
235  {
242  std::cout << prefix << "result['" << d.name << "'] = " << strippedType
243  << "Type()" << std::endl;
244  std::cout << prefix << "(<" << strippedType << "Type?> result['" << d.name
245  << "']).modelptr = GetParamPtr[" << strippedType << "](p, '" << d.name
246  << "')" << std::endl;
247 
254  std::map<std::string, util::ParamData>& parameters = params.Parameters();
255  for (auto it = parameters.begin(); it != parameters.end(); ++it)
256  {
257  // Is it an input parameter of the same type?
258  util::ParamData& data = it->second;
259  if (data.input && data.cppType == d.cppType && data.required)
260  {
261  std::cout << prefix << "if (<" << strippedType << "Type> result['"
262  << d.name << "']).modelptr == (<" << strippedType << "Type> "
263  << data.name << ").modelptr:" << std::endl;
264  std::cout << prefix << " (<" << strippedType << "Type> result['"
265  << d.name << "']).modelptr = <" << strippedType << "*> 0"
266  << std::endl;
267  std::cout << prefix << " result['" << d.name << "'] = " << data.name
268  << std::endl;
269  }
270  else if (data.input && data.cppType == d.cppType)
271  {
272  std::cout << prefix << "if " << data.name << " is not None:"
273  << std::endl;
274  std::cout << prefix << " if (<" << strippedType << "Type> result['"
275  << d.name << "']).modelptr == (<" << strippedType << "Type> "
276  << data.name << ").modelptr:" << std::endl;
277  std::cout << prefix << " (<" << strippedType << "Type> result['"
278  << d.name << "']).modelptr = <" << strippedType << "*> 0"
279  << std::endl;
280  std::cout << prefix << " result['" << d.name << "'] = " << data.name
281  << std::endl;
282  }
283  }
284  }
285 }
286 
303 template<typename T>
305  const void* input,
306  void* /* output */)
307 {
308  typedef std::tuple<util::Params, std::tuple<size_t, bool>> TupleType;
309  TupleType* tuple = (TupleType*) input;
310 
311  PrintOutputProcessing<typename std::remove_pointer<T>::type>(std::get<0>(*tuple),
312  d, std::get<0>(std::get<1>(*tuple)), std::get<1>(std::get<1>(*tuple)));
313 }
314 
315 } // namespace python
316 } // namespace bindings
317 } // namespace mlpack
318 
319 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
void PrintOutputProcessing(util::Params &, util::ParamData &d, const size_t indent, const bool onlyOutput, 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 output processing for a regular parameter type.
The core includes that mlpack expects; standard C++ includes and Armadillo.
bool input
True if this option is an input option (otherwise, it is output).
Definition: param_data.hpp:73
python
Definition: CMakeLists.txt:7
std::map< std::string, ParamData > & Parameters()
Get the map of parameters.
Definition: params.hpp:96
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
The Params class holds all information about the parameters passed to a specific binding.
Definition: params.hpp:20
std::string cppType
The true name of the type, as it would be written in C++.
Definition: param_data.hpp:81
if(NOT BUILD_GO_SHLIB) macro(add_go_binding name) endmacro() return() endif() endmacro() macro(post_go_setup) if(BUILD_GO_BINDINGS) file(APPEND "$
Definition: CMakeLists.txt:3