forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_expression.py
More file actions
264 lines (233 loc) · 10.3 KB
/
Copy pathtest_expression.py
File metadata and controls
264 lines (233 loc) · 10.3 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
import pytest
import iarray as ia
import numpy
import numpy as np
# Expression
@pytest.mark.parametrize("method, shape, chunkshape, blockshape, dtype, expression", [
(ia.EVAL_ITERBLOSC, [100, 100], [23, 32], [10, 10], np.float64, "cos(x)"), # TODO: fix this
(ia.EVAL_ITERBLOSC, [100, 100], [10, 99], [4, 12], np.float64, "x"),
(ia.EVAL_ITERBLOSC, [1000], [110], [55], np.float32, "x"),
(ia.EVAL_ITERBLOSC, [1000], [100], [30], np.float64, "(cos(x) - 1.35) * (sin(x) - 4.45) * tan(x - 8.5)"),
(ia.EVAL_AUTO, [1000], [100], [25], np.float64, "(cos(x) - 1.35) * (sin(x) - 4.45) * tan(x - 8.5)"),
(ia.EVAL_ITERCHUNK, [1000], [367], [77], np.float32, "(abs(-x) - 1.35) * ceil(x) * floor(x - 8.5)"),
(ia.EVAL_ITERBLOSC, [100, 100, 100], [25, 25, 33], [12, 16, 8], np.float64,
"sinh(x) + (cosh(x) - 1.35) - tanh(x + .2)"),
(ia.EVAL_ITERBLOSC, [223], [100], [30], np.float64, "sinh(x) + (cosh(x) - 1.35) - tanh(x + .2)"),
(ia.EVAL_ITERCHUNK, [100, 100, 55], [10, 5, 10], [3, 4, 3], np.float64,
"asin(x) + (acos(x) - 1.35) - atan(x + .2)"),
(ia.EVAL_ITERCHUNK, [100, 100, 55], [10, 5, 10], [3, 4, 3], np.float64,
"arcsin(x) + (arccos(x) - 1.35) - arctan(x + .2)"), # check NumPy naming convention for ufuncs
(ia.EVAL_AUTO, [1000], None, None, np.float64, "exp(x) + (log(x) - 1.35) - log10(x + .2)"),
(ia.EVAL_ITERCHUNK, [1000], None, None, np.float32, "sqrt(x) + atan2(x, x) + pow(x, x)"),
(ia.EVAL_AUTO, [1000], None, None, np.float32, "sqrt(x) + arctan2(x, x) + power(x, x)"), # NumPy conventions
(ia.EVAL_AUTO, [100, 100], None, None, np.float64, "(x - cos(1)) * 2"),
(ia.EVAL_ITERCHUNK, [8, 6, 7, 4, 5], None, None, np.float32,
"(x - cos(y)) * (sin(x) + y) + 2 * x + y"),
(ia.EVAL_ITERBLOSC, [17, 12, 15, 15, 8], [8, 6, 7, 4, 5], [4, 3, 3, 4, 5], np.float64,
"(x - cos(y)) * (sin(x) + y) + 2 * x + y"),
])
def test_expression(method, shape, chunkshape, blockshape, dtype, expression):
# The ranges below are important for not overflowing operations
if chunkshape is None:
storage = ia.StorageProperties(backend="plainbuffer")
else:
storage = ia.StorageProperties(backend="blosc",
chunkshape=chunkshape,
blockshape=blockshape,
enforce_frame=False,
filename=None)
eval_method = method
x = ia.linspace(ia.dtshape(shape, dtype), 2.1, .2, storage=storage)
y = ia.linspace(ia.dtshape(shape, dtype), 0, 1, storage=storage)
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
expr = ia.Expr(eval_method=eval_method)
expr.bind("x", x)
expr.bind("y", y)
expr.bind_out_properties(ia.dtshape(shape, dtype), storage=storage)
expr.compile(expression)
iout = expr.eval()
npout = ia.iarray2numpy(iout)
# Evaluate using a different engine
ufunc_repls = {
"asin": "arcsin",
"acos": "arccos",
"atan": "arctan",
"atan2": "arctan2",
"pow": "power",
}
for ufunc in ufunc_repls.keys():
if ufunc in expression:
if ufunc == "pow" and "power" in expression:
# Don't do a replacement twice
break
expression = expression.replace(ufunc, ufunc_repls[ufunc])
for ufunc in ia.UFUNC_LIST:
if ufunc in expression:
idx = expression.find(ufunc)
# Prevent replacing an ufunc with np.ufunc twice (not terribly solid, but else, test will crash)
if "np." not in expression[idx - len("np.arc"):idx]:
expression = expression.replace(ufunc + "(", "np." + ufunc + "(")
npout2 = eval(expression, {"x": npx, "y": npy, "np": numpy})
decimal = 5 if dtype is np.float32 else 10
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)
# ufuncs
@pytest.mark.parametrize("ufunc, ia_expr", [
("abs(x)", "abs(x)"),
("arccos(x)", "acos(x)"),
("arcsin(x)", "asin(x)"),
("arctan(x)", "atan(x)"),
("arctan2(x, y)", "atan2(x, y)"),
("ceil(x)", "ceil(x)"),
("cos(x)", "cos(x)"),
("cosh(x)", "cosh(x)"),
("exp(x)", "exp(x)"),
("floor(x)", "floor(x)"),
("log(x)", "log(x)"),
("log10(x)", "log10(x)"),
# ("negative(x)", "negate(x)"),
("power(x, y)", "pow(x, y)"),
("sin(x)", "sin(x)"),
("sinh(x)", "sinh(x)"),
("sqrt(x)", "sqrt(x)"),
("tan(x)", "tan(x)"),
("tanh(x)", "tanh(x)"),
])
def test_ufuncs(ufunc, ia_expr):
shape = [200, 300]
chunkshape = [40, 40]
bshape = [10, 17]
eval_method = ia.EVAL_ITERCHUNK
if chunkshape is None:
storage = ia.StorageProperties(backend="plainbuffer")
else:
storage = ia.StorageProperties(backend="blosc",
chunkshape=chunkshape,
blockshape=bshape,
enforce_frame=False,
filename=None)
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
x = ia.linspace(ia.dtshape(shape, dtype), .1, .9, storage=storage)
y = ia.linspace(ia.dtshape(shape, dtype), 0, 1, storage=storage)
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
# Low-level ironarray eval
expr = ia.Expr(eval_method=eval_method)
expr.bind("x", x)
expr.bind("y", y)
expr.bind_out_properties(ia.dtshape(shape, dtype), storage=storage)
expr.compile(ia_expr)
iout = expr.eval()
npout = ia.iarray2numpy(iout)
decimal = 6 if dtype is np.float32 else 7
# High-level ironarray eval
lazy_expr = eval("ia." + ufunc, {"ia": ia, "x": x, "y": y})
iout2 = lazy_expr.eval(eval_method=eval_method, dtype=dtype)
npout2 = ia.iarray2numpy(iout2)
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)
# High-level ironarray eval, but via numpy ufunc machinery
# TODO: the next ufuncs still have some problems with the numpy machinery (bug?)
# abs(x) : TypeError: bad operand type for abs(): 'IArray'
# ceil(x) : TypeError: must be real number, not IArray
# floor(x): TypeError: must be real number, not IArray
# negative(x) : TypeError: bad operand type for unary -: 'IArray'
# power(x,y) : TypeError: unsupported operand type(s) for ** or pow(): 'IArray' and 'IArray'
if ufunc not in ("abs(x)", "ceil(x)", "floor(x)", "negative(x)", "power(x, y)"):
lazy_expr = eval("np." + ufunc, {"np": np, "x": x, "y": y})
iout2 = lazy_expr.eval(eval_method=eval_method, dtype=dtype)
npout2 = ia.iarray2numpy(iout2)
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)
npout2 = eval("np." + ufunc, {"np": np, "x": npx, "y": npy}) # pure numpy
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)
# ufuncs inside of expressions
@pytest.mark.parametrize("ufunc", [
"abs",
"arccos",
"arcsin",
"arctan",
"arctan2",
"ceil",
"cos",
"cosh",
"exp",
"floor",
"log",
"log10",
# "negative",
"power",
"sin",
"sinh",
"sqrt",
"tan",
"tanh",
])
def test_expr_ufuncs(ufunc):
shape = [200, 300]
chunkshape = [40, 50]
bshape = [20, 20]
eval_method = ia.EVAL_ITERCHUNK
storage = ia.StorageProperties(backend="blosc",
chunkshape=chunkshape,
blockshape=bshape,
enforce_frame=False,
filename=None)
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
x = ia.linspace(ia.dtshape(shape, dtype), .1, .9, storage=storage)
y = ia.linspace(ia.dtshape(shape, dtype), 0, 1, storage=storage)
# NumPy computation
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
if ufunc in ("arctan2", "power"):
npout = eval("1 + 2 * np.%s(x, y)" % ufunc, {"np": np, "x": npx, "y": npy})
else:
npout = eval("1 + 2 * np.%s(x)" % ufunc, {"np": np, "x": npx})
# High-level ironarray eval
if ufunc in ("arctan2", "power"):
lazy_expr = eval("1 + 2* x.%s(y)" % ufunc, {"x": x, "y": y})
else:
lazy_expr = eval("1 + 2 * x.%s()" % ufunc, {"x": x})
iout2 = lazy_expr.eval(eval_method=eval_method, dtype=dtype)
npout2 = ia.iarray2numpy(iout2)
decimal = 6 if dtype is np.float32 else 7
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)
# Different operand fusions inside expressions
@pytest.mark.parametrize("expr", [
"x + y",
"(x + y) + z",
"(x + y) * (x + z)",
"(x + y + z) * (x + z)",
"(x + y - z) * (x + y + t)",
"(x - z + t) * (x + y - z)",
"(x - z + t) * (z + t - x)",
"(x - z + t + y) * (t - y + z - x)",
])
def test_expr_fusion(expr):
shape = [200, 300]
chunkshape = [40, 50]
bshape = [20, 20]
eval_method = ia.EVAL_ITERCHUNK
storage = ia.StorageProperties(backend="blosc",
chunkshape=chunkshape,
blockshape=bshape,
enforce_frame=False,
filename=None)
for dtype in np.float64, np.float32:
# The ranges below are important for not overflowing operations
x = ia.linspace(ia.dtshape(shape, dtype), .1, .9, storage=storage)
y = ia.linspace(ia.dtshape(shape, dtype), 0., 1., storage=storage)
z = ia.linspace(ia.dtshape(shape, dtype), 0., 2., storage=storage)
t = ia.linspace(ia.dtshape(shape, dtype), 0., 3., storage=storage)
# NumPy computation
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
npz = ia.iarray2numpy(z)
npt = ia.iarray2numpy(t)
npout = eval("%s" % expr, {"np": np, "x": npx, "y": npy, "z": npz, "t": npt})
# High-level ironarray eval
lazy_expr = eval(expr, {"x": x, "y": y, "z": z, "t": t})
iout2 = lazy_expr.eval(eval_method=eval_method, dtype=dtype)
npout2 = ia.iarray2numpy(iout2)
decimal = 6 if dtype is np.float32 else 7
np.testing.assert_almost_equal(npout, npout2, decimal=decimal)