-
Notifications
You must be signed in to change notification settings - Fork 2
/
gen.cpp
67 lines (49 loc) · 1.81 KB
/
gen.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
#include <Python.h>
#include <cstdio>
#define PY_ARRAY_UNIQUE_SYMBOL gen_ARRAY_API
#include "numpy/arrayobject.h"
#include "generate.h"
// Module method definitions
static PyObject* generate_features_cpp(PyObject *self, PyObject *args) {
srand(time(NULL));
char *filename, *ref, *region;
if (!PyArg_ParseTuple(args, "sss", &filename, &ref, ®ion)) return NULL;
auto result = generate_features(filename, ref, region);
PyObject* return_tuple = PyTuple_New(2);
PyObject* pos_list = PyList_New(result->positions.size());
PyObject* X_list = PyList_New(result->X.size());
for (int i = 0, size=result->positions.size(); i < size; i++) {
auto& pos_element = result->positions[i];
PyObject* inner_list = PyList_New(pos_element.size());
for (int j = 0, s = pos_element.size(); j < s; j++) {
PyObject* pos_tuple = PyTuple_New(2);
PyTuple_SetItem(pos_tuple, 0, PyLong_FromLong(pos_element[j].first));
PyTuple_SetItem(pos_tuple, 1, PyLong_FromLong(pos_element[j].second));
PyList_SetItem(inner_list, j, pos_tuple);
}
PyList_SetItem(pos_list, i, inner_list);
PyList_SetItem(X_list, i, result->X[i]);
}
PyTuple_SetItem(return_tuple, 0, pos_list);
PyTuple_SetItem(return_tuple, 1, X_list);
return return_tuple;
}
static PyMethodDef gen_methods[] = {
{
"generate_features", generate_features_cpp, METH_VARARGS,
"Generate features for polisher."
},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef gen_definition = {
PyModuleDef_HEAD_INIT,
"gen",
"Feature generation.",
-1,
gen_methods
};
PyMODINIT_FUNC PyInit_gen(void) {
Py_Initialize();
import_array();
return PyModule_Create(&gen_definition);
}