Skip to content

Commit 3f2f722

Browse files
committed
Fix a bug when indexing reduction expressions with ints
1 parent f43ac50 commit 3f2f722

File tree

3 files changed

+21
-5
lines changed

3 files changed

+21
-5
lines changed

src/blosc2/lazyexpr.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010

1111
import ast
1212
import asyncio
13+
import builtins
1314
import concurrent.futures
1415
import copy
1516
import inspect
@@ -2601,7 +2602,7 @@ def _compute_expr(self, item, kwargs): # noqa: C901
26012602
_globals = get_expr_globals(self.expression)
26022603
lazy_expr = eval(self.expression, _globals, self.operands)
26032604
if not isinstance(lazy_expr, blosc2.LazyExpr):
2604-
key, _ = process_key(item, self.shape)
2605+
key, mask = process_key(item, self.shape)
26052606
# An immediate evaluation happened (e.g. all operands are numpy arrays)
26062607
if hasattr(self, "_where_args"):
26072608
# We need to apply the where() operation
@@ -2618,7 +2619,11 @@ def _compute_expr(self, item, kwargs): # noqa: C901
26182619
# This is not exactly optimized, but it works for now
26192620
self._output[:] = lazy_expr[key]
26202621
return self._output
2621-
return lazy_expr[key]
2622+
arr = lazy_expr[key]
2623+
if builtins.sum(mask) > 0:
2624+
# Correct shape to adjust to NumPy convention
2625+
arr.shape = tuple(arr.shape[i] for i in range(len(mask)) if not mask[i])
2626+
return arr
26222627

26232628
return chunked_eval(lazy_expr.expression, lazy_expr.operands, item, **kwargs)
26242629

tests/ndarray/test_lazyexpr.py

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1451,8 +1451,6 @@ def test_to_cframe():
14511451
N = 1_000
14521452
dtype = "float64"
14531453
a = blosc2.linspace(0, 1, N * N, dtype=dtype, shape=(N, N))
1454-
b = blosc2.linspace(1, 2, N * N, dtype=dtype, shape=(N, N))
1455-
c = blosc2.linspace(0, 1, N, dtype=dtype, shape=(N,))
14561454
expr = a**3 + blosc2.sin(a**2)
14571455
cframe = expr.to_cframe()
14581456
assert len(cframe) > 0

tests/ndarray/test_reductions.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,10 +426,23 @@ def test_reduction_index():
426426
arr = blosc2.lazyexpr("sum(a,axis=0)", {"a": a})
427427
newarr = arr.compute()
428428
assert arr[:10].shape == (10,)
429-
assert arr[0].shape == (1,)
429+
assert arr[0].shape == ()
430430
assert arr.shape == newarr.shape
431431

432432

433+
@pytest.mark.parametrize("idx", [0, 1, (0,), slice(1, 2), (slice(0, 1),), slice(0, 4), (0, 2)])
434+
def test_reduction_index2(idx):
435+
N = 10
436+
shape = (N, N, N)
437+
a = blosc2.linspace(0, 1, num=np.prod(shape), shape=(N, N, N))
438+
expr = blosc2.lazyexpr("a.sum(axis=1)")
439+
out = expr[idx]
440+
na = blosc2.asarray(a)
441+
nout = na.sum(axis=1)[idx]
442+
assert out.shape == nout.shape
443+
assert np.allclose(out, nout)
444+
445+
433446
def test_slice_lazy():
434447
shape = (20, 20)
435448
a = blosc2.linspace(0, 20, num=np.prod(shape), shape=shape)

0 commit comments

Comments
 (0)