forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathlazy_expr.py
More file actions
273 lines (239 loc) · 9.89 KB
/
lazy_expr.py
File metadata and controls
273 lines (239 loc) · 9.89 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
###########################################################################################
# Copyright ironArray SL 2021.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of ironArray SL
# ("Confidential Information"). You shall not disclose such Confidential Information
# and shall use it only in accordance with the terms of the license agreement.
###########################################################################################
import iarray as ia
from iarray.expr_udf import expr_udf
def fuse_operands(operands1, operands2):
new_operands = {}
dup_operands = {}
new_pos = len(operands1)
for k2, v2 in operands2.items():
try:
k1 = list(operands1.keys())[list(operands1.values()).index(v2)]
# The operand is duplicated; keep track of it
dup_operands[k2] = k1
except ValueError:
# The value is not among operands1, so rebase it
new_op = f"o{new_pos}"
new_pos += 1
new_operands[new_op] = operands2[k2]
return new_operands, dup_operands
def fuse_expressions(expr, new_base, dup_op):
new_expr = ""
skip_to_char = 0
old_base = 0
prev_pos = {}
for i in range(len(expr)):
if i < skip_to_char:
continue
if expr[i] == "o":
if i > 0 and (expr[i - 1] != " " and expr[i - 1] != "("):
# Not a variable
new_expr += expr[i]
continue
# This is a variable. Find the end of it.
j = i + 1
for k in range(len(expr[j:])):
if expr[j + k] in " )[":
j = k
break
if expr[i + j] == ")":
j -= 1
old_pos = int(expr[i + 1 : i + j + 1])
old_op = f"o{old_pos}"
if old_op not in dup_op:
if old_pos in prev_pos:
# Keep track of duplicated old positions inside expr
new_pos = prev_pos[old_pos]
else:
new_pos = old_base + new_base
old_base += 1
new_expr += f"o{new_pos}"
prev_pos[old_pos] = new_pos
else:
new_expr += dup_op[old_op]
skip_to_char = i + j + 1
else:
new_expr += expr[i]
return new_expr
class LazyExpr:
"""Class for hosting lazy expressions.
This is not meant to be called directly from user space.
Once the lazy expression is created, it can be evaluated via :func:`LazyExpr.eval`.
"""
def __init__(self, new_op):
value1, op, value2 = new_op
if op is not None and op.startswith(f"{ia.dflt_ulib}"):
# A scalar UDF call
args = tuple(value2)
nops = 0
self.operands = {}
new_args = []
for arg in args:
if isinstance(arg, ia.IArray):
self.operands[f"o{nops}"] = arg
new_args.append(f"o{nops}")
nops += 1
else:
new_args.append(f"{arg}")
new_args = ", ".join(new_args)
self.expression = f"{op}({new_args})"
return
if value2 is None:
# ufunc
if isinstance(value1, LazyExpr):
self.expression = f"{op}({self.expression})"
else:
self.operands = {"o0": value1}
self.expression = "o0" if op is None else f"{op}(o0)"
return
elif op in ("atan2", "pow"):
self.operands = {"o0": value1, "o1": value2}
self.expression = f"{op}(o0, o1)"
return
if isinstance(value1, (int, float)) and isinstance(value2, (int, float)):
self.expression = f"({value1} {op} {value2})"
elif isinstance(value2, (int, float)):
self.operands = {"o0": value1}
self.expression = f"(o0 {op} {value2})"
elif isinstance(value1, (int, float)):
self.operands = {"o0": value2}
self.expression = f"({value1} {op} o0)"
else:
if value1 is value2:
self.operands = {"o0": value1}
self.expression = f"(o0 {op} o0)"
elif isinstance(value1, LazyExpr) or isinstance(value2, LazyExpr):
if isinstance(value1, LazyExpr):
self.expression = value1.expression
self.operands = {"o0": value2}
else:
self.expression = value2.expression
self.operands = {"o0": value1}
self.update_expr(new_op)
else:
# This is the very first time that a LazyExpr is formed from two operands
# that are not LazyExpr themselves
self.operands = {"o0": value1, "o1": value2}
self.expression = f"(o0 {op} o1)"
def update_expr(self, new_op):
# We use a lot the original IArray.__eq__ as 'is', so deactivate the overloaded one
ia._disable_overloaded_equal = True
# One of the two operands are LazyExpr instances
value1, op, value2 = new_op
if isinstance(value1, LazyExpr) and isinstance(value2, LazyExpr):
# Expression fusion
# Fuse operands in expressions and detect duplicates
new_op, dup_op = fuse_operands(value1.operands, value2.operands)
# Take expression 2 and rebase the operands while removing duplicates
new_expr = fuse_expressions(value2.expression, len(value1.operands), dup_op)
self.expression = f"({self.expression} {op} {new_expr})"
self.operands.update(new_op)
elif isinstance(value1, LazyExpr):
if op == "not":
self.expression = f"({op}{self.expression})"
elif isinstance(value2, (int, float)):
self.expression = f"({self.expression} {op} {value2})"
else:
try:
op_name = list(value1.operands.keys())[
list(value1.operands.values()).index(value2)
]
except ValueError:
op_name = f"o{len(self.operands)}"
self.operands[op_name] = value2
self.expression = f"({self.expression} {op} {op_name})"
else:
if isinstance(value1, (int, float)):
self.expression = f"({value1} {op} {self.expression})"
else:
try:
op_name = list(value2.operands.keys())[
list(value2.operands.values()).index(value1)
]
except ValueError:
op_name = f"o{len(self.operands)}"
self.operands[op_name] = value1
if op == "[]": # syntactic sugar for slicing
self.expression = f"({op_name}[{self.expression}])"
else:
self.expression = f"({op_name} {op} {self.expression})"
ia._disable_overloaded_equal = False
return self
def __add__(self, value):
return self.update_expr(new_op=(self, "+", value))
def __radd__(self, value):
return self.update_expr(new_op=(value, "+", self))
def __sub__(self, value):
return self.update_expr(new_op=(self, "-", value))
def __rsub__(self, value):
return self.update_expr(new_op=(value, "-", self))
def __mul__(self, value):
return self.update_expr(new_op=(self, "*", value))
def __rmul__(self, value):
return self.update_expr(new_op=(value, "*", self))
def __truediv__(self, value):
return self.update_expr(new_op=(self, "/", value))
def __rtruediv__(self, value):
return self.update_expr(new_op=(value, "/", self))
def __and__(self, value):
return self.update_expr(new_op=(self, "and", value))
def __rand__(self, value):
return self.update_expr(new_op=(value, "and", self))
def __or__(self, value):
return self.update_expr(new_op=(self, "or", value))
def __ror__(self, value):
return self.update_expr(new_op=(value, "or", self))
def __invert__(self):
return self.update_expr(new_op=(self, "not", None))
def eval(self, debug=0, cfg: ia.Config = None, **kwargs) -> ia.IArray:
"""Evaluate the lazy expression in self.
Parameters
----------
cfg : :class:`Config`
The configuration for this operation. If None (default), the current
configuration will be used.
kwargs : dict
A dictionary for setting some or all of the fields in the :class:`Config`
dataclass that should override the current configuration.
Returns
-------
:ref:`IArray`
The output array.
"""
if cfg is None:
cfg = ia.get_config_defaults()
with ia.config(cfg=cfg, **kwargs) as cfg:
expr = ia.expr_from_string(self.expression, self.operands, debug=debug, cfg=cfg)
out = expr.eval()
return out
def __str__(self):
expression = f"{self.expression}"
return expression
if __name__ == "__main__":
# Check representations of default config
import numpy as np
print(ia.get_config_defaults())
print()
# Create initial containers
dtshape_ = [40, 20]
a1 = ia.linspace(dtshape_, 0, 10)
a2 = ia.linspace(dtshape_, 0, 10)
a3 = ia.linspace(dtshape_, 0, 10)
a4 = ia.linspace(dtshape_, 0, 10)
# Evaluate with different methods
# a3 = ia.tan(a1) * (ia.sin(a2) * ia.sin(a2) + ia.cos(a3)) + (ia.sqrt(a4) * 2)
ia_expr = ia.sin(a1) + 2 * a1 + 1
ia_expr += 2
print(ia_expr)
ia_res = ia_expr.eval().data
np_res = np.sin(ia.iarray2numpy(a1)) + 2 * ia.iarray2numpy(a1) + 1 + 2
# print(np_res)
np.testing.assert_allclose(ia_res, np_res)
print("Everything is working fine")