-
Notifications
You must be signed in to change notification settings - Fork 26
/
Copy pathdata.c
170 lines (149 loc) · 3.54 KB
/
data.c
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
#include "TH.h"
#include "luaT.h"
#include "lualib.h"
#define max_(a,b) (a>=b ? a : b)
#define min_(a,b) (a<=b ? a : b)
static int svm_readbinary(lua_State *L)
{
int normindex = 2;
int maxrows = -1;
// read the file name or the file pointer
int ownfile = 1;
const char *fname = lua_tostring(L,1);
FILE *fp;
if (fname == NULL)
{
fp = (*(FILE **)luaL_checkudata(L, 1, LUA_FILEHANDLE));
ownfile = 0;
// check if next entry is a number, then use it as number of
// samples to read
if (lua_isnumber(L,2))
{
maxrows = (int)lua_tonumber(L,2);
normindex = 3;
}
}
else
{
fp = fopen(fname,"r");
printf("Reading %s\n",fname);
}
luaL_argcheck(L, fp != NULL, 1, "File could not be opened");
//
int normalize = 0;
if lua_isnil(L,normindex)
normalize = 1;
else if lua_isboolean(L,normindex)
normalize = lua_toboolean(L,normindex);
// printf("norm=%d nil=%d bool=%d \n",normalize,lua_isnil(L,2),lua_isboolean(L,2));
char y;
int nf;
int i;
lua_newtable(L);
int cntr = 1;
int npos = 0;
int nneg = 0;
int maxdim = 0;
int minsparse = INT_MAX;
int maxsparse = 0;
while (maxrows-- && fread((void*)&y,sizeof(char),1,fp))
{
fread((void*)&nf,sizeof(int),1,fp);
THIntTensor *indices = THIntTensor_newWithSize1d(nf);
THFloatTensor *vals = THFloatTensor_newWithSize1d(nf);
int *indices_data = THIntTensor_data(indices);
float *vals_data = THFloatTensor_data(vals);
for (i=0; i<nf; i++)
{
fread((void*)indices_data++,sizeof(int),1,fp);
fread((void*)vals_data++,sizeof(float),1,fp);
}
if (normalize)
THFloatTensor_div(vals,vals,THFloatTensor_normall(vals,2));
if (y>0)
npos += 1;
else
nneg += 1;
maxdim = max_(maxdim,indices_data[-1]);
minsparse = min_(minsparse,nf);
maxsparse = max_(maxsparse,nf);
lua_newtable(L);
{
lua_pushnumber(L,(y ? 1 : -1));
lua_rawseti(L,-2,1);
lua_newtable(L);
{
luaT_pushudata(L,indices,"torch.IntTensor");
lua_rawseti(L,-2,1);
luaT_pushudata(L,vals,"torch.FloatTensor");
lua_rawseti(L,-2,2);
}
lua_rawseti(L,-2,2);
}
lua_rawseti(L,-2,cntr);
cntr++;
}
cntr--;
if (ownfile)
{
fclose(fp);
}
if (maxrows < -1)
{
printf("# of positive samples = %d\n",npos);
printf("# of negative samples = %d\n",nneg);
printf("# of total samples = %d\n",cntr);
printf("# of max dimensions = %d\n",maxdim);
printf("Min # of dims = %d\n",minsparse);
printf("Max # of dims = %d\n",maxsparse);
lua_pushnumber(L,(double)maxdim);
return 2;
}
return 1;
}
static int svm_infobinary(lua_State *L)
{
// read the file name or the file pointer
const char *fname = lua_tostring(L,1);
FILE *fp = fopen(fname,"r");
printf("Reading %s\n",fname);
luaL_argcheck(L, fp != NULL, 1, "File could not be opened");
char y;
int nf;
int cntr = 1;
int npos = 0;
int nneg = 0;
int maxdim = 0;
while (fread((void*)&y,sizeof(char),1,fp))
{
if (y>0)
npos += 1;
else
nneg += 1;
fread((void*)&nf,sizeof(int),1,fp);
fseek(fp,(nf-1)*2*4,SEEK_CUR);
fread((void*)&nf,sizeof(int),1,fp);
fseek(fp,4,SEEK_CUR);
maxdim = max_(maxdim,nf);
cntr++;
}
cntr--;
fclose(fp);
printf("# of positive samples = %d\n",npos);
printf("# of negative samples = %d\n",nneg);
printf("# of total samples = %d\n",cntr);
printf("# of max dimensions = %d\n",maxdim);
lua_pushnumber(L,(double)cntr);
lua_pushnumber(L,(double)maxdim);
return 2;
}
static const struct luaL_Reg svm_util__ [] = {
{"binread", svm_readbinary},
{"bininfo", svm_infobinary},
{NULL, NULL}
};
int libsvm_data_init(lua_State *L)
{
luaL_register(L, "svm", svm_util__);
return 1;
}