12 #ifndef MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP 13 #define MLPACK_BINDINGS_PYTHON_PRINT_INPUT_PROCESSING_HPP 33 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
35 const typename boost::disable_if<std::is_same<T,
36 std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
40 if (d.
name ==
"copy_all_inputs")
43 const std::string prefix(indent,
' ');
45 std::string def =
"None";
46 if (std::is_same<T, bool>::value)
50 std::string name = (d.
name ==
"lambda") ?
"lambda_" : d.
name;
60 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 64 std::cout << prefix <<
"if " << name <<
" is not " << def <<
":" 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 <<
"]";
75 std::cout <<
")" << std::endl;
76 std::cout << prefix <<
" CLI.SetPassed(<const string> '" << d.
name 80 if (d.
name ==
"verbose")
81 std::cout << prefix <<
" EnableVerbose()" << std::endl;
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 <<
"]";
93 std::cout <<
")" << std::endl;
94 std::cout << prefix <<
"CLI.SetPassed(<const string> '" << d.
name <<
"')" 97 std::cout << std::endl;
107 const typename boost::enable_if<arma::is_arma_type<T>>::type* = 0)
109 const std::string prefix(indent,
' ');
122 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 126 std::cout << prefix <<
"if " << d.
name <<
" is not None:" << std::endl;
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))" 137 std::cout << prefix <<
" CLI.SetPassed(<const string> '" << d.
name <<
"')" 139 std::cout << prefix <<
" del " << d.
name <<
"_mat";
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))" 152 std::cout << prefix <<
"CLI.SetPassed(<const string> '" << d.
name <<
"')" 154 std::cout << prefix <<
"del " << d.
name <<
"_mat";
156 std::cout << std::endl;
166 const typename boost::disable_if<arma::is_arma_type<T>>::type* = 0,
170 std::string strippedType, printedType, defaultsType;
173 const std::string prefix(indent,
' ');
191 std::cout << prefix <<
"# Detect if the parameter was passed; set if so." 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 <<
"')" 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 <<
"')" 228 std::cout << std::endl;
238 const typename boost::enable_if<std::is_same<T,
239 std::tuple<data::DatasetInfo, arma::mat>>>::type* = 0)
242 const std::string prefix(indent,
' ');
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." 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'))" 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]" 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 <<
"')" 271 std::cout << prefix <<
" del " << d.
name <<
"_mat" << std::endl;
275 std::cout << prefix << d.
name <<
"_tuple = to_matrix_with_info(" << d.
name 276 <<
", dtype=np.double, copy=CLI.HasParam('copy_all_inputs'))" 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]" 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 <<
"')" 287 std::cout << prefix <<
"del " << d.
name <<
"_mat" << std::endl;
289 std::cout << std::endl;
308 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++.