You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

libsvmread.c 4.1 kB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4. #include <ctype.h>
  5. #include <errno.h>
  6. #include "mex.h"
  7. #ifdef MX_API_VER
  8. #if MX_API_VER < 0x07030000
  9. typedef int mwIndex;
  10. #endif
  11. #endif
  12. #ifndef max
  13. #define max(x,y) (((x)>(y))?(x):(y))
  14. #endif
  15. #ifndef min
  16. #define min(x,y) (((x)<(y))?(x):(y))
  17. #endif
  18. void exit_with_help()
  19. {
  20. mexPrintf(
  21. "Usage: [label_vector, instance_matrix] = libsvmread('filename');\n"
  22. );
  23. }
  24. static void fake_answer(int nlhs, mxArray *plhs[])
  25. {
  26. int i;
  27. for(i=0;i<nlhs;i++)
  28. plhs[i] = mxCreateDoubleMatrix(0, 0, mxREAL);
  29. }
  30. static char *line;
  31. static int max_line_len;
  32. static char* readline(FILE *input)
  33. {
  34. int len;
  35. if(fgets(line,max_line_len,input) == NULL)
  36. return NULL;
  37. while(strrchr(line,'\n') == NULL)
  38. {
  39. max_line_len *= 2;
  40. line = (char *) realloc(line, max_line_len);
  41. len = (int) strlen(line);
  42. if(fgets(line+len,max_line_len-len,input) == NULL)
  43. break;
  44. }
  45. return line;
  46. }
  47. // read in a problem (in libsvm format)
  48. void read_problem(const char *filename, int nlhs, mxArray *plhs[])
  49. {
  50. int max_index, min_index, inst_max_index;
  51. size_t elements, k, i, l=0;
  52. FILE *fp = fopen(filename,"r");
  53. char *endptr;
  54. mwIndex *ir, *jc;
  55. double *labels, *samples;
  56. if(fp == NULL)
  57. {
  58. mexPrintf("can't open input file %s\n",filename);
  59. fake_answer(nlhs, plhs);
  60. return;
  61. }
  62. max_line_len = 1024;
  63. line = (char *) malloc(max_line_len*sizeof(char));
  64. max_index = 0;
  65. min_index = 1; // our index starts from 1
  66. elements = 0;
  67. while(readline(fp) != NULL)
  68. {
  69. char *idx, *val;
  70. // features
  71. int index = 0;
  72. inst_max_index = -1; // strtol gives 0 if wrong format, and precomputed kernel has <index> start from 0
  73. strtok(line," \t"); // label
  74. while (1)
  75. {
  76. idx = strtok(NULL,":"); // index:value
  77. val = strtok(NULL," \t");
  78. if(val == NULL)
  79. break;
  80. errno = 0;
  81. index = (int) strtol(idx,&endptr,10);
  82. if(endptr == idx || errno != 0 || *endptr != '\0' || index <= inst_max_index)
  83. {
  84. mexPrintf("Wrong input format at line %d\n",l+1);
  85. fake_answer(nlhs, plhs);
  86. return;
  87. }
  88. else
  89. inst_max_index = index;
  90. min_index = min(min_index, index);
  91. elements++;
  92. }
  93. max_index = max(max_index, inst_max_index);
  94. l++;
  95. }
  96. rewind(fp);
  97. // y
  98. plhs[0] = mxCreateDoubleMatrix(l, 1, mxREAL);
  99. // x^T
  100. if (min_index <= 0)
  101. plhs[1] = mxCreateSparse(max_index-min_index+1, l, elements, mxREAL);
  102. else
  103. plhs[1] = mxCreateSparse(max_index, l, elements, mxREAL);
  104. labels = mxGetPr(plhs[0]);
  105. samples = mxGetPr(plhs[1]);
  106. ir = mxGetIr(plhs[1]);
  107. jc = mxGetJc(plhs[1]);
  108. k=0;
  109. for(i=0;i<l;i++)
  110. {
  111. char *idx, *val, *label;
  112. jc[i] = k;
  113. readline(fp);
  114. label = strtok(line," \t\n");
  115. if(label == NULL)
  116. {
  117. mexPrintf("Empty line at line %d\n",i+1);
  118. fake_answer(nlhs, plhs);
  119. return;
  120. }
  121. labels[i] = strtod(label,&endptr);
  122. if(endptr == label || *endptr != '\0')
  123. {
  124. mexPrintf("Wrong input format at line %d\n",i+1);
  125. fake_answer(nlhs, plhs);
  126. return;
  127. }
  128. // features
  129. while(1)
  130. {
  131. idx = strtok(NULL,":");
  132. val = strtok(NULL," \t");
  133. if(val == NULL)
  134. break;
  135. ir[k] = (mwIndex) (strtol(idx,&endptr,10) - min_index); // precomputed kernel has <index> start from 0
  136. errno = 0;
  137. samples[k] = strtod(val,&endptr);
  138. if (endptr == val || errno != 0 || (*endptr != '\0' && !isspace(*endptr)))
  139. {
  140. mexPrintf("Wrong input format at line %d\n",i+1);
  141. fake_answer(nlhs, plhs);
  142. return;
  143. }
  144. ++k;
  145. }
  146. }
  147. jc[l] = k;
  148. fclose(fp);
  149. free(line);
  150. {
  151. mxArray *rhs[1], *lhs[1];
  152. rhs[0] = plhs[1];
  153. if(mexCallMATLAB(1, lhs, 1, rhs, "transpose"))
  154. {
  155. mexPrintf("Error: cannot transpose problem\n");
  156. fake_answer(nlhs, plhs);
  157. return;
  158. }
  159. plhs[1] = lhs[0];
  160. }
  161. }
  162. void mexFunction( int nlhs, mxArray *plhs[],
  163. int nrhs, const mxArray *prhs[] )
  164. {
  165. char filename[256];
  166. if(nrhs != 1 || nlhs != 2)
  167. {
  168. exit_with_help();
  169. fake_answer(nlhs, plhs);
  170. return;
  171. }
  172. mxGetString(prhs[0], filename, mxGetN(prhs[0]) + 1);
  173. if(filename == NULL)
  174. {
  175. mexPrintf("Error: filename is NULL\n");
  176. return;
  177. }
  178. read_problem(filename, nlhs, plhs);
  179. return;
  180. }

A Python package for graph kernels, graph edit distances and graph pre-image problem.