-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathCPythonHyperTime.cpp
375 lines (303 loc) · 11.4 KB
/
CPythonHyperTime.cpp
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include "CPythonHyperTime.h"
/*
//using namespace std; // nejsem si jist, myslim, ze to nepotrebuju
// tyto promenne by mely byt public
long measurements = 0;
PyObject *pModel;
const long numberOfDimensions = 2;
const long maxMeasurements = 10000000;
//double tableOfMeasurements[numberOfDimensions][maxMeasurements];
double(*tableOfMeasurements)[numberOfDimensions]{ new
double[maxMeasurements][numberOfDimensions] };
*/
// stolen from https://github.com/davisking/dlib/blob/master/tools/python/src/numpy_returns.cpp
// we need this wonky stuff because different versions of numpy's import_array macro
// contain differently typed return statements inside import_array().
#if PY_VERSION_HEX >= 0x03000000
#define DLIB_NUMPY_IMPORT_ARRAY_RETURN_TYPE void*
#define DLIB_NUMPY_IMPORT_RETURN return 0
#else
#define DLIB_NUMPY_IMPORT_ARRAY_RETURN_TYPE void
#define DLIB_NUMPY_IMPORT_RETURN return
#endif
// tyto promenne by mely byt public
long measurements = 0;
const long numberOfDimensions = 2;
const long maxMeasurements = 10000000;
//double tableOfMeasurements[numberOfDimensions][maxMeasurements];
double(*tableOfMeasurements)[numberOfDimensions]{ new double[maxMeasurements][numberOfDimensions] };
DLIB_NUMPY_IMPORT_ARRAY_RETURN_TYPE import_numpy_stuff()
{
import_array();
DLIB_NUMPY_IMPORT_RETURN;
}
CPythonHyperTime::CPythonHyperTime(int id)
{
// strcpy(id,name); //? nevim, zda to budu pouzivat //nebudes
// instead of export PYTHONPATH=`pwd` in terminal
// stolen from https://stackoverflow.com/questions/46493307/embed-python-numpy-in-c
setenv("PYTHONPATH", "../src/models/python", 1);
/* Py_SetProgramName(argv[0]); //default 'python', I will not call that */
//initialize the python interpreter
Py_Initialize();
//importing of the python script
pModuleName = PyUnicode_FromString("python_module");//name must be changed
pModule = PyImport_Import(pModuleName);
Py_DECREF(pModuleName); // free memory
//checking the existence
if (!pModule){
std::cout << "python module can not be imported" << std::endl;
// some kill command ??? :)
}
/*
int measurements = 0;
PyObject *pModel;
const int numberOfDimensions = 2;
const int maxMeasurements = 10000;
double tableOfMeasurements [maxMeasurements][numberOfDimensions];
*/
}
void CPythonHyperTime::init(int iMaxPeriod,int elements,int numActivities)
{
measurements = 0;
}
CPythonHyperTime::~CPythonHyperTime()
{
// tady pravdepodobne
Py_DECREF(pModel);
Py_XDECREF(pFunc2);
Py_Finalize(); // shuts the interpreter down
// clear the memory
free(tableOfMeasurements);
}
// adds new state observations at given times
int CPythonHyperTime::add(uint32_t time,float state)
{
tableOfMeasurements[measurements][0] = (double)time;
tableOfMeasurements[measurements][1] = (double)state;
measurements++;
return 0;
}
/*
int CPythonHyperTime::exportToArray(double* array,int maxLen)
{
return -1;
}
int CPythonHyperTime::importFromArray(double* array,int len)
{
return -1;
}
*/
/*not required in incremental version*/
void CPythonHyperTime::update(int maxOrder,unsigned int* times,float* signal,int length)
{
//initializing numpy array api
//instead of import_array();
import_numpy_stuff();
// Convert it to a NumPy array
//npy_intp dims[numberOfDimensions]{numberOfDimensions,maxMeasurements};
//npy_intp dims[numberOfDimensions]{numberOfDimensions,measurements};
npy_intp dims[2]{measurements, numberOfDimensions};
//PyObject *pArray = PyArray_SimpleNewFromData(
//numberOfDimensions, dims, NPY_FLOAT, reinterpret_cast<void*>(tableOfMeasurements));
PyObject *pArray = PyArray_SimpleNewFromData(
2, dims, NPY_DOUBLE, reinterpret_cast<void*>(tableOfMeasurements));
if (!pArray)
std::cout << "numpy array was not created" << std::endl;
// call python function
PyObject *pFunc = PyObject_GetAttrString(pModule,"python_function_update"); // name must be changed
if (!pFunc)
std::cout << "python function was not created" << std::endl; //?
if (!PyCallable_Check(pFunc))
std::cout << "python function is not callable." << std::endl;
// v kazdem pripade bych nemel delat to numpy array z celeho toho arraye, ale jen z vyuzite casti. Pak se pouzije tato cas kodu, nebot pArray nebude obsahovat nesmyslne (nenaplnene) radky
// np_ret = mymodule.array_tutorial(np_arr)
pModel = PyObject_CallFunctionObjArgs(pFunc, pArray, NULL);
if (!pModel)
std::cout << "python function did not respond" << std::endl;
//zde predpokladame, ze pModel je pythoni objekt obsahujici libovolny pythoni bordel, ktery definuje model
/*
//### tady zacina docasna cast
// np_ret = mymodule.array_tutorial(np_arr)
PyObject *pArgs0 = PyTuple_New(2);
PyTuple_SetItem(pArgs0, 0, pArray);
PyObject *pValue0 = PyInt_FromLong(measurements);
if (!pValue0)
std::cout << "unable to convert value" << std::endl;
PyTuple_SetItem(pArgs0, 1, pValue0);
// np_ret = mymodule.array_tutorial(np_arr)
PyObject *pModel = PyObject_CallObject(pFunc, pArgs0);
Py_DECREF(pArgs0);
if (!pModel)
std::cout << "python function did not respond" << std::endl;
//zde predpokladame, ze pModel je pythoni objekt obsahujici libovolny pythoni bordel, ktery definuje model
// nicmene, ja tady budu muset ukoncit tu funkci a smazat bordel z ramky
Py_DECREF(pValue0);
//### tady konci docasna cast
*/
Py_DECREF(pArray);
Py_XDECREF(pFunc); //XDECREF?
std::cout << "update prosel" << std::endl;
}
/*text representation of the fremen model*/
void CPythonHyperTime::print(bool verbose)
{
}
float CPythonHyperTime::estimate(uint32_t time)
{
double test = (double)time;
// call python function
// PyObject *pFunc2 = PyObject_GetAttrString(pModule,"python_function_estimate"); // name must be changed
pFunc2 = PyObject_GetAttrString(pModule,"python_function_estimate");
if (!pFunc2)
std::cout << "python function does not exista" << std::endl; //?
if (!PyCallable_Check(pFunc2))
std::cout << "python function is not callable." << std::endl;
if (!pModel)
std::cout << "pModel does not exists" << std::endl;
Py_INCREF(pModel);
PyObject *pArgs = PyTuple_New(2);
PyTuple_SetItem(pArgs, 0, pModel);
PyObject *pValue = PyFloat_FromDouble(test);
if (!pValue)
std::cout << "unable to convert value" << std::endl;
PyTuple_SetItem(pArgs, 1, pValue);
// np_ret = mymodule.array_tutorial(np_arr)
//PyObject *pModel = PyObject_CallFunctionObjArgs(pFunc, pArray, NULL);
PyObject *pEstimate = PyObject_CallObject(pFunc2, pArgs);
Py_DECREF(pArgs);
if (!pEstimate)
std::cout << "python function did not respond" << std::endl;
float estimateVal = PyFloat_AsDouble(pEstimate);
Py_DECREF(pValue);
Py_DECREF(pEstimate);
// Py_XDECREF(pFunc2);
return estimateVal;
}
float CPythonHyperTime::predict(uint32_t time)
{
return estimate(time);
}
int CPythonHyperTime::save(const char* name,bool lossy)
{
FILE* file = fopen(name,"w");
double array[10000];
int len = exportToArray(array,10000);
std::cout << "Saved model with " << len << std::endl;
fwrite(array,sizeof(double),len,file);
fclose(file);
return 0;
PyObject *pFunc3 = PyObject_GetAttrString(pModule,"python_function_save");
if (!pFunc3)
std::cout << "python function save does not exista" << std::endl; //?
if (!PyCallable_Check(pFunc3))
std::cout << "python function save is not callable." << std::endl;
if (!pModel)
std::cout << "pModel does not exists, unable to save" << std::endl;
Py_INCREF(pModel);
PyObject *pArgs3 = PyTuple_New(2);
PyTuple_SetItem(pArgs3, 0, pModel);
// takhle to snad jde zavolat
PyObject *pPath3 = PyString_FromString(name);//?
if (!pPath3)
std::cout << "unable to convert name to python string" << std::endl;
PyTuple_SetItem(pArgs3, 1, pPath3);
PyObject_CallObject(pFunc3, pArgs3);
Py_DECREF(pPath3);
Py_DECREF(pArgs3);
Py_XDECREF(pFunc3);
//FILE* file = fopen(name,"w");
//save(file);
//fclose(file);
return 0;
}
int CPythonHyperTime::load(const char* name)
{
FILE* file = fopen(name,"r");
double* array = new double[MAX_TEMPORAL_MODEL_SIZE];
int len = fread(array,sizeof(double),MAX_TEMPORAL_MODEL_SIZE,file);
importFromArray(array,len);
delete[] array;
fclose(file);
return 0;
PyObject *pFunc4 = PyObject_GetAttrString(pModule,"python_function_load");
if (!pFunc4)
std::cout << "python function load does not exists" << std::endl; //?
if (!PyCallable_Check(pFunc4))
std::cout << "python function load is not callable." << std::endl;
// takhle to snad jde zavolat
PyObject *pPath4 = PyString_FromString(name);//?
if (!pPath4)
std::cout << "unable to convert name to python string" << std::endl;
pModel = PyObject_CallFunctionObjArgs(pFunc4, pPath4, NULL);
if (!pModel)
std::cout << "pModel does not exists after load" << std::endl;
Py_DECREF(pPath4);
Py_XDECREF(pFunc4);
//FILE* file = fopen(name,"r");
//load(file);
//fclose(file);
return 0;
}
int CPythonHyperTime::save(FILE* file,bool lossy)
{
return 0;
}
int CPythonHyperTime::load(FILE* file)
{
return 0;
}
int CPythonHyperTime::exportToArray(double* array,int maxLen)
{
//import_numpy_stuff();
PyObject *pFunc5 = PyObject_GetAttrString(pModule,"python_function_model_to_array");
if (!pFunc5)
std::cout << "python function model to array does not exists" << std::endl; //?
if (!PyCallable_Check(pFunc5))
std::cout << "python function model to array is not callable." << std::endl;
if (!pModel)
std::cout << "pModel does not exists, there is nothing to export" << std::endl;
Py_INCREF(pModel);
PyObject *numpyArray5 = PyObject_CallFunctionObjArgs(pFunc5, pModel, NULL);
if (!numpyArray5)
std::cout << "pArray does not exists after model to array" << std::endl;
PyArrayObject *pArray5 = reinterpret_cast<PyArrayObject*>(numpyArray5);
//otestovat pArray5?
double* temp_array;
temp_array = reinterpret_cast<double*>(PyArray_DATA(pArray5));
int pos = 0;
double length_of_array = temp_array[pos];
array[pos++] = type;
for(int i = pos; i<length_of_array; i++){
//std::cout << "pozice " << i << " vkladana hodnota " << temp_array[i] << std::endl;
array[i] = temp_array[i];
//std::cout << "pozice " << i << " vlozena hodnota " << array[i] << std::endl;
}
Py_DECREF(numpyArray5);
//Py_DECREF(pArray5);
Py_XDECREF(pFunc5);
return length_of_array;
}
int CPythonHyperTime::importFromArray(double* array,int len)
{
//instead of import_array();
import_numpy_stuff();
// Convert it to a NumPy array
npy_intp dims[1]{len};
PyObject *pArray6 = PyArray_SimpleNewFromData(
1, dims, NPY_DOUBLE, reinterpret_cast<void*>(array));
if (!pArray6)
std::cout << "numpy array was not created from the array" << std::endl;
// call python function
PyObject *pFunc6 = PyObject_GetAttrString(pModule,"python_function_array_to_model");
if (!pFunc6)
std::cout << "python function array to model was not created" << std::endl; //?
if (!PyCallable_Check(pFunc6))
std::cout << "python function array to model is not callable." << std::endl;
pModel = PyObject_CallFunctionObjArgs(pFunc6, pArray6, NULL);
if (!pModel)
std::cout << "python function did not respond, not sure what is inside pModel" << std::endl;
Py_DECREF(pArray6);
Py_XDECREF(pFunc6); //XDECREF?
return 0;
}