-
Notifications
You must be signed in to change notification settings - Fork 0
/
spkmeansmodule.c
203 lines (176 loc) · 5.71 KB
/
spkmeansmodule.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
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
#define PY_SSIZE_T_CLEAN
#include "spkmeans.h"
#include <Python.h>
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
/* helper function - saves the output to Python into a list */
static void makeListFromMatrix(PyObject* lst,double** matrix,int n,int k){
int i, j;
PyObject* row;
for (i = 0; i < n; i++){
row = PyList_New(0);
if (!row){
Py_DECREF(lst);
errorOccured();
}
for (j = 0; j < k; j++){
PyList_Append(row,PyFloat_FromDouble((double)matrix[i][j]));
}
PyList_Append(lst, row);
}
}
/* helper function - saves the matix input from Python into a C matrix */
static double** makeMatrixFromPyInput(PyObject* py_matrix,int n,int vec_length)
{
int i,j;
double** new_matrix;
PyObject* element;
new_matrix = allocateMem(n, vec_length);
for (i = 0; i < n; i++){
for (j = 0; j < vec_length ; j++) {
element = PyList_GetItem(py_matrix, (vec_length*i) + j);
new_matrix[i][j] = PyFloat_AsDouble(element);
}
}
Py_DECREF(py_matrix);
return new_matrix;
}
/**
* function called from python.
* performs the spkmeans by C code,
* and returns the matrix needed for python.
*/
static PyObject* getMatrixByGoal(PyObject *self, PyObject *args){
PyObject *py_matrix;
PyObject *final_mat;
PyObject *eigens_list;
int n, k, i;
int vec_length;
char* goal;
double** original_matrix;
double** w_matrix;
double** d_matrix;
double** l_matrix;
double** t_matrix;
struct eigens* eigens_arr;
if (!PyArg_ParseTuple(args, "iiiOs",&k, &n, &vec_length, &py_matrix, &goal)){
errorOccured();
}
original_matrix = makeMatrixFromPyInput(py_matrix, n, vec_length);
final_mat = PyList_New(0);
if (!final_mat)
errorOccured();
/* choose which goal to perform */
if (strcmp(goal, "wam") == 0){
w_matrix = wamCalc(original_matrix, n, vec_length);
makeListFromMatrix(final_mat, w_matrix, n, n);
freeMatrix(w_matrix, n);
}
else if (strcmp(goal, "ddg") == 0){
w_matrix = wamCalc(original_matrix, n, vec_length);
d_matrix = ddgCalc(w_matrix, n);
makeListFromMatrix(final_mat, d_matrix, n, n);
freeMatrix(w_matrix, n);
freeMatrix(d_matrix, n);
}
else if (strcmp(goal, "lnorm") == 0){
w_matrix = wamCalc(original_matrix, n, vec_length);
d_matrix = ddgCalc(w_matrix, n);
l_matrix = lnormCalc(w_matrix, d_matrix, n);
makeListFromMatrix(final_mat, l_matrix, n, n);
freeMatrix(w_matrix, n);
freeMatrix(d_matrix, n);
freeMatrix(l_matrix, n);
}
else if (strcmp(goal, "jacobi") == 0){
double** jacobi_matrix;
eigens_arr = jacobiCalc(original_matrix, n, 0);
jacobi_matrix = jacobiMatForPrint(eigens_arr, n);
eigens_list = PyList_New(0);
if (!eigens_list){
Py_DECREF(final_mat);
errorOccured();
}
for (i=0; i<n; i++){
PyList_Append(eigens_list, PyFloat_FromDouble((double)eigens_arr[i].value));
}
PyList_Append(final_mat, eigens_list);
makeListFromMatrix(final_mat, jacobi_matrix, n, n);
for (i = 0; i < n ; i++){
free(eigens_arr[i].vector);
}
free(eigens_arr);
freeMatrix(jacobi_matrix, n);
}
else if (strcmp(goal, "spk") == 0){
w_matrix = wamCalc(original_matrix, n, vec_length);
d_matrix = ddgCalc(w_matrix, n);
l_matrix = lnormCalc(w_matrix, d_matrix, n);
eigens_arr = jacobiCalc(l_matrix, n, 1);
if (k == 0)
k = eigengapHeuristic(eigens_arr, n);
t_matrix = createMatrixT(eigens_arr, n, k);
makeListFromMatrix(final_mat, t_matrix, n, k);
for (i = 0; i < n ; i++){
free(eigens_arr[i].vector);
}
freeMatrix(w_matrix, n);
freeMatrix(d_matrix, n);
freeMatrix(l_matrix, n);
freeMatrix(t_matrix, n);
free(eigens_arr);
}
freeMatrix(original_matrix, n);
return Py_BuildValue("O", final_mat);
}
/**
* function called from python.
* performs the kmeans++ algorithm,
* returns the centroids of the clusters.
*/
static PyObject* fit(PyObject *self, PyObject *args){
PyObject *centroids_arr;
PyObject *py_centroids;
PyObject *py_vectors;
int n, k, d, max_iter;
double** elements;
double** centroids;
if (!PyArg_ParseTuple(args, "iiiiOO", &k, &n, &d, &max_iter, &py_centroids, &py_vectors)){
errorOccured();
}
elements = makeMatrixFromPyInput(py_vectors, n, d);
centroids = makeMatrixFromPyInput(py_centroids, k, d);
/* K-means operation done by spkmeans.c functions */
getFinalCentroids(centroids, elements, k, d, n, max_iter, 0);
centroids_arr = PyList_New(0);
if (!centroids_arr)
errorOccured();
makeListFromMatrix(centroids_arr, centroids, k, k);
freeMatrix(elements, n);
freeMatrix(centroids, k);
return Py_BuildValue("O", centroids_arr);
}
/* Python C-API functions */
static PyMethodDef spkmeansmoduleMethods[] = {
{"getMatrixByGoal", (PyCFunction) getMatrixByGoal, METH_VARARGS, PyDoc_STR
("C-api for python, retrieving the matrix accoring to the goal")},
{"fit", (PyCFunction) fit, METH_VARARGS, PyDoc_STR("Kmeans")},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef _moduledef = {
PyModuleDef_HEAD_INIT,
"spkmeansmodule",
NULL,
-1,
spkmeansmoduleMethods
};
PyMODINIT_FUNC PyInit_spkmeansmodule(void)
{
PyObject *module;
module = PyModule_Create(&_moduledef);
if (!module){
errorOccured();
}
return module;
}