|
2 | 2 | import json |
3 | 3 | import math |
4 | 4 | import pickle |
| 5 | +import time |
5 | 6 | from itertools import accumulate |
6 | 7 | from typing import Any, Literal |
7 | 8 |
|
@@ -865,6 +866,52 @@ async def test_special_complex_fill_values_roundtrip(fill_value: Any, expected: |
865 | 866 | assert actual["fill_value"] == expected |
866 | 867 |
|
867 | 868 |
|
| 869 | +async def test_creation_from_other_zarr(tmpdir): |
| 870 | + src = zarr.zeros( |
| 871 | + (2000, 20000), chunks=(1000, 1000), dtype="uint8", store=LocalStore(str(tmpdir)) |
| 872 | + ) |
| 873 | + src[:] = 1 |
| 874 | + for _i in range(10): |
| 875 | + start_time = time.time() |
| 876 | + c = zarr.array(src, store=MemoryStore()) |
| 877 | + end_time = time.time() |
| 878 | + print(f"Time fast: {end_time - start_time} seconds") |
| 879 | + |
| 880 | + start_time = time.time() |
| 881 | + b = zarr.zeros(src.shape, chunks=src.chunks, store=MemoryStore()) |
| 882 | + b[:] = src[:] |
| 883 | + end_time = time.time() |
| 884 | + print(f"Time slow: {end_time - start_time} seconds") |
| 885 | + |
| 886 | + assert b[123, 123] == 1 |
| 887 | + assert c[123, 123] == 1 |
| 888 | + |
| 889 | + |
| 890 | +@pytest.mark.parametrize("shape", [(1,), (2, 3), (4, 5, 6)]) |
| 891 | +@pytest.mark.parametrize("dtype", ["uint8", "float32"]) |
| 892 | +@pytest.mark.parametrize("array_type", ["async", "sync"]) |
| 893 | +async def test_nbytes( |
| 894 | + shape: tuple[int, ...], dtype: str, array_type: Literal["async", "sync"] |
| 895 | +) -> None: |
| 896 | + """ |
| 897 | + Test that the ``nbytes`` attribute of an Array or AsyncArray correctly reports the capacity of |
| 898 | + the chunks of that array. |
| 899 | + """ |
| 900 | + store = MemoryStore() |
| 901 | + arr = Array.create(store=store, shape=shape, dtype=dtype, fill_value=0) |
| 902 | + if array_type == "async": |
| 903 | + assert arr._async_array.nbytes == np.prod(arr.shape) * arr.dtype.itemsize |
| 904 | + else: |
| 905 | + assert arr.nbytes == np.prod(arr.shape) * arr.dtype.itemsize |
| 906 | + |
| 907 | + |
| 908 | +async def test_scalar_array() -> None: |
| 909 | + arr = zarr.array(1.5) |
| 910 | + assert arr[...] == 1.5 |
| 911 | + assert arr[()] == 1.5 |
| 912 | + assert arr.shape == () |
| 913 | + |
| 914 | + |
868 | 915 | @pytest.mark.parametrize("shape", [(1,), (2, 3), (4, 5, 6)]) |
869 | 916 | @pytest.mark.parametrize("dtype", ["uint8", "float32"]) |
870 | 917 | @pytest.mark.parametrize("array_type", ["async", "sync"]) |
|
0 commit comments