-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathPythonInterface.h
More file actions
239 lines (179 loc) · 6.52 KB
/
PythonInterface.h
File metadata and controls
239 lines (179 loc) · 6.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
#ifndef PythonInterface_h
#define PythonInterface_h
#include "Python.h"
#include <numpy/arrayobject.h>
#define NPY_NO_DEPRECATED_API NPY_1_7_API_VERSION
class PythonInterface
{
public:
PyObject *pName, *pModule, *pDict, *pFunc, *pArgs, *python_class, *object, *result, *initFunc;
PythonInterface(const std::string& moduleName)
{
setenv("PYTHONPATH",".",1);
Py_Initialize ();
pName = PyUnicode_FromString ((char*)moduleName.data());
pModule = PyImport_Import(pName);
//pDict = PyModule_GetDict(pModule);
import_array ();
if (pModule == nullptr)
{
PyErr_Print();
std::cerr << "Fails to import the module.\n";
return;
}
// dict is a borrowed reference.
pDict = PyModule_GetDict(pModule);
if (pDict == nullptr)
{
PyErr_Print();
std::cerr << "Fails to get the dictionary.\n";
return;
}
// Builds the name of a callable class
python_class = PyDict_GetItemString(pDict, (char*)moduleName.data());
if (python_class == nullptr)
{
PyErr_Print();
std::cerr << "Fails to get the Python class.\n";
return;
}
// Creates an instance of the class
if (PyCallable_Check(python_class))
{
object = PyObject_CallObject(python_class, nullptr);
Py_DECREF(python_class);
}
else
{
PyErr_Print(); // TODO: throw an error
std::cout << "Cannot instantiate the Python class" << std::endl;
return;
}
PyRun_SimpleString("import sys");
PyRun_SimpleString("print(sys.path)");
PyRun_SimpleString("sys.path = ['/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6', '/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/lib-dynload', '/Users/mhuwiler/Library/Python/3.6/lib/python/site-packages', '/opt/local/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/site-packages']");
PyRun_SimpleString("print(sys.path)");
PyErr_Print();
}
~PythonInterface()
{
Py_DECREF(pName);
Py_DECREF (pModule);
Py_DECREF (pDict);
Py_DECREF(python_class);
Py_Finalize ();
}
std::vector<double> EvaluateArray(const vector<double>& data)
{
double *ptr = const_cast<double*>(data.data());
npy_intp dims[1] = { static_cast<npy_intp>(data.size()) };
PyObject *py_array;
py_array = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, ptr);
pArgs = PyTuple_New (1);
PyTuple_SetItem (pArgs, 0, py_array);
pFunc = PyObject_GetAttrString (object, (char*)"pyArray");
if (PyCallable_Check (pFunc))
{
result = PyObject_CallObject(pFunc, pArgs);
}
else
{
cout << "Function is not callable !" << endl;
}
double *response = static_cast<double*>(PyArray_DATA((PyArrayObject*)result));
std::vector<double> returnvec;
returnvec.reserve(data.size());
/*for (int i=0; i<data.size(); i++)
{
std::cout << *(response + i) << ", ";
returnvec.push_back(*(response + i));
//response++;
}
std::cout << std::endl; */
//PyObject* myResult = PyObject_CallMethod(object, "Add2toNumber", "(d)", a);
Py_DECREF (py_array);
Py_DECREF (pFunc);
return returnvec;
}
std::vector<float> Evaluate(const vector<float>& data)
{
std::vector<double> vec = castVector(data);
double *ptr = vec.data(); //const_cast<float*>(data.data());
npy_intp dims[1] = { static_cast<npy_intp>(vec.size()) };
PyObject *py_array;
//std::cout << "Inside Evaluate function " << std::endl;
py_array = PyArray_SimpleNewFromData(1, dims, NPY_DOUBLE, ptr);
pArgs = PyTuple_New (1);
PyTuple_SetItem (pArgs, 0, py_array);
PyErr_Print();
//std::cout << "Before function export " << std::endl;
pFunc = PyObject_GetAttrString (object, (char*)"Eval");
PyErr_Print();
//std::cout << "After function export " << std::endl;
if (PyCallable_Check (pFunc))
{
result = PyObject_CallObject(pFunc, pArgs);
}
else
{
cout << "Function is not callable !" << endl;
}
double *response = static_cast<double*>(PyArray_DATA((PyArrayObject*)result));
std::vector<float> returnvec;
returnvec.reserve(20);
std::vector<float> probSig;
probSig.reserve(20);
std::vector<float> probBkg;
probBkg.reserve(20);
for (int i=0; i<3*20; i++)
{
//std::cout << *(response + i) << ", ";
float value = static_cast<float>(*(response + i));
returnvec.push_back(value);
if ((i%3) == 1)
{
probSig.push_back(value);
}
if ((i%3) == 2)
{
probBkg.push_back(value);
}
//response++;
}
//std::cout << std::endl;
//PyObject* myResult = PyObject_CallMethod(object, "Add2toNumber", "(d)", a);
Py_DECREF (py_array);
Py_DECREF (pFunc);
return probSig;
}
void Initialise(std::string modelname,int givenbatchsize, int givennumpoints)
{
initFunc = PyObject_GetAttrString (object, (char*)"Initialise");
PyObject *model = PyUnicode_FromStringAndSize(modelname.data(), modelname.size());
PyObject *batchsize = PyLong_FromLong(static_cast<long>(givenbatchsize));
PyObject *numpoints = PyLong_FromLong(static_cast<long>(givennumpoints));
PyObject *localArgs = PyTuple_New (3);
PyTuple_SetItem (localArgs, 0, model);
PyTuple_SetItem (localArgs, 1, batchsize);
PyTuple_SetItem (localArgs, 2, numpoints);
if (PyCallable_Check (initFunc))
{
result = PyObject_CallObject(initFunc, localArgs);
}
else
{
cout << "Function is not callable !" << endl;
}
}
std::vector<double> castVector(std::vector<float> vec)
{
std::vector<double> result;
result.reserve(vec.size());
for (auto element : vec)
{
result.push_back(static_cast<double>(element));
}
return result;
}
};
#endif