Skip to content

Commit

Permalink
fix(array): thread order parameter through to array __init__
Browse files Browse the repository at this point in the history
  • Loading branch information
jhamman committed Oct 19, 2024
1 parent 0eb4c8a commit f71cdc1
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 14 deletions.
10 changes: 3 additions & 7 deletions src/zarr/api/asynchronous.py
Original file line number Diff line number Diff line change
Expand Up @@ -712,7 +712,7 @@ async def create(
dtype: npt.DTypeLike | None = None,
compressor: dict[str, JSON] | None = None, # TODO: default and type change
fill_value: Any | None = 0, # TODO: need type
order: MemoryOrder | None = None, # TODO: default change
order: MemoryOrder | None = None,
store: str | StoreLike | None = None,
synchronizer: Any | None = None,
overwrite: bool = False,
Expand Down Expand Up @@ -761,6 +761,7 @@ async def create(
Default value to use for uninitialized portions of the array.
order : {'C', 'F'}, optional
Memory layout to be used within each chunk.
Default is set in Zarr's config (`array.order`).
store : Store or str
Store or path to directory in file system or name of zip file.
synchronizer : object, optional
Expand Down Expand Up @@ -834,12 +835,6 @@ async def create(
else:
chunk_shape = shape

if order is not None:
warnings.warn(
"order is deprecated, use config `array.order` instead",
DeprecationWarning,
stacklevel=2,
)
if synchronizer is not None:
warnings.warn("synchronizer is not yet implemented", RuntimeWarning, stacklevel=2)
if chunk_store is not None:
Expand Down Expand Up @@ -889,6 +884,7 @@ async def create(
codecs=codecs,
dimension_names=dimension_names,
attributes=attributes,
order=order,
**kwargs,
)

Expand Down
12 changes: 5 additions & 7 deletions src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -472,10 +472,6 @@ async def create(
raise ValueError(
"dimension_separator cannot be used for arrays with version 3. Use chunk_key_encoding instead."
)
if order is not None:
raise ValueError(
"order cannot be used for arrays with version 3. Use a transpose codec instead."
)
if filters is not None:
raise ValueError(
"filters cannot be used for arrays with version 3. Use array-to-array codecs instead."
Expand All @@ -495,6 +491,7 @@ async def create(
dimension_names=dimension_names,
attributes=attributes,
exists_ok=exists_ok,
order=order,
)
elif zarr_format == 2:
if dtype is str or dtype == "str":
Expand Down Expand Up @@ -546,6 +543,7 @@ async def _create_v3(
dtype: npt.DTypeLike,
chunk_shape: ChunkCoords,
fill_value: Any | None = None,
order: Literal["C", "F"] | None = None,
chunk_key_encoding: (
ChunkKeyEncoding
| tuple[Literal["default"], Literal[".", "/"]]
Expand Down Expand Up @@ -589,7 +587,7 @@ async def _create_v3(
attributes=attributes or {},
)

array = cls(metadata=metadata, store_path=store_path)
array = cls(metadata=metadata, store_path=store_path, order=order)
await array._save_metadata(metadata, ensure_parents=True)
return array

Expand All @@ -612,7 +610,7 @@ async def _create_v2(
if not exists_ok:
await ensure_no_existing_node(store_path, zarr_format=2)
if order is None:
order = "C"
order = config.get("array.order", "C")

if dimension_separator is None:
dimension_separator = "."
Expand All @@ -628,7 +626,7 @@ async def _create_v2(
filters=filters,
attributes=attributes,
)
array = cls(metadata=metadata, store_path=store_path)
array = cls(metadata=metadata, store_path=store_path, order=order)
await array._save_metadata(metadata, ensure_parents=True)
return array

Expand Down
16 changes: 16 additions & 0 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -206,6 +206,22 @@ def test_open_with_mode_w_minus(tmp_path: pathlib.Path) -> None:
zarr.open(store=tmp_path, mode="w-")


@pytest.mark.parametrize("order", ["C", "F", None])
@pytest.mark.parametrize("zarr_format", [2, 3])
def test_array_order(order: str | None, zarr_format: int) -> None:
arr = zarr.ones(shape=(2, 2), order=order, zarr_format=zarr_format)
expected = order or zarr.config.get("array.order")
assert arr.order == expected

vals = np.asarray(arr)
if expected == "C":
assert vals.flags.c_contiguous
elif expected == "F":
assert vals.flags.f_contiguous
else:
raise AssertionError


# def test_lazy_loader():
# foo = np.arange(100)
# bar = np.arange(100, 0, -1)
Expand Down
17 changes: 17 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -417,3 +417,20 @@ def test_update_attrs(zarr_format: int) -> None:

arr2 = zarr.open_array(store=store, zarr_format=zarr_format)
assert arr2.attrs["foo"] == "bar"


@pytest.mark.parametrize("order", ["C", "F", None])
@pytest.mark.parametrize("zarr_format", [2, 3])
@pytest.mark.parametrize("store", ["memory"], indirect=True)
def test_array_create_order(order: str | None, zarr_format: int, store: MemoryStore) -> None:
arr = Array.create(store=store, shape=(2, 2), order=order, zarr_format=zarr_format, dtype="i4")
expected = order or zarr.config.get("array.order")
assert arr.order == expected

vals = np.asarray(arr)
if expected == "C":
assert vals.flags.c_contiguous
elif expected == "F":
assert vals.flags.f_contiguous
else:
raise AssertionError

0 comments on commit f71cdc1

Please sign in to comment.