Repository navigation
Expand file tree
/
Copy pathtest_binsparse.py
More file actions
64 lines (54 loc) · 1.75 KB
/
Copy pathtest_binsparse.py
File metadata and controls
64 lines (54 loc) · 1.75 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
import pytest
import numpy as np
from binsparse import COORMatrix, DMATRMatrix
from binsparse.conversions import from_numpy, to_numpy
from saps_framework.binsparse_utils import binsparse_equal
@pytest.mark.parametrize("array", [np.array([1, 2, 3]), np.array([[1, 2], [3, 4]])])
def test_binsparse_numpy(array):
x = from_numpy(array)
assert np.array_equal(to_numpy(x), array)
assert x.shape == array.shape
assert binsparse_equal(from_numpy(array.copy()), x)
@pytest.mark.parametrize(
"indices, values, shape",
[((np.array([1, 2, 3]), np.array([0, 1, 2])), np.array([0.1, 0.2, 0.3]), (4, 3))],
)
def test_binsparse_coo(indices, values, shape):
x = COORMatrix(
shape,
len(values),
indices_0=indices[0],
indices_1=indices[1],
values=values,
)
assert np.array_equal(x.indices_0, indices[0])
assert np.array_equal(x.indices_1, indices[1])
assert len(x.shape) == len(indices) == len(shape)
assert binsparse_equal(
COORMatrix(
shape,
len(values),
indices_0=indices[0].copy(),
indices_1=indices[1].copy(),
values=values.copy(),
),
x,
)
def test_binsparse_equal_compares_sparse_and_dense_formats_by_value():
sparse_tensor = COORMatrix(
(3, 3),
3,
indices_0=np.array([0, 1, 2]),
indices_1=np.array([0, 2, 1]),
values=np.array([1.0, 2.0, 3.0], dtype=np.float32),
)
dense_tensor = DMATRMatrix(
(3, 3),
9,
values=np.array(
[1.0, 0.0, 0.0, 0.0, 0.0, 2.0, 0.0, 3.0, 0.0],
dtype=np.float32,
),
)
assert binsparse_equal(sparse_tensor, dense_tensor)
assert binsparse_equal(dense_tensor, sparse_tensor)