-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathspkmeansmodule.c
More file actions
211 lines (183 loc) · 7.22 KB
/
Copy pathspkmeansmodule.c
File metadata and controls
211 lines (183 loc) · 7.22 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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "spkmeans.h"
static PyObject* goalsAndSpkOne(PyObject *self, PyObject *args);
static PyObject* spkTwo(PyObject *self, PyObject *args);
static double** PyList_ToMat(PyObject *pyLst, int N, int d);
static PyObject* Mat_ToPyList(double** mat, int N, int d);
/* Activates for all goals and phase one of SPK : steps 1 to 5 in the algorithm.
* If goal != spk, does the required calculations and prints the result.
* If goal == spk, returns T of size Nxk. */
static PyObject* goalsAndSpkOne(PyObject *self, PyObject *args)
{
int i,N,d,k;
char *goal;
const char *filepath;
double **observations, **mat;
double *diag;
PyObject *toReturn=Py_None; /* toReturn only needed for SPK so default value is None */
if(!PyArg_ParseTuple(args, "iss",&k,&goal,&filepath)) /* Parses arguments from python to c*/
{
return NULL;
}
/* Reads and returns observations matrix from given file. Observations are of size Nxd. N,d updated accordingly (passed by reference). */
observations = readObservationsFile(filepath,goal,&N,&d);
/* An if block matching the goal will be executed.
* The point of an "if-else tree" is to not check the other goals if one is selected, to increase efficiency. */
if (strcmp(goal,"wam") == 0)
{
/***** goal: wam *****/
mat = weightAdjMat(observations,d,N); /** observations freed inside **/
printVectorsArray(mat,N,N); /**Printing the WAM**/
for (i=0; i<N; i++)
{
free(mat[i]);
}
free(mat);
}
else
{
/***** goal: ddg *****/
if (strcmp(goal,"ddg") == 0)
{
mat = weightAdjMat(observations, d, N); /** observations freed inside **/
diag = diagDegMat(mat, N); /**calculates the DDG**/
for (i = 0; i < N; i++)
{
free(mat[i]);
}
free(mat);
printDiagMat(diag, N); /** Printing the DDG **/
free(diag);
}
else
{
/***** goal: lnorm *****/
if (strcmp(goal, "lnorm") == 0)
{
mat = weightAdjMat(observations, d, N); /** observations freed inside **/
diag = diagDegWrapper(mat, N); /**calculates the DDG^(-0.5)**/
Laplacian(mat, diag, N); /**on-place on returnedMatrix, now returnedMatrix=Lnorm**/
free(diag);
printVectorsArray(mat, N, N); /** Printing Lnorm **/
for (i = 0; i < N; i++)
{
free(mat[i]);
}
free(mat);
}
else
{
/***** goal: jacobi *****/
if (strcmp(goal,"jacobi") == 0)
{
mat = jacobiWrapper(observations, N); /** observations freed inside **/
printVectorsArray(mat, N + 1, N); /**Printing the returned eigenvectors and eigenvalues. N+1 since first row is eigenvalues**/
for (i = 0; i < N; i++)
{
free(mat[i]);
}
free(mat);
}
else
{
/***** goal: spk *****/
if (strcmp(goal,"spk") == 0)
{
mat = spkInit(observations, d, N, &k); /* observations freed inside. Returned T, and k updated if needed(since passed by-reference)*/
toReturn = Mat_ToPyList(mat, N, k); /* mat freed inside! converting mat to a python returnable */
}
}
}
}
}
return Py_BuildValue("O",toReturn); /* If goal = spk, T will be returned. Else, Py_None */
}
/* Activates for phase two of SPK : step 6 of the algorithm, after centroids were chosen in python.
* Executes the Kmeans algorithm (With Kmeans++ initialization) and prints the centroids. */
static PyObject* spkTwo(PyObject *self, PyObject *args)
{
int i,N,k;
PyObject *datapointsPy, *centIndicesPy;
double **datapoints;
int *centIndices;
if(!PyArg_ParseTuple(args, "iiOO", &N,&k,&datapointsPy,¢IndicesPy)) /* Parses arguments from Python to C */
{
return NULL;
}
/* Turning the list of centroid indices (indices of rows in T) into a C array */
centIndices = (int*)calloc(k,sizeof(int));
if (centIndices == NULL)
{
printf("An Error Has Occured\n");
assert(centIndices != NULL);
}
for (i=0; i<k; i++)
{
centIndices[i] = (int)PyFloat_AsDouble(PyList_GetItem(centIndicesPy,i));
}
datapoints = PyList_ToMat(datapointsPy, N, k);
/* Phase 2 of the SPK algorithm. datapoints and centIndices freed inside. Prints centroids. */
spkPython(datapoints, centIndices,N,k);
return Py_BuildValue("O",Py_None); /* Returning None */
}
/* Converts a PyObject (which represents a list of lists) to an N x d matrix of doubles, which is returned. */
static double** PyList_ToMat(PyObject *pyLst, int N, int d)
{
int i,j;
PyObject *pyVector;
double** mat = (double**)calloc(N,sizeof(double*));
if (mat == NULL)
{
printf("An Error Has Occured\n");
assert(mat != NULL);
}
for (i=0; i<N; i++)
{
mat[i] = (double*)calloc(d,sizeof(double));
if (mat[i] == NULL)
{
printf("An Error Has Occured\n");
assert(mat[i] != NULL);
}
pyVector = PyList_GetItem(pyLst,i); /* temp vector, points to ith inner list */
for (j=0; j<d; j++)
{
mat[i][j] = PyFloat_AsDouble(PyList_GetItem(pyVector,j));
}
}
return mat;
}
/* Converts an N x d doubles matrix to a PyObject (represents a list of lists), which is returned. mat freed inside. */
static PyObject* Mat_ToPyList(double** mat, int N, int d)
{
int i,j;
PyObject *pyLst, *pyVector;
pyLst = PyList_New(N);
for (i=0; i<N; i++)
{
pyVector = PyList_New(d); /* building the ith row as a list of floats of len N */
for (j=0; j<d; j++)
{
PyList_SetItem(pyVector,j,PyFloat_FromDouble(mat[i][j]));
}
PyList_SetItem(pyLst,i,pyVector);
free(mat[i]);
}
free(mat);
return pyLst;
}
static PyMethodDef spkMethods[] = {
{"goalsAndSpkOne",(PyCFunction)goalsAndSpkOne,METH_VARARGS,PyDoc_STR("Activates for all goals and phase one of SPK : steps 1 to 5 in the algorithm. Reads observations from given filepath. Arguments: k, goal, filepath")},
{"spkTwo",(PyCFunction)spkTwo,METH_VARARGS,PyDoc_STR("Activates for phase two of SPK : step 6 of the algorithm, after centroids were chosen in python. Arguments: N, k, T, centroid indices")},
{NULL, NULL, 0, NULL}};
static struct PyModuleDef moduledef = { PyModuleDef_HEAD_INIT, "spkmeans", NULL, -1, spkMethods};
PyMODINIT_FUNC PyInit_spkmeans(void)
{
PyObject *m;
m = PyModule_Create(&moduledef);
if (!m) {
return NULL;
}
return m;
}