-
Notifications
You must be signed in to change notification settings - Fork 93
Expand file tree
/
Copy pathtest_sparse_vector.py
More file actions
156 lines (123 loc) · 5.81 KB
/
Copy pathtest_sparse_vector.py
File metadata and controls
156 lines (123 loc) · 5.81 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
from pgvector import SparseVector
import pytest
from struct import pack
from .conftest import numpy as np
try:
from scipy import sparse
except ImportError:
sparse = None # type: ignore
class TestSparseVector:
def test_list(self) -> None:
vec = SparseVector([1, 0, 2, 0, 3, 0])
assert vec.to_list() == [1, 0, 2, 0, 3, 0]
if np is not None:
assert np.array_equal(vec.to_numpy(), [1, 0, 2, 0, 3, 0])
assert vec.indices() == [0, 2, 4]
def test_list_empty(self) -> None:
assert SparseVector([]).to_list() == []
def test_list_str(self) -> None:
with pytest.raises(ValueError, match='could not convert string to float'):
SparseVector([1, 'two', 3]) # type: ignore
def test_list_dimensions(self) -> None:
with pytest.raises(ValueError) as error:
SparseVector([1, 0, 2, 0, 3, 0], 6) # type: ignore
assert str(error.value) == 'extra argument'
def test_ndarray(self) -> None:
if np is None:
pytest.skip('NumPy required')
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) -> None:
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) -> None:
assert SparseVector({}, 0).to_list() == []
def test_dict_no_dimensions(self) -> None:
with pytest.raises(ValueError) as error:
SparseVector({0: 1, 2: 2, 4: 3}) # type: ignore
assert str(error.value) == 'missing dimensions'
def test_coo_array(self) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
arr = sparse.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]
assert isinstance(vec.values()[0], float)
def test_coo_array_dimensions(self) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
with pytest.raises(ValueError) as error:
SparseVector(sparse.coo_array(np.array([1, 0, 2, 0, 3, 0])), 6) # type: ignore
assert str(error.value) == 'extra argument'
def test_coo_matrix(self) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
mat = sparse.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) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
arr = sparse.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) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
arr = sparse.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) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
mat = sparse.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) -> None:
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) -> None:
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)
assert SparseVector([1, 0, 2, 0, 3, 0]) != 1
def test_dimensions(self) -> None:
assert SparseVector([1, 0, 2, 0, 3, 0]).dimensions() == 6
def test_indices(self) -> None:
assert SparseVector([1, 0, 2, 0, 3, 0]).indices() == [0, 2, 4]
def test_values(self) -> None:
assert SparseVector([1, 0, 2, 0, 3, 0]).values() == [1, 2, 3]
def test_to_coo(self) -> None:
if np is None or sparse is None:
pytest.skip('NumPy and SciPy required')
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) -> None:
vec = SparseVector({}, 3)
assert vec.to_list() == SparseVector.from_text(vec.to_text()).to_list()
def test_from_text(self) -> None:
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 np is not None:
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
assert vec.to_text() == '{1:1.5,3:2.0,5:3.0}/6'
def test_from_binary(self) -> None:
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 np is not None:
assert np.array_equal(vec.to_numpy(), [1.5, 0, 2, 0, 3, 0])
assert vec.to_binary() == data