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


ml_classifiers
Author(s): Scott Niekum , Joshua Whitley
autogenerated on Tue May 14 2019 02:28:35