-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathIncrementalSparseMatrix_test.py
106 lines (48 loc) · 2.1 KB
/
IncrementalSparseMatrix_test.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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on 09/09/2018
@author: Maurizio Ferrari Dacrema
"""
import unittest
import scipy.sparse as sps
from Data_manager.IncrementalSparseMatrix import IncrementalSparseMatrix
def sparse_are_equals(A, B):
if A.shape != B.shape:
return False
return (A!=B).nnz==0
def random_string():
import string
from random import choice, randint
min_char = 8
max_char = 12
allchar = string.ascii_letters + string.digits + string.punctuation
randomstring = "".join(choice(allchar) for x in range(randint(min_char, max_char)))
return randomstring
class MyTestCase(unittest.TestCase):
def test_IncrementalSparseMatrix_add_lists(self):
n_rows = 100
n_cols = 200
randomMatrix = sps.random(n_rows, n_cols, density=0.01, format='coo')
incrementalMatrix = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)
incrementalMatrix.add_data_lists(randomMatrix.row.copy(),
randomMatrix.col.copy(),
randomMatrix.data.copy())
randomMatrix_incremental = incrementalMatrix.get_SparseMatrix()
assert sparse_are_equals(randomMatrix, randomMatrix_incremental)
def test_IncrementalSparseMatrix_add_rows(self):
import numpy as np
n_rows = 100
n_cols = 200
randomMatrix = sps.random(n_rows, n_cols, density=0.01, format='csr')
incrementalMatrix = IncrementalSparseMatrix(n_rows=n_rows, n_cols=n_cols)
for row in range(n_rows):
row_data = randomMatrix.indices[randomMatrix.indptr[row]:randomMatrix.indptr[row+1]]
incrementalMatrix.add_single_row(row,
row_data,
5.0)
randomMatrix.data = np.ones_like(randomMatrix.data)*5.0
randomMatrix_incremental = incrementalMatrix.get_SparseMatrix()
assert sparse_are_equals(randomMatrix, randomMatrix_incremental)
if __name__ == '__main__':
unittest.main()