Passing numpy working partially

This commit is contained in:
2023-11-03 22:00:46 +01:00
parent 28bbe18115
commit bec04bc3a6
8 changed files with 283 additions and 80 deletions

View File

@@ -4,8 +4,11 @@
#include <iostream>
#include <string>
#include <map>
#include <boost/python/numpy.hpp>
#include "PyHelper.hpp"
namespace pywrap {
namespace np = boost::python::numpy;
PyWrap* PyWrap::wrapper = nullptr;
std::mutex PyWrap::mutex;
@@ -26,7 +29,17 @@ namespace pywrap {
if (PyStatus_Exception(status)) {
throw std::runtime_error("Error initializing Python");
}
np::initialize();
}
PyWrap::~PyWrap()
{
for (const auto& item : moduleClassMap) {
Py_DECREF(std::get<0>(item.second));
Py_DECREF(std::get<1>(item.second));
Py_DECREF(std::get<2>(item.second));
}
Py_Finalize();
}
void PyWrap::importClass(const std::string& moduleName, const std::string& className)
@@ -68,18 +81,10 @@ namespace pywrap {
std::cout << "Limpieza terminada" << std::endl;
}
PyWrap::~PyWrap()
{
for (const auto& item : moduleClassMap) {
Py_DECREF(std::get<0>(item.second));
Py_DECREF(std::get<1>(item.second));
Py_DECREF(std::get<2>(item.second));
}
Py_Finalize();
}
void PyWrap::errorAbort(const std::string& message)
{
std::cerr << message << std::endl;
std::cout << message << std::endl;
PyErr_Print();
exit(1);
}
@@ -115,9 +120,8 @@ namespace pywrap {
std::cout << "Llamando método fit" << std::endl;
PyObject* instance = getClass(moduleName, className);
PyObject* result;
const char method[] = "fit";
if (!(result = PyObject_CallMethodObjArgs(instance, PyBytes_FromString(method), X, y, NULL)))
std::string method = "fit";
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
errorAbort("Couldn't call method fit");
Py_DECREF(result);
}
@@ -126,8 +130,8 @@ namespace pywrap {
std::cout << "Llamando método predict" << std::endl;
PyObject* instance = getClass(moduleName, className);
PyObject* result;
const char method[] = "predict";
if (!(result = PyObject_CallMethodObjArgs(instance, PyBytes_FromString(method), X, NULL)))
std::string method = "predict";
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, NULL)))
errorAbort("Couldn't call method predict");
return result; // The caller has to decref the result
}
@@ -136,8 +140,8 @@ namespace pywrap {
std::cout << "Llamando método score" << std::endl;
PyObject* instance = getClass(moduleName, className);
PyObject* result;
const char method[] = "score";
if (!(result = PyObject_CallMethodObjArgs(instance, PyBytes_FromString(method), X, y, NULL)))
std::string method = "score";
if (!(result = PyObject_CallMethodObjArgs(instance, PyUnicode_FromString(method.c_str()), X, y, NULL)))
errorAbort("Couldn't call method score");
return PyFloat_AsDouble(result);
}