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
69 lines (54 loc) · 2.47 KB
/
Copy pathtest_sparse_vector.py
File metadata and controls
69 lines (54 loc) · 2.47 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
import numpy as np
from pgvector.utils import SparseVector
import pytest
from scipy.sparse import coo_array
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 vec.to_numpy().tolist() == [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_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_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_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 SparseVector([1, 0, 2, 0, 3, 0]).to_coo().toarray().tolist() == [[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()