From 8a9c86a22d3b20b80172250fcd8579fcb112297d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ricardo=20Montan=CC=83ana?= Date: Sat, 8 Jul 2023 00:39:10 +0200 Subject: [PATCH] Update BayesNetwork class --- bayesclass/BayesNetwork.cpp | 654 ++++++++++++++++++++++-------------- bayesclass/BayesNetwork.pyx | 16 +- 2 files changed, 419 insertions(+), 251 deletions(-) diff --git a/bayesclass/BayesNetwork.cpp b/bayesclass/BayesNetwork.cpp index c96498e..13a87a8 100644 --- a/bayesclass/BayesNetwork.cpp +++ b/bayesclass/BayesNetwork.cpp @@ -1074,6 +1074,23 @@ static int __Pyx_ParseOptionalKeywords(PyObject *kwds, PyObject **argnames[],\ static void __Pyx_RaiseArgtupleInvalid(const char* func_name, int exact, Py_ssize_t num_min, Py_ssize_t num_max, Py_ssize_t num_found); +/* ListCompAppend.proto */ +#if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS +static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) { + PyListObject* L = (PyListObject*) list; + Py_ssize_t len = Py_SIZE(list); + if (likely(L->allocated > len)) { + Py_INCREF(x); + PyList_SET_ITEM(list, len, x); + __Pyx_SET_SIZE(list, len + 1); + return 0; + } + return PyList_Append(list, x); +} +#else +#define __Pyx_ListComp_Append(L,x) PyList_Append(L,x) +#endif + /* PyObjectGetAttrStr.proto */ #if CYTHON_USE_TYPE_SLOTS static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject* attr_name); @@ -1081,13 +1098,6 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject #define __Pyx_PyObject_GetAttrStr(o,n) PyObject_GetAttr(o,n) #endif -/* PyCFunctionFastCall.proto */ -#if CYTHON_FAST_PYCCALL -static CYTHON_INLINE PyObject *__Pyx_PyCFunction_FastCall(PyObject *func, PyObject **args, Py_ssize_t nargs); -#else -#define __Pyx_PyCFunction_FastCall(func, args, nargs) (assert(0), NULL) -#endif - /* PyFunctionFastCall.proto */ #if CYTHON_FAST_PYCALL #define __Pyx_PyFunction_FastCall(func, args, nargs)\ @@ -1126,37 +1136,66 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg #define __Pyx_PyObject_Call(func, arg, kw) PyObject_Call(func, arg, kw) #endif -/* PyObjectCall2Args.proto */ -static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2); - /* PyObjectCallMethO.proto */ #if CYTHON_COMPILING_IN_CPYTHON static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg); #endif +/* PyObjectCallNoArg.proto */ +#if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func); +#else +#define __Pyx_PyObject_CallNoArg(func) __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL) +#endif + +/* PyCFunctionFastCall.proto */ +#if CYTHON_FAST_PYCCALL +static CYTHON_INLINE PyObject *__Pyx_PyCFunction_FastCall(PyObject *func, PyObject **args, Py_ssize_t nargs); +#else +#define __Pyx_PyCFunction_FastCall(func, args, nargs) (assert(0), NULL) +#endif + /* PyObjectCallOneArg.proto */ static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObject *arg); +/* PyObjectCall2Args.proto */ +static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2); + +/* IncludeCppStringH.proto */ +#include + +/* decode_c_string_utf16.proto */ +static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16(const char *s, Py_ssize_t size, const char *errors) { + int byteorder = 0; + return PyUnicode_DecodeUTF16(s, size, errors, &byteorder); +} +static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16LE(const char *s, Py_ssize_t size, const char *errors) { + int byteorder = -1; + return PyUnicode_DecodeUTF16(s, size, errors, &byteorder); +} +static CYTHON_INLINE PyObject *__Pyx_PyUnicode_DecodeUTF16BE(const char *s, Py_ssize_t size, const char *errors) { + int byteorder = 1; + return PyUnicode_DecodeUTF16(s, size, errors, &byteorder); +} + +/* decode_c_bytes.proto */ +static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes( + const char* cstring, Py_ssize_t length, Py_ssize_t start, Py_ssize_t stop, + const char* encoding, const char* errors, + PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)); + +/* decode_cpp_string.proto */ +static CYTHON_INLINE PyObject* __Pyx_decode_cpp_string( + std::string cppstring, Py_ssize_t start, Py_ssize_t stop, + const char* encoding, const char* errors, + PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) { + return __Pyx_decode_c_bytes( + cppstring.data(), cppstring.size(), start, stop, encoding, errors, decode_func); +} + /* GetBuiltinName.proto */ static PyObject *__Pyx_GetBuiltinName(PyObject *name); -/* ListCompAppend.proto */ -#if CYTHON_USE_PYLIST_INTERNALS && CYTHON_ASSUME_SAFE_MACROS -static CYTHON_INLINE int __Pyx_ListComp_Append(PyObject* list, PyObject* x) { - PyListObject* L = (PyListObject*) list; - Py_ssize_t len = Py_SIZE(list); - if (likely(L->allocated > len)) { - Py_INCREF(x); - PyList_SET_ITEM(list, len, x); - __Pyx_SET_SIZE(list, len + 1); - return 0; - } - return PyList_Append(list, x); -} -#else -#define __Pyx_ListComp_Append(L,x) PyList_Append(L,x) -#endif - /* PyObject_GenericGetAttrNoDict.proto */ #if CYTHON_USE_TYPE_SLOTS && CYTHON_USE_PYTYPE_LOOKUP && PY_VERSION_HEX < 0x03070000 static CYTHON_INLINE PyObject* __Pyx_PyObject_GenericGetAttrNoDict(PyObject* obj, PyObject* attr_name); @@ -1361,7 +1400,6 @@ static CYTHON_INLINE PyObject *__pyx_convert_PyUnicode_string_to_py_std__in_stri static CYTHON_INLINE PyObject *__pyx_convert_PyStr_string_to_py_std__in_string(std::string const &); /*proto*/ static CYTHON_INLINE PyObject *__pyx_convert_PyBytes_string_to_py_std__in_string(std::string const &); /*proto*/ static CYTHON_INLINE PyObject *__pyx_convert_PyByteArray_string_to_py_std__in_string(std::string const &); /*proto*/ -static PyObject *__pyx_convert_vector_to_py_std_3a__3a_string(const std::vector &); /*proto*/ #define __Pyx_MODULE_NAME "bayesclass.BayesNet" extern int __pyx_module_is_main_bayesclass__BayesNet; int __pyx_module_is_main_bayesclass__BayesNet = 0; @@ -1574,7 +1612,7 @@ static void __pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_2__dealloc__(struct _ * def __dealloc__(self): * del self.thisptr # <<<<<<<<<<<<<< * def fit(self, X, y, features, className): - * self.thisptr.fit(X, y, features, className) + * features_bytes = [x.encode() for x in features] */ delete __pyx_v_self->thisptr; @@ -1594,8 +1632,8 @@ static void __pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_2__dealloc__(struct _ * def __dealloc__(self): * del self.thisptr * def fit(self, X, y, features, className): # <<<<<<<<<<<<<< - * self.thisptr.fit(X, y, features, className) - * return self + * features_bytes = [x.encode() for x in features] + * self.thisptr.fit(X, y, features_bytes, className.encode()) */ /* Python wrapper */ @@ -1685,12 +1723,21 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_5fit(PyObject *_ } static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_4fit(struct __pyx_obj_10bayesclass_8BayesNet_BayesNetwork *__pyx_v_self, PyObject *__pyx_v_X, PyObject *__pyx_v_y, PyObject *__pyx_v_features, PyObject *__pyx_v_className) { + PyObject *__pyx_v_features_bytes = NULL; + PyObject *__pyx_7genexpr__pyx_v_x = NULL; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations - std::vector > __pyx_t_1; - std::vector __pyx_t_2; - std::vector __pyx_t_3; - std::string __pyx_t_4; + PyObject *__pyx_t_1 = NULL; + PyObject *__pyx_t_2 = NULL; + Py_ssize_t __pyx_t_3; + PyObject *(*__pyx_t_4)(PyObject *); + PyObject *__pyx_t_5 = NULL; + PyObject *__pyx_t_6 = NULL; + PyObject *__pyx_t_7 = NULL; + std::vector > __pyx_t_8; + std::vector __pyx_t_9; + std::vector __pyx_t_10; + std::string __pyx_t_11; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; @@ -1699,19 +1746,119 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_4fit(struct __py /* "bayesclass/BayesNetwork.pyx":28 * del self.thisptr * def fit(self, X, y, features, className): - * self.thisptr.fit(X, y, features, className) # <<<<<<<<<<<<<< + * features_bytes = [x.encode() for x in features] # <<<<<<<<<<<<<< + * self.thisptr.fit(X, y, features_bytes, className.encode()) * return self - * def predict(self, X): */ - __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 28, __pyx_L1_error) - __pyx_t_2 = __pyx_convert_vector_from_py_int(__pyx_v_y); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 28, __pyx_L1_error) - __pyx_t_3 = __pyx_convert_vector_from_py_std_3a__3a_string(__pyx_v_features); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 28, __pyx_L1_error) - __pyx_t_4 = __pyx_convert_string_from_py_std__in_string(__pyx_v_className); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 28, __pyx_L1_error) - __pyx_v_self->thisptr->fit(__pyx_t_1, __pyx_t_2, __pyx_t_3, __pyx_t_4); + { /* enter inner scope */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_1); + if (likely(PyList_CheckExact(__pyx_v_features)) || PyTuple_CheckExact(__pyx_v_features)) { + __pyx_t_2 = __pyx_v_features; __Pyx_INCREF(__pyx_t_2); __pyx_t_3 = 0; + __pyx_t_4 = NULL; + } else { + __pyx_t_3 = -1; __pyx_t_2 = PyObject_GetIter(__pyx_v_features); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_4 = Py_TYPE(__pyx_t_2)->tp_iternext; if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 28, __pyx_L5_error) + } + for (;;) { + if (likely(!__pyx_t_4)) { + if (likely(PyList_CheckExact(__pyx_t_2))) { + if (__pyx_t_3 >= PyList_GET_SIZE(__pyx_t_2)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_5 = PyList_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_5); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 28, __pyx_L5_error) + #else + __pyx_t_5 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_5); + #endif + } else { + if (__pyx_t_3 >= PyTuple_GET_SIZE(__pyx_t_2)) break; + #if CYTHON_ASSUME_SAFE_MACROS && !CYTHON_AVOID_BORROWED_REFS + __pyx_t_5 = PyTuple_GET_ITEM(__pyx_t_2, __pyx_t_3); __Pyx_INCREF(__pyx_t_5); __pyx_t_3++; if (unlikely(0 < 0)) __PYX_ERR(0, 28, __pyx_L5_error) + #else + __pyx_t_5 = PySequence_ITEM(__pyx_t_2, __pyx_t_3); __pyx_t_3++; if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_5); + #endif + } + } else { + __pyx_t_5 = __pyx_t_4(__pyx_t_2); + if (unlikely(!__pyx_t_5)) { + PyObject* exc_type = PyErr_Occurred(); + if (exc_type) { + if (likely(__Pyx_PyErr_GivenExceptionMatches(exc_type, PyExc_StopIteration))) PyErr_Clear(); + else __PYX_ERR(0, 28, __pyx_L5_error) + } + break; + } + __Pyx_GOTREF(__pyx_t_5); + } + __Pyx_XDECREF_SET(__pyx_7genexpr__pyx_v_x, __pyx_t_5); + __pyx_t_5 = 0; + __pyx_t_6 = __Pyx_PyObject_GetAttrStr(__pyx_7genexpr__pyx_v_x, __pyx_n_s_encode); if (unlikely(!__pyx_t_6)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_6); + __pyx_t_7 = NULL; + if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_6))) { + __pyx_t_7 = PyMethod_GET_SELF(__pyx_t_6); + if (likely(__pyx_t_7)) { + PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_6); + __Pyx_INCREF(__pyx_t_7); + __Pyx_INCREF(function); + __Pyx_DECREF_SET(__pyx_t_6, function); + } + } + __pyx_t_5 = (__pyx_t_7) ? __Pyx_PyObject_CallOneArg(__pyx_t_6, __pyx_t_7) : __Pyx_PyObject_CallNoArg(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); __pyx_t_7 = 0; + if (unlikely(!__pyx_t_5)) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_GOTREF(__pyx_t_5); + __Pyx_DECREF(__pyx_t_6); __pyx_t_6 = 0; + if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_5))) __PYX_ERR(0, 28, __pyx_L5_error) + __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; + } + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __Pyx_XDECREF(__pyx_7genexpr__pyx_v_x); __pyx_7genexpr__pyx_v_x = 0; + goto __pyx_L8_exit_scope; + __pyx_L5_error:; + __Pyx_XDECREF(__pyx_7genexpr__pyx_v_x); __pyx_7genexpr__pyx_v_x = 0; + goto __pyx_L1_error; + __pyx_L8_exit_scope:; + } /* exit inner scope */ + __pyx_v_features_bytes = ((PyObject*)__pyx_t_1); + __pyx_t_1 = 0; /* "bayesclass/BayesNetwork.pyx":29 * def fit(self, X, y, features, className): - * self.thisptr.fit(X, y, features, className) + * features_bytes = [x.encode() for x in features] + * self.thisptr.fit(X, y, features_bytes, className.encode()) # <<<<<<<<<<<<<< + * return self + * def predict(self, X): + */ + __pyx_t_8 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 29, __pyx_L1_error) + __pyx_t_9 = __pyx_convert_vector_from_py_int(__pyx_v_y); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 29, __pyx_L1_error) + __pyx_t_10 = __pyx_convert_vector_from_py_std_3a__3a_string(__pyx_v_features_bytes); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 29, __pyx_L1_error) + __pyx_t_2 = __Pyx_PyObject_GetAttrStr(__pyx_v_className, __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_2); + __pyx_t_5 = NULL; + if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_2))) { + __pyx_t_5 = PyMethod_GET_SELF(__pyx_t_2); + if (likely(__pyx_t_5)) { + PyObject* function = PyMethod_GET_FUNCTION(__pyx_t_2); + __Pyx_INCREF(__pyx_t_5); + __Pyx_INCREF(function); + __Pyx_DECREF_SET(__pyx_t_2, function); + } + } + __pyx_t_1 = (__pyx_t_5) ? __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_t_5) : __Pyx_PyObject_CallNoArg(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_5); __pyx_t_5 = 0; + if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; + __pyx_t_11 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 29, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; + __pyx_v_self->thisptr->fit(__pyx_t_8, __pyx_t_9, __pyx_t_10, __pyx_t_11); + + /* "bayesclass/BayesNetwork.pyx":30 + * features_bytes = [x.encode() for x in features] + * self.thisptr.fit(X, y, features_bytes, className.encode()) * return self # <<<<<<<<<<<<<< * def predict(self, X): * return self.thisptr.predict(X) @@ -1725,22 +1872,29 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_4fit(struct __py * def __dealloc__(self): * del self.thisptr * def fit(self, X, y, features, className): # <<<<<<<<<<<<<< - * self.thisptr.fit(X, y, features, className) - * return self + * features_bytes = [x.encode() for x in features] + * self.thisptr.fit(X, y, features_bytes, className.encode()) */ /* function exit code */ __pyx_L1_error:; + __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_2); + __Pyx_XDECREF(__pyx_t_5); + __Pyx_XDECREF(__pyx_t_6); + __Pyx_XDECREF(__pyx_t_7); __Pyx_AddTraceback("bayesclass.BayesNet.BayesNetwork.fit", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; + __Pyx_XDECREF(__pyx_v_features_bytes); + __Pyx_XDECREF(__pyx_7genexpr__pyx_v_x); __Pyx_XGIVEREF(__pyx_r); __Pyx_RefNannyFinishContext(); return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":30 - * self.thisptr.fit(X, y, features, className) +/* "bayesclass/BayesNetwork.pyx":31 + * self.thisptr.fit(X, y, features_bytes, className.encode()) * return self * def predict(self, X): # <<<<<<<<<<<<<< * return self.thisptr.predict(X) @@ -1770,7 +1924,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_6predict(struct int __pyx_clineno = 0; __Pyx_RefNannySetupContext("predict", 0); - /* "bayesclass/BayesNetwork.pyx":31 + /* "bayesclass/BayesNetwork.pyx":32 * return self * def predict(self, X): * return self.thisptr.predict(X) # <<<<<<<<<<<<<< @@ -1778,15 +1932,15 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_6predict(struct * return self.thisptr.predict_proba(X) */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 31, __pyx_L1_error) - __pyx_t_2 = __pyx_convert_vector_to_py_int(__pyx_v_self->thisptr->predict(__pyx_t_1)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 31, __pyx_L1_error) + __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 32, __pyx_L1_error) + __pyx_t_2 = __pyx_convert_vector_to_py_int(__pyx_v_self->thisptr->predict(__pyx_t_1)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 32, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_r = __pyx_t_2; __pyx_t_2 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":30 - * self.thisptr.fit(X, y, features, className) + /* "bayesclass/BayesNetwork.pyx":31 + * self.thisptr.fit(X, y, features_bytes, className.encode()) * return self * def predict(self, X): # <<<<<<<<<<<<<< * return self.thisptr.predict(X) @@ -1804,7 +1958,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_6predict(struct return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":32 +/* "bayesclass/BayesNetwork.pyx":33 * def predict(self, X): * return self.thisptr.predict(X) * def predict_proba(self, X): # <<<<<<<<<<<<<< @@ -1835,7 +1989,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_8predict_proba(s int __pyx_clineno = 0; __Pyx_RefNannySetupContext("predict_proba", 0); - /* "bayesclass/BayesNetwork.pyx":33 + /* "bayesclass/BayesNetwork.pyx":34 * return self.thisptr.predict(X) * def predict_proba(self, X): * return self.thisptr.predict_proba(X) # <<<<<<<<<<<<<< @@ -1843,14 +1997,14 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_8predict_proba(s * return self.thisptr.score(X, y) */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 33, __pyx_L1_error) - __pyx_t_2 = __pyx_convert_vector_to_py_std_3a__3a_vector_3c_double_3e___(__pyx_v_self->thisptr->predict_proba(__pyx_t_1)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 33, __pyx_L1_error) + __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 34, __pyx_L1_error) + __pyx_t_2 = __pyx_convert_vector_to_py_std_3a__3a_vector_3c_double_3e___(__pyx_v_self->thisptr->predict_proba(__pyx_t_1)); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 34, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_r = __pyx_t_2; __pyx_t_2 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":32 + /* "bayesclass/BayesNetwork.pyx":33 * def predict(self, X): * return self.thisptr.predict(X) * def predict_proba(self, X): # <<<<<<<<<<<<<< @@ -1869,7 +2023,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_8predict_proba(s return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":34 +/* "bayesclass/BayesNetwork.pyx":35 * def predict_proba(self, X): * return self.thisptr.predict_proba(X) * def score(self, X, y): # <<<<<<<<<<<<<< @@ -1911,11 +2065,11 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_11score(PyObject case 1: if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_y)) != 0)) kw_args--; else { - __Pyx_RaiseArgtupleInvalid("score", 1, 2, 2, 1); __PYX_ERR(0, 34, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("score", 1, 2, 2, 1); __PYX_ERR(0, 35, __pyx_L3_error) } } if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "score") < 0)) __PYX_ERR(0, 34, __pyx_L3_error) + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "score") < 0)) __PYX_ERR(0, 35, __pyx_L3_error) } } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { goto __pyx_L5_argtuple_error; @@ -1928,7 +2082,7 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_11score(PyObject } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("score", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 34, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("score", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 35, __pyx_L3_error) __pyx_L3_error:; __Pyx_AddTraceback("bayesclass.BayesNet.BayesNetwork.score", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); @@ -1952,7 +2106,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_10score(struct _ int __pyx_clineno = 0; __Pyx_RefNannySetupContext("score", 0); - /* "bayesclass/BayesNetwork.pyx":35 + /* "bayesclass/BayesNetwork.pyx":36 * return self.thisptr.predict_proba(X) * def score(self, X, y): * return self.thisptr.score(X, y) # <<<<<<<<<<<<<< @@ -1960,15 +2114,15 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_10score(struct _ * self.thisptr.addNode(str.encode(name), states) */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 35, __pyx_L1_error) - __pyx_t_2 = __pyx_convert_vector_from_py_int(__pyx_v_y); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 35, __pyx_L1_error) - __pyx_t_3 = PyFloat_FromDouble(__pyx_v_self->thisptr->score(__pyx_t_1, __pyx_t_2)); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 35, __pyx_L1_error) + __pyx_t_1 = __pyx_convert_vector_from_py_std_3a__3a_vector_3c_int_3e___(__pyx_v_X); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 36, __pyx_L1_error) + __pyx_t_2 = __pyx_convert_vector_from_py_int(__pyx_v_y); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 36, __pyx_L1_error) + __pyx_t_3 = PyFloat_FromDouble(__pyx_v_self->thisptr->score(__pyx_t_1, __pyx_t_2)); if (unlikely(!__pyx_t_3)) __PYX_ERR(0, 36, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_3); __pyx_r = __pyx_t_3; __pyx_t_3 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":34 + /* "bayesclass/BayesNetwork.pyx":35 * def predict_proba(self, X): * return self.thisptr.predict_proba(X) * def score(self, X, y): # <<<<<<<<<<<<<< @@ -1987,7 +2141,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_10score(struct _ return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":36 +/* "bayesclass/BayesNetwork.pyx":37 * def score(self, X, y): * return self.thisptr.score(X, y) * def addNode(self, name, states): # <<<<<<<<<<<<<< @@ -2029,11 +2183,11 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_13addNode(PyObje case 1: if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_states)) != 0)) kw_args--; else { - __Pyx_RaiseArgtupleInvalid("addNode", 1, 2, 2, 1); __PYX_ERR(0, 36, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("addNode", 1, 2, 2, 1); __PYX_ERR(0, 37, __pyx_L3_error) } } if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "addNode") < 0)) __PYX_ERR(0, 36, __pyx_L3_error) + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "addNode") < 0)) __PYX_ERR(0, 37, __pyx_L3_error) } } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { goto __pyx_L5_argtuple_error; @@ -2046,7 +2200,7 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_13addNode(PyObje } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("addNode", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 36, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("addNode", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 37, __pyx_L3_error) __pyx_L3_error:; __Pyx_AddTraceback("bayesclass.BayesNet.BayesNetwork.addNode", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); @@ -2072,14 +2226,14 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_12addNode(struct int __pyx_clineno = 0; __Pyx_RefNannySetupContext("addNode", 0); - /* "bayesclass/BayesNetwork.pyx":37 + /* "bayesclass/BayesNetwork.pyx":38 * return self.thisptr.score(X, y) * def addNode(self, name, states): * self.thisptr.addNode(str.encode(name), states) # <<<<<<<<<<<<<< * def addEdge(self, source, destination): * self.thisptr.addEdge(str.encode(source), str.encode(destination)) */ - __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 37, __pyx_L1_error) + __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 38, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_t_3 = NULL; if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_2))) { @@ -2093,15 +2247,15 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_12addNode(struct } __pyx_t_1 = (__pyx_t_3) ? __Pyx_PyObject_Call2Args(__pyx_t_2, __pyx_t_3, __pyx_v_name) : __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_v_name); __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; - if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 37, __pyx_L1_error) + if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 38, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_t_4 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 37, __pyx_L1_error) + __pyx_t_4 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 38, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_v_states); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 37, __pyx_L1_error) + __pyx_t_5 = __Pyx_PyInt_As_int(__pyx_v_states); if (unlikely((__pyx_t_5 == (int)-1) && PyErr_Occurred())) __PYX_ERR(0, 38, __pyx_L1_error) __pyx_v_self->thisptr->addNode(__pyx_t_4, __pyx_t_5); - /* "bayesclass/BayesNetwork.pyx":36 + /* "bayesclass/BayesNetwork.pyx":37 * def score(self, X, y): * return self.thisptr.score(X, y) * def addNode(self, name, states): # <<<<<<<<<<<<<< @@ -2124,7 +2278,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_12addNode(struct return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":38 +/* "bayesclass/BayesNetwork.pyx":39 * def addNode(self, name, states): * self.thisptr.addNode(str.encode(name), states) * def addEdge(self, source, destination): # <<<<<<<<<<<<<< @@ -2166,11 +2320,11 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_15addEdge(PyObje case 1: if (likely((values[1] = __Pyx_PyDict_GetItemStr(__pyx_kwds, __pyx_n_s_destination)) != 0)) kw_args--; else { - __Pyx_RaiseArgtupleInvalid("addEdge", 1, 2, 2, 1); __PYX_ERR(0, 38, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("addEdge", 1, 2, 2, 1); __PYX_ERR(0, 39, __pyx_L3_error) } } if (unlikely(kw_args > 0)) { - if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "addEdge") < 0)) __PYX_ERR(0, 38, __pyx_L3_error) + if (unlikely(__Pyx_ParseOptionalKeywords(__pyx_kwds, __pyx_pyargnames, 0, values, pos_args, "addEdge") < 0)) __PYX_ERR(0, 39, __pyx_L3_error) } } else if (PyTuple_GET_SIZE(__pyx_args) != 2) { goto __pyx_L5_argtuple_error; @@ -2183,7 +2337,7 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_15addEdge(PyObje } goto __pyx_L4_argument_unpacking_done; __pyx_L5_argtuple_error:; - __Pyx_RaiseArgtupleInvalid("addEdge", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 38, __pyx_L3_error) + __Pyx_RaiseArgtupleInvalid("addEdge", 1, 2, 2, PyTuple_GET_SIZE(__pyx_args)); __PYX_ERR(0, 39, __pyx_L3_error) __pyx_L3_error:; __Pyx_AddTraceback("bayesclass.BayesNet.BayesNetwork.addEdge", __pyx_clineno, __pyx_lineno, __pyx_filename); __Pyx_RefNannyFinishContext(); @@ -2209,14 +2363,14 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_14addEdge(struct int __pyx_clineno = 0; __Pyx_RefNannySetupContext("addEdge", 0); - /* "bayesclass/BayesNetwork.pyx":39 + /* "bayesclass/BayesNetwork.pyx":40 * self.thisptr.addNode(str.encode(name), states) * def addEdge(self, source, destination): * self.thisptr.addEdge(str.encode(source), str.encode(destination)) # <<<<<<<<<<<<<< * def getFeatures(self): - * return self.thisptr.getFeatures() + * res = self.thisptr.getFeatures() */ - __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 39, __pyx_L1_error) + __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_t_3 = NULL; if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_2))) { @@ -2230,12 +2384,12 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_14addEdge(struct } __pyx_t_1 = (__pyx_t_3) ? __Pyx_PyObject_Call2Args(__pyx_t_2, __pyx_t_3, __pyx_v_source) : __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_v_source); __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; - if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 39, __pyx_L1_error) + if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_t_4 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 39, __pyx_L1_error) + __pyx_t_4 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 39, __pyx_L1_error) + __pyx_t_2 = __Pyx_PyObject_GetAttrStr(((PyObject *)(&PyUnicode_Type)), __pyx_n_s_encode); if (unlikely(!__pyx_t_2)) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_2); __pyx_t_3 = NULL; if (CYTHON_UNPACK_METHODS && likely(PyMethod_Check(__pyx_t_2))) { @@ -2249,14 +2403,14 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_14addEdge(struct } __pyx_t_1 = (__pyx_t_3) ? __Pyx_PyObject_Call2Args(__pyx_t_2, __pyx_t_3, __pyx_v_destination) : __Pyx_PyObject_CallOneArg(__pyx_t_2, __pyx_v_destination); __Pyx_XDECREF(__pyx_t_3); __pyx_t_3 = 0; - if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 39, __pyx_L1_error) + if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_DECREF(__pyx_t_2); __pyx_t_2 = 0; - __pyx_t_5 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 39, __pyx_L1_error) + __pyx_t_5 = __pyx_convert_string_from_py_std__in_string(__pyx_t_1); if (unlikely(PyErr_Occurred())) __PYX_ERR(0, 40, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; __pyx_v_self->thisptr->addEdge(__pyx_t_4, __pyx_t_5); - /* "bayesclass/BayesNetwork.pyx":38 + /* "bayesclass/BayesNetwork.pyx":39 * def addNode(self, name, states): * self.thisptr.addNode(str.encode(name), states) * def addEdge(self, source, destination): # <<<<<<<<<<<<<< @@ -2279,12 +2433,12 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_14addEdge(struct return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":40 +/* "bayesclass/BayesNetwork.pyx":41 * def addEdge(self, source, destination): * self.thisptr.addEdge(str.encode(source), str.encode(destination)) * def getFeatures(self): # <<<<<<<<<<<<<< - * return self.thisptr.getFeatures() - * def getClassName(self): + * res = self.thisptr.getFeatures() + * return [x.decode() for x in res] */ /* Python wrapper */ @@ -2301,39 +2455,67 @@ static PyObject *__pyx_pw_10bayesclass_8BayesNet_12BayesNetwork_17getFeatures(Py } static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_16getFeatures(struct __pyx_obj_10bayesclass_8BayesNet_BayesNetwork *__pyx_v_self) { + std::vector __pyx_v_res; + std::string __pyx_8genexpr1__pyx_v_x; PyObject *__pyx_r = NULL; __Pyx_RefNannyDeclarations PyObject *__pyx_t_1 = NULL; + std::vector ::iterator __pyx_t_2; + std::string __pyx_t_3; + PyObject *__pyx_t_4 = NULL; int __pyx_lineno = 0; const char *__pyx_filename = NULL; int __pyx_clineno = 0; __Pyx_RefNannySetupContext("getFeatures", 0); - /* "bayesclass/BayesNetwork.pyx":41 + /* "bayesclass/BayesNetwork.pyx":42 * self.thisptr.addEdge(str.encode(source), str.encode(destination)) * def getFeatures(self): - * return self.thisptr.getFeatures() # <<<<<<<<<<<<<< + * res = self.thisptr.getFeatures() # <<<<<<<<<<<<<< + * return [x.decode() for x in res] * def getClassName(self): - * return self.thisptr.getClassName() + */ + __pyx_v_res = __pyx_v_self->thisptr->getFeatures(); + + /* "bayesclass/BayesNetwork.pyx":43 + * def getFeatures(self): + * res = self.thisptr.getFeatures() + * return [x.decode() for x in res] # <<<<<<<<<<<<<< + * def getClassName(self): + * return self.thisptr.getClassName().decode() */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __pyx_convert_vector_to_py_std_3a__3a_string(__pyx_v_self->thisptr->getFeatures()); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 41, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); + { /* enter inner scope */ + __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 43, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_1); + __pyx_t_2 = __pyx_v_res.begin(); + for (;;) { + if (!(__pyx_t_2 != __pyx_v_res.end())) break; + __pyx_t_3 = *__pyx_t_2; + ++__pyx_t_2; + __pyx_8genexpr1__pyx_v_x = __pyx_t_3; + __pyx_t_4 = __Pyx_decode_cpp_string(__pyx_8genexpr1__pyx_v_x, 0, PY_SSIZE_T_MAX, NULL, NULL, NULL); if (unlikely(!__pyx_t_4)) __PYX_ERR(0, 43, __pyx_L1_error) + __Pyx_GOTREF(__pyx_t_4); + if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_4))) __PYX_ERR(0, 43, __pyx_L1_error) + __Pyx_DECREF(__pyx_t_4); __pyx_t_4 = 0; + } + } /* exit inner scope */ __pyx_r = __pyx_t_1; __pyx_t_1 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":40 + /* "bayesclass/BayesNetwork.pyx":41 * def addEdge(self, source, destination): * self.thisptr.addEdge(str.encode(source), str.encode(destination)) * def getFeatures(self): # <<<<<<<<<<<<<< - * return self.thisptr.getFeatures() - * def getClassName(self): + * res = self.thisptr.getFeatures() + * return [x.decode() for x in res] */ /* function exit code */ __pyx_L1_error:; __Pyx_XDECREF(__pyx_t_1); + __Pyx_XDECREF(__pyx_t_4); __Pyx_AddTraceback("bayesclass.BayesNet.BayesNetwork.getFeatures", __pyx_clineno, __pyx_lineno, __pyx_filename); __pyx_r = NULL; __pyx_L0:; @@ -2342,11 +2524,11 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_16getFeatures(st return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":42 - * def getFeatures(self): - * return self.thisptr.getFeatures() +/* "bayesclass/BayesNetwork.pyx":44 + * res = self.thisptr.getFeatures() + * return [x.decode() for x in res] * def getClassName(self): # <<<<<<<<<<<<<< - * return self.thisptr.getClassName() + * return self.thisptr.getClassName().decode() * def getClassNumStates(self): */ @@ -2372,25 +2554,25 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_18getClassName(s int __pyx_clineno = 0; __Pyx_RefNannySetupContext("getClassName", 0); - /* "bayesclass/BayesNetwork.pyx":43 - * return self.thisptr.getFeatures() + /* "bayesclass/BayesNetwork.pyx":45 + * return [x.decode() for x in res] * def getClassName(self): - * return self.thisptr.getClassName() # <<<<<<<<<<<<<< + * return self.thisptr.getClassName().decode() # <<<<<<<<<<<<<< * def getClassNumStates(self): * return self.thisptr.getClassNumStates() */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __pyx_convert_PyBytes_string_to_py_std__in_string(__pyx_v_self->thisptr->getClassName()); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 43, __pyx_L1_error) + __pyx_t_1 = __Pyx_decode_cpp_string(__pyx_v_self->thisptr->getClassName(), 0, PY_SSIZE_T_MAX, NULL, NULL, NULL); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 45, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __pyx_r = __pyx_t_1; __pyx_t_1 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":42 - * def getFeatures(self): - * return self.thisptr.getFeatures() + /* "bayesclass/BayesNetwork.pyx":44 + * res = self.thisptr.getFeatures() + * return [x.decode() for x in res] * def getClassName(self): # <<<<<<<<<<<<<< - * return self.thisptr.getClassName() + * return self.thisptr.getClassName().decode() * def getClassNumStates(self): */ @@ -2405,9 +2587,9 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_18getClassName(s return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":44 +/* "bayesclass/BayesNetwork.pyx":46 * def getClassName(self): - * return self.thisptr.getClassName() + * return self.thisptr.getClassName().decode() * def getClassNumStates(self): # <<<<<<<<<<<<<< * return self.thisptr.getClassNumStates() * def __reduce__(self): @@ -2435,23 +2617,23 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_20getClassNumSta int __pyx_clineno = 0; __Pyx_RefNannySetupContext("getClassNumStates", 0); - /* "bayesclass/BayesNetwork.pyx":45 - * return self.thisptr.getClassName() + /* "bayesclass/BayesNetwork.pyx":47 + * return self.thisptr.getClassName().decode() * def getClassNumStates(self): * return self.thisptr.getClassNumStates() # <<<<<<<<<<<<<< * def __reduce__(self): * return (BayesNetwork, ()) */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->thisptr->getClassNumStates()); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 45, __pyx_L1_error) + __pyx_t_1 = __Pyx_PyInt_From_int(__pyx_v_self->thisptr->getClassNumStates()); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 47, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __pyx_r = __pyx_t_1; __pyx_t_1 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":44 + /* "bayesclass/BayesNetwork.pyx":46 * def getClassName(self): - * return self.thisptr.getClassName() + * return self.thisptr.getClassName().decode() * def getClassNumStates(self): # <<<<<<<<<<<<<< * return self.thisptr.getClassNumStates() * def __reduce__(self): @@ -2468,7 +2650,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_20getClassNumSta return __pyx_r; } -/* "bayesclass/BayesNetwork.pyx":46 +/* "bayesclass/BayesNetwork.pyx":48 * def getClassNumStates(self): * return self.thisptr.getClassNumStates() * def __reduce__(self): # <<<<<<<<<<<<<< @@ -2497,13 +2679,13 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_22__reduce__(CYT int __pyx_clineno = 0; __Pyx_RefNannySetupContext("__reduce__", 0); - /* "bayesclass/BayesNetwork.pyx":47 + /* "bayesclass/BayesNetwork.pyx":49 * return self.thisptr.getClassNumStates() * def __reduce__(self): * return (BayesNetwork, ()) # <<<<<<<<<<<<<< */ __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 47, __pyx_L1_error) + __pyx_t_1 = PyTuple_New(2); if (unlikely(!__pyx_t_1)) __PYX_ERR(0, 49, __pyx_L1_error) __Pyx_GOTREF(__pyx_t_1); __Pyx_INCREF(((PyObject *)__pyx_ptype_10bayesclass_8BayesNet_BayesNetwork)); __Pyx_GIVEREF(((PyObject *)__pyx_ptype_10bayesclass_8BayesNet_BayesNetwork)); @@ -2515,7 +2697,7 @@ static PyObject *__pyx_pf_10bayesclass_8BayesNet_12BayesNetwork_22__reduce__(CYT __pyx_t_1 = 0; goto __pyx_L0; - /* "bayesclass/BayesNetwork.pyx":46 + /* "bayesclass/BayesNetwork.pyx":48 * def getClassNumStates(self): * return self.thisptr.getClassNumStates() * def __reduce__(self): # <<<<<<<<<<<<<< @@ -3384,71 +3566,6 @@ static CYTHON_INLINE PyObject *__pyx_convert_PyByteArray_string_to_py_std__in_st return __pyx_r; } -/* "vector.to_py":60 - * - * @cname("__pyx_convert_vector_to_py_std_3a__3a_string") - * cdef object __pyx_convert_vector_to_py_std_3a__3a_string(vector[X]& v): # <<<<<<<<<<<<<< - * return [v[i] for i in range(v.size())] - * - */ - -static PyObject *__pyx_convert_vector_to_py_std_3a__3a_string(const std::vector &__pyx_v_v) { - size_t __pyx_v_i; - PyObject *__pyx_r = NULL; - __Pyx_RefNannyDeclarations - PyObject *__pyx_t_1 = NULL; - size_t __pyx_t_2; - size_t __pyx_t_3; - size_t __pyx_t_4; - PyObject *__pyx_t_5 = NULL; - int __pyx_lineno = 0; - const char *__pyx_filename = NULL; - int __pyx_clineno = 0; - __Pyx_RefNannySetupContext("__pyx_convert_vector_to_py_std_3a__3a_string", 0); - - /* "vector.to_py":61 - * @cname("__pyx_convert_vector_to_py_std_3a__3a_string") - * cdef object __pyx_convert_vector_to_py_std_3a__3a_string(vector[X]& v): - * return [v[i] for i in range(v.size())] # <<<<<<<<<<<<<< - * - * - */ - __Pyx_XDECREF(__pyx_r); - __pyx_t_1 = PyList_New(0); if (unlikely(!__pyx_t_1)) __PYX_ERR(1, 61, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_1); - __pyx_t_2 = __pyx_v_v.size(); - __pyx_t_3 = __pyx_t_2; - for (__pyx_t_4 = 0; __pyx_t_4 < __pyx_t_3; __pyx_t_4+=1) { - __pyx_v_i = __pyx_t_4; - __pyx_t_5 = __pyx_convert_PyBytes_string_to_py_std__in_string((__pyx_v_v[__pyx_v_i])); if (unlikely(!__pyx_t_5)) __PYX_ERR(1, 61, __pyx_L1_error) - __Pyx_GOTREF(__pyx_t_5); - if (unlikely(__Pyx_ListComp_Append(__pyx_t_1, (PyObject*)__pyx_t_5))) __PYX_ERR(1, 61, __pyx_L1_error) - __Pyx_DECREF(__pyx_t_5); __pyx_t_5 = 0; - } - __pyx_r = __pyx_t_1; - __pyx_t_1 = 0; - goto __pyx_L0; - - /* "vector.to_py":60 - * - * @cname("__pyx_convert_vector_to_py_std_3a__3a_string") - * cdef object __pyx_convert_vector_to_py_std_3a__3a_string(vector[X]& v): # <<<<<<<<<<<<<< - * return [v[i] for i in range(v.size())] - * - */ - - /* function exit code */ - __pyx_L1_error:; - __Pyx_XDECREF(__pyx_t_1); - __Pyx_XDECREF(__pyx_t_5); - __Pyx_AddTraceback("vector.to_py.__pyx_convert_vector_to_py_std_3a__3a_string", __pyx_clineno, __pyx_lineno, __pyx_filename); - __pyx_r = 0; - __pyx_L0:; - __Pyx_XGIVEREF(__pyx_r); - __Pyx_RefNannyFinishContext(); - return __pyx_r; -} - static PyObject *__pyx_tp_new_10bayesclass_8BayesNet_BayesNetwork(PyTypeObject *t, PyObject *a, PyObject *k) { PyObject *o; if (likely((t->tp_flags & Py_TPFLAGS_IS_ABSTRACT) == 0)) { @@ -3944,11 +4061,11 @@ if (!__Pyx_RefNanny) { if (PyDict_SetItem(__pyx_d, __pyx_n_s_test, __pyx_t_1) < 0) __PYX_ERR(0, 1, __pyx_L1_error) __Pyx_DECREF(__pyx_t_1); __pyx_t_1 = 0; - /* "vector.to_py":60 + /* "string.to_py":55 * - * @cname("__pyx_convert_vector_to_py_std_3a__3a_string") - * cdef object __pyx_convert_vector_to_py_std_3a__3a_string(vector[X]& v): # <<<<<<<<<<<<<< - * return [v[i] for i in range(v.size())] + * @cname("__pyx_convert_PyByteArray_string_to_py_std__in_string") + * cdef inline object __pyx_convert_PyByteArray_string_to_py_std__in_string(const string& s): # <<<<<<<<<<<<<< + * return __Pyx_PyByteArray_FromStringAndSize(s.data(), s.size()) * */ @@ -4150,29 +4267,6 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_GetAttrStr(PyObject* obj, PyObject } #endif -/* PyCFunctionFastCall */ -#if CYTHON_FAST_PYCCALL -static CYTHON_INLINE PyObject * __Pyx_PyCFunction_FastCall(PyObject *func_obj, PyObject **args, Py_ssize_t nargs) { - PyCFunctionObject *func = (PyCFunctionObject*)func_obj; - PyCFunction meth = PyCFunction_GET_FUNCTION(func); - PyObject *self = PyCFunction_GET_SELF(func); - int flags = PyCFunction_GET_FLAGS(func); - assert(PyCFunction_Check(func)); - assert(METH_FASTCALL == (flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))); - assert(nargs >= 0); - assert(nargs == 0 || args != NULL); - /* _PyCFunction_FastCallDict() must not be called with an exception set, - because it may clear it (directly or indirectly) and so the - caller loses its exception */ - assert(!PyErr_Occurred()); - if ((PY_VERSION_HEX < 0x030700A0) || unlikely(flags & METH_KEYWORDS)) { - return (*((__Pyx_PyCFunctionFastWithKeywords)(void*)meth)) (self, args, nargs, NULL); - } else { - return (*((__Pyx_PyCFunctionFast)(void*)meth)) (self, args, nargs); - } -} -#endif - /* PyFunctionFastCall */ #if CYTHON_FAST_PYCALL static PyObject* __Pyx_PyFunction_FastCallNoKw(PyCodeObject *co, PyObject **args, Py_ssize_t na, @@ -4312,35 +4406,6 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_Call(PyObject *func, PyObject *arg } #endif -/* PyObjectCall2Args */ -static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2) { - PyObject *args, *result = NULL; - #if CYTHON_FAST_PYCALL - if (PyFunction_Check(function)) { - PyObject *args[2] = {arg1, arg2}; - return __Pyx_PyFunction_FastCall(function, args, 2); - } - #endif - #if CYTHON_FAST_PYCCALL - if (__Pyx_PyFastCFunction_Check(function)) { - PyObject *args[2] = {arg1, arg2}; - return __Pyx_PyCFunction_FastCall(function, args, 2); - } - #endif - args = PyTuple_New(2); - if (unlikely(!args)) goto done; - Py_INCREF(arg1); - PyTuple_SET_ITEM(args, 0, arg1); - Py_INCREF(arg2); - PyTuple_SET_ITEM(args, 1, arg2); - Py_INCREF(function); - result = __Pyx_PyObject_Call(function, args, NULL); - Py_DECREF(args); - Py_DECREF(function); -done: - return result; -} - /* PyObjectCallMethO */ #if CYTHON_COMPILING_IN_CPYTHON static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject *arg) { @@ -4361,6 +4426,51 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_CallMethO(PyObject *func, PyObject } #endif +/* PyObjectCallNoArg */ +#if CYTHON_COMPILING_IN_CPYTHON +static CYTHON_INLINE PyObject* __Pyx_PyObject_CallNoArg(PyObject *func) { +#if CYTHON_FAST_PYCALL + if (PyFunction_Check(func)) { + return __Pyx_PyFunction_FastCall(func, NULL, 0); + } +#endif +#if defined(__Pyx_CyFunction_USED) && defined(NDEBUG) + if (likely(PyCFunction_Check(func) || __Pyx_CyFunction_Check(func))) +#else + if (likely(PyCFunction_Check(func))) +#endif + { + if (likely(PyCFunction_GET_FLAGS(func) & METH_NOARGS)) { + return __Pyx_PyObject_CallMethO(func, NULL); + } + } + return __Pyx_PyObject_Call(func, __pyx_empty_tuple, NULL); +} +#endif + +/* PyCFunctionFastCall */ +#if CYTHON_FAST_PYCCALL +static CYTHON_INLINE PyObject * __Pyx_PyCFunction_FastCall(PyObject *func_obj, PyObject **args, Py_ssize_t nargs) { + PyCFunctionObject *func = (PyCFunctionObject*)func_obj; + PyCFunction meth = PyCFunction_GET_FUNCTION(func); + PyObject *self = PyCFunction_GET_SELF(func); + int flags = PyCFunction_GET_FLAGS(func); + assert(PyCFunction_Check(func)); + assert(METH_FASTCALL == (flags & ~(METH_CLASS | METH_STATIC | METH_COEXIST | METH_KEYWORDS | METH_STACKLESS))); + assert(nargs >= 0); + assert(nargs == 0 || args != NULL); + /* _PyCFunction_FastCallDict() must not be called with an exception set, + because it may clear it (directly or indirectly) and so the + caller loses its exception */ + assert(!PyErr_Occurred()); + if ((PY_VERSION_HEX < 0x030700A0) || unlikely(flags & METH_KEYWORDS)) { + return (*((__Pyx_PyCFunctionFastWithKeywords)(void*)meth)) (self, args, nargs, NULL); + } else { + return (*((__Pyx_PyCFunctionFast)(void*)meth)) (self, args, nargs); + } +} +#endif + /* PyObjectCallOneArg */ #if CYTHON_COMPILING_IN_CPYTHON static PyObject* __Pyx__PyObject_CallOneArg(PyObject *func, PyObject *arg) { @@ -4401,6 +4511,62 @@ static CYTHON_INLINE PyObject* __Pyx_PyObject_CallOneArg(PyObject *func, PyObjec } #endif +/* PyObjectCall2Args */ +static CYTHON_UNUSED PyObject* __Pyx_PyObject_Call2Args(PyObject* function, PyObject* arg1, PyObject* arg2) { + PyObject *args, *result = NULL; + #if CYTHON_FAST_PYCALL + if (PyFunction_Check(function)) { + PyObject *args[2] = {arg1, arg2}; + return __Pyx_PyFunction_FastCall(function, args, 2); + } + #endif + #if CYTHON_FAST_PYCCALL + if (__Pyx_PyFastCFunction_Check(function)) { + PyObject *args[2] = {arg1, arg2}; + return __Pyx_PyCFunction_FastCall(function, args, 2); + } + #endif + args = PyTuple_New(2); + if (unlikely(!args)) goto done; + Py_INCREF(arg1); + PyTuple_SET_ITEM(args, 0, arg1); + Py_INCREF(arg2); + PyTuple_SET_ITEM(args, 1, arg2); + Py_INCREF(function); + result = __Pyx_PyObject_Call(function, args, NULL); + Py_DECREF(args); + Py_DECREF(function); +done: + return result; +} + +/* decode_c_bytes */ +static CYTHON_INLINE PyObject* __Pyx_decode_c_bytes( + const char* cstring, Py_ssize_t length, Py_ssize_t start, Py_ssize_t stop, + const char* encoding, const char* errors, + PyObject* (*decode_func)(const char *s, Py_ssize_t size, const char *errors)) { + if (unlikely((start < 0) | (stop < 0))) { + if (start < 0) { + start += length; + if (start < 0) + start = 0; + } + if (stop < 0) + stop += length; + } + if (stop > length) + stop = length; + if (unlikely(stop <= start)) + return __Pyx_NewRef(__pyx_empty_unicode); + length = stop - start; + cstring += start; + if (decode_func) { + return decode_func(cstring, length, errors); + } else { + return PyUnicode_Decode(cstring, length, encoding, errors); + } +} + /* GetBuiltinName */ static PyObject *__Pyx_GetBuiltinName(PyObject *name) { PyObject* result = __Pyx_PyObject_GetAttrStr(__pyx_b, name); diff --git a/bayesclass/BayesNetwork.pyx b/bayesclass/BayesNetwork.pyx index a00f386..b5fae4f 100644 --- a/bayesclass/BayesNetwork.pyx +++ b/bayesclass/BayesNetwork.pyx @@ -7,10 +7,10 @@ from libcpp.string cimport string cdef extern from "Network.h" namespace "bayesnet": cdef cppclass Network: Network(float, float) except + - void fit(vector[vector[int]], vector[int], vector[string], string) - vector[int] predict(vector[vector[int]]) - vector[vector[double]] predict_proba(vector[vector[int]]) - float score(const vector[vector[int]], const vector[int]) + void fit(vector[vector[int]]&, vector[int]&, vector[string]&, string) + vector[int] predict(vector[vector[int]]&) + vector[vector[double]] predict_proba(vector[vector[int]]&) + float score(const vector[vector[int]]&, const vector[int]&) void addNode(string, int); void addEdge(string, string); vector[string] getFeatures(); @@ -25,7 +25,8 @@ cdef class BayesNetwork: def __dealloc__(self): del self.thisptr def fit(self, X, y, features, className): - self.thisptr.fit(X, y, features, className) + features_bytes = [x.encode() for x in features] + self.thisptr.fit(X, y, features_bytes, className.encode()) return self def predict(self, X): return self.thisptr.predict(X) @@ -38,9 +39,10 @@ cdef class BayesNetwork: def addEdge(self, source, destination): self.thisptr.addEdge(str.encode(source), str.encode(destination)) def getFeatures(self): - return self.thisptr.getFeatures() + res = self.thisptr.getFeatures() + return [x.decode() for x in res] def getClassName(self): - return self.thisptr.getClassName() + return self.thisptr.getClassName().decode() def getClassNumStates(self): return self.thisptr.getClassNumStates() def __reduce__(self):