-
Notifications
You must be signed in to change notification settings - Fork 2
/
svmloader.pyx
240 lines (205 loc) · 7.95 KB
/
svmloader.pyx
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
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
cimport cython
from libc.stdlib cimport strtol, strtod
from libc.limits cimport INT_MAX
import array
from cpython cimport array
import numpy as np
cimport numpy as cnp
import scipy.sparse as sp
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
#@cython.initializedcheck(False)
cdef _load_svmfile(fp, dtype, ltype, bint zero_based, bint multilabels):
cdef char * s
cdef char * end
cdef Py_ssize_t idx
cdef Py_ssize_t last_idx
cdef double value
cdef char dt = 'f' if dtype == 'f' else 'd' # defaut is double
cdef char lt = 'd' if ltype == 'd' else 'i' # default is int
cdef array.array data = array.array(dtype)
cdef array.array indices = array.array('i')
cdef array.array indptr = array.array('i', [0])
if not multilabels:
labels = array.array(ltype)
else:
labels = []
cls = float if lt == 'd' else int
cdef Py_ssize_t sz = 0
cdef Py_ssize_t nrows = 0
for line in fp:
s = line
if s[0] == '#':
continue
nrows += 1
# get the label
if not multilabels:
array.resize_smart(labels, nrows)
if lt == 'i':
labels[nrows-1] = strtol(s, &end, 10)
else:
labels[nrows-1] = strtod(s, &end)
if s==end:
raise ValueError('invalid label')
s = end
else:
labl, line = line.split(None, 1)
s = line
label = sorted([cls(val) for val in labl.split(b',')])
labels.append(tuple(label))
# process the line
last_idx = -1
while s[0] != '#' and s[0] != '\n' and s[0] != 0:
# get index
idx = strtol(s, &end, 10)
if s == end:
raise ValueError('invalid index')
if idx < 0 or idx > INT_MAX:
raise ValueError('invalid index (out of range)')
s = end
if not zero_based:
if idx == 0:
raise ValueError('invalid index 0 with one-based indexes')
idx -= 1
if idx <= last_idx:
raise ValueError('indices should be sorted and uniques')
last_idx = idx
# ensure we have correct separator
while s[0] == ' ':
s += 1
if s[0] != ':':
raise ValueError('invalid separator')
s += 1
# get value
value = strtod(s, &end)
if s == end:
raise ValueError('invalid value')
s = end
while s[0] == ' ':
s += 1
sz += 1
array.resize_smart(indices, sz)
array.resize_smart(data, sz)
indices.data.as_ints[sz-1] = idx
if dt == 'd':
data.data.as_doubles[sz-1] = value
else:
data.data.as_floats[sz-1] = value
array.resize_smart(indptr, nrows+1)
indptr.data.as_ints[nrows] = sz
if not multilabels:
labels = np.frombuffer(labels, dtype=ltype)
return (np.frombuffer(data, dtype=dtype),
np.frombuffer(indices, dtype='i'),
np.frombuffer(indptr, dtype='i'),
labels)
def _openfile(filename):
import os.path
_, ext = os.path.splitext(filename)
if ext == ".gz":
import gzip
fp = gzip.open(filename, "rb")
elif ext == ".bz2":
from bz2 import BZ2File
fp = BZ2File(filename, "rb")
else:
fp = open(filename, "rb")
return fp
def load_svmfile(filename, dtype='d', ltype='i', nfeatures=None, zero_based=True, multilabels=False):
"""\
Load a sparse CSR matrix from filename at svmlib format.
Files in .gz or .bz2 format will be uncompressed on the fly.
:param filename: the file name
:type filename: str
:param dtype: type of data, must be either 'd' (double) or 'f' (float)
:type dtype: str
:param ltype: type of labels, must be either 'i' (int) or 'd' (double)
:type ltype: str
:param nfeatures: the number of columns (infered from file if is None)
:type nfeatures: int
:param zero_based: indicates if columns indexes are zero-based or one-based
:type zero_based: bool
:param multilabels: indicates if file uses multiple labels per row
:type multilabels: bool
:returns: (labels, sparse_matrix) tuple
:rtype: (:class:`numpy.ndarray`, :class:`scipy.sparse.csr_matrix`)
"""
assert(dtype=='f' or dtype=='d'), 'dtype must be "d" or "f"'
assert(ltype=='i' or ltype=='d'), 'ltype must be "i" or "d"'
fp = _openfile(filename)
data, indices, indptr, y = _load_svmfile(fp, dtype, ltype, zero_based, multilabels)
fp.close()
if nfeatures is None:
X = sp.csr_matrix((data, indices, indptr), copy=False)
else:
X = sp.csr_matrix((data, indices, indptr), shape=(len(indptr)-1, nfeatures), copy=False)
return X, y
def load_svmfiles(filenames, dtype='d', ltype='i', zero_based=True, multilabels=False):
"""\
Load a sparse CSR matrix list from list of filenames at svmlib format.
Files in .gz or .bz2 format will be uncompressed on the fly.
The number of features will be infered from the maximum indice found
on all files.
:param filenames: the list of files names
:type filenames: list
:param dtype: type of data, must be either 'd' (double) or 'f' (float)
:type dtype: str
:param ltype: type of labels, must be either 'i' (int) or 'd' (double)
:type ltype: str
:param zero_based: indicates if columns indexes are zero-based or one-based
:type zero_based: bool
:param multilabels: indicates if file uses multiple labels per row
:type multilabels: bool
:returns: a list [labels_0, matrix_0, .., labels_n, matrix_n]
"""
assert(dtype=='f' or dtype=='d'), 'dtype must be "d" or "f"'
assert(ltype=='i' or ltype=='d'), 'ltype must be "i" or "d"'
Xlst = []
ylst = []
for filename in filenames:
with _openfile(filename) as fp:
try:
data, indices, indptr, y = _load_svmfile(fp, dtype, ltype, zero_based, multilabels)
except ValueError as err:
err.args = 'in %s, %s' % (filename, err.args[0]),
raise
Xlst.append((data, indices, indptr))
ylst.append(y)
# get nfeatures as the maximum indice
nfeatures = max(max(indices) for _, indices, _ in Xlst) + 1
lst = []
for (data, indices, indptr), y in zip(Xlst, ylst):
X = sp.csr_matrix((data, indices, indptr), shape=(len(indptr)-1, nfeatures), copy=False)
lst.append(X)
lst.append(y)
return lst
@cython.boundscheck(False) # turn off bounds-checking for entire function
@cython.wraparound(False) # turn off negative index wrapping for entire function
def save_svmfile(filename, mat, labels=None, zero_based=False):
"""\
Save a sparse CSR matrix to filename at svmlib format (convenient function).
:param filename: the output filesname
:type filename: str
:param mat: the sparse csr matrix
:type dtype: :class:`scipy.sparse.csr_matrix`
:param labels: the rows labels
:type ltype: indexable
:param zero_based: indicates if columns indexes are zero-based or one-based
:type zero_based: bool
"""
assert isinstance(mat, sp.csr_matrix), 'not a CSR matrix'
cdef cnp.int_t[:] indices = mat.indices
cdef cnp.int_t[:] indptr = mat.indptr
cdef cnp.ndarray data = mat.data
cdef Py_ssize_t i
cdef Py_ssize_t j
cdef Py_ssize_t offset
offset = 0 if zero_based else 1
with open(filename, 'w') as fp:
for i in range(indptr.shape[0] - 1):
if labels is not None:
fp.write('%s ' % labels[i])
row = ' '.join(['%d:%g' % (indices[j] + offset, data[j])
for j in range(indptr[i], indptr[i+1])])
fp.write(row)
fp.write('\n')