-
Notifications
You must be signed in to change notification settings - Fork 16
Expand file tree
/
Copy pathtest_resolving.py
More file actions
297 lines (253 loc) · 10.7 KB
/
Copy pathtest_resolving.py
File metadata and controls
297 lines (253 loc) · 10.7 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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
import numpy as np
import pytest
from graphblas import backend, binary, dtypes, replace, unary
from graphblas.core.expr import Updater
from graphblas.core.utils import ensure_type
from graphblas import Matrix, Scalar, Vector # isort:skip (for dask-graphblas)
suitesparse = backend == "suitesparse"
def test_from_coo_dtype_resolving():
u = Vector.from_coo([0, 1, 2], [1, 2, 3], dtype=dtypes.INT32)
assert u.dtype == "INT32"
u = Vector.from_coo([0, 1, 2], [1, 2, 3], dtype="INT32")
assert u.dtype == dtypes.INT32
M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8)
assert M.dtype == "UINT8"
M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=float)
assert M.dtype == dtypes.FP64
def test_from_coo_invalid_dtype():
# We now rely on numpy to coerce the data
A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.BOOL)
expected = Matrix.from_coo([0, 1, 2], [2, 0, 1], [False, True, True], dtype=dtypes.BOOL)
assert A.isequal(expected)
with pytest.raises(ValueError, match="object dtype for values is not allowed"):
Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, object()])
def test_resolve_ops_using_common_dtype():
# C << A.ewise_mult(B, binary.plus) <-- PLUS should use FP64 because unify(INT64, FP64) -> FP64
u = Vector.from_coo([0, 1, 3], [1, 2, 3], dtype=dtypes.INT64)
v = Vector.from_coo([0, 1, 3], [0.1, 0.1, 0.1], dtype="FP64")
w = Vector("FP32", u.size)
w << u.ewise_mult(v, binary.plus)
result = Vector.from_coo([0, 1, 3], [1.1, 2.1, 3.1], dtype="FP32")
assert w.isclose(result, check_dtype=True)
def test_order_of_updater_params_does_not_matter():
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
mask = Vector.from_coo([0, 3], [True, True])
accum = binary.plus
result = Vector.from_coo([0, 3], [5, 10])
# mask, accum, replace=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(mask.V, accum, replace=True) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
# accum, mask, replace=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(accum, mask.V, replace=True) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
# accum, mask=, replace=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(accum, mask=mask.V, replace=True) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
# mask, accum=, replace=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(mask.V, accum=accum, replace=True) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
# replace=, mask=, accum=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(replace=True, mask=mask.V, accum=accum) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
# replace, mask=, accum=
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
v(replace, mask=mask.V, accum=accum) << u.ewise_mult(u, binary.times)
assert v.isequal(result)
def test_updater_replace_no_mask():
u = Vector.from_coo([0, 1, 2], [1, 2, 3])
with pytest.raises(
TypeError, match="'replace' argument may only be True if a mask is provided"
):
u(replace=True)
with pytest.raises(
TypeError, match="'replace' argument may only be True if a mask is provided"
):
u(replace)
def test_replace_repr():
assert repr(replace) == "replace"
assert str(replace) == "replace"
def test_updater_repeat_argument_types():
mask = Vector.from_coo([0, 3], [True, True])
accum = binary.plus
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
with pytest.raises(TypeError, match="multiple"):
v(mask.S, mask.S)
with pytest.raises(TypeError, match="multiple"):
v(mask.S, mask=mask.S)
with pytest.raises(TypeError, match="multiple"):
v(accum, accum)
with pytest.raises(TypeError, match="multiple"):
v(accum, accum=accum)
def test_updater_bad_types():
v = Vector.from_coo([0, 1, 2, 3], [4, 3, 2, 1])
M = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], dtype=dtypes.UINT8)
with pytest.raises(TypeError, match="Invalid mask"):
v(mask=object())
with pytest.raises(TypeError, match="Invalid mask"):
v[[1, 2]].new(mask=object())
with pytest.raises(TypeError, match="Mask object must be type Vector"):
v.ewise_mult(v).new(mask=M.S)
with pytest.raises(TypeError, match="Invalid"):
v(object())
with pytest.raises(TypeError, match="Expected type: BinaryOp"):
v(unary.one)
def test_already_resolved_ops_allowed_in_updater():
# C(binary.plus['FP64']) << ...
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
u(binary.plus["INT64"]) << u.ewise_mult(u, binary.times["INT64"])
result = Vector.from_coo([0, 1, 3], [2, 6, 12])
assert u.isequal(result)
def test_updater_returns_updater():
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
y = u(accum=binary.times)
assert isinstance(y, Updater)
z = y << u.apply(unary.ainv)
assert z is None
assert isinstance(y, Updater)
final_result = Vector.from_coo([0, 1, 3], [-1, -4, -9])
assert u.isequal(final_result)
def test_updater_only_once():
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
with pytest.raises(TypeError, match="'Assigner' object is not callable"):
u()[0]()
with pytest.raises(TypeError, match="'Assigner' object is not callable"):
u(mask=u.S)[0]()
with pytest.raises(TypeError, match="'Assigner' object is not callable"):
u(accum=binary.plus)[0]()
with pytest.raises(TypeError, match="not callable"):
u()()
with pytest.raises(TypeError, match="'Assigner' object is not callable"):
u[[0, 1]]()()
# While we're at it...
with pytest.raises(TypeError, match="is not subscriptable"):
u[[0, 1]]()[0]
with pytest.raises(TypeError, match="is not subscriptable"):
u()[[0, 1]][0]
with pytest.raises(TypeError, match="autocompute"):
u[[0, 1]][0]
def test_bad_extract_with_updater():
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
assert u[0].new() == 1
with pytest.raises(AttributeError, match="'Assigner' object has no attribute 'value'"):
u(mask=u.S)[0].value
with pytest.raises(AttributeError, match="has no attribute"):
u[[0, 1]].value
with pytest.raises(AttributeError, match="'Assigner' object has no attribute 'new'"):
u(mask=u.S)[0].new()
with pytest.raises(TypeError, match="Assignment value must be a valid expression"):
u << u(mask=u.S)[[1, 2]]
with pytest.raises(TypeError, match="Assignment value must be a valid expression"):
u << u()[[1, 2]]
with pytest.raises(ValueError, match="escriptor"):
u[0].new(mask=u.S)
s = Scalar.from_value(10)
with pytest.raises(TypeError, match="Indexing not supported for Scalars"):
del s()[0]
with pytest.raises(TypeError, match="Indexing not supported for Scalars"):
s()[0] = 1
with pytest.raises(TypeError, match="Indexing not supported for Scalars"):
s()[0]
def test_updater_on_rhs():
u = Vector.from_coo([0, 1, 3], [1, 2, 3])
with pytest.raises(TypeError, match="Assignment value must be a valid expression"):
u << u(mask=u.S)
with pytest.raises(TypeError, match="Assignment value must be a valid expression"):
u << u()
with pytest.raises(TypeError, match="Bad type for argument `value`"):
u[:] << u()
def test_py_indices():
v = Vector.from_coo(np.arange(5), np.arange(5))
idx = v[:].resolved_indexes.py_indices
assert idx == slice(None)
w = v[idx].new()
assert w.isequal(v)
idx = v[3].resolved_indexes.py_indices
assert idx == 3
idx = v[[0, 2]].resolved_indexes.py_indices
np.testing.assert_array_equal(idx, [0, 2])
idx = v[0:0].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(1, 1) # strange, but expected
else:
np.testing.assert_array_equal(idx, [])
w = v[idx].new()
assert w.size == 0
idx = v[2:0].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(2, 1) # strange, but expected
else:
np.testing.assert_array_equal(idx, [])
w = v[idx].new()
assert w.size == 0
idx = v[::-1].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(None, None, -1)
else:
np.testing.assert_array_equal(idx, list(range(5))[::-1])
w = v[idx].new()
expected = Vector.from_coo(np.arange(5), np.arange(5)[::-1])
assert w.isequal(expected)
idx = v[4:-3:-1].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(None, -v.size - 1 + 3, -1)
else:
np.testing.assert_array_equal(idx, list(range(5))[4:-3:-1])
w = v[idx].new()
expected = Vector.from_coo(np.arange(2), np.arange(5)[4:-3:-1])
assert w.isequal(expected)
idx = v[1::2].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(1, None, 2)
else:
np.testing.assert_array_equal(idx, list(range(5))[1::2])
w = v[idx].new()
expected = Vector.from_coo(np.arange(2), np.arange(5)[1::2])
assert w.isequal(expected)
A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3], nrows=10, ncols=10)
idx = A[1:6, 8:-8:-2].resolved_indexes.py_indices
if suitesparse:
assert idx == (slice(1, 6), slice(8, -8, -2))
else:
np.testing.assert_array_equal(idx[0], list(range(10))[1:6])
np.testing.assert_array_equal(idx[1], list(range(10))[8:-8:-2])
# start=0 -> None
idx = v[0:2].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(None, 2)
else:
np.testing.assert_array_equal(idx, list(range(5))[0:2])
# stop=size -> None
idx = v[1 : v.size].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(1, None)
else:
np.testing.assert_array_equal(idx, list(range(5))[1 : v.size])
# step=1 -> None
idx = v[1:3:1].resolved_indexes.py_indices
if suitesparse:
assert idx == slice(1, 3)
else:
np.testing.assert_array_equal(idx, list(range(5))[1:3:1])
# All together now!
idx = v[0 : v.size : 1].resolved_indexes.py_indices
assert idx == slice(None)
def test_ensure_type():
v = Vector.from_coo([0, 1, 2], [1, 2, 3])
A = Matrix.from_coo([0, 1, 2], [2, 0, 1], [0, 2, 3])
assert ensure_type(v, Vector) is v
assert ensure_type(A, Matrix) is A
assert ensure_type(v, (Matrix, Vector)) is v
assert ensure_type(A, (Matrix, Vector)) is A
with pytest.raises(TypeError):
ensure_type(A, Vector)
assert ensure_type(v + 1, Vector).isequal((v + 1).new())
assert ensure_type(v + 1, (Vector,)).isequal((v + 1).new())
assert ensure_type(A.mxm(A), Matrix).isequal(A.mxm(A).new())
with pytest.raises(TypeError):
ensure_type(A.mxm(A), (Vector,))
ensure_type(4, int) # why not