-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_extract_sparse_array.py
More file actions
93 lines (72 loc) · 3.02 KB
/
Copy pathtest_extract_sparse_array.py
File metadata and controls
93 lines (72 loc) · 3.02 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
import delayedarray
import numpy
import scipy.sparse
def test_extract_sparse_array_csc_matrix():
y = scipy.sparse.random(100, 20, 0.2).tocsc()
# Full:
out = delayedarray.to_sparse_array(y)
assert isinstance(out, delayedarray.SparseNdarray)
assert (numpy.array(out) == y.toarray()).all()
# Consecutive subset.
subs = (range(10, 80), range(5, 15))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y[numpy.ix_(*subs)].toarray()).all()
# Non-consecutive subset.
subs = (range(10, 80, 5), range(5, 15, 2))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y[numpy.ix_(*subs)].toarray()).all()
# Empty.
empty = scipy.sparse.random(100, 20, 0).tocsc()
out = delayedarray.to_sparse_array(empty)
assert (numpy.array(out) == numpy.zeros((100, 20))).all()
# Nosub.
out = delayedarray.extract_sparse_array(y, ([], [1]))
assert out.shape == (0, 1)
out = delayedarray.extract_sparse_array(y, ([0], []))
assert out.shape == (1, 0)
def test_extract_sparse_array_csr_matrix():
y = scipy.sparse.random(100, 20, 0.2).tocsr()
# Full:
out = delayedarray.to_sparse_array(y)
assert isinstance(out, delayedarray.SparseNdarray)
assert (numpy.array(out) == y.toarray()).all()
# Consecutive subset.
subs = (range(10, 80), range(5, 15))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y[numpy.ix_(*subs)].toarray()).all()
# Non-consecutive subset.
subs = (range(10, 80, 5), range(5, 15, 2))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y[numpy.ix_(*subs)].toarray()).all()
# Empty.
empty = scipy.sparse.random(100, 20, 0).tocsr()
out = delayedarray.to_sparse_array(empty)
assert (numpy.array(out) == numpy.zeros((100, 20))).all()
# Nosub.
out = delayedarray.extract_sparse_array(y, ([], [1]))
assert out.shape == (0, 1)
out = delayedarray.extract_sparse_array(y, ([0], []))
assert out.shape == (1, 0)
def test_extract_sparse_array_coo_matrix():
y = scipy.sparse.random(100, 20, 0.2).tocoo()
# Full:
out = delayedarray.to_sparse_array(y)
assert isinstance(out, delayedarray.SparseNdarray)
assert (numpy.array(out) == y.toarray()).all()
# Consecutive subset.
subs = (range(10, 80), range(5, 15))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y.toarray()[numpy.ix_(*subs)]).all()
# Non-consecutive subset.
subs = (range(10, 80, 5), range(5, 15, 2))
out = delayedarray.extract_sparse_array(y, subs)
assert (numpy.array(out) == y.toarray()[numpy.ix_(*subs)]).all()
# Empty.
empty = scipy.sparse.random(100, 20, 0).tocoo()
out = delayedarray.to_sparse_array(empty)
assert (numpy.array(out) == numpy.zeros((100, 20))).all()
# Nosub.
out = delayedarray.extract_sparse_array(y, ([], [1]))
assert out.shape == (0, 1)
out = delayedarray.extract_sparse_array(y, ([0], []))
assert out.shape == (1, 0)