Skip to content

Commit d60bd7e

Browse files
committed
New evaluate func to emulate numexpr one
1 parent 3e9e08c commit d60bd7e

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/blosc2/__init__.py

+1
Original file line numberDiff line numberDiff line change
@@ -247,6 +247,7 @@ class Tuner(Enum):
247247
_open_lazyarray,
248248
get_expr_operands,
249249
validate_expr,
250+
evaluate,
250251
)
251252
from .proxy import Proxy, ProxySource, ProxyNDSource, ProxyNDField, SimpleProxy, jit
252253

src/blosc2/lazyexpr.py

+50
Original file line numberDiff line numberDiff line change
@@ -2984,6 +2984,56 @@ def _open_lazyarray(array):
29842984
return new_expr
29852985

29862986

2987+
# Mimim numexpr's evaluate function
2988+
def evaluate(ex, local_dict=None, global_dict=None):
2989+
"""
2990+
Evaluate a string expression using the Blosc2 compute engine.
2991+
2992+
This is a drop-in replacement for numexpr.evaluate, but using the Blosc2
2993+
compute engine. This allows for:
2994+
2995+
1) Use more functionality (e.g. reductions) than numexpr.
2996+
2) Use both NumPy arrays and Blosc2 NDArrays in the same expression.
2997+
2998+
As NDArrays can be on-disk, the expression can be evaluated without loading
2999+
the whole array into memory (i.e. using an out-of-core approach).
3000+
3001+
Parameters
3002+
----------
3003+
ex: str
3004+
The expression to evaluate.
3005+
local_dict: dict, optional
3006+
The local dictionary to use when looking for operands in the expression.
3007+
If not provided, the local dictionary of the caller will be used.
3008+
global_dict: dict, optional
3009+
The global dictionary to use when looking for operands in the expression.
3010+
If not provided, the global dictionary of the caller will be used.
3011+
3012+
Returns
3013+
-------
3014+
out: NumPy array
3015+
A NumPy array is returned.
3016+
3017+
Examples
3018+
--------
3019+
>>> import blosc2
3020+
>>> import numpy as np
3021+
>>> dtype = np.float64
3022+
>>> shape = [3, 3]
3023+
>>> size = shape[0] * shape[1]
3024+
>>> a = np.linspace(0, 5, num=size, dtype=dtype).reshape(shape)
3025+
>>> b = blosc2.linspace(0, 5, num=size, dtype=dtype, shape=shape)
3026+
>>> expr = 'a * b + 2'
3027+
>>> out = blosc2.evaluate(expr)
3028+
>>> out
3029+
[[ 2. 2.390625 3.5625 ]
3030+
[ 5.515625 8.25 11.765625]
3031+
[16.0625 21.140625 27. ]]
3032+
"""
3033+
lexpr = lazyexpr(ex, local_dict=local_dict, global_dict=global_dict)
3034+
return lexpr[:]
3035+
3036+
29873037
if __name__ == "__main__":
29883038
from time import time
29893039

0 commit comments

Comments
 (0)