Skip to content

Commit 2c19072

Browse files
committed
improve test
1 parent 9a32d1f commit 2c19072

File tree

2 files changed

+40
-22
lines changed

2 files changed

+40
-22
lines changed

src/zarr/api/asynchronous.py

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
from zarr.core.group import AsyncGroup, ConsolidatedMetadata, GroupMetadata
2828
from zarr.core.metadata import ArrayMetadataDict, ArrayV2Metadata, ArrayV3Metadata
2929
from zarr.core.metadata.v2 import _default_filters_and_compressor
30+
from zarr.core.sync import sync
3031
from zarr.errors import NodeTypeValidationError
3132
from zarr.storage import (
3233
StoreLike,
@@ -535,7 +536,7 @@ async def tree(grp: AsyncGroup, expand: bool | None = None, level: int | None =
535536

536537

537538
async def array(
538-
data: npt.ArrayLike, **kwargs: Any
539+
data: npt.ArrayLike | Array, **kwargs: Any
539540
) -> AsyncArray[ArrayV2Metadata] | AsyncArray[ArrayV3Metadata]:
540541
"""Create an array filled with `data`.
541542
@@ -553,11 +554,17 @@ async def array(
553554
"""
554555

555556
if isinstance(data, Array):
556-
chunks = kwargs.pop("chunks", None) or data.chunks
557-
new_array = await create(shape=data.shape, chunks=chunks, dtype=data.dtype, **kwargs)
557+
# fill missing arguments with metadata of data Array
558+
kwargs.setdefault("dtype", data.dtype)
559+
kwargs.setdefault("attributes", data.attrs)
560+
kwargs.setdefault("chunks", data.chunks)
561+
kwargs.setdefault("fill_value", data.fill_value)
562+
563+
new_array = await create(data.shape, **kwargs)
558564

559565
async def _copy_chunk(chunk_coords: ChunkCoords) -> None:
560-
await new_array.setitem(chunk_coords, await data._async_array.getitem(chunk_coords))
566+
arr = await data._async_array.getitem(chunk_coords)
567+
await new_array.setitem(chunk_coords, arr)
561568

562569
# Stream data from the source array to the new array
563570
await concurrent_map(

tests/test_array.py

Lines changed: 29 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
import numpy as np
1111
import pytest
1212
from numcodecs import Zstd
13+
from numpy import dtype
14+
from numpy.ma.testutils import assert_array_equal
1315

1416
import zarr.api.asynchronous
1517
from zarr import Array, AsyncArray, Group
@@ -892,21 +894,30 @@ async def test_scalar_array() -> None:
892894

893895

894896
async def test_creation_from_other_zarr(tmpdir):
895-
src = zarr.zeros(
896-
(2000, 20000), chunks=(1000, 1000), dtype="uint8", store=LocalStore(str(tmpdir))
897-
)
898-
src[:] = 1
899-
for _i in range(10):
900-
start_time = time.time()
901-
c = zarr.array(src, store=MemoryStore())
902-
end_time = time.time()
903-
print(f"Time fast: {end_time - start_time} seconds")
904-
905-
start_time = time.time()
906-
b = zarr.zeros(src.shape, chunks=src.chunks, store=MemoryStore())
907-
b[:] = src[:]
908-
end_time = time.time()
909-
print(f"Time slow: {end_time - start_time} seconds")
910-
911-
assert b[123, 123] == 1
912-
assert c[123, 123] == 1
897+
src_fill_value = 2
898+
src_dtype = np.dtype("uint8")
899+
src_attributes = {}
900+
src_chunks = (2, 2)
901+
902+
src = zarr.create((10, 10), chunks=src_chunks, dtype=src_dtype, store=LocalStore(str(tmpdir)), fill_value = src_fill_value, attributes=src_attributes)
903+
src[:] = np.arange(100).reshape((10,10))
904+
905+
result = zarr.array(src, store=MemoryStore())
906+
assert_array_equal(result[:], src[:])
907+
assert result.fill_value == src_fill_value
908+
assert result.dtype==src_dtype
909+
assert result.attrs.asdict() == src_attributes
910+
assert result.chunks == src_chunks
911+
912+
new_fill_value = 3
913+
new_dtype = np.dtype("uint16")
914+
new_attributes = {"foo":"bar"}
915+
new_chunks = (5, 10)
916+
917+
result2 = zarr.array(src, store=MemoryStore(), chunks=new_chunks, dtype=new_dtype, fill_value = new_fill_value, attributes=new_attributes)
918+
919+
assert_array_equal(result2[:], src[:])
920+
assert result2.fill_value == new_fill_value
921+
assert result2.dtype == new_dtype
922+
assert result2.attrs == new_attributes
923+
assert result2.chunks == new_chunks

0 commit comments

Comments
 (0)