Adding tensor methods
This commit is contained in:
2
pspp.jnl
2
pspp.jnl
@@ -1 +1,3 @@
|
|||||||
GET FILE="/home/rmontanana/Code/covbench/data/covid_v9_20220630.sav".
|
GET FILE="/home/rmontanana/Code/covbench/data/covid_v9_20220630.sav".
|
||||||
|
SHOW SYSTEM.
|
||||||
|
SHOW SYSTEM.
|
||||||
|
@@ -1,4 +1,5 @@
|
|||||||
#include "PyClassifier.h"
|
#include "PyClassifier.h"
|
||||||
|
#include <torch/csrc/autograd/python_variable.h>
|
||||||
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
|
|
||||||
@@ -13,13 +14,21 @@ namespace pywrap {
|
|||||||
pyWrap->clean(module, className);
|
pyWrap->clean(module, className);
|
||||||
}
|
}
|
||||||
|
|
||||||
std::string PyClassifier::callMethod(const std::string& method)
|
std::string PyClassifier::version()
|
||||||
|
{
|
||||||
|
return pyWrap->version(module, className);
|
||||||
|
}
|
||||||
|
|
||||||
|
std::string PyClassifier::callMethodString(const std::string& method)
|
||||||
{
|
{
|
||||||
return pyWrap->callMethodString(module, className, method);
|
return pyWrap->callMethodString(module, className, method);
|
||||||
}
|
}
|
||||||
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
PyClassifier& PyClassifier::fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states)
|
||||||
{
|
{
|
||||||
|
PyObject* Xp = NULL;//THPVariable_Wrap(X);
|
||||||
|
PyObject* yp = NULL;//THPVariable_Wrap(y);
|
||||||
|
pyWrap->fit(module, className, Xp, yp);
|
||||||
|
return *this;
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace PyWrap */
|
} /* namespace PyWrap */
|
@@ -12,7 +12,10 @@ namespace pywrap {
|
|||||||
PyClassifier(const std::string& module, const std::string& className);
|
PyClassifier(const std::string& module, const std::string& className);
|
||||||
virtual ~PyClassifier();
|
virtual ~PyClassifier();
|
||||||
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states);
|
PyClassifier& fit(torch::Tensor& X, torch::Tensor& y, const std::vector<std::string>& features, const std::string& className, std::map<std::string, std::vector<int>>& states);
|
||||||
std::string callMethod(const std::string& method);
|
torch::Tensor predict(torch::Tensor& X);
|
||||||
|
double score(torch::Tensor& X, torch::Tensor& y);
|
||||||
|
std::string version();
|
||||||
|
std::string callMethodString(const std::string& method);
|
||||||
private:
|
private:
|
||||||
PyWrap* pyWrap;
|
PyWrap* pyWrap;
|
||||||
std::string module;
|
std::string module;
|
||||||
|
211
src/PyWrap.cc
211
src/PyWrap.cc
@@ -83,34 +83,19 @@ namespace pywrap {
|
|||||||
PyErr_Print();
|
PyErr_Print();
|
||||||
exit(1);
|
exit(1);
|
||||||
}
|
}
|
||||||
template<typename T>
|
PyObject* PyWrap::getClass(const std::string& moduleName, const std::string& className)
|
||||||
T PyWrap::callMethod(const std::string& moduleName, const std::string& className, const std::string& method)
|
|
||||||
{
|
{
|
||||||
std::cout << "Llamando método" << std::endl;
|
|
||||||
auto item = moduleClassMap.find({ moduleName, className });
|
auto item = moduleClassMap.find({ moduleName, className });
|
||||||
if (item == moduleClassMap.end()) {
|
if (item == moduleClassMap.end()) {
|
||||||
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
||||||
}
|
}
|
||||||
std::cout << "Clase encontrada" << std::endl;
|
std::cout << "Clase encontrada" << std::endl;
|
||||||
PyObject* instance = std::get<2>(item->second);
|
return std::get<2>(item->second);
|
||||||
PyObject* result;
|
|
||||||
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
|
||||||
errorAbort("Couldn't call method " + method);
|
|
||||||
|
|
||||||
T value = PyUnicode_AsUTF8(result);
|
|
||||||
std::cout << "Result: " << value << std::endl;
|
|
||||||
Py_DECREF(result);
|
|
||||||
return value;
|
|
||||||
}
|
}
|
||||||
std::string PyWrap::callMethodString(const std::string& moduleName, const std::string& className, const std::string& method)
|
std::string PyWrap::callMethodString(const std::string& moduleName, const std::string& className, const std::string& method)
|
||||||
{
|
{
|
||||||
std::cout << "Llamando método" << std::endl;
|
std::cout << "Llamando método " << method << std::endl;
|
||||||
auto item = moduleClassMap.find({ moduleName, className });
|
PyObject* instance = getClass(moduleName, className);
|
||||||
if (item == moduleClassMap.end()) {
|
|
||||||
errorAbort("Module " + moduleName + " and class " + className + " not found");
|
|
||||||
}
|
|
||||||
std::cout << "Clase encontrada" << std::endl;
|
|
||||||
PyObject* instance = std::get<2>(item->second);
|
|
||||||
PyObject* result;
|
PyObject* result;
|
||||||
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
if (!(result = PyObject_CallMethod(instance, method.c_str(), NULL)))
|
||||||
errorAbort("Couldn't call method " + method);
|
errorAbort("Couldn't call method " + method);
|
||||||
@@ -120,83 +105,119 @@ namespace pywrap {
|
|||||||
Py_DECREF(result);
|
Py_DECREF(result);
|
||||||
return value;
|
return value;
|
||||||
}
|
}
|
||||||
// void PyWrap::doCommand2()
|
std::string PyWrap::version(const std::string& moduleName, const std::string& className)
|
||||||
// {
|
{
|
||||||
// PyObject* list = Py_BuildValue("[s]", "Stree");
|
return callMethodString(moduleName, className, "version");
|
||||||
// // PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
|
}
|
||||||
// PyObject* module = PyImport_ImportModule("stree");
|
|
||||||
// if (PyErr_Occurred()) {
|
|
||||||
// PyErr_Print();
|
|
||||||
// cout << "Fails to obtain the module.\n";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// cout << "Antes de empezar" << endl;
|
|
||||||
// if (module != nullptr) {
|
|
||||||
// cout << "Lo consiguió!!!" << endl;
|
|
||||||
// // dict is a borrowed reference.
|
|
||||||
// auto pdict = PyModule_GetDict(module);
|
|
||||||
// if (pdict == nullptr) {
|
|
||||||
// cout << "Fails to get the dictionary.\n";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// Py_DECREF(module);
|
|
||||||
// PyObject* pKeys = PyDict_Keys(pdict);
|
|
||||||
// PyObject* pValues = PyDict_Values(pdict);
|
|
||||||
// map<string, string> my_map;
|
|
||||||
// cout << "size: " << PyDict_Size(pdict) << endl;
|
|
||||||
// char* cstr_key = new char[100];
|
|
||||||
// char* cstr_value = new char[500];
|
|
||||||
// for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
|
|
||||||
// PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
|
|
||||||
// PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
|
|
||||||
// //cout << cstr<< " "<< cstr2 <<endl;
|
|
||||||
// my_map.emplace(cstr_key, cstr_value);
|
|
||||||
// }
|
|
||||||
// for (auto x : my_map) {
|
|
||||||
// cout << x.first << " : " << x.second << endl;
|
|
||||||
// }
|
|
||||||
// // Builds the name of a callable class
|
|
||||||
// const char* class_name = "Stree";
|
|
||||||
// auto python_class = PyDict_GetItemString(pdict, class_name);
|
|
||||||
// // if (PyErr_Occurred()) {
|
|
||||||
// // PyErr_Print();
|
|
||||||
// // cout << "Fails to obtain the class.\n";
|
|
||||||
// // return;
|
|
||||||
// // }
|
|
||||||
// if (python_class == nullptr) {
|
|
||||||
// cout << "Fails to get the Python class.\n";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// Py_DECREF(pdict);
|
|
||||||
// cout << "Clase: " << python_class << endl;
|
|
||||||
// PyObject* object;
|
|
||||||
// // Creates an instance of the class
|
|
||||||
// if (PyCallable_Check(python_class)) {
|
|
||||||
// cout << "Es callable" << endl;
|
|
||||||
// object = PyObject_CallObject(python_class, NULL);
|
|
||||||
// Py_DECREF(python_class);
|
|
||||||
// } else {
|
|
||||||
// std::cout << "Cannot instantiate the Python class" << endl;
|
|
||||||
// Py_DECREF(python_class);
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// if (PyErr_Occurred()) {
|
|
||||||
// PyErr_Print();
|
|
||||||
// cout << "Fails to create the Python object.\n";
|
|
||||||
// return;
|
|
||||||
// }
|
|
||||||
// auto val = PyObject_CallMethod(object, "version", NULL);
|
|
||||||
// if (val != nullptr) {
|
|
||||||
// cout << "Valor: " << val << endl;
|
|
||||||
// } else {
|
|
||||||
// cout << "No se pudo ejecutar" << endl;
|
|
||||||
// }
|
|
||||||
|
|
||||||
// } else {
|
void PyWrap::fit(const std::string& moduleName, const std::string& className, PyObject* X, PyObject* y)
|
||||||
// cout << "No lo consiguió :(" << endl;
|
{
|
||||||
// }
|
std::cout << "Llamando método fit" << std::endl;
|
||||||
// Py_RunMain();
|
PyObject* instance = getClass(moduleName, className);
|
||||||
// }
|
PyObject* result;
|
||||||
|
const char method[] = "fit";
|
||||||
|
|
||||||
|
if (!(result = PyObject_CallMethodObjArgs(instance, PyBytes_FromString(method), X, y, NULL)))
|
||||||
|
errorAbort("Couldn't call method fit");
|
||||||
|
Py_DECREF(result);
|
||||||
|
}
|
||||||
|
PyObject* PyWrap::predict(const std::string& moduleName, const std::string& className, PyObject* X)
|
||||||
|
{
|
||||||
|
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)))
|
||||||
|
errorAbort("Couldn't call method predict");
|
||||||
|
return result; // The caller has to decref the result
|
||||||
|
}
|
||||||
|
double PyWrap::score(const std::string& moduleName, const std::string& className, PyObject* X, PyObject* y)
|
||||||
|
{
|
||||||
|
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)))
|
||||||
|
errorAbort("Couldn't call method score");
|
||||||
|
return PyFloat_AsDouble(result);
|
||||||
|
}
|
||||||
|
// void PyWrap::doCommand2()
|
||||||
|
// {
|
||||||
|
// PyObject* list = Py_BuildValue("[s]", "Stree");
|
||||||
|
// // PyObject* module = PyImport_ImportModuleEx("stree", NULL, NULL, list);
|
||||||
|
// PyObject* module = PyImport_ImportModule("stree");
|
||||||
|
// if (PyErr_Occurred()) {
|
||||||
|
// PyErr_Print();
|
||||||
|
// cout << "Fails to obtain the module.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// cout << "Antes de empezar" << endl;
|
||||||
|
// if (module != nullptr) {
|
||||||
|
// cout << "Lo consiguió!!!" << endl;
|
||||||
|
// // dict is a borrowed reference.
|
||||||
|
// auto pdict = PyModule_GetDict(module);
|
||||||
|
// if (pdict == nullptr) {
|
||||||
|
// cout << "Fails to get the dictionary.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// Py_DECREF(module);
|
||||||
|
// PyObject* pKeys = PyDict_Keys(pdict);
|
||||||
|
// PyObject* pValues = PyDict_Values(pdict);
|
||||||
|
// map<string, string> my_map;
|
||||||
|
// cout << "size: " << PyDict_Size(pdict) << endl;
|
||||||
|
// char* cstr_key = new char[100];
|
||||||
|
// char* cstr_value = new char[500];
|
||||||
|
// for (Py_ssize_t i = 0; i < PyDict_Size(pdict); ++i) {
|
||||||
|
// PyArg_Parse(PyList_GetItem(pKeys, i), "s", &cstr_key);
|
||||||
|
// PyArg_Parse(PyList_GetItem(pValues, i), "s", &cstr_value);
|
||||||
|
// //cout << cstr<< " "<< cstr2 <<endl;
|
||||||
|
// my_map.emplace(cstr_key, cstr_value);
|
||||||
|
// }
|
||||||
|
// for (auto x : my_map) {
|
||||||
|
// cout << x.first << " : " << x.second << endl;
|
||||||
|
// }
|
||||||
|
// // Builds the name of a callable class
|
||||||
|
// const char* class_name = "Stree";
|
||||||
|
// auto python_class = PyDict_GetItemString(pdict, class_name);
|
||||||
|
// // if (PyErr_Occurred()) {
|
||||||
|
// // PyErr_Print();
|
||||||
|
// // cout << "Fails to obtain the class.\n";
|
||||||
|
// // return;
|
||||||
|
// // }
|
||||||
|
// if (python_class == nullptr) {
|
||||||
|
// cout << "Fails to get the Python class.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// Py_DECREF(pdict);
|
||||||
|
// cout << "Clase: " << python_class << endl;
|
||||||
|
// PyObject* object;
|
||||||
|
// // Creates an instance of the class
|
||||||
|
// if (PyCallable_Check(python_class)) {
|
||||||
|
// cout << "Es callable" << endl;
|
||||||
|
// object = PyObject_CallObject(python_class, NULL);
|
||||||
|
// Py_DECREF(python_class);
|
||||||
|
// } else {
|
||||||
|
// std::cout << "Cannot instantiate the Python class" << endl;
|
||||||
|
// Py_DECREF(python_class);
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// if (PyErr_Occurred()) {
|
||||||
|
// PyErr_Print();
|
||||||
|
// cout << "Fails to create the Python object.\n";
|
||||||
|
// return;
|
||||||
|
// }
|
||||||
|
// auto val = PyObject_CallMethod(object, "version", NULL);
|
||||||
|
// if (val != nullptr) {
|
||||||
|
// cout << "Valor: " << val << endl;
|
||||||
|
// } else {
|
||||||
|
// cout << "No se pudo ejecutar" << endl;
|
||||||
|
// }
|
||||||
|
|
||||||
|
// } else {
|
||||||
|
// cout << "No lo consiguió :(" << endl;
|
||||||
|
// }
|
||||||
|
// Py_RunMain();
|
||||||
|
// }
|
||||||
|
|
||||||
PyStatus PyWrap::initPython()
|
PyStatus PyWrap::initPython()
|
||||||
{
|
{
|
||||||
|
12
src/PyWrap.h
12
src/PyWrap.h
@@ -16,9 +16,6 @@ namespace pywrap {
|
|||||||
static PyWrap* GetInstance();
|
static PyWrap* GetInstance();
|
||||||
void operator=(const PyWrap&) = delete;
|
void operator=(const PyWrap&) = delete;
|
||||||
~PyWrap();
|
~PyWrap();
|
||||||
template<typename T>
|
|
||||||
T callMethod(const std::string& moduleName, const std::string& className, const std::string& method);
|
|
||||||
std::string callMethodString(const std::string& moduleName, const std::string& className, const std::string& method);
|
|
||||||
// template<typename T> T returnMethod(PyObject* result);
|
// template<typename T> T returnMethod(PyObject* result);
|
||||||
// template<std::string> std::string returnMethod(PyObject* result);
|
// template<std::string> std::string returnMethod(PyObject* result);
|
||||||
// template<int> int returnMethod(PyObject* result);
|
// template<int> int returnMethod(PyObject* result);
|
||||||
@@ -29,11 +26,18 @@ namespace pywrap {
|
|||||||
// // at::Tensor& THPVariable_Unpack(PyObject * obj);
|
// // at::Tensor& THPVariable_Unpack(PyObject * obj);
|
||||||
// return THPVariable_Unpack(result);
|
// return THPVariable_Unpack(result);
|
||||||
// };
|
// };
|
||||||
void importClass(const std::string& moduleName, const std::string& className);
|
// PyObject* callMethodArgs(const std::string& moduleName, const std::string& className, const std::string& method, PyObject* args);
|
||||||
|
void fit(const std::string& moduleName, const std::string& className, PyObject* X, PyObject* y);
|
||||||
|
PyObject* predict(const std::string& moduleName, const std::string& className, PyObject* X);
|
||||||
|
std::string callMethodString(const std::string& moduleName, const std::string& className, const std::string& method);
|
||||||
|
std::string version(const std::string& moduleName, const std::string& className);
|
||||||
|
double score(const std::string& moduleName, const std::string& className, PyObject* X, PyObject* y);
|
||||||
void clean(const std::string& moduleName, const std::string& className);
|
void clean(const std::string& moduleName, const std::string& className);
|
||||||
|
void importClass(const std::string& moduleName, const std::string& className);
|
||||||
// void doCommand2();
|
// void doCommand2();
|
||||||
private:
|
private:
|
||||||
PyWrap();
|
PyWrap();
|
||||||
|
PyObject* getClass(const std::string& moduleName, const std::string& className);
|
||||||
void errorAbort(const std::string& message);
|
void errorAbort(const std::string& message);
|
||||||
PyStatus initPython();
|
PyStatus initPython();
|
||||||
static PyWrap* wrapper;
|
static PyWrap* wrapper;
|
||||||
|
@@ -1,11 +1,6 @@
|
|||||||
#include "STree.h"
|
#include "STree.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
|
|
||||||
void STree::version()
|
|
||||||
{
|
|
||||||
std::cout << "Version: " << callMethod("version") << std::endl;
|
|
||||||
}
|
|
||||||
|
|
||||||
} /* namespace pywrap */
|
} /* namespace pywrap */
|
@@ -7,7 +7,6 @@ namespace pywrap {
|
|||||||
public:
|
public:
|
||||||
STree() : PyClassifier("stree", "Stree") {};
|
STree() : PyClassifier("stree", "Stree") {};
|
||||||
~STree() = default;
|
~STree() = default;
|
||||||
void version();
|
|
||||||
private:
|
private:
|
||||||
|
|
||||||
};
|
};
|
||||||
|
@@ -1,11 +1,10 @@
|
|||||||
#include "SVC.h"
|
#include "SVC.h"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
namespace pywrap {
|
namespace pywrap {
|
||||||
|
|
||||||
void SVC::version()
|
std::string SVC::version()
|
||||||
{
|
{
|
||||||
std::cout << "repr_html: " << callMethod("_repr_html_") << std::endl;
|
return callMethodString("_repr_html_");
|
||||||
}
|
}
|
||||||
|
|
||||||
} /* namespace pywrap */
|
} /* namespace pywrap */
|
Reference in New Issue
Block a user