Remove trace messages

This commit is contained in:
2023-11-06 23:44:33 +01:00
parent ec68e142ac
commit 472f51daf6
4 changed files with 4 additions and 26 deletions

View File

@@ -1,6 +1,5 @@
#include "PyClassifier.h"
#include "numpy/arrayobject.h"
#include <iostream>
namespace pywrap {
namespace bp = boost::python;
@@ -12,9 +11,7 @@ namespace pywrap {
}
PyClassifier::~PyClassifier()
{
std::cout << "Cleaning Classifier" << std::endl;
pyWrap->clean(module, className);
std::cout << "Classifier cleaned" << std::endl;
}
np::ndarray tensor2numpy(torch::Tensor& X)
{
@@ -52,13 +49,12 @@ namespace pywrap {
auto Xn = tensor2numpy(X);
CPyObject Xp = bp::incref(bp::object(Xn).ptr());
PyObject* incoming = pyWrap->predict(module, className, Xp);
std::cout << "Return from predict" << std::endl;
bp::handle<> handle(incoming);
bp::object object(handle);
np::ndarray prediction = np::from_object(object);
if (PyErr_Occurred()) {
PyErr_Print();
throw std::runtime_error("Error cleaning module " + module + " and class " + className);
throw std::runtime_error("Error creating object for predict in " + module + " and class " + className);
}
int* data = reinterpret_cast<int*>(prediction.get_data());
std::vector<int> v1(data, data + prediction.shape(0));

View File

@@ -14,14 +14,12 @@ namespace pywrap {
public:
CPyInstance()
{
std::cout << "PyHelper:Initializing Python interpreter" << std::endl;
Py_Initialize();
np::initialize();
}
~CPyInstance()
{
std::cout << "PyHelper:Finalizing Python interpreter" << std::endl;
Py_Finalize();
}
};

View File

@@ -1,7 +1,6 @@
#define PY_SSIZE_T_CLEAN
#include <stdexcept>
#include "PyWrap.h"
#include <iostream>
#include <string>
#include <map>
#include <boost/python/numpy.hpp>
@@ -17,32 +16,22 @@ namespace pywrap {
{
std::lock_guard<std::mutex> lock(mutex);
if (wrapper == nullptr) {
std::cout << "Creando instancia" << std::endl;
wrapper = new PyWrap();
pyInstance = new CPyInstance();
std::cout << "Instancia creada" << std::endl;
}
return wrapper;
}
void PyWrap::RemoveInstance()
{
if (wrapper != nullptr) {
std::cout << "Liberando instancia Python Stack" << std::endl;
if (pyInstance != nullptr) {
std::cout << "-Liberando Python => PyHelper" << std::endl;
delete pyInstance;
} else {
std::cout << "*No había instancia de python para liberar. => PyHelper" << std::endl;
}
pyInstance = nullptr;
if (wrapper != nullptr) {
std::cout << "-Liberando PyWrap." << std::endl;
delete wrapper;
} else {
std::cout << "*No había instancia de PyWrap para liberar." << std::endl;
}
wrapper = nullptr;
std::cout << "Instancia liberada" << std::endl;
}
}
void PyWrap::importClass(const std::string& moduleName, const std::string& className)
@@ -53,7 +42,7 @@ namespace pywrap {
}
CPyObject module = PyImport_ImportModule(moduleName.c_str());
if (PyErr_Occurred()) {
errorAbort("Could't import module " + moduleName);
errorAbort("Couldn't import module " + moduleName);
}
CPyObject classObject = PyObject_GetAttrString(module, className.c_str());
if (PyErr_Occurred()) {
@@ -68,17 +57,14 @@ namespace pywrap {
classObject.AddRef();
instance.AddRef();
moduleClassMap.insert({ { moduleName, className }, { module.getObject(), classObject.getObject(), instance.getObject() } });
std::cout << "Clase importada" << std::endl;
}
void PyWrap::clean(const std::string& moduleName, const std::string& className)
{
std::lock_guard<std::mutex> lock(mutex);
std::cout << "Start cleaning " << moduleName << "." << className << std::endl;
auto result = moduleClassMap.find({ moduleName, className });
if (result == moduleClassMap.end()) {
return;
}
std::cout << "--> Cleaning PyObject" << std::endl;
Py_DECREF(std::get<0>(result->second));
Py_DECREF(std::get<1>(result->second));
Py_DECREF(std::get<2>(result->second));
@@ -90,11 +76,10 @@ namespace pywrap {
if (moduleClassMap.empty()) {
RemoveInstance();
}
std::cout << "End Cleaning " << moduleName << "." << className << std::endl;
}
void PyWrap::errorAbort(const std::string& message)
{
std::cout << message << std::endl;
std::cerr << message << std::endl;
PyErr_Print();
RemoveInstance();
exit(1);
@@ -146,7 +131,6 @@ namespace pywrap {
PyObject* PyWrap::predict(const std::string& moduleName, const std::string& className, CPyObject& X)
{
std::cout << "Llamando método predict" << std::endl;
PyObject* instance = getClass(moduleName, className);
PyObject* result;
std::string method = "predict";

View File

@@ -44,7 +44,7 @@ int main(int argc, char* argv[])
{
cout << "* Begin." << endl;
{
auto [X, y, features, className, states] = loadDataset("wine", true);
auto [X, y, features, className, states] = loadDataset("wine", false);
cout << "X: " << X.sizes() << endl;
cout << "y: " << y.sizes() << endl;
auto clf = pywrap::STree();