-
Notifications
You must be signed in to change notification settings - Fork 58
/
test_matrix_csr.py
366 lines (273 loc) · 10.4 KB
/
test_matrix_csr.py
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
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
import pickle
import numpy as np
import scipy.sparse as sps
import lenskit.matrix as lm
from lenskit.util.test import rand_csr
from pytest import mark, approx, raises
@mark.parametrize('copy', [True, False])
def test_csr_from_sps(copy):
# initialize sparse matrix
mat = np.random.randn(10, 5)
mat[mat <= 0] = 0
smat = sps.csr_matrix(mat)
# make sure it's sparse
assert smat.nnz == np.sum(mat > 0)
csr = lm.CSR.from_scipy(smat, copy=copy)
assert csr.nnz == smat.nnz
assert csr.nrows == smat.shape[0]
assert csr.ncols == smat.shape[1]
assert all(csr.rowptrs == smat.indptr)
assert all(csr.colinds == smat.indices)
assert all(csr.values == smat.data)
assert isinstance(csr.rowptrs, np.ndarray)
assert isinstance(csr.colinds, np.ndarray)
assert isinstance(csr.values, np.ndarray)
def test_csr_is_numpy_compatible():
# initialize sparse matrix
mat = np.random.randn(10, 5)
mat[mat <= 0] = 0
smat = sps.csr_matrix(mat)
# make sure it's sparse
assert smat.nnz == np.sum(mat > 0)
csr = lm.CSR.from_scipy(smat)
d2 = csr.values * 10
assert d2 == approx(smat.data * 10)
def test_csr_from_coo():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
assert csr.nrows == 4
assert csr.ncols == 3
assert csr.nnz == 4
assert csr.values == approx(vals)
def test_csr_rowinds():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
ris = csr.rowinds()
assert all(ris == rows)
def test_csr_set_values():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
v2 = np.random.randn(4)
csr.values = v2
assert all(csr.values == v2)
def test_csr_set_values_oversize():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
v2 = np.random.randn(6)
csr.values = v2
assert all(csr.values == v2[:4])
def test_csr_set_values_undersize():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
v2 = np.random.randn(3)
with raises(ValueError):
csr.values = v2
assert all(csr.values == vals)
def test_csr_set_values_none():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
csr.values = None
assert csr.values is None
assert all(csr.row(0) == [0, 1, 1])
assert all(csr.row(1) == [1, 0, 0])
assert all(csr.row(3) == [0, 1, 0])
def test_csr_str():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
assert '4x3' in str(csr)
def test_csr_row():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_) + 1
csr = lm.CSR.from_coo(rows, cols, vals)
assert all(csr.row(0) == np.array([0, 1, 2], dtype=np.float_))
assert all(csr.row(1) == np.array([3, 0, 0], dtype=np.float_))
assert all(csr.row(2) == np.array([0, 0, 0], dtype=np.float_))
assert all(csr.row(3) == np.array([0, 4, 0], dtype=np.float_))
def test_csr_sparse_row():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
assert all(csr.row_cs(0) == np.array([1, 2], dtype=np.int32))
assert all(csr.row_cs(1) == np.array([0], dtype=np.int32))
assert all(csr.row_cs(2) == np.array([], dtype=np.int32))
assert all(csr.row_cs(3) == np.array([1], dtype=np.int32))
assert all(csr.row_vs(0) == np.array([0, 1], dtype=np.float_))
assert all(csr.row_vs(1) == np.array([2], dtype=np.float_))
assert all(csr.row_vs(2) == np.array([], dtype=np.float_))
assert all(csr.row_vs(3) == np.array([3], dtype=np.float_))
def test_csr_transpose():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
csc = csr.transpose()
assert csc.nrows == csr.ncols
assert csc.ncols == csr.nrows
assert all(csc.rowptrs == [0, 1, 3, 4])
assert csc.colinds.max() == 3
assert csc.values.sum() == approx(vals.sum())
for r, c, v in zip(rows, cols, vals):
row = csc.row(c)
assert row[r] == v
def test_csr_transpose_coords():
rows = np.array([0, 0, 1, 3], dtype=np.int32)
cols = np.array([1, 2, 0, 1], dtype=np.int32)
vals = np.arange(4, dtype=np.float_)
csr = lm.CSR.from_coo(rows, cols, vals)
csc = csr.transpose(False)
assert csc.nrows == csr.ncols
assert csc.ncols == csr.nrows
assert all(csc.rowptrs == [0, 1, 3, 4])
assert csc.colinds.max() == 3
assert csc.values is None
for r, c, v in zip(rows, cols, vals):
row = csc.row(c)
assert row[r] == 1
def test_csr_transpose_many():
for i in range(50):
mat = np.random.randn(100, 50)
mat[mat <= 0] = 0
smat = sps.csr_matrix(mat)
csr = lm.CSR.from_scipy(smat)
csrt = csr.transpose()
assert csrt.nrows == 50
assert csrt.ncols == 100
s2 = csrt.to_scipy()
smat = smat.T.tocsr()
assert all(smat.indptr == csrt.rowptrs)
assert np.all(s2.toarray() == smat.toarray())
def test_csr_row_nnzs():
# initialize sparse matrix
mat = np.random.randn(10, 5)
mat[mat <= 0] = 0
smat = sps.csr_matrix(mat)
# make sure it's sparse
assert smat.nnz == np.sum(mat > 0)
csr = lm.CSR.from_scipy(smat)
nnzs = csr.row_nnzs()
assert nnzs.sum() == csr.nnz
for i in range(10):
row = mat[i, :]
assert nnzs[i] == np.sum(row > 0)
def test_csr_from_coo_rand():
for i in range(100):
coords = np.random.choice(np.arange(50 * 100, dtype=np.int32), 1000, False)
rows = np.mod(coords, 100, dtype=np.int32)
cols = np.floor_divide(coords, 100, dtype=np.int32)
vals = np.random.randn(1000)
csr = lm.CSR.from_coo(rows, cols, vals, (100, 50))
rowinds = csr.rowinds()
assert csr.nrows == 100
assert csr.ncols == 50
assert csr.nnz == 1000
for i in range(100):
sp = csr.rowptrs[i]
ep = csr.rowptrs[i+1]
assert ep - sp == np.sum(rows == i)
points, = np.nonzero(rows == i)
assert len(points) == ep - sp
po = np.argsort(cols[points])
points = points[po]
assert all(np.sort(csr.colinds[sp:ep]) == cols[points])
assert all(np.sort(csr.row_cs(i)) == cols[points])
assert all(csr.values[np.argsort(csr.colinds[sp:ep]) + sp] == vals[points])
assert all(rowinds[sp:ep] == i)
row = np.zeros(50)
row[cols[points]] = vals[points]
assert np.sum(csr.row(i)) == approx(np.sum(vals[points]))
assert all(csr.row(i) == row)
def test_csr_from_coo_novals():
for i in range(50):
coords = np.random.choice(np.arange(50 * 100, dtype=np.int32), 1000, False)
rows = np.mod(coords, 100, dtype=np.int32)
cols = np.floor_divide(coords, 100, dtype=np.int32)
csr = lm.CSR.from_coo(rows, cols, None, (100, 50))
assert csr.nrows == 100
assert csr.ncols == 50
assert csr.nnz == 1000
for i in range(100):
sp = csr.rowptrs[i]
ep = csr.rowptrs[i+1]
assert ep - sp == np.sum(rows == i)
points, = np.nonzero(rows == i)
po = np.argsort(cols[points])
points = points[po]
assert all(np.sort(csr.colinds[sp:ep]) == cols[points])
assert np.sum(csr.row(i)) == len(points)
def test_csr_to_sps():
# initialize sparse matrix
mat = np.random.randn(10, 5)
mat[mat <= 0] = 0
# get COO
smat = sps.coo_matrix(mat)
# make sure it's sparse
assert smat.nnz == np.sum(mat > 0)
csr = lm.CSR.from_coo(smat.row, smat.col, smat.data, shape=smat.shape)
assert csr.nnz == smat.nnz
assert csr.nrows == smat.shape[0]
assert csr.ncols == smat.shape[1]
smat2 = csr.to_scipy()
assert sps.isspmatrix(smat2)
assert sps.isspmatrix_csr(smat2)
for i in range(csr.nrows):
assert smat2.indptr[i] == csr.rowptrs[i]
assert smat2.indptr[i+1] == csr.rowptrs[i+1]
sp = smat2.indptr[i]
ep = smat2.indptr[i+1]
assert all(smat2.indices[sp:ep] == csr.colinds[sp:ep])
assert all(smat2.data[sp:ep] == csr.values[sp:ep])
def test_mean_center():
for n in range(50):
csr = rand_csr()
spm = csr.to_scipy().copy()
m2 = csr.normalize_rows('center')
assert len(m2) == 100
for i in range(csr.nrows):
vs = csr.row_vs(i)
if len(vs) > 0:
assert np.mean(vs) == approx(0.0)
assert vs + m2[i] == approx(spm.getrow(i).toarray()[0, csr.row_cs(i)])
def test_unit_norm():
for n in range(50):
csr = rand_csr()
spm = csr.to_scipy().copy()
m2 = csr.normalize_rows('unit')
assert len(m2) == 100
for i in range(csr.nrows):
vs = csr.row_vs(i)
if len(vs) > 0:
assert np.linalg.norm(vs) == approx(1.0)
assert vs * m2[i] == approx(spm.getrow(i).toarray()[0, csr.row_cs(i)])
@mark.parametrize("values", [True, False])
def test_csr_pickle(values):
csr = rand_csr(100, 50, 1000, values=values)
assert csr.nrows == 100
assert csr.ncols == 50
assert csr.nnz == 1000
data = pickle.dumps(csr)
csr2 = pickle.loads(data)
assert csr2.nrows == csr.nrows
assert csr2.ncols == csr.ncols
assert csr2.nnz == csr.nnz
assert all(csr2.rowptrs == csr.rowptrs)
assert all(csr2.colinds == csr.colinds)
if values:
assert all(csr2.values == csr.values)
else:
assert csr2.values is None