12 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
13 #define MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP
34 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
36 const typename boost::disable_if<std::is_same<T,
37 std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
41 if (d.
name ==
"copy_all_inputs")
44 const std::string prefix(indent,
' ');
46 std::string def =
"None";
47 if (std::is_same<T, bool>::value)
51 std::string name = (d.
name ==
"lambda") ?
"lambda_" : d.
name;
61 std::cout << prefix <<
"# Detect if the parameter was passed; set if so."
65 std::cout << prefix <<
"if " << name <<
" is not " << def <<
":"
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 <<
"]";
76 std::cout <<
")" << std::endl;
77 std::cout << prefix <<
" CLI.SetPassed(<const string> '" << d.
name
81 if (d.
name ==
"verbose")
82 std::cout << prefix <<
" EnableVerbose()" << std::endl;
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 <<
"]";
94 std::cout <<
")" << std::endl;
95 std::cout << prefix <<
"CLI.SetPassed(<const string> '" << d.
name <<
"')"
98 std::cout << std::endl;
108 const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
110 const std::string prefix(indent,
' ');
123 std::cout << prefix <<
"# Detect if the parameter was passed; set if so."
127 std::cout << prefix <<
"if " << d.
name <<
" is not None:" << std::endl;
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))"
138 std::cout << prefix <<
" CLI.SetPassed(<const string> '" << d.
name <<
"')"
140 std::cout << prefix <<
" del " << d.
name <<
"_mat";
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))"
153 std::cout << prefix <<
"CLI.SetPassed(<const string> '" << d.
name <<
"')"
155 std::cout << prefix <<
"del " << d.
name <<
"_mat";
157 std::cout << std::endl;
167 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
171 std::string strippedType, printedType, defaultsType;
174 const std::string prefix(indent,
' ');
192 std::cout << prefix <<
"# Detect if the parameter was passed; set if so."
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 <<
"')"
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 <<
"')"
229 std::cout << std::endl;
239 const typename boost::enable_if<std::is_same<T,
240 std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
243 const std::string prefix(indent,
' ');
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."
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'))"
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]"
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 <<
"')"
272 std::cout << prefix <<
" del " << d.
name <<
"_mat" << std::endl;
276 std::cout << prefix << d.
name <<
"_tuple = to_matrix_with_info(" << d.
name
277 <<
", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))"
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]"
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 <<
"')"
288 std::cout << prefix <<
"del " << d.
name <<
"_mat" << std::endl;
290 std::cout << std::endl;
309 PrintInputProcessing<typename std::remove_pointer<T>::type>(d,
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...
std::string name
Name of this parameter.
bool required
True if this option is required.
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...
std::string cppType
The true name of the type, as it would be written in C++.