Skip to content

Commit 516fb2f

Browse files
authored
Make allow_irregular default to True for rechunk (#933)
* Change allow_irregular default to True; update tests * Add test for explicit allow_irregular=False * Remove no-longer-needed Zarr Python v2 check
1 parent 9087ef0 commit 516fb2f

5 files changed

Lines changed: 22 additions & 29 deletions

File tree

cubed/core/array.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -179,7 +179,7 @@ def plan(
179179
)
180180

181181
def rechunk(
182-
self: T_ChunkedArray, chunks, *, min_mem=None, allow_irregular=False
182+
self: T_ChunkedArray, chunks, *, min_mem=None, allow_irregular=True
183183
) -> T_ChunkedArray:
184184
"""Change the chunking of this array without changing its shape or data.
185185

cubed/core/ops.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -948,7 +948,7 @@ def _map_blocks(
948948
)
949949

950950

951-
def rechunk(x, chunks, *, min_mem=None, allow_irregular=False):
951+
def rechunk(x, chunks, *, min_mem=None, allow_irregular=True):
952952
"""Change the chunking of an array without changing its shape or data.
953953
954954
Parameters
@@ -969,7 +969,7 @@ def rechunk(x, chunks, *, min_mem=None, allow_irregular=False):
969969
return out
970970

971971

972-
def _rechunk_plan(x, chunks, *, min_mem=None, allow_irregular=False):
972+
def _rechunk_plan(x, chunks, *, min_mem=None, allow_irregular=True):
973973
if isinstance(chunks, dict):
974974
chunks = {validate_axis(c, x.ndim): v for c, v in chunks.items()}
975975
for i in range(x.ndim):
@@ -1052,7 +1052,7 @@ def split_chunksizes(n: int, sc: int, tc: int) -> tuple[int]:
10521052
return tuple(np.diff(c).tolist())
10531053

10541054

1055-
def _rechunk(x, copy_chunks, target_chunks, allow_irregular=False):
1055+
def _rechunk(x, copy_chunks, target_chunks, allow_irregular=True):
10561056
# rechunk x so that its target store has target_chunks, using copy_chunks as the size of chunks for copying from source to target
10571057

10581058
normalized_copy_chunks = normalize_chunks(copy_chunks, x.shape, dtype=x.dtype)

cubed/core/rechunk.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -311,7 +311,7 @@ def from_plan(cls, rechunk_plan: RechunkPlan, plan: FinalizedPlan):
311311
)
312312

313313

314-
def rechunk_plan(x, chunks, *, min_mem=None, allow_irregular=False):
314+
def rechunk_plan(x, chunks, *, min_mem=None, allow_irregular=True):
315315
from cubed.core.ops import _rechunk_plan
316316

317317
copy_ops = []

cubed/tests/test_array_api.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
import numpy as np
22
import pytest
3-
import zarr
43

54
import cubed
65
import cubed.array_api as xp
@@ -14,8 +13,6 @@
1413
skip_if_cupy,
1514
)
1615

17-
ZARR_PYTHON_V2 = zarr.__version__[0] == "2"
18-
1916

2017
@pytest.fixture
2118
def spec(tmp_path):
@@ -662,7 +659,6 @@ def test_flip(executor, shape, chunks, axis):
662659

663660

664661

665-
@pytest.mark.skipif(ZARR_PYTHON_V2, reason="fails with Zarr v2")
666662
def test_flip_zero_size_dim(spec):
667663
x = np.ones((0, 4))
668664
a = xp.asarray(x, chunks=(1, 2), spec=spec)

cubed/tests/test_rechunk.py

Lines changed: 17 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
import pytest
2-
import zarr
32

43
import cubed
54
import cubed as xp
@@ -29,7 +28,7 @@
2928
"expected_max_output_blocks",
3029
),
3130
[
32-
(None, 5, 7, 9), # multistage rechunk - more stages, lower fan in/out
31+
(None, 3, 16, 15), # multistage rechunk - more stages, lower fan in/out
3332
(0, 1, 3771, 3460), # single stage rechunk - very high fan in/out
3433
],
3534
)
@@ -97,11 +96,9 @@ def test_rechunk_era5_chunk_sizes(spec):
9796

9897
rechunk_plan = list(_rechunk_plan(a, target_chunks))
9998
assert rechunk_plan == [
100-
((93, 721, 1440), (93, 240, 480)),
101-
((465, 240, 480), (465, 120, 240)),
102-
((2325, 120, 240), (2325, 40, 120)),
103-
((11625, 40, 120), (11625, 20, 60)),
104-
((58125, 20, 60), (58125, 10, 30)),
99+
((93, 721, 1440), (93, 173, 396)),
100+
((1447, 173, 396), (1447, 41, 109)),
101+
((22528, 41, 109), (22528, 10, 30)),
105102
((350640, 10, 30), (350640, 10, 10)),
106103
]
107104

@@ -140,32 +137,32 @@ def test_rechunk_hypothesis_generated_bug():
140137
from cubed.core.ops import _rechunk_plan
141138

142139
rechunk_plan = list(_rechunk_plan(a, target_chunks))
143-
assert rechunk_plan == [((30, 376), (15, 376)), ((15, 1001), (5, 146))]
144-
for copy_chunks, target_chunks in rechunk_plan:
145-
verify_chunk_compatibility(shape, copy_chunks, target_chunks)
140+
assert rechunk_plan == [((38, 376), (15, 376)), ((15, 1001), (5, 146))]
146141

147142
b = a.rechunk((5, 146))
148143

149144
assert_array_equal(b.compute(), nxp.ones(shape))
150145

151146

152-
@pytest.mark.skipif(
153-
zarr.__version__[0] == "2",
154-
reason="irregular chunking is not supported for Zarr Python v2",
155-
)
156-
def test_rechunk_hypothesis_generated_bug_allow_irregular():
157-
rechunk_shapes = (tuple([1001, 1001]), (38, 376), (5, 146))
158-
shape, source_chunks, target_chunks = rechunk_shapes
147+
def test_rechunk_allow_irregular_false():
148+
# Verify the regular planner still works when allow_irregular=False is
149+
# passed explicitly. Regular intermediates must be integer multiples of
150+
# target chunks, so copy_chunks[0]=30 (multiple of 15) instead of 38.
151+
shape = (1001, 1001)
152+
source_chunks = (38, 376)
153+
target_chunks = (5, 146)
159154

160155
spec = cubed.Spec(allowed_mem=8000000 / 10)
161156
a = xp.ones(shape, chunks=source_chunks, spec=spec)
162157

163158
from cubed.core.ops import _rechunk_plan
164159

165-
rechunk_plan = list(_rechunk_plan(a, target_chunks, allow_irregular=True))
166-
assert rechunk_plan == [((38, 376), (15, 376)), ((15, 1001), (5, 146))]
160+
rechunk_plan = list(_rechunk_plan(a, target_chunks, allow_irregular=False))
161+
assert rechunk_plan == [((30, 376), (15, 376)), ((15, 1001), (5, 146))]
162+
for copy_chunks, store_chunks in rechunk_plan:
163+
verify_chunk_compatibility(shape, copy_chunks, store_chunks)
167164

168-
b = a.rechunk((5, 146), allow_irregular=True)
165+
b = a.rechunk(target_chunks, allow_irregular=False)
169166

170167
assert_array_equal(b.compute(), nxp.ones(shape))
171168

0 commit comments

Comments
 (0)