-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsymnmfmodule.c
More file actions
177 lines (148 loc) · 4.51 KB
/
symnmfmodule.c
File metadata and controls
177 lines (148 loc) · 4.51 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
#define PY_SSIZE_T_CLEAN
#include <Python.h>
#include "symnmf.h"
static PyObject* symnmf(PyObject *self, PyObject *args);
static PyObject* sym(PyObject *self, PyObject *args);
static PyObject* ddg(PyObject *self, PyObject *args);
static PyObject* norm(PyObject *self, PyObject *args);
double** convert_to_matrix(PyObject* inputList, int rows, int cols);
PyObject* convert_to_PyObject(double** matrix, int rows, int cols);
PyObject* symnmf_ops(PyObject *self, PyObject *args, int command){ /*command == 0 means sym, command == 1 means ddg, command == 2 means norm */
int num_of_coordinates;
int N;
double** datapoints_arr;
PyObject* inputList;
PyObject* result;
double** matrix;
if (!PyArg_ParseTuple(args, "O", &inputList)) {
return NULL; /* Parsing failed, return an error */
}
/* Get dimensions */
N = (int)PySequence_Size(inputList);
num_of_coordinates = (int)PySequence_Size(PySequence_GetItem(inputList, 0));
datapoints_arr = convert_to_matrix(inputList, N, num_of_coordinates);
if (!datapoints_arr){
return NULL; /* Conversion falied*/
}
switch(command){
case 0:
matrix = sym_c(datapoints_arr, num_of_coordinates, N);
break;
case 1:
matrix = ddg_c(datapoints_arr, num_of_coordinates, N);
break;
case 2:
matrix = norm_c(datapoints_arr, num_of_coordinates, N);
break;
default:
return NULL;
}
/* Convert to PyObject, free memory and return */
result = convert_to_PyObject(matrix, N, N);
free_matrix(datapoints_arr, N);
free_matrix(matrix, N);
return result;
}
static PyObject* symnmf(PyObject *self, PyObject *args){
int k, N;
PyObject *inputList1, *inputList2, *result;
double **symnmf_matrix, **H, **W;
if (!PyArg_ParseTuple(args, "OOii", &inputList1, &inputList2, &N, &k)) {
return NULL; /* Parsing failed, return an error */
}
H = convert_to_matrix(inputList1, N, k);
W = convert_to_matrix(inputList2, N, N);
symnmf_matrix = symnmf_c(H, W, N, k);
result = convert_to_PyObject(symnmf_matrix, N, k);
free_matrix(W, N);
free_matrix(symnmf_matrix, N);
return result;
}
static PyObject* sym(PyObject *self, PyObject *args){
return symnmf_ops(self, args, 0);
}
static PyObject* ddg(PyObject *self, PyObject *args){
return symnmf_ops(self, args, 1);
}
static PyObject* norm(PyObject *self, PyObject *args){
return symnmf_ops(self, args, 2);
}
static PyMethodDef matrixMethods[] = {
{"symnmf",
(PyCFunction) symnmf,
METH_VARARGS,
PyDoc_STR("Executing the SymNMF algoritm")},
{"sym",
(PyCFunction) sym,
METH_VARARGS,
PyDoc_STR("Computing the similarity matrix")},
{"ddg",
(PyCFunction) ddg,
METH_VARARGS,
PyDoc_STR("Computing the diagonal degerr matrix")},
{"norm",
(PyCFunction) norm,
METH_VARARGS,
PyDoc_STR("Computing the normalized similarity matrix")},
{NULL, NULL, 0, NULL}
};
static struct PyModuleDef symnmfmodule = {
PyModuleDef_HEAD_INIT,
"SymNMF",
NULL,
-1,
matrixMethods
};
PyMODINIT_FUNC PyInit_SymNMF(void){
PyObject *m;
m = PyModule_Create(&symnmfmodule);
if (!m){
return NULL;
}
return m;
}
double** convert_to_matrix(PyObject* inputList, int rows, int cols){
Py_ssize_t i, j;
PyObject* sublist;
double** datapoints_arr = allocate_matrix(rows, cols, 1);
if (!datapoints_arr){
return NULL;
}
for(i=0; i<rows; i++){
sublist = PySequence_GetItem(inputList, i);
for(j=0; j<cols; j++){
datapoints_arr[i][j] = PyFloat_AsDouble(PySequence_GetItem(sublist, j));
}
}
return datapoints_arr;
}
PyObject* convert_to_PyObject(double** matrix, int rows, int cols){
PyObject* result;
Py_ssize_t i, j;
PyObject* innerList;
if (matrix == NULL){
return NULL;
}
result = PyList_New(rows);
if (result == NULL){
return NULL;
}
for(i=0; i< rows; i++){
innerList = PyList_New(cols);
if (!innerList){
Py_DECREF(result);
return NULL;
}
for(j=0; j< cols; j++){
PyObject* floatObj = PyFloat_FromDouble(matrix[i][j]);
if (!floatObj){
Py_DECREF(innerList);
Py_DECREF(result);
return NULL;
}
PyList_SET_ITEM(innerList, j, floatObj);
}
PyList_SET_ITEM(result, i, innerList);
}
return result;
}