forked from andreasjansson/python-emd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
pyemd.c
164 lines (137 loc) · 4.33 KB
/
pyemd.c
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
#include <Python.h>
#include "emd.h"
// define PyInt_* macros for Python 3.x
#ifndef PyInt_Check
#define PyInt_Check PyLong_Check
#define PyInt_FromLong PyLong_FromLong
#define PyInt_AsLong PyLong_AsLong
#define PyInt_Type PyLong_Type
#endif
float double_dist(feature_t *f1, feature_t *f2)
{
double d = *f1 - *f2;
return sqrt(d * d);
}
static PyObject *compute_emd(PyObject *self, PyObject *args, PyObject *keywds)
{
static char *kwlist[] = {"feature1", "feature2", "weight1", "weight2",
NULL};
PyObject *feature1, *feature2, *weight1, *weight2;
double *f1, *f2;
float *w1, *w2;
int length1, length2;
signature_t signature1, signature2;
float distance;
PyObject *item;
int i;
if(!PyArg_ParseTupleAndKeywords(args, keywds, "OOOO", kwlist,
&feature1, &feature2, &weight1, &weight2))
return NULL;
if(!PySequence_Check(feature1)) {
PyErr_SetString(PyExc_TypeError, "feature1 must be a sequence");
return NULL;
}
if(!PySequence_Check(feature2)) {
PyErr_SetString(PyExc_TypeError, "feature2 must be a sequence");
return NULL;
}
if(!PySequence_Check(weight1)) {
PyErr_SetString(PyExc_TypeError, "weight1 must be a sequence");
return NULL;
}
if(!PySequence_Check(weight2)) {
PyErr_SetString(PyExc_TypeError, "weight2 must be a sequence");
return NULL;
}
length1 = PySequence_Size(feature1);
length2 = PySequence_Size(feature2);
if(PySequence_Size(weight1) != length1) {
PyErr_SetString(PyExc_TypeError, "feature1 and weight1 must be the same");
return NULL;
}
if(PySequence_Size(weight2) != length2) {
PyErr_SetString(PyExc_TypeError, "feature2 and weight2 must be the same");
return NULL;
}
f1 = alloca(length1 * sizeof(double));
f2 = alloca(length2 * sizeof(double));
w1 = alloca(length1 * sizeof(double));
w2 = alloca(length2 * sizeof(double));
for(i = 0; i < length1; i ++) {
item = PySequence_GetItem(feature1, i);
if(!PyFloat_Check(item) && !PyInt_Check(item)) {
Py_DECREF(item);
PyErr_SetString(PyExc_TypeError, "f1 should be a sequence of numbers");
return NULL;
}
f1[i] = PyFloat_AsDouble(item);
Py_DECREF(item);
item = PySequence_GetItem(weight1, i);
if(!PyFloat_Check(item) && !PyInt_Check(item)) {
Py_DECREF(item);
PyErr_SetString(PyExc_TypeError, "w1 should be a sequence of numbers");
return NULL;
}
w1[i] = PyFloat_AsDouble(item);
Py_DECREF(item);
}
for(i = 0; i < length2; i ++) {
item = PySequence_GetItem(feature2, i);
if(!PyFloat_Check(item) && !PyInt_Check(item)) {
Py_DECREF(item);
PyErr_SetString(PyExc_TypeError, "f2 should be a sequence of numbers");
return NULL;
}
f2[i] = PyFloat_AsDouble(item);
Py_DECREF(item);
item = PySequence_GetItem(weight2, i);
if(!PyFloat_Check(item) && !PyInt_Check(item)) {
Py_DECREF(item);
PyErr_SetString(PyExc_TypeError, "w2 should be a sequence of numbers");
return NULL;
}
w2[i] = PyFloat_AsDouble(item);
Py_DECREF(item);
}
signature1.n = length1;
signature1.Features = f1;
signature1.Weights = w1;
signature2.n = length2;
signature2.Features = f2;
signature2.Weights = w2;
distance = emd(&signature1, &signature2, double_dist, 0, 0);
return Py_BuildValue("d", distance);
}
static PyMethodDef functions[] = {
{"emd", (PyCFunction)compute_emd, METH_VARARGS | METH_KEYWORDS,
"Compute the Earth Mover's Distance."},
{NULL, NULL, 0, NULL}
};
#if PY_MAJOR_VERSION >= 3
//-----------------------------------------------------------------------------
// Declaration of module definition for Python 3.x.
//-----------------------------------------------------------------------------
static struct PyModuleDef g_ModuleDef = {
PyModuleDef_HEAD_INIT,
"emd",
NULL,
-1,
functions, // methods
NULL, // m_reload
NULL, // traverse
NULL, // clear
NULL // free
};
#endif
#if PY_MAJOR_VERSION >= 3
PyObject * PyInit_emd(void)
{
return PyModule_Create(&g_ModuleDef);
#else
PyMODINIT_FUNC initemd(void)
{
Py_InitModule(
"emd", functions
);
#endif
}