GenSVM
test_gensvm_io.c
Go to the documentation of this file.
1 
27 #include "minunit.h"
28 #include "gensvm_io.h"
29 
31 {
32  char *filename = "./data/test_file_read_data.txt";
33  struct GenData *data = gensvm_init_data();
34 
35  // start test code //
36  gensvm_read_data(data, filename);
37 
38  // check if dimensions are correctly read
39  mu_assert(data->n == 5, "Incorrect value for n");
40  mu_assert(data->m == 3, "Incorrect value for m");
41  mu_assert(data->r == 3, "Incorrect value for r");
42  mu_assert(data->K == 4, "Incorrect value for K");
43 
44  // check if all data is read correctly.
45  mu_assert(matrix_get(data->Z, data->m+1, 0, 1) == 0.7065937536993949,
46  "Incorrect Z value at 0, 1");
47  mu_assert(matrix_get(data->Z, data->m+1, 0, 2) == 0.7016517970438980,
48  "Incorrect Z value at 0, 2");
49  mu_assert(matrix_get(data->Z, data->m+1, 0, 3) == 0.1548611397288129,
50  "Incorrect Z value at 0, 3");
51  mu_assert(matrix_get(data->Z, data->m+1, 1, 1) == 0.4604987687863951,
52  "Incorrect Z value at 1, 1");
53  mu_assert(matrix_get(data->Z, data->m+1, 1, 2) == 0.6374142980176117,
54  "Incorrect Z value at 1, 2");
55  mu_assert(matrix_get(data->Z, data->m+1, 1, 3) == 0.0370930278245423,
56  "Incorrect Z value at 1, 3");
57  mu_assert(matrix_get(data->Z, data->m+1, 2, 1) == 0.3798777132278375,
58  "Incorrect Z value at 2, 1");
59  mu_assert(matrix_get(data->Z, data->m+1, 2, 2) == 0.5745070018747664,
60  "Incorrect Z value at 2, 2");
61  mu_assert(matrix_get(data->Z, data->m+1, 2, 3) == 0.2570906697837264,
62  "Incorrect Z value at 2, 3");
63  mu_assert(matrix_get(data->Z, data->m+1, 3, 1) == 0.2789376050039792,
64  "Incorrect Z value at 3, 1");
65  mu_assert(matrix_get(data->Z, data->m+1, 3, 2) == 0.4853242744610165,
66  "Incorrect Z value at 3, 2");
67  mu_assert(matrix_get(data->Z, data->m+1, 3, 3) == 0.1894010436762711,
68  "Incorrect Z value at 3, 3");
69  mu_assert(matrix_get(data->Z, data->m+1, 4, 1) == 0.7630904372339489,
70  "Incorrect Z value at 4, 1");
71  mu_assert(matrix_get(data->Z, data->m+1, 4, 2) == 0.1341546320318005,
72  "Incorrect Z value at 4, 2");
73  mu_assert(matrix_get(data->Z, data->m+1, 4, 3) == 0.6827430912944857,
74  "Incorrect Z value at 4, 3");
75  // check if RAW = Z
76  mu_assert(data->Z == data->RAW, "Z pointer doesn't equal RAW pointer");
77 
78  // check if labels read correctly
79  mu_assert(data->y[0] == 2, "Incorrect label read at 0");
80  mu_assert(data->y[1] == 1, "Incorrect label read at 1");
81  mu_assert(data->y[2] == 3, "Incorrect label read at 2");
82  mu_assert(data->y[3] == 4, "Incorrect label read at 3");
83  mu_assert(data->y[4] == 3, "Incorrect label read at 4");
84 
85  // check if the column of ones is added
86  mu_assert(matrix_get(data->Z, data->m+1, 0, 0) == 1,
87  "Incorrect Z value at 0, 0");
88  mu_assert(matrix_get(data->Z, data->m+1, 1, 0) == 1,
89  "Incorrect Z value at 1, 0");
90  mu_assert(matrix_get(data->Z, data->m+1, 2, 0) == 1,
91  "Incorrect Z value at 2, 0");
92  mu_assert(matrix_get(data->Z, data->m+1, 3, 0) == 1,
93  "Incorrect Z value at 3, 0");
94  mu_assert(matrix_get(data->Z, data->m+1, 4, 0) == 1,
95  "Incorrect Z value at 4, 0");
96 
97  // end test code //
98 
99  gensvm_free_data(data);
100  return NULL;
101 }
102 
104 {
105  char *filename = "./data/test_file_read_data_sparse.txt";
106  struct GenData *data = gensvm_init_data();
107 
108  // start test code //
109  gensvm_read_data(data, filename);
110 
111  // check if dimensions are correctly read
112  mu_assert(data->n == 10, "Incorrect value for n");
113  mu_assert(data->m == 3, "Incorrect value for m");
114  mu_assert(data->r == 3, "Incorrect value for r");
115  mu_assert(data->K == 4, "Incorrect value for K");
116 
117  // check if dense data pointers are NULL
118  mu_assert(data->Z == NULL, "Z pointer isn't NULL");
119  mu_assert(data->RAW == NULL, "RAW pointer isn't NULL");
120 
121  // check sparse data structure
122  mu_assert(data->spZ != NULL, "spZ is NULL");
123  mu_assert(data->spZ->nnz == 14, "Incorrect nnz");
124  mu_assert(data->spZ->n_row == 10, "Incorrect n_row");
125  mu_assert(data->spZ->n_col == 4, "Incorrect n_col");
126 
127  // check sparse values
128  mu_assert(data->spZ->values[0] == 1.0,
129  "Incorrect nonzero value at 0");
130  mu_assert(data->spZ->values[1] == 0.7016517970438980,
131  "Incorrect nonzero value at 1");
132  mu_assert(data->spZ->values[2] == 1.0,
133  "Incorrect nonzero value at 2");
134  mu_assert(data->spZ->values[3] == 0.0370930278245423,
135  "Incorrect nonzero value at 3");
136  mu_assert(data->spZ->values[4] == 1.0,
137  "Incorrect nonzero value at 4");
138  mu_assert(data->spZ->values[5] == 1.0,
139  "Incorrect nonzero value at 5");
140  mu_assert(data->spZ->values[6] == 0.4853242744610165,
141  "Incorrect nonzero value at 6");
142  mu_assert(data->spZ->values[7] == 1.0,
143  "Incorrect nonzero value at 7");
144  mu_assert(data->spZ->values[8] == 0.7630904372339489,
145  "Incorrect nonzero value at 8");
146  mu_assert(data->spZ->values[9] == 1.0,
147  "Incorrect nonzero value at 9");
148  mu_assert(data->spZ->values[10] == 1.0,
149  "Incorrect nonzero value at 10");
150  mu_assert(data->spZ->values[11] == 1.0,
151  "Incorrect nonzero value at 11");
152  mu_assert(data->spZ->values[12] == 1.0,
153  "Incorrect nonzero value at 12");
154  mu_assert(data->spZ->values[13] == 1.0,
155  "Incorrect nonzero value at 13");
156 
157  // check sparse row lengths
158  mu_assert(data->spZ->ia[0] == 0, "Incorrect ia value at 0");
159  mu_assert(data->spZ->ia[1] == 2, "Incorrect ia value at 1");
160  mu_assert(data->spZ->ia[2] == 4, "Incorrect ia value at 2");
161  mu_assert(data->spZ->ia[3] == 5, "Incorrect ia value at 3");
162  mu_assert(data->spZ->ia[4] == 7, "Incorrect ia value at 4");
163  mu_assert(data->spZ->ia[5] == 9, "Incorrect ia value at 5");
164  mu_assert(data->spZ->ia[6] == 10, "Incorrect ia value at 5");
165  mu_assert(data->spZ->ia[7] == 11, "Incorrect ia value at 5");
166  mu_assert(data->spZ->ia[8] == 12, "Incorrect ia value at 5");
167  mu_assert(data->spZ->ia[9] == 13, "Incorrect ia value at 5");
168  mu_assert(data->spZ->ia[10] == 14, "Incorrect ia value at 5");
169 
170  // check sparse column indices
171  mu_assert(data->spZ->ja[0] == 0, "Incorrect ja value at 0");
172  mu_assert(data->spZ->ja[1] == 2, "Incorrect ja value at 1");
173  mu_assert(data->spZ->ja[2] == 0, "Incorrect ja value at 2");
174  mu_assert(data->spZ->ja[3] == 3, "Incorrect ja value at 3");
175  mu_assert(data->spZ->ja[4] == 0, "Incorrect ja value at 4");
176  mu_assert(data->spZ->ja[5] == 0, "Incorrect ja value at 5");
177  mu_assert(data->spZ->ja[6] == 2, "Incorrect ja value at 6");
178  mu_assert(data->spZ->ja[7] == 0, "Incorrect ja value at 7");
179  mu_assert(data->spZ->ja[8] == 1, "Incorrect ja value at 8");
180  mu_assert(data->spZ->ja[9] == 0, "Incorrect ja value at 7");
181  mu_assert(data->spZ->ja[10] == 0, "Incorrect ja value at 7");
182  mu_assert(data->spZ->ja[11] == 0, "Incorrect ja value at 7");
183  mu_assert(data->spZ->ja[12] == 0, "Incorrect ja value at 7");
184  mu_assert(data->spZ->ja[13] == 0, "Incorrect ja value at 7");
185 
186  // check if labels read correctly
187  mu_assert(data->y[0] == 2, "Incorrect label read at 0");
188  mu_assert(data->y[1] == 1, "Incorrect label read at 1");
189  mu_assert(data->y[2] == 3, "Incorrect label read at 2");
190  mu_assert(data->y[3] == 4, "Incorrect label read at 3");
191  mu_assert(data->y[4] == 3, "Incorrect label read at 4");
192 
193  // end test code //
194 
195  gensvm_free_data(data);
196  return NULL;
197 }
198 
200 {
201  char *filename = "./data/test_file_read_data_no_label.txt";
202  struct GenData *data = gensvm_init_data();
203 
204  // start test code //
205  gensvm_read_data(data, filename);
206 
207  // check if dimensions are correctly read
208  mu_assert(data->n == 5, "Incorrect value for n");
209  mu_assert(data->m == 3, "Incorrect value for m");
210  mu_assert(data->r == 3, "Incorrect value for r");
211  mu_assert(data->K == 0, "Incorrect value for K");
212 
213  // check if all data is read correctly.
214  mu_assert(matrix_get(data->Z, data->m+1, 0, 1) == 0.7065937536993949,
215  "Incorrect Z value at 0, 1");
216  mu_assert(matrix_get(data->Z, data->m+1, 0, 2) == 0.7016517970438980,
217  "Incorrect Z value at 0, 2");
218  mu_assert(matrix_get(data->Z, data->m+1, 0, 3) == 0.1548611397288129,
219  "Incorrect Z value at 0, 3");
220  mu_assert(matrix_get(data->Z, data->m+1, 1, 1) == 0.4604987687863951,
221  "Incorrect Z value at 1, 1");
222  mu_assert(matrix_get(data->Z, data->m+1, 1, 2) == 0.6374142980176117,
223  "Incorrect Z value at 1, 2");
224  mu_assert(matrix_get(data->Z, data->m+1, 1, 3) == 0.0370930278245423,
225  "Incorrect Z value at 1, 3");
226  mu_assert(matrix_get(data->Z, data->m+1, 2, 1) == 0.3798777132278375,
227  "Incorrect Z value at 2, 1");
228  mu_assert(matrix_get(data->Z, data->m+1, 2, 2) == 0.5745070018747664,
229  "Incorrect Z value at 2, 2");
230  mu_assert(matrix_get(data->Z, data->m+1, 2, 3) == 0.2570906697837264,
231  "Incorrect Z value at 2, 3");
232  mu_assert(matrix_get(data->Z, data->m+1, 3, 1) == 0.2789376050039792,
233  "Incorrect Z value at 3, 1");
234  mu_assert(matrix_get(data->Z, data->m+1, 3, 2) == 0.4853242744610165,
235  "Incorrect Z value at 3, 2");
236  mu_assert(matrix_get(data->Z, data->m+1, 3, 3) == 0.1894010436762711,
237  "Incorrect Z value at 3, 3");
238  mu_assert(matrix_get(data->Z, data->m+1, 4, 1) == 0.7630904372339489,
239  "Incorrect Z value at 4, 1");
240  mu_assert(matrix_get(data->Z, data->m+1, 4, 2) == 0.1341546320318005,
241  "Incorrect Z value at 4, 2");
242  mu_assert(matrix_get(data->Z, data->m+1, 4, 3) == 0.6827430912944857,
243  "Incorrect Z value at 4, 3");
244  // check if RAW = Z
245  mu_assert(data->Z == data->RAW, "Z pointer doesn't equal RAW pointer");
246 
247  // check if labels read correctly
248  mu_assert(data->y == NULL, "Outcome pointer is not NULL");
249 
250  // check if the column of ones is added
251  mu_assert(matrix_get(data->Z, data->m+1, 0, 0) == 1,
252  "Incorrect Z value at 0, 0");
253  mu_assert(matrix_get(data->Z, data->m+1, 1, 0) == 1,
254  "Incorrect Z value at 1, 0");
255  mu_assert(matrix_get(data->Z, data->m+1, 2, 0) == 1,
256  "Incorrect Z value at 2, 0");
257  mu_assert(matrix_get(data->Z, data->m+1, 3, 0) == 1,
258  "Incorrect Z value at 3, 0");
259  mu_assert(matrix_get(data->Z, data->m+1, 4, 0) == 1,
260  "Incorrect Z value at 4, 0");
261 
262  // end test code //
263 
264  gensvm_free_data(data);
265  return NULL;
266 }
267 
269 {
270  char *filename = "./data/test_file_read_data_libsvm.txt";
271  struct GenData *data = gensvm_init_data();
272 
273  // start test code //
274  gensvm_read_data_libsvm(data, filename);
275 
276  // check if dimensions are correctly read
277  mu_assert(data->n == 5, "Incorrect value for n");
278  mu_assert(data->m == 3, "Incorrect value for m");
279  mu_assert(data->r == 3, "Incorrect value for r");
280  mu_assert(data->K == 4, "Incorrect value for K");
281 
282  // check if all data is read correctly.
283  mu_assert(matrix_get(data->Z, data->m+1, 0, 1) == 0.7065937536993949,
284  "Incorrect Z value at 0, 1");
285  mu_assert(matrix_get(data->Z, data->m+1, 0, 2) == 0.7016517970438980,
286  "Incorrect Z value at 0, 2");
287  mu_assert(matrix_get(data->Z, data->m+1, 0, 3) == 0.1548611397288129,
288  "Incorrect Z value at 0, 3");
289  mu_assert(matrix_get(data->Z, data->m+1, 1, 1) == 0.4604987687863951,
290  "Incorrect Z value at 1, 1");
291  mu_assert(matrix_get(data->Z, data->m+1, 1, 2) == 0.6374142980176117,
292  "Incorrect Z value at 1, 2");
293  mu_assert(matrix_get(data->Z, data->m+1, 1, 3) == 0.0370930278245423,
294  "Incorrect Z value at 1, 3");
295  mu_assert(matrix_get(data->Z, data->m+1, 2, 1) == 0.3798777132278375,
296  "Incorrect Z value at 2, 1");
297  mu_assert(matrix_get(data->Z, data->m+1, 2, 2) == 0.5745070018747664,
298  "Incorrect Z value at 2, 2");
299  mu_assert(matrix_get(data->Z, data->m+1, 2, 3) == 0.2570906697837264,
300  "Incorrect Z value at 2, 3");
301  mu_assert(matrix_get(data->Z, data->m+1, 3, 1) == 0.2789376050039792,
302  "Incorrect Z value at 3, 1");
303  mu_assert(matrix_get(data->Z, data->m+1, 3, 2) == 0.4853242744610165,
304  "Incorrect Z value at 3, 2");
305  mu_assert(matrix_get(data->Z, data->m+1, 3, 3) == 0.1894010436762711,
306  "Incorrect Z value at 3, 3");
307  mu_assert(matrix_get(data->Z, data->m+1, 4, 1) == 0.7630904372339489,
308  "Incorrect Z value at 4, 1");
309  mu_assert(matrix_get(data->Z, data->m+1, 4, 2) == 0.1341546320318005,
310  "Incorrect Z value at 4, 2");
311  mu_assert(matrix_get(data->Z, data->m+1, 4, 3) == 0.6827430912944857,
312  "Incorrect Z value at 4, 3");
313  // check if RAW = Z
314  mu_assert(data->Z == data->RAW, "Z pointer doesn't equal RAW pointer");
315 
316  // check if labels read correctly
317  mu_assert(data->y[0] == 2, "Incorrect label read at 0");
318  mu_assert(data->y[1] == 1, "Incorrect label read at 1");
319  mu_assert(data->y[2] == 3, "Incorrect label read at 2");
320  mu_assert(data->y[3] == 4, "Incorrect label read at 3");
321  mu_assert(data->y[4] == 3, "Incorrect label read at 4");
322 
323  // check if the column of ones is added
324  mu_assert(matrix_get(data->Z, data->m+1, 0, 0) == 1,
325  "Incorrect Z value at 0, 0");
326  mu_assert(matrix_get(data->Z, data->m+1, 1, 0) == 1,
327  "Incorrect Z value at 1, 0");
328  mu_assert(matrix_get(data->Z, data->m+1, 2, 0) == 1,
329  "Incorrect Z value at 2, 0");
330  mu_assert(matrix_get(data->Z, data->m+1, 3, 0) == 1,
331  "Incorrect Z value at 3, 0");
332  mu_assert(matrix_get(data->Z, data->m+1, 4, 0) == 1,
333  "Incorrect Z value at 4, 0");
334 
335  // end test code //
336 
337  gensvm_free_data(data);
338 
339  return NULL;
340 }
341 
343 {
344  char *filename = "./data/test_file_read_data_libsvm_0.txt";
345  struct GenData *data = gensvm_init_data();
346 
347  // start test code //
348  gensvm_read_data_libsvm(data, filename);
349 
350  // check if dimensions are correctly read
351  mu_assert(data->n == 5, "Incorrect value for n");
352  mu_assert(data->m == 3, "Incorrect value for m");
353  mu_assert(data->r == 3, "Incorrect value for r");
354  mu_assert(data->K == 4, "Incorrect value for K");
355 
356  // check if all data is read correctly.
357  mu_assert(matrix_get(data->Z, data->m+1, 0, 1) == 0.7065937536993949,
358  "Incorrect Z value at 0, 1");
359  mu_assert(matrix_get(data->Z, data->m+1, 0, 2) == 0.7016517970438980,
360  "Incorrect Z value at 0, 2");
361  mu_assert(matrix_get(data->Z, data->m+1, 0, 3) == 0.1548611397288129,
362  "Incorrect Z value at 0, 3");
363  mu_assert(matrix_get(data->Z, data->m+1, 1, 1) == 0.4604987687863951,
364  "Incorrect Z value at 1, 1");
365  mu_assert(matrix_get(data->Z, data->m+1, 1, 2) == 0.6374142980176117,
366  "Incorrect Z value at 1, 2");
367  mu_assert(matrix_get(data->Z, data->m+1, 1, 3) == 0.0370930278245423,
368  "Incorrect Z value at 1, 3");
369  mu_assert(matrix_get(data->Z, data->m+1, 2, 1) == 0.3798777132278375,
370  "Incorrect Z value at 2, 1");
371  mu_assert(matrix_get(data->Z, data->m+1, 2, 2) == 0.5745070018747664,
372  "Incorrect Z value at 2, 2");
373  mu_assert(matrix_get(data->Z, data->m+1, 2, 3) == 0.2570906697837264,
374  "Incorrect Z value at 2, 3");
375  mu_assert(matrix_get(data->Z, data->m+1, 3, 1) == 0.2789376050039792,
376  "Incorrect Z value at 3, 1");
377  mu_assert(matrix_get(data->Z, data->m+1, 3, 2) == 0.4853242744610165,
378  "Incorrect Z value at 3, 2");
379  mu_assert(matrix_get(data->Z, data->m+1, 3, 3) == 0.1894010436762711,
380  "Incorrect Z value at 3, 3");
381  mu_assert(matrix_get(data->Z, data->m+1, 4, 1) == 0.7630904372339489,
382  "Incorrect Z value at 4, 1");
383  mu_assert(matrix_get(data->Z, data->m+1, 4, 2) == 0.1341546320318005,
384  "Incorrect Z value at 4, 2");
385  mu_assert(matrix_get(data->Z, data->m+1, 4, 3) == 0.6827430912944857,
386  "Incorrect Z value at 4, 3");
387  // check if RAW = Z
388  mu_assert(data->Z == data->RAW, "Z pointer doesn't equal RAW pointer");
389 
390  // check if labels read correctly
391  mu_assert(data->y[0] == 2, "Incorrect label read at 0");
392  mu_assert(data->y[1] == 1, "Incorrect label read at 1");
393  mu_assert(data->y[2] == 3, "Incorrect label read at 2");
394  mu_assert(data->y[3] == 4, "Incorrect label read at 3");
395  mu_assert(data->y[4] == 3, "Incorrect label read at 4");
396 
397  // check if the column of ones is added
398  mu_assert(matrix_get(data->Z, data->m+1, 0, 0) == 1,
399  "Incorrect Z value at 0, 0");
400  mu_assert(matrix_get(data->Z, data->m+1, 1, 0) == 1,
401  "Incorrect Z value at 1, 0");
402  mu_assert(matrix_get(data->Z, data->m+1, 2, 0) == 1,
403  "Incorrect Z value at 2, 0");
404  mu_assert(matrix_get(data->Z, data->m+1, 3, 0) == 1,
405  "Incorrect Z value at 3, 0");
406  mu_assert(matrix_get(data->Z, data->m+1, 4, 0) == 1,
407  "Incorrect Z value at 4, 0");
408 
409  // end test code //
410 
411  gensvm_free_data(data);
412 
413  return NULL;
414 }
415 
417 {
418  char *filename = "./data/test_file_read_data_sparse_libsvm.txt";
419  struct GenData *data = gensvm_init_data();
420 
421  // start test code //
422  gensvm_read_data_libsvm(data, filename);
423 
424  // check if dimensions are correctly read
425  mu_assert(data->n == 10, "Incorrect value for n");
426  mu_assert(data->m == 3, "Incorrect value for m");
427  mu_assert(data->r == 3, "Incorrect value for r");
428  mu_assert(data->K == 4, "Incorrect value for K");
429 
430  // check if dense data pointers are NULL
431  mu_assert(data->Z == NULL, "Z pointer isn't NULL");
432  mu_assert(data->RAW == NULL, "RAW pointer isn't NULL");
433 
434  // check sparse data structure
435  mu_assert(data->spZ != NULL, "spZ is NULL");
436  mu_assert(data->spZ->nnz == 14, "Incorrect nnz");
437  mu_assert(data->spZ->n_row == 10, "Incorrect n_row");
438  mu_assert(data->spZ->n_col == 4, "Incorrect n_col");
439 
440  // check sparse values
441  mu_assert(data->spZ->values[0] == 1.0,
442  "Incorrect nonzero value at 0");
443  mu_assert(data->spZ->values[1] == 0.7016517970438980,
444  "Incorrect nonzero value at 1");
445  mu_assert(data->spZ->values[2] == 1.0,
446  "Incorrect nonzero value at 2");
447  mu_assert(data->spZ->values[3] == 0.0370930278245423,
448  "Incorrect nonzero value at 3");
449  mu_assert(data->spZ->values[4] == 1.0,
450  "Incorrect nonzero value at 4");
451  mu_assert(data->spZ->values[5] == 1.0,
452  "Incorrect nonzero value at 5");
453  mu_assert(data->spZ->values[6] == 0.4853242744610165,
454  "Incorrect nonzero value at 6");
455  mu_assert(data->spZ->values[7] == 1.0,
456  "Incorrect nonzero value at 7");
457  mu_assert(data->spZ->values[8] == 0.7630904372339489,
458  "Incorrect nonzero value at 8");
459  mu_assert(data->spZ->values[9] == 1.0,
460  "Incorrect nonzero value at 9");
461  mu_assert(data->spZ->values[10] == 1.0,
462  "Incorrect nonzero value at 10");
463  mu_assert(data->spZ->values[11] == 1.0,
464  "Incorrect nonzero value at 11");
465  mu_assert(data->spZ->values[12] == 1.0,
466  "Incorrect nonzero value at 12");
467  mu_assert(data->spZ->values[13] == 1.0,
468  "Incorrect nonzero value at 13");
469 
470  // check sparse row lengths
471  mu_assert(data->spZ->ia[0] == 0, "Incorrect ia value at 0");
472  mu_assert(data->spZ->ia[1] == 2, "Incorrect ia value at 1");
473  mu_assert(data->spZ->ia[2] == 4, "Incorrect ia value at 2");
474  mu_assert(data->spZ->ia[3] == 5, "Incorrect ia value at 3");
475  mu_assert(data->spZ->ia[4] == 7, "Incorrect ia value at 4");
476  mu_assert(data->spZ->ia[5] == 9, "Incorrect ia value at 5");
477  mu_assert(data->spZ->ia[6] == 10, "Incorrect ia value at 5");
478  mu_assert(data->spZ->ia[7] == 11, "Incorrect ia value at 5");
479  mu_assert(data->spZ->ia[8] == 12, "Incorrect ia value at 5");
480  mu_assert(data->spZ->ia[9] == 13, "Incorrect ia value at 5");
481  mu_assert(data->spZ->ia[10] == 14, "Incorrect ia value at 5");
482 
483  // check sparse column indices
484  mu_assert(data->spZ->ja[0] == 0, "Incorrect ja value at 0");
485  mu_assert(data->spZ->ja[1] == 2, "Incorrect ja value at 1");
486  mu_assert(data->spZ->ja[2] == 0, "Incorrect ja value at 2");
487  mu_assert(data->spZ->ja[3] == 3, "Incorrect ja value at 3");
488  mu_assert(data->spZ->ja[4] == 0, "Incorrect ja value at 4");
489  mu_assert(data->spZ->ja[5] == 0, "Incorrect ja value at 5");
490  mu_assert(data->spZ->ja[6] == 2, "Incorrect ja value at 6");
491  mu_assert(data->spZ->ja[7] == 0, "Incorrect ja value at 7");
492  mu_assert(data->spZ->ja[8] == 1, "Incorrect ja value at 8");
493  mu_assert(data->spZ->ja[9] == 0, "Incorrect ja value at 7");
494  mu_assert(data->spZ->ja[10] == 0, "Incorrect ja value at 7");
495  mu_assert(data->spZ->ja[11] == 0, "Incorrect ja value at 7");
496  mu_assert(data->spZ->ja[12] == 0, "Incorrect ja value at 7");
497  mu_assert(data->spZ->ja[13] == 0, "Incorrect ja value at 7");
498 
499  // check if labels read correctly
500  mu_assert(data->y[0] == 2, "Incorrect label read at 0");
501  mu_assert(data->y[1] == 1, "Incorrect label read at 1");
502  mu_assert(data->y[2] == 3, "Incorrect label read at 2");
503  mu_assert(data->y[3] == 4, "Incorrect label read at 3");
504  mu_assert(data->y[4] == 3, "Incorrect label read at 4");
505 
506  // end test code //
507 
508  gensvm_free_data(data);
509 
510  return NULL;
511 }
512 
514 {
515  char *filename = "./data/test_file_read_data_no_label_libsvm.txt";
516  struct GenData *data = gensvm_init_data();
517 
518  // start test code //
519  gensvm_read_data_libsvm(data, filename);
520 
521  // check if dimensions are correctly read
522  mu_assert(data->n == 5, "Incorrect value for n");
523  mu_assert(data->m == 3, "Incorrect value for m");
524  mu_assert(data->r == 3, "Incorrect value for r");
525  mu_assert(data->K == 0, "Incorrect value for K");
526 
527  // check if all data is read correctly.
528  mu_assert(matrix_get(data->Z, data->m+1, 0, 1) == 0.7065937536993949,
529  "Incorrect Z value at 0, 1");
530  mu_assert(matrix_get(data->Z, data->m+1, 0, 2) == 0.7016517970438980,
531  "Incorrect Z value at 0, 2");
532  mu_assert(matrix_get(data->Z, data->m+1, 0, 3) == 0.1548611397288129,
533  "Incorrect Z value at 0, 3");
534  mu_assert(matrix_get(data->Z, data->m+1, 1, 1) == 0.4604987687863951,
535  "Incorrect Z value at 1, 1");
536  mu_assert(matrix_get(data->Z, data->m+1, 1, 2) == 0.6374142980176117,
537  "Incorrect Z value at 1, 2");
538  mu_assert(matrix_get(data->Z, data->m+1, 1, 3) == 0.0370930278245423,
539  "Incorrect Z value at 1, 3");
540  mu_assert(matrix_get(data->Z, data->m+1, 2, 1) == 0.3798777132278375,
541  "Incorrect Z value at 2, 1");
542  mu_assert(matrix_get(data->Z, data->m+1, 2, 2) == 0.5745070018747664,
543  "Incorrect Z value at 2, 2");
544  mu_assert(matrix_get(data->Z, data->m+1, 2, 3) == 0.2570906697837264,
545  "Incorrect Z value at 2, 3");
546  mu_assert(matrix_get(data->Z, data->m+1, 3, 1) == 0.2789376050039792,
547  "Incorrect Z value at 3, 1");
548  mu_assert(matrix_get(data->Z, data->m+1, 3, 2) == 0.4853242744610165,
549  "Incorrect Z value at 3, 2");
550  mu_assert(matrix_get(data->Z, data->m+1, 3, 3) == 0.1894010436762711,
551  "Incorrect Z value at 3, 3");
552  mu_assert(matrix_get(data->Z, data->m+1, 4, 1) == 0.7630904372339489,
553  "Incorrect Z value at 4, 1");
554  mu_assert(matrix_get(data->Z, data->m+1, 4, 2) == 0.1341546320318005,
555  "Incorrect Z value at 4, 2");
556  mu_assert(matrix_get(data->Z, data->m+1, 4, 3) == 0.6827430912944857,
557  "Incorrect Z value at 4, 3");
558  // check if RAW = Z
559  mu_assert(data->Z == data->RAW, "Z pointer doesn't equal RAW pointer");
560 
561  // check if labels read correctly
562  mu_assert(data->y == NULL, "Outcome pointer is not NULL");
563 
564  // check if the column of ones is added
565  mu_assert(matrix_get(data->Z, data->m+1, 0, 0) == 1,
566  "Incorrect Z value at 0, 0");
567  mu_assert(matrix_get(data->Z, data->m+1, 1, 0) == 1,
568  "Incorrect Z value at 1, 0");
569  mu_assert(matrix_get(data->Z, data->m+1, 2, 0) == 1,
570  "Incorrect Z value at 2, 0");
571  mu_assert(matrix_get(data->Z, data->m+1, 3, 0) == 1,
572  "Incorrect Z value at 3, 0");
573  mu_assert(matrix_get(data->Z, data->m+1, 4, 0) == 1,
574  "Incorrect Z value at 4, 0");
575 
576  // end test code //
577 
578  gensvm_free_data(data);
579 
580  return NULL;
581 }
582 
584 {
585  struct GenModel *model = gensvm_init_model();
586  char *filename = "./data/test_read_model.txt";
587 
588  // start test code //
589  gensvm_read_model(model, filename);
590 
591  mu_assert(model->p == 0.21398, "Incorrect read for model->p");
592  mu_assert(model->lambda == 0.902131, "Incorrect read for "
593  "model->lambda");
594  mu_assert(model->kappa == 1.0213, "Incorrect read for model->kappa");
595  mu_assert(model->epsilon == 1e-10, "Incorrect read for "
596  "model->epsilon");
597  mu_assert(model->weight_idx == 2, "Incorrect read for "
598  "model->weight_idx");
599 
600  mu_assert(strcmp(model->data_file, "./data/test_file_read_data.txt")
601  == 0, "Incorrect read for model->data_file");
602  mu_assert(model->n == 10, "Incorrect read for model->n");
603  mu_assert(model->m == 2, "Incorrect read for model->m");
604  mu_assert(model->K == 3, "Incorrect read for model->K");
605 
606  mu_assert(matrix_get(model->V, model->K-1, 0, 0) == 0.1234,
607  "Incorrect model->V element at 0, 0");
608  mu_assert(matrix_get(model->V, model->K-1, 0, 1) == -0.4321,
609  "Incorrect model->V element at 0, 1");
610  mu_assert(matrix_get(model->V, model->K-1, 1, 0) == -0.5678,
611  "Incorrect model->V element at 1, 0");
612  mu_assert(matrix_get(model->V, model->K-1, 1, 1) == 0.9876,
613  "Incorrect model->V element at 1, 1");
614  mu_assert(matrix_get(model->V, model->K-1, 2, 0) == 0.9012,
615  "Incorrect model->V element at 2, 0");
616  mu_assert(matrix_get(model->V, model->K-1, 2, 1) == -0.5555,
617  "Incorrect model->V element at 2, 1");
618 
619  // end test code //
620 
621  gensvm_free_model(model);
622 
623  return NULL;
624 }
625 
627 {
628  struct GenModel *model = gensvm_init_model();
629 
630  model->p = 0.90328;
631  model->lambda = 0.0130;
632  model->kappa = 1.1832;
633  model->epsilon = 1e-8;
634  model->weight_idx = 1;
635  model->data_file = strdup("./data/test_file_read_data.txt");
636  model->n = 10;
637  model->m = 2;
638  model->K = 3;
639 
640  model->V = Calloc(double, (model->m+1)*(model->K-1));
641  matrix_set(model->V, model->K-1, 0, 0, 0.4989893785603748);
642  matrix_set(model->V, model->K-1, 0, 1, 0.0599082796573645);
643  matrix_set(model->V, model->K-1, 1, 0, 0.7918204761759593);
644  matrix_set(model->V, model->K-1, 1, 1, 0.6456613497110559);
645  matrix_set(model->V, model->K-1, 2, 0, 0.9711956316284261);
646  matrix_set(model->V, model->K-1, 2, 1, 0.5010714686310176);
647 
648  // start test code //
649  gensvm_write_model(model, "./data/test_write_model.txt");
650 
651  FILE *fid = fopen("./data/test_write_model.txt", "r");
652  mu_assert(fid != NULL, "Couldn't open output file for reading");
653 
654  char buffer[GENSVM_MAX_LINE_LENGTH];
655 
656  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
657  mu_assert(strcmp(buffer, "Output file for GenSVM (version 0.1.4)\n")
658  == 0, "Line doesn't contain expected content (0).\n");
659 
660  // skip the time line
661  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
662 
663  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
664  mu_assert(strcmp(buffer, "\n") == 0,
665  "Line doesn't contain expected content (1).\n");
666 
667  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
668  mu_assert(strcmp(buffer, "Model:\n") == 0,
669  "Line doesn't contain expected content (2).\n");
670 
671  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
672  mu_assert(strcmp(buffer, "p = 0.9032800000000000\n") == 0,
673  "Line doesn't contain expected content (3).\n");
674 
675  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
676  mu_assert(strcmp(buffer, "lambda = 0.0130000000000000\n") == 0,
677  "Line doesn't contain expected content (4).\n");
678 
679  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
680  mu_assert(strcmp(buffer, "kappa = 1.1832000000000000\n") == 0,
681  "Line doesn't contain expected content (5).\n");
682 
683  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
684  mu_assert(strcmp(buffer, "epsilon = 1e-08\n") == 0,
685  "Line doesn't contain expected content (6).\n");
686 
687  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
688  mu_assert(strcmp(buffer, "weight_idx = 1\n") == 0,
689  "Line doesn't contain expected content (7).\n");
690 
691  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
692  mu_assert(strcmp(buffer, "\n") == 0,
693  "Line doesn't contain expected content (8).\n");
694 
695  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
696  mu_assert(strcmp(buffer, "Data:\n") == 0,
697  "Line doesn't contain expected content (9).\n");
698 
699  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
700  mu_assert(strcmp(buffer, "filename = ./data/test_file_read_data.txt\n")
701  == 0, "Line doesn't contain expected content (10).\n");
702 
703  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
704  mu_assert(strcmp(buffer, "n = 10\n") == 0,
705  "Line doesn't contain expected content (11).\n");
706 
707  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
708  mu_assert(strcmp(buffer, "m = 2\n") == 0,
709  "Line doesn't contain expected content (12).\n");
710 
711  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
712  mu_assert(strcmp(buffer, "K = 3\n") == 0,
713  "Line doesn't contain expected content (13).\n");
714 
715  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
716  mu_assert(strcmp(buffer, "\n") == 0,
717  "Line doesn't contain expected content (14).\n");
718 
719  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
720  mu_assert(strcmp(buffer, "Output:\n") == 0,
721  "Line doesn't contain expected content (15).\n");
722 
723  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
724  mu_assert(strcmp(buffer, "+0.4989893785603748 +0.0599082796573645\n")
725  == 0, "Line doesn't contain expected content (16).\n");
726 
727  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
728  mu_assert(strcmp(buffer, "+0.7918204761759593 +0.6456613497110559\n")
729  == 0, "Line doesn't contain expected content (17).\n");
730 
731  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
732  mu_assert(strcmp(buffer, "+0.9711956316284261 +0.5010714686310176\n")
733  == 0, "Line doesn't contain expected content (18).\n");
734 
735  fclose(fid);
736 
737  // end test code //
738 
739  gensvm_free_model(model);
740 
741  return NULL;
742 }
743 
745 {
746  int n = 5,
747  m = 3,
748  K = 3;
749  struct GenData *data = gensvm_init_data();
750  long *predy = Calloc(long, n);
751 
752  data->n = n;
753  data->m = m;
754  data->K = K;
755 
756  data->Z = Calloc(double, data->n * (data->m+1));
757 
758  matrix_set(data->Z, data->m+1, 0, 0, 1.0);
759  matrix_set(data->Z, data->m+1, 1, 0, 1.0);
760  matrix_set(data->Z, data->m+1, 2, 0, 1.0);
761  matrix_set(data->Z, data->m+1, 3, 0, 1.0);
762  matrix_set(data->Z, data->m+1, 4, 0, 1.0);
763 
764  matrix_set(data->Z, data->m+1, 0, 1, 0.7065937536993949);
765  matrix_set(data->Z, data->m+1, 0, 2, 0.7016517970438980);
766  matrix_set(data->Z, data->m+1, 0, 3, 0.1548611397288129);
767  matrix_set(data->Z, data->m+1, 1, 1, 0.4604987687863951);
768  matrix_set(data->Z, data->m+1, 1, 2, 0.6374142980176117);
769  matrix_set(data->Z, data->m+1, 1, 3, 0.0370930278245423);
770  matrix_set(data->Z, data->m+1, 2, 1, 0.3798777132278375);
771  matrix_set(data->Z, data->m+1, 2, 2, 0.5745070018747664);
772  matrix_set(data->Z, data->m+1, 2, 3, 0.2570906697837264);
773  matrix_set(data->Z, data->m+1, 3, 1, 0.2789376050039792);
774  matrix_set(data->Z, data->m+1, 3, 2, 0.4853242744610165);
775  matrix_set(data->Z, data->m+1, 3, 3, 0.1894010436762711);
776  matrix_set(data->Z, data->m+1, 4, 1, 0.7630904372339489);
777  matrix_set(data->Z, data->m+1, 4, 2, 0.1341546320318005);
778  matrix_set(data->Z, data->m+1, 4, 3, 0.6827430912944857);
779 
780  predy[0] = 3;
781  predy[1] = 2;
782  predy[2] = 1;
783  predy[3] = 2;
784  predy[4] = 1;
785 
786  // start test code //
787 
788  gensvm_write_predictions(data, predy,
789  "./data/test_write_predictions.txt");
790 
791  FILE *fid = fopen("./data/test_write_predictions.txt", "r");
792  mu_assert(fid != NULL, "Couldn't open output file for reading");
793 
794  char buffer[GENSVM_MAX_LINE_LENGTH];
795  // skip the first two lines
796  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
797  mu_assert(strcmp(buffer, "5\n") == 0,
798  "Line doesn't contain expected content (0).\n");
799 
800  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
801  mu_assert(strcmp(buffer, "3\n") == 0,
802  "Line doesn't contain expected content (1).\n");
803 
804  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
805  mu_assert(strcmp(buffer, "0.7065937536993949 0.7016517970438980 "
806  "0.1548611397288129 3\n") == 0,
807  "Line doesn't contain expected content (2).\n");
808 
809  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
810  mu_assert(strcmp(buffer, "0.4604987687863951 0.6374142980176117 "
811  "0.0370930278245423 2\n") == 0,
812  "Line doesn't contain expected content (3).\n");
813 
814  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
815  mu_assert(strcmp(buffer, "0.3798777132278375 0.5745070018747664 "
816  "0.2570906697837264 1\n") == 0,
817  "Line doesn't contain expected content (4).\n");
818 
819 
820  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
821  mu_assert(strcmp(buffer, "0.2789376050039792 0.4853242744610165 "
822  "0.1894010436762711 2\n") == 0,
823  "Line doesn't contain expected content (5).\n");
824 
825 
826  fgets(buffer, GENSVM_MAX_LINE_LENGTH, fid);
827  mu_assert(strcmp(buffer, "0.7630904372339489 0.1341546320318005 "
828  "0.6827430912944857 1\n") == 0,
829  "Line doesn't contain expected content (6).\n");
830 
831  fclose(fid);
832  // end test code //
833 
834  gensvm_free_data(data);
835  free(predy);
836 
837  return NULL;
838 }
839 
841 {
842  // not sure how to unit test this function.
843 
844  mu_test_missing();
845  return NULL;
846 }
847 
848 char *all_tests()
849 {
850  mu_suite_start();
858 
863 
864  return NULL;
865 }
866 
Minimal unit testing framework for C.
#define Calloc(type, size)
Definition: gensvm_memory.h:40
char * test_gensvm_read_data_libsvm_sparse()
char * test_gensvm_time_string()
void gensvm_write_predictions(struct GenData *data, long *predy, char *output_filename)
Write predictions to file.
Definition: gensvm_io.c:556
long * ja
column indices, should be of length nnz
Definition: gensvm_sparse.h:67
double epsilon
stopping criterion for the IM algorithm.
Definition: gensvm_base.h:101
long n_col
number of columns of the original matrix
Definition: gensvm_sparse.h:60
void gensvm_read_model(struct GenModel *model, char *model_filename)
Read model from file.
Definition: gensvm_io.c:410
double p
parameter for the L-p norm in the loss function
Definition: gensvm_base.h:103
void gensvm_read_data(struct GenData *dataset, char *data_file)
Read data from file.
Definition: gensvm_io.c:47
#define mu_assert(test, message)
Definition: minunit.h:29
#define GENSVM_MAX_LINE_LENGTH
long K
number of classes
Definition: gensvm_base.h:58
#define matrix_get(M, cols, i, j)
char * test_gensvm_read_data()
char * test_gensvm_write_model()
double * Z
Definition: gensvm_base.h:68
void gensvm_free_model(struct GenModel *model)
Free allocated GenModel struct.
Definition: gensvm_base.c:211
long nnz
number of nonzero elements
Definition: gensvm_sparse.h:56
char * all_tests()
int weight_idx
which weights to use (1 = unit, 2 = group)
Definition: gensvm_base.h:93
double * V
augmented weight matrix
Definition: gensvm_base.h:115
#define mu_run_test(test)
Definition: minunit.h:35
long * y
array of class labels, 1..K
Definition: gensvm_base.h:66
struct GenModel * gensvm_init_model(void)
Initialize a GenModel structure.
Definition: gensvm_base.c:102
A structure to represent the data.
Definition: gensvm_base.h:57
double * values
actual nonzero values, should be of length nnz
Definition: gensvm_sparse.h:63
char * test_gensvm_read_data_sparse()
A structure to represent a single GenSVM model.
Definition: gensvm_base.h:92
char * test_gensvm_read_data_no_label()
void gensvm_write_model(struct GenModel *model, char *output_filename)
Write model to file.
Definition: gensvm_io.c:494
#define mu_test_missing()
Definition: minunit.h:60
char * data_file
filename of the data
Definition: gensvm_base.h:134
char * test_gensvm_read_data_libsvm_0based()
long n
number of instances in the dataset
Definition: gensvm_base.h:97
void gensvm_free_data(struct GenData *data)
Free allocated GenData struct.
Definition: gensvm_base.c:73
long r
number of eigenvalues (width of Z)
Definition: gensvm_base.h:64
Header file for gensvm_io.c.
double kappa
parameter for the Huber hinge function
Definition: gensvm_base.h:105
RUN_TESTS(all_tests)
long K
number of classes in the dataset
Definition: gensvm_base.h:95
char * test_gensvm_read_data_libsvm_no_label()
long m
number of predictors (width of RAW)
Definition: gensvm_base.h:62
#define matrix_set(M, cols, i, j, val)
long n
number of instances
Definition: gensvm_base.h:60
struct GenData * gensvm_init_data(void)
Initialize a GenData structure.
Definition: gensvm_base.c:45
void gensvm_read_data_libsvm(struct GenData *dataset, char *data_file)
Read data from a file in LibSVM/SVMlight format.
Definition: gensvm_io.c:178
char * test_gensvm_read_model()
long m
number of predictor variables in the dataset
Definition: gensvm_base.h:99
long * ia
cumulative row lengths, should be of length n_row+1
Definition: gensvm_sparse.h:65
#define mu_suite_start()
Definition: minunit.h:24
double * RAW
augmented raw data matrix
Definition: gensvm_base.h:73
struct GenSparse * spZ
sparse representation of the augmented data matrix
Definition: gensvm_base.h:71
double lambda
regularization parameter in the loss function
Definition: gensvm_base.h:107
char * test_gensvm_write_predictions()
long n_row
number of rows of the original matrix
Definition: gensvm_sparse.h:58
char * test_gensvm_read_data_libsvm()