Skip to content

Commit 558f363

Browse files
author
Luke Shaw
committed
Correct calculation of index offset
1 parent 541a88e commit 558f363

File tree

2 files changed

+22
-5
lines changed

2 files changed

+22
-5
lines changed

src/blosc2/lazyexpr.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1875,15 +1875,16 @@ def reduce_slices( # noqa: C901
18751875
cslice = step_handler(cslice, _slice)
18761876
chunks_ = tuple(s.stop - s.start for s in cslice)
18771877
unit_steps = np.all([s.step == 1 for s in cslice])
1878+
# Get the starts for the slice (needed for offset calculations when intra-chunk slicing)
1879+
starts = [s.start if s.start is not None else 0 for s in cslice]
18781880
if _slice == () and fast_path and unit_steps:
18791881
# Fast path
18801882
full_chunk = chunks_ == chunks
18811883
fill_chunk_operands(
18821884
operands, cslice, chunks_, full_chunk, aligned, nchunk, iter_disk, chunk_operands, reduc=True
18831885
)
18841886
else:
1885-
# Get the starts and stops for the slice
1886-
starts = [s.start if s.start is not None else 0 for s in cslice]
1887+
# Get the stops for the slice
18871888
stops = [s.stop if s.stop is not None else sh for s, sh in zip(cslice, chunks_, strict=True)]
18881889
# Get the slice of each operand
18891890
for key, value in operands.items():
@@ -1968,12 +1969,15 @@ def reduce_slices( # noqa: C901
19681969
)
19691970
if reduce_args["axis"] is None: # indexing into flattened array
19701971
result_val = result_val[np.unravel_index(result, shape=result_val.shape)]
1971-
result += np.ravel_multi_index(offset, shape)
1972+
idx_within_cslice = np.unravel_index(result, shape=chunks_)
1973+
result = np.ravel_multi_index(
1974+
tuple(o + i for o, i in zip(starts, idx_within_cslice, strict=True)), shape
1975+
)
19721976
else: # axis is an integer
19731977
result_val = np.take_along_axis(
19741978
result_val, np.expand_dims(result, axis=reduce_args["axis"]), axis=reduce_args["axis"]
19751979
)
1976-
result += offset[reduce_args["axis"]]
1980+
result += starts[reduce_args["axis"]]
19771981
else:
19781982
result = reduce_op.value.reduce(result, **reduce_args)
19791983

src/blosc2/shape_utils.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -105,7 +105,20 @@
105105
]
106106

107107
linalg_attrs = ["T", "mT"]
108-
reducers = ["sum", "prod", "min", "max", "std", "mean", "var", "any", "all", "count_nonzero"]
108+
reducers = [
109+
"sum",
110+
"prod",
111+
"min",
112+
"max",
113+
"std",
114+
"mean",
115+
"var",
116+
"any",
117+
"all",
118+
"count_nonzero",
119+
"argmax",
120+
"argmin",
121+
]
109122

110123
# All the available constructors and reducers necessary for the (string) expression evaluator
111124
constructors = [

0 commit comments

Comments
 (0)