Skip to content

Commit

Permalink
New evaluate func to emulate numexpr one
Browse files Browse the repository at this point in the history
  • Loading branch information
FrancescAlted committed Feb 6, 2025
1 parent 3e9e08c commit d60bd7e
Show file tree
Hide file tree
Showing 2 changed files with 51 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/blosc2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -247,6 +247,7 @@ class Tuner(Enum):
_open_lazyarray,
get_expr_operands,
validate_expr,
evaluate,
)
from .proxy import Proxy, ProxySource, ProxyNDSource, ProxyNDField, SimpleProxy, jit

Expand Down
50 changes: 50 additions & 0 deletions src/blosc2/lazyexpr.py
Original file line number Diff line number Diff line change
Expand Up @@ -2984,6 +2984,56 @@ def _open_lazyarray(array):
return new_expr


# Mimim numexpr's evaluate function
def evaluate(ex, local_dict=None, global_dict=None):
"""
Evaluate a string expression using the Blosc2 compute engine.
This is a drop-in replacement for numexpr.evaluate, but using the Blosc2
compute engine. This allows for:
1) Use more functionality (e.g. reductions) than numexpr.
2) Use both NumPy arrays and Blosc2 NDArrays in the same expression.
As NDArrays can be on-disk, the expression can be evaluated without loading
the whole array into memory (i.e. using an out-of-core approach).
Parameters
----------
ex: str
The expression to evaluate.
local_dict: dict, optional
The local dictionary to use when looking for operands in the expression.
If not provided, the local dictionary of the caller will be used.
global_dict: dict, optional
The global dictionary to use when looking for operands in the expression.
If not provided, the global dictionary of the caller will be used.
Returns
-------
out: NumPy array
A NumPy array is returned.
Examples
--------
>>> import blosc2
>>> import numpy as np
>>> dtype = np.float64
>>> shape = [3, 3]
>>> size = shape[0] * shape[1]
>>> a = np.linspace(0, 5, num=size, dtype=dtype).reshape(shape)
>>> b = blosc2.linspace(0, 5, num=size, dtype=dtype, shape=shape)
>>> expr = 'a * b + 2'
>>> out = blosc2.evaluate(expr)
>>> out
[[ 2. 2.390625 3.5625 ]
[ 5.515625 8.25 11.765625]
[16.0625 21.140625 27. ]]
"""
lexpr = lazyexpr(ex, local_dict=local_dict, global_dict=global_dict)
return lexpr[:]


if __name__ == "__main__":
from time import time

Expand Down

0 comments on commit d60bd7e

Please sign in to comment.