Skip to content

Commit 7045763

Browse files
authored
Merge pull request #404 from Blosc/fixIndexing
Fix incorrect appending of dim to computed reductions
2 parents cb2b4d3 + 4f65e32 commit 7045763

File tree

2 files changed

+11
-8
lines changed

2 files changed

+11
-8
lines changed

src/blosc2/lazyexpr.py

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@
3737
import blosc2
3838
from blosc2 import compute_chunks_blocks
3939
from blosc2.info import InfoReporter
40-
from blosc2.ndarray import _check_allowed_dtypes, get_chunks_idx, is_inside_new_expr
40+
from blosc2.ndarray import _check_allowed_dtypes, get_chunks_idx, is_inside_new_expr, process_key
4141

4242
if not blosc2.IS_WASM:
4343
import numexpr
@@ -2434,23 +2434,24 @@ def _compute_expr(self, item, kwargs): # noqa: C901
24342434
_globals = get_expr_globals(self.expression)
24352435
lazy_expr = eval(self.expression, _globals, self.operands)
24362436
if not isinstance(lazy_expr, blosc2.LazyExpr):
2437+
key, _ = process_key(item, self.shape)
24372438
# An immediate evaluation happened (e.g. all operands are numpy arrays)
24382439
if hasattr(self, "_where_args"):
24392440
# We need to apply the where() operation
24402441
if len(self._where_args) == 1:
24412442
# We have a single argument
24422443
where_x = self._where_args["_where_x"]
2443-
return (where_x[:][lazy_expr])[item]
2444+
return (where_x[:][lazy_expr])[key]
24442445
if len(self._where_args) == 2:
24452446
# We have two arguments
24462447
where_x = self._where_args["_where_x"]
24472448
where_y = self._where_args["_where_y"]
2448-
return np.where(lazy_expr, where_x, where_y)[item]
2449+
return np.where(lazy_expr, where_x, where_y)[key]
24492450
if hasattr(self, "_output"):
24502451
# This is not exactly optimized, but it works for now
2451-
self._output[:] = lazy_expr[item]
2452+
self._output[:] = lazy_expr[key]
24522453
return self._output
2453-
return lazy_expr[item]
2454+
return lazy_expr[key]
24542455

24552456
return chunked_eval(lazy_expr.expression, lazy_expr.operands, item, **kwargs)
24562457

tests/ndarray/test_reductions.py

Lines changed: 5 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -423,6 +423,8 @@ def test_save_constructor_reduce2(shape, disk, compute):
423423
def test_reduction_index():
424424
shape = (20, 20)
425425
a = blosc2.linspace(0, 20, num=np.prod(shape), shape=shape)
426-
expr = blosc2.sum(a, axis=0)
427-
assert expr[:10].shape == (10,)
428-
assert expr[0].shape == ()
426+
arr = blosc2.lazyexpr("sum(a,axis=0)", {"a": a})
427+
newarr = arr.compute()
428+
assert arr[:10].shape == (10,)
429+
assert arr[0].shape == (1,)
430+
assert arr.shape == newarr.shape

0 commit comments

Comments
 (0)