Skip to content

Commit 548395d

Browse files
author
Luke Shaw
committed
Add slicing to string lazyexprs
1 parent 1f0f33f commit 548395d

File tree

2 files changed

+24
-2
lines changed

2 files changed

+24
-2
lines changed

src/blosc2/lazyexpr.py

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -116,7 +116,7 @@ def ne_evaluate(expression, local_dict=None, **kwargs):
116116
constructors = ("arange", "linspace", "fromiter", "zeros", "ones", "empty", "full", "frombuffer")
117117
# Note that, as reshape is accepted as a method too, it should always come last in the list
118118
constructors += ("reshape",)
119-
reducers = ("sum", "prod", "min", "max", "std", "mean", "var", "any", "all")
119+
reducers = ("sum", "prod", "min", "max", "std", "mean", "var", "any", "all", "slice")
120120

121121
functions = [
122122
"sin",
@@ -551,7 +551,20 @@ def compute_smaller_slice(larger_shape, smaller_shape, larger_slice):
551551
_blacklist_re = re.compile("|".join(validation_patterns))
552552

553553
# Define valid method names
554-
valid_methods = {"sum", "prod", "min", "max", "std", "mean", "var", "any", "all", "where", "reshape"}
554+
valid_methods = {
555+
"sum",
556+
"prod",
557+
"min",
558+
"max",
559+
"std",
560+
"mean",
561+
"var",
562+
"any",
563+
"all",
564+
"where",
565+
"reshape",
566+
"slice",
567+
}
555568
valid_methods |= {"int8", "int16", "int32", "int64", "uint8", "uint16", "uint32", "uint64"}
556569
valid_methods |= {"float32", "float64", "complex64", "complex128"}
557570
valid_methods |= {"bool", "str", "bytes"}
@@ -2534,6 +2547,7 @@ def find_args(expr):
25342547
def _compute_expr(self, item, kwargs): # noqa: C901
25352548
if any(method in self.expression for method in reducers):
25362549
# We have reductions in the expression (probably coming from a string lazyexpr)
2550+
# Also includes slice
25372551
_globals = get_expr_globals(self.expression)
25382552
lazy_expr = eval(self.expression, _globals, self.operands)
25392553
if not isinstance(lazy_expr, blosc2.LazyExpr):

tests/ndarray/test_reductions.py

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -428,3 +428,11 @@ def test_reduction_index():
428428
assert arr[:10].shape == (10,)
429429
assert arr[0].shape == (1,)
430430
assert arr.shape == newarr.shape
431+
432+
433+
def test_slice_in_lazy():
434+
shape = (20, 20)
435+
a = blosc2.linspace(0, 20, num=np.prod(shape), shape=shape)
436+
arr = blosc2.lazyexpr("anarr.slice(slice(10,15)) + 1", {"anarr": a})
437+
newarr = arr.compute()
438+
np.testing.assert_allclose(newarr[:], a.slice(slice(10, 15))[:] + 1)

0 commit comments

Comments
 (0)