@@ -2984,6 +2984,56 @@ def _open_lazyarray(array):
2984
2984
return new_expr
2985
2985
2986
2986
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
+
2987
3037
if __name__ == "__main__" :
2988
3038
from time import time
2989
3039
0 commit comments