forked from inaos/iron-array-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathexpression.py
More file actions
112 lines (94 loc) · 3.41 KB
/
Copy pathexpression.py
File metadata and controls
112 lines (94 loc) · 3.41 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
###########################################################################################
# Copyright INAOS GmbH, Thalwil, 2018.
# Copyright Francesc Alted, 2018.
#
# All rights reserved.
#
# This software is the confidential and proprietary information of INAOS GmbH
# and Francesc Alted ("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 import iarray_ext as ext
from iarray import py2llvm
# The main expression class
class Expr(ext.Expression):
"""An class that is meant to hold an expression.
This is not meant to be called directly from user space.
See Also
--------
expr_from_string
expr_from_udf
"""
def __init__(self, dtshape, cfg=None, **kwargs):
with ia.config(cfg=cfg, dtshape=dtshape, **kwargs) as cfg:
self.cfg = cfg
super().__init__(self.cfg)
super().bind_out_properties(dtshape, cfg.storage)
def eval(self) -> ia.IArray:
"""Evaluate the expression in self.
Returns
-------
IArray
The output array.
"""
return super().eval()
def check_inputs(inputs: list):
first_input = inputs[0]
for input_ in inputs[1:]:
if first_input.shape != input_.shape:
raise ValueError("Inputs should have the same shape")
if first_input.dtype != input_.dtype:
raise TypeError("Inputs should have the same dtype")
return first_input.dtshape
def expr_from_string(sexpr: str, inputs: dict, cfg: ia.Config = None, **kwargs) -> Expr:
"""Create an `Expr` instance from a expression in string form.
Parameters
----------
sexpr : str
An expression in string format.
inputs : dict
Map of variables in `sexpr` to actual arrays.
cfg : Config
The configuration for running the expression.
If None (default), global defaults are used.
kwargs : dict
A dictionary for setting some or all of the fields in the Config
dataclass that should override the current configuration.
Returns
-------
Expr
An expression ready to be evaluated via :func:`Expr.eval`.
"""
dtshape = check_inputs(list(inputs.values()))
expr = Expr(dtshape=dtshape, cfg=cfg, **kwargs)
for i in inputs:
expr.bind(i, inputs[i])
expr.compile(sexpr)
return expr
def expr_from_udf(udf: py2llvm.Function, inputs: list, cfg=None, **kwargs) -> Expr:
"""Create an `Expr` instance from an UDF function.
Parameters
----------
udf : py2llvm.Function
A User Defined Function.
inputs : list
List of arrays whose values are passed as arguments, after the output,
to the UDF function.
cfg : Config
The configuration for running the expression.
If None (default), global defaults are used.
kwargs : dict
A dictionary for setting some or all of the fields in the Config
dataclass that should override the current configuration.
Returns
-------
Expr
An expression ready to be evaluated via :func:`Expr.eval`.
"""
dtshape = check_inputs(inputs)
expr = Expr(dtshape=dtshape, cfg=cfg, **kwargs)
for i in inputs:
expr.bind("", i)
expr.compile_udf(udf)
return expr