-
Notifications
You must be signed in to change notification settings - Fork 95
Expand file tree
/
Copy pathtest_sparse_vector.py
More file actions
139 lines (114 loc) · 5.3 KB
/
Copy pathtest_sparse_vector.py
File metadata and controls
139 lines (114 loc) · 5.3 KB
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
from pgvector import SparseVector
import pytest
from struct import pack
try:
import numpy as np
NUMPY_AVAILABLE = True
except ImportError:
NUMPY_AVAILABLE = False
try:
from scipy.sparse import coo_array, coo_matrix, csr_array, csr_matrix
SCIPY_AVAILABLE = True
except ImportError:
SCIPY_AVAILABLE = False
class TestSparseVector:
def test_list(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
if NUMPY_AVAILABLE:
assert np.array_equal(vec.to_numpy(), [1, 0, 2, 0, 3, 0])
assert vec.indices() == [0, 2, 4]
def test_list_empty(self):
assert SparseVector([]).to_list() == []
def test_list_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector([1, 0, 2, 0, 3, 0], 6) # ty: ignore[invalid-argument-type]
assert str(error.value) == 'extra argument'
@pytest.mark.skipif(not NUMPY_AVAILABLE, reason='NumPy required')
def test_ndarray(self):
vec = SparseVector(np.array([1, 0, 2, 0, 3, 0]))
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
def test_dict(self):
vec = SparseVector({2: 2, 4: 3, 0: 1, 3: 0}, 6)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
def test_dict_empty(self):
assert SparseVector({}, 0).to_list() == []
def test_dict_no_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector({0: 1, 2: 2, 4: 3})
assert str(error.value) == 'missing dimensions'
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_coo_array(self):
arr = coo_array(np.array([1, 0, 2, 0, 3, 0]))
vec = SparseVector(arr)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_coo_array_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector(coo_array(np.array([1, 0, 2, 0, 3, 0])), 6) # ty: ignore[invalid-argument-type]
assert str(error.value) == 'extra argument'
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_coo_matrix(self):
mat = coo_matrix(np.array([1, 0, 2, 0, 3, 0]))
vec = SparseVector(mat)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_dok_array(self):
arr = coo_array(np.array([1, 0, 2, 0, 3, 0])).todok()
vec = SparseVector(arr)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_csr_array(self):
arr = csr_array(np.array([[1, 0, 2, 0, 3, 0]]))
vec = SparseVector(arr)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
@pytest.mark.skipif(not SCIPY_AVAILABLE, reason='SciPy required')
def test_csr_matrix(self):
mat = csr_matrix(np.array([1, 0, 2, 0, 3, 0]))
vec = SparseVector(mat)
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert vec.indices() == [0, 2, 4]
def test_repr(self):
assert repr(SparseVector([1, 0, 2, 0, 3, 0])) == 'SparseVector({0: 1.0, 2: 2.0, 4: 3.0}, 6)'
assert str(SparseVector([1, 0, 2, 0, 3, 0])) == 'SparseVector({0: 1.0, 2: 2.0, 4: 3.0}, 6)'
def test_equality(self):
assert SparseVector([1, 0, 2, 0, 3, 0]) == SparseVector([1, 0, 2, 0, 3, 0])
assert SparseVector([1, 0, 2, 0, 3, 0]) != SparseVector([1, 0, 2, 0, 3, 1])
assert SparseVector([1, 0, 2, 0, 3, 0]) == SparseVector({2: 2, 4: 3, 0: 1, 3: 0}, 6)
assert SparseVector({}, 1) != SparseVector({}, 2)
def test_dimensions(self):
assert SparseVector([1, 0, 2, 0, 3, 0]).dimensions() == 6
def test_indices(self):
assert SparseVector([1, 0, 2, 0, 3, 0]).indices() == [0, 2, 4]
def test_values(self):
assert SparseVector([1, 0, 2, 0, 3, 0]).values() == [1, 2, 3]
@pytest.mark.skipif(not NUMPY_AVAILABLE or not SCIPY_AVAILABLE, reason='NumPy and SciPy required')
def test_to_coo(self):
assert np.array_equal(SparseVector([1, 0, 2, 0, 3, 0]).to_coo().toarray(), [[1, 0, 2, 0, 3, 0]])
def test_zero_vector_text(self):
vec = SparseVector({}, 3)
assert vec.to_list() == SparseVector.from_text(vec.to_text()).to_list()
def test_from_text(self):
vec = SparseVector.from_text('{1:1.5,3:2,5:3}/6')
assert vec.dimensions() == 6
assert vec.indices() == [0, 2, 4]
assert vec.values() == [1.5, 2, 3]
assert vec.to_list() == [1.5, 0, 2, 0, 3, 0]
if NUMPY_AVAILABLE:
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
def test_from_binary(self):
data = pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3)
vec = SparseVector.from_binary(data)
assert vec.dimensions() == 6
assert vec.indices() == [0, 2, 4]
assert vec.values() == [1.5, 2, 3]
assert vec.to_list() == [1.5, 0, 2, 0, 3, 0]
if NUMPY_AVAILABLE:
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
assert vec.to_binary() == data