forked from pgvector/pgvector-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_sparse_vector.py
More file actions
166 lines (131 loc) · 6.14 KB
/
test_sparse_vector.py
File metadata and controls
166 lines (131 loc) · 6.14 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
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
import numpy as np
from pgvector import SparseVector
import pytest
from scipy.sparse import coo_array, coo_matrix, csr_array, csr_matrix
from struct import pack
class TestSparseVector:
def test_list(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
assert np.array_equal(vec.to_numpy(), [1, 0, 2, 0, 3, 0])
assert vec.indices() == [0, 2, 4]
def test_list_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector([1, 0, 2, 0, 3, 0], 6)
assert str(error.value) == 'extra argument'
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_no_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector({0: 1, 2: 2, 4: 3})
assert str(error.value) == 'missing dimensions'
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]
def test_coo_array_dimensions(self):
with pytest.raises(ValueError) as error:
SparseVector(coo_array(np.array([1, 0, 2, 0, 3, 0])), 6)
assert str(error.value) == 'extra argument'
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]
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]
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]
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]
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]
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]
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
assert vec.to_binary() == data
def test_to_db(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert SparseVector._to_db(vec) == '{1:1.0,3:2.0,5:3.0}/6'
def test_to_db_list(self):
assert SparseVector._to_db([1, 0, 2, 0, 3, 0]) == '{1:1.0,3:2.0,5:3.0}/6'
def test_to_db_none(self):
assert SparseVector._to_db(None) is None
def test_to_db_dim(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert SparseVector._to_db(vec, 6) == '{1:1.0,3:2.0,5:3.0}/6'
def test_to_db_dim_invalid(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
with pytest.raises(ValueError, match='expected 5 dimensions, not 6'):
SparseVector._to_db(vec, 5)
def test_to_db_binary(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
result = SparseVector._to_db_binary(vec)
assert result == vec.to_binary()
def test_to_db_binary_list(self):
result = SparseVector._to_db_binary([1, 0, 2, 0, 3, 0])
assert result == SparseVector([1, 0, 2, 0, 3, 0]).to_binary()
def test_to_db_binary_none(self):
assert SparseVector._to_db_binary(None) is None
def test_from_db_text(self):
result = SparseVector._from_db('{1:1.5,3:2,5:3}/6')
assert result.to_list() == [1.5, 0, 2, 0, 3, 0]
def test_from_db_none(self):
assert SparseVector._from_db(None) is None
def test_from_db_sparsevector(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert SparseVector._from_db(vec) is vec
def test_from_db_binary(self):
data = pack('>iii3i3f', 6, 3, 0, 0, 2, 4, 1.5, 2, 3)
result = SparseVector._from_db_binary(data)
assert result.to_list() == [1.5, 0, 2, 0, 3, 0]
def test_from_db_binary_none(self):
assert SparseVector._from_db_binary(None) is None
def test_from_db_binary_sparsevector(self):
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert SparseVector._from_db_binary(vec) is vec