forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression_chunks_blocks.py
More file actions
128 lines (105 loc) · 4.13 KB
/
Copy pathexpression_chunks_blocks.py
File metadata and controls
128 lines (105 loc) · 4.13 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
# This uses the binary code in LLVM .bc file for evaluating expressions. Only meant for developers, really.
import iarray as ia
import numpy
import numpy as np
from time import time
def te_expression(method, shape, xcfg, ycfg, zcfg, dtype, expression):
x = ia.linspace(0.1, 0.2, int(np.prod(shape)), shape=shape, dtype=dtype, cfg=xcfg)
y = ia.linspace(0, 1, int(np.prod(shape)), shape=shape, dtype=dtype, cfg=ycfg)
npx = ia.iarray2numpy(x)
npy = ia.iarray2numpy(y)
expr = ia.expr_from_string(expression, {"x": x, "y": y}, cfg=zcfg, eval_method=method)
t0 = time()
iout = expr.eval()
t1 = time()
print("Eval cfg", iout.cfg)
print("Evaluation time:", round(t1 - t0, 4))
npout = ia.iarray2numpy(iout)
# Evaluate using a different engine (numpy)
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.UNIVERSAL_MATH_FUNCS:
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})
tol = 1e-6 if dtype is np.float32 else 1e-14
np.testing.assert_allclose(npout, npout2, rtol=tol, atol=tol)
ia.remove_urlpath(xcfg.urlpath)
ia.remove_urlpath(ycfg.urlpath)
if zcfg is not None:
ia.remove_urlpath(zcfg.urlpath)
print("Expression with same chunks and blocks for all operands")
# Parameters
method = ia.Eval.ITERCHUNK
shape = [80, 600, 700]
chunks = [40, 400, 400]
blocks = [10, 100, 100]
dtype = np.float32
expression = "(x - cos(y)) * (sin(x) + y) + 2 * x + y"
xcontiguous = False
xurlpath = "test_expression_xsparse.iarr"
ycontiguous = True
yurlpath = None
ia.remove_urlpath(xurlpath)
ia.remove_urlpath(yurlpath)
ia.remove_urlpath("test_expression_zarray.iarr")
xcfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=xcontiguous, urlpath=xurlpath)
ycfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=ycontiguous, urlpath=yurlpath)
te_expression(
method=method, shape=shape, xcfg=xcfg, ycfg=ycfg, zcfg=None, dtype=dtype, expression=expression
)
print("Expression with different chunks anb blocks for the operands")
# Parameters
method = ia.Eval.ITERCHUNK
dtype = np.float32
expression = "(x - cos(y)) * (sin(x) + y) + 2 * x + y"
xcontiguous = False
xurlpath = "test_expression_xsparse.iarr"
ycontiguous = True
yurlpath = None
ia.remove_urlpath(xurlpath)
ia.remove_urlpath(yurlpath)
ia.remove_urlpath("test_expression_zarray.iarr")
xcfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=xcontiguous, urlpath=xurlpath)
ycfg = ia.Config(chunks=[40, 300, 300], blocks=blocks, contiguous=ycontiguous, urlpath=yurlpath)
print("config defaults ", ia.get_config_defaults())
te_expression(
method=method, shape=shape, xcfg=xcfg, ycfg=ycfg, zcfg=None, dtype=dtype, expression=expression
)
print("Expression with cfg parameters for the output")
# Parameters
method = ia.Eval.ITERCHUNK
dtype = np.float32
expression = "(x - cos(y)) * (sin(x) + y) + 2 * x + y"
xcontiguous = False
xurlpath = "test_expression_xsparse.iarr"
ycontiguous = True
yurlpath = None
ia.remove_urlpath(xurlpath)
ia.remove_urlpath(yurlpath)
ia.remove_urlpath("test_expression_zarray.iarr")
xcfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=xcontiguous, urlpath=xurlpath)
ycfg = ia.Config(chunks=chunks, blocks=blocks, contiguous=ycontiguous, urlpath=yurlpath)
zcfg = ia.Config(
chunks=[40, 300, 300],
blocks=[10, 100, 100],
contiguous=xcontiguous,
urlpath="test_expression_zarray.iarr",
)
te_expression(
method=method, shape=shape, xcfg=xcfg, ycfg=ycfg, zcfg=zcfg, dtype=dtype, expression=expression
)