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::ParamData& d,
31  const size_t indent,
32  const bool onlyOutput,
33  const typename boost::disable_if>::type* = 0,
34  const typename boost::disable_if<data::HasSerialize>::type* = 0,
35  const typename boost::disable_if
36  std::tuple>>::type* = 0)
37 {
38  const std::string prefix(indent, ' ');
39 
40  if (onlyOutput)
41  {
47  std::cout << prefix << "result = " << "IO.GetParam[" << GetCythonType(d)
48  << "](\"" << d.name << "\")";
49  if (GetCythonType(d) == "string")
50  {
51  std::cout << std::endl << prefix << "result = result.decode(\"UTF-8\")";
52  }
53  else if (GetCythonType(d) == "vector[string]")
54  {
55  std::cout << std::endl << prefix
56  << "result = [x.decode(\"UTF-8\") for x in result]";
57  }
58  }
59  else
60  {
66  std::cout << prefix << "result['" << d.name << "'] = IO.GetParam["
67  << GetCythonType(d) << "](\"" << d.name << "\")" << std::endl;
68  if (GetCythonType(d) == "string")
69  {
70  std::cout << prefix << "result['" << d.name << "'] = result['" << d.name
71  << "'].decode(\"UTF-8\")" << std::endl;
72  }
73  else if (GetCythonType(d) == "vector[string]")
74  {
75  std::cout << prefix << "result['" << d.name << "'] = [x.decode(\"UTF-8\")"
76  << " for x in result['" << d.name << "']]" << std::endl;
77  }
78  }
79 }
80 
84 template<typename T>
86  util::ParamData& d,
87  const size_t indent,
88  const bool onlyOutput,
89  const typename boost::enable_if>::type* = 0)
90 {
91  const std::string prefix(indent, ' ');
92 
93  if (onlyOutput)
94  {
102  std::cout << prefix << "result = arma_numpy." << GetArmaType()
103  << "_to_numpy_" << GetNumpyTypeChar() << "(IO.GetParam["
104  << GetCythonType(d) << "](\"" << d.name << "\"))" << std::endl;
105  }
106  else
107  {
116  std::cout << prefix << "result['" << d.name
117  << "'] = arma_numpy." << GetArmaType() << "_to_numpy_"
118  << GetNumpyTypeChar() << "(IO.GetParam[" << GetCythonType(d)
119  << "]('" << d.name << "'))" << std::endl;
120  }
121 }
122 
126 template<typename T>
128  util::ParamData& d,
129  const size_t indent,
130  const bool onlyOutput,
131  const typename boost::enable_if
132  std::tuple>>::type* = 0)
133 {
134  const std::string prefix(indent, ' ');
135 
136  // Print the output with the matrix type. The dimension information doesn't
137  // need to go back.
138  if (onlyOutput)
139  {
145  std::cout << prefix << "result = arma_numpy.mat_to_numpy_"
146  << GetNumpyTypeChar()
147  << "(GetParamWithInfo[arma.Mat[double]]('" << d.name << "'))"
148  << std::endl;
149  }
150  else
151  {
158  std::cout << prefix << "result['" << d.name
159  << "'] = arma_numpy.mat_to_numpy_" << GetNumpyTypeChar()
160  << "(GetParamWithInfo[arma.Mat[double]]('" << d.name << "'))"
161  << std::endl;
162  }
163 }
164 
168 template<typename T>
170  util::ParamData& d,
171  const size_t indent,
172  const bool onlyOutput,
173  const typename boost::disable_if>::type* = 0,
174  const typename boost::enable_if<data::HasSerialize>::type* = 0)
175 {
176  // Get the type names we need to use.
177  std::string strippedType, printedType, defaultsType;
178  StripType(d.cppType, strippedType, printedType, defaultsType);
179 
180  const std::string prefix(indent, ' ');
181 
182  if (onlyOutput)
183  {
190  std::cout << prefix << "result = " << strippedType << "Type()" << std::endl;
191  std::cout << prefix << "(<" << strippedType << "Type?> result).modelptr = "
192  << "GetParamPtr[" << strippedType << "]('" << d.name << "')"
193  << std::endl;
194 
201  std::map& parameters = IO::Parameters();
202  for (auto it = parameters.begin(); it != parameters.end(); ++it)
203  {
204  // Is it an input parameter of the same type?
205  util::ParamData& data = it->second;
206  if (data.input && data.cppType == d.cppType && data.required)
207  {
208  std::cout << prefix << "if (<" << strippedType
209  << "Type> result).modelptr" << d.name << " == (<" << strippedType
210  << "Type> " << data.name << ").modelptr:" << std::endl;
211  std::cout << prefix << " (<" << strippedType
212  << "Type> result).modelptr = <" << strippedType << "*> 0"
213  << std::endl;
214  std::cout << prefix << " result = " << data.name << std::endl;
215  }
216  else if (data.input && data.cppType == d.cppType)
217  {
218  std::cout << prefix << "if " << data.name << " is not None:"
219  << std::endl;
220  std::cout << prefix << " if (<" << strippedType
221  << "Type> result).modelptr" << d.name << " == (<" << strippedType
222  << "Type> " << data.name << ").modelptr:" << std::endl;
223  std::cout << prefix << " (<" << strippedType
224  << "Type> result).modelptr = <" << strippedType << "*> 0"
225  << std::endl;
226  std::cout << prefix << " result = " << data.name << std::endl;
227  }
228  }
229  }
230  else
231  {
238  std::cout << prefix << "result['" << d.name << "'] = " << strippedType
239  << "Type()" << std::endl;
240  std::cout << prefix << "(<" << strippedType << "Type?> result['" << d.name
241  << "']).modelptr = GetParamPtr[" << strippedType << "]('" << d.name
242  << "')" << std::endl;
243 
250  std::map& parameters = IO::Parameters();
251  for (auto it = parameters.begin(); it != parameters.end(); ++it)
252  {
253  // Is it an input parameter of the same type?
254  util::ParamData& data = it->second;
255  if (data.input && data.cppType == d.cppType && data.required)
256  {
257  std::cout << prefix << "if (<" << strippedType << "Type> result['"
258  << d.name << "']).modelptr == (<" << strippedType << "Type> "
259  << data.name << ").modelptr:" << std::endl;
260  std::cout << prefix << " (<" << strippedType << "Type> result['"
261  << d.name << "']).modelptr = <" << strippedType << "*> 0"
262  << std::endl;
263  std::cout << prefix << " result['" << d.name << "'] = " << data.name
264  << std::endl;
265  }
266  else if (data.input && data.cppType == d.cppType)
267  {
268  std::cout << prefix << "if " << data.name << " is not None:"
269  << std::endl;
270  std::cout << prefix << " if (<" << strippedType << "Type> result['"
271  << d.name << "']).modelptr == (<" << strippedType << "Type> "
272  << data.name << ").modelptr:" << std::endl;
273  std::cout << prefix << " (<" << strippedType << "Type> result['"
274  << d.name << "']).modelptr = <" << strippedType << "*> 0"
275  << std::endl;
276  std::cout << prefix << " result['" << d.name << "'] = " << data.name
277  << std::endl;
278  }
279  }
280  }
281 }
282 
297 template<typename T>
299  const void* input,
300  void* /* output */)
301 {
302  std::tuple* tuple = (std::tuple*) input;
303 
304  PrintOutputProcessing::type>(d,
305  std::get<0>(*tuple), std::get<1>(*tuple));
306 }
307 
308 } // namespace python
309 } // namespace bindings
310 } // namespace mlpack
311 
312 #endif
Linear algebra utility functions, generally performed on matrices or vectors.
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:6
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 PrintOutputProcessing(util::ParamData &d, const size_t indent, const bool onlyOutput, 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 output processing for a regular parameter type.
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
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
static std::map< std::string, util::ParamData > & Parameters()
Return a modifiable list of parameters that IO knows about.