Skip to content

Commit 1812787

Browse files
committed
Delegate logic for chunks to AsyncArray._create
1 parent feb4aa2 commit 1812787

File tree

4 files changed

+16
-8
lines changed

4 files changed

+16
-8
lines changed

changes/xxxx.bugfix.rst

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
When creating arrays without explicitly specifying a chunk size using `zarr.create` and other
2+
array creation routines, the chunk size will now set automatically instead of defaulting to the data shape.
3+
For large arrays this will result in smaller default chunk sizes.
4+
To retain previous behaviour, explicitly set the chunk shape to the data shape.
5+
6+
This fix matches the existing chunking behaviour of
7+
`zarr.save_array` and `zarr.api.asynchronous.AsyncArray.create`.

src/zarr/api/asynchronous.py

Lines changed: 0 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -992,19 +992,11 @@ async def create(
992992
)
993993

994994
if zarr_format == 2:
995-
if chunks is None:
996-
chunks = shape
997995
dtype = parse_dtype(dtype, zarr_format)
998996
if not filters:
999997
filters = _default_filters(dtype)
1000998
if compressor == "auto":
1001999
compressor = _default_compressor(dtype)
1002-
elif zarr_format == 3 and chunk_shape is None: # type: ignore[redundant-expr]
1003-
if chunks is not None:
1004-
chunk_shape = chunks
1005-
chunks = None
1006-
else:
1007-
chunk_shape = shape
10081000

10091001
if synchronizer is not None:
10101002
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)

src/zarr/core/chunk_grids.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,9 @@ def _guess_chunks(
6464
if isinstance(shape, int):
6565
shape = (shape,)
6666

67+
if typesize == 0:
68+
return shape
69+
6770
ndims = len(shape)
6871
# require chunks to have non-zero length for all dimensions
6972
chunks = np.maximum(np.array(shape, dtype="=f8"), 1)

tests/test_api.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1301,3 +1301,9 @@ def test_no_overwrite_load(tmp_path: Path) -> None:
13011301
with contextlib.suppress(NotImplementedError):
13021302
zarr.load(store)
13031303
assert existing_fpath.exists()
1304+
1305+
1306+
def test_auto_chunks() -> None:
1307+
# Check chunks are automatically set to a sensible default
1308+
a = zarr.ones(shape=(1000, 1000), dtype=np.uint8)
1309+
assert a.chunks == (500, 500)

0 commit comments

Comments
 (0)