Skip to content

Commit f0ef6be

Browse files
committed
Fix just the issue with indexing lazy expressions
1 parent 808e891 commit f0ef6be

File tree

3 files changed

+12
-14
lines changed

3 files changed

+12
-14
lines changed

src/blosc2/lazyexpr.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2449,6 +2449,7 @@ def _compute_expr(self, item, kwargs): # noqa: C901
24492449
if hasattr(self, "_output"):
24502450
# This is not exactly optimized, but it works for now
24512451
self._output[:] = lazy_expr[item]
2452+
return self._output
24522453
return lazy_expr[item]
24532454

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

src/blosc2/ndarray.py

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -61,15 +61,14 @@ def make_key_hashable(key):
6161
else:
6262
return key
6363

64+
6465
def process_key(key, shape):
66+
if key is None:
67+
key = tuple(slice(None) for _ in range(len(shape)))
6568
key = ndindex.ndindex(key).expand(shape).raw
66-
key_ = ()
67-
mask = () #get integer indices where dimension collapses
68-
for k in key: #handle multiple Nones in key
69-
if k is not None:
70-
key_ += (k if isinstance(k, slice) else slice(k, k + 1, None),)
71-
mask += (isinstance(k, int),)
72-
return key_, mask
69+
mask = tuple(isinstance(k, int) for k in key)
70+
key = tuple(k if isinstance(k, slice) else slice(k, k + 1, None) for k in key)
71+
return key, mask
7372

7473

7574
def get_ndarray_start_stop(ndim, key, shape):
@@ -1470,7 +1469,6 @@ def __getitem__( # noqa: C901
14701469
[3.3333, 3.3333, 3.3333, 3.3333, 3.3333]])
14711470
"""
14721471
# First try some fast paths for common cases
1473-
newaxes = None
14741472
if isinstance(key, np.integer):
14751473
# Massage the key to a tuple and go the fast path
14761474
key_ = (slice(key, key + 1), *(slice(None),) * (self.ndim - 1))
@@ -1521,8 +1519,6 @@ def __getitem__( # noqa: C901
15211519
start, stop, step = get_ndarray_start_stop(self.ndim, key_, self.shape)
15221520
shape = np.array([sp - st for st, sp in zip(start, stop, strict=True)])
15231521
shape = tuple(shape[[not m for m in mask]])
1524-
# Add new axes if necessary
1525-
newaxes = tuple(i for i, k in enumerate(key) if k is None)
15261522

15271523
# Create the array to store the result
15281524
arr = np.empty(shape, dtype=self.dtype)
@@ -1537,9 +1533,9 @@ def __getitem__( # noqa: C901
15371533
self._last_read.clear()
15381534
inmutable_key = make_key_hashable(key)
15391535
self._last_read[inmutable_key] = nparr
1540-
1541-
return nparr.expand_dims(newaxes) if newaxes is not None else nparr
1542-
1536+
1537+
return nparr
1538+
15431539
def __setitem__(self, key: int | slice | Sequence[slice], value: object):
15441540
"""Set a slice of the array.
15451541

tests/ndarray/test_reductions.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -419,9 +419,10 @@ def test_save_constructor_reduce2(shape, disk, compute):
419419
blosc2.remove_urlpath(urlpath_b)
420420
blosc2.remove_urlpath("out.b2nd")
421421

422+
422423
def test_reduction_index():
423424
shape = (20, 20)
424425
a = blosc2.linspace(0, 20, num=np.prod(shape), shape=shape)
425426
expr = blosc2.sum(a, axis=0)
426427
assert expr[:10].shape == (10,)
427-
assert expr[0].shape == ()
428+
assert expr[0].shape == ()

0 commit comments

Comments
 (0)