|
| 1 | +from typing import Any, Iterable, Literal, Tuple, Dict |
| 2 | +import numpy as np |
| 3 | +import numpy.typing as npt |
| 4 | +import numcodecs |
| 5 | +from hypothesis import strategies as st |
| 6 | +import hypothesis.extra.numpy as npst |
| 7 | +from hypothesis import assume |
| 8 | +from dataclasses import dataclass, field |
| 9 | + |
| 10 | +from zarr.codecs.bytes import BytesCodec |
| 11 | +from zarr.core.chunk_grids import RegularChunkGrid, ChunkGrid |
| 12 | +from zarr.core.chunk_key_encodings import DefaultChunkKeyEncoding, ChunkKeyEncoding |
| 13 | +from zarr.core.metadata.v2 import ArrayV2Metadata |
| 14 | +from zarr.core.metadata.v3 import ArrayV3Metadata |
| 15 | +from zarr.core.chunk_key_encodings import ChunkKeyEncoding, ChunkKeyEncodingLike |
| 16 | + |
| 17 | + |
| 18 | +from .dtypes import v2_dtypes, v3_dtypes |
| 19 | + |
| 20 | +def simple_text(): |
| 21 | + """A strategy for generating simple text strings.""" |
| 22 | + return st.text(st.characters(min_codepoint=32, max_codepoint=126), min_size=1, max_size=10) |
| 23 | + |
| 24 | + |
| 25 | +def simple_attrs(): |
| 26 | + """A strategy for generating simple attribute dictionaries.""" |
| 27 | + return st.dictionaries( |
| 28 | + simple_text(), |
| 29 | + st.one_of(st.integers(), |
| 30 | + st.floats(allow_nan=False, allow_infinity=False), |
| 31 | + st.booleans(), |
| 32 | + simple_text())) |
| 33 | + |
| 34 | + |
| 35 | +def array_shapes(min_dims=1, max_dims=3, max_len=100): |
| 36 | + """A strategy for generating array shapes.""" |
| 37 | + return st.lists(st.integers(min_value=1, max_value=max_len), min_size=min_dims, max_size=max_dims) |
| 38 | + |
| 39 | + |
| 40 | +# def zarr_compressors(): |
| 41 | +# """A strategy for generating Zarr compressors.""" |
| 42 | +# return st.sampled_from([None, Blosc(), GZip(), Zstd(), LZ4()]) |
| 43 | + |
| 44 | + |
| 45 | +# def zarr_codecs(): |
| 46 | +# """A strategy for generating Zarr codecs.""" |
| 47 | +# return st.sampled_from([BytesCodec(), Blosc(), GZip(), Zstd(), LZ4()]) |
| 48 | + |
| 49 | + |
| 50 | +def zarr_filters(): |
| 51 | + """A strategy for generating Zarr filters.""" |
| 52 | + return st.lists(st.just(numcodecs.Delta(dtype='i4')), min_size=0, max_size=2) # Example filter, expand as needed |
| 53 | + |
| 54 | + |
| 55 | +def zarr_storage_transformers(): |
| 56 | + """A strategy for generating Zarr storage transformers.""" |
| 57 | + return st.lists(st.dictionaries(simple_text(), st.one_of(st.integers(), st.floats(), st.booleans(), simple_text())), min_size=0, max_size=2) |
| 58 | + |
| 59 | + |
| 60 | +@st.composite |
| 61 | +def array_metadata_v2(draw: st.DrawFn) -> ArrayV2Metadata: |
| 62 | + """Generates valid ArrayV2Metadata objects for property-based testing.""" |
| 63 | + dims = draw(st.integers(min_value=1, max_value=3)) # Limit dimensions for complexity |
| 64 | + shape = tuple(draw(array_shapes(min_dims=dims, max_dims=dims, max_len=100))) |
| 65 | + max_chunk_len = max(shape) if shape else 100 |
| 66 | + chunks = tuple(draw(st.lists(st.integers(min_value=1, max_value=max_chunk_len), min_size=dims, max_size=dims))) |
| 67 | + |
| 68 | + # Validate shape and chunks relationship |
| 69 | + assume(all(c <= s for s, c in zip(shape, chunks))) # Chunk size must be <= shape |
| 70 | + |
| 71 | + dtype = draw(v2_dtypes()) |
| 72 | + fill_value = draw(st.one_of([st.none(), npst.from_dtype(dtype)])) |
| 73 | + order = draw(st.sampled_from(["C", "F"])) |
| 74 | + dimension_separator = draw(st.sampled_from([".", "/"])) |
| 75 | + #compressor = draw(zarr_compressors()) |
| 76 | + filters = tuple(draw(zarr_filters())) if draw(st.booleans()) else None |
| 77 | + attributes = draw(simple_attrs()) |
| 78 | + |
| 79 | + # Construct the metadata object. Type hints are crucial here for correctness. |
| 80 | + return ArrayV2Metadata( |
| 81 | + shape=shape, |
| 82 | + dtype=dtype, |
| 83 | + chunks=chunks, |
| 84 | + fill_value=fill_value, |
| 85 | + order=order, |
| 86 | + dimension_separator=dimension_separator, |
| 87 | + # compressor=compressor, |
| 88 | + filters=filters, |
| 89 | + attributes=attributes, |
| 90 | + ) |
| 91 | + |
| 92 | + |
| 93 | +@st.composite |
| 94 | +def array_metadata_v3(draw: st.DrawFn) -> ArrayV3Metadata: |
| 95 | + """Generates valid ArrayV3Metadata objects for property-based testing.""" |
| 96 | + dims = draw(st.integers(min_value=1, max_value=3)) |
| 97 | + shape = tuple(draw(array_shapes(min_dims=dims, max_dims=dims, max_len=100))) |
| 98 | + max_chunk_len = max(shape) if shape else 100 |
| 99 | + chunks = tuple(draw(st.lists(st.integers(min_value=1, max_value=max_chunk_len), min_size=dims, max_size=dims))) |
| 100 | + assume(all(c <= s for s, c in zip(shape, chunks))) |
| 101 | + |
| 102 | + dtype = draw(v3_dtypes()) |
| 103 | + fill_value = draw(npst.from_dtype(dtype)) |
| 104 | + chunk_grid = RegularChunkGrid(chunks) # Ensure chunks is passed as tuple. |
| 105 | + chunk_key_encoding = DefaultChunkKeyEncoding(separator="/") # Or st.sampled_from(["/", "."]) |
| 106 | + #codecs = tuple(draw(st.lists(zarr_codecs(), min_size=0, max_size=3))) |
| 107 | + attributes = draw(simple_attrs()) |
| 108 | + dimension_names = tuple(draw(st.lists(st.one_of(st.none(), simple_text()), min_size=dims, max_size=dims))) if draw(st.booleans()) else None |
| 109 | + storage_transformers = tuple(draw(zarr_storage_transformers())) |
| 110 | + |
| 111 | + return ArrayV3Metadata( |
| 112 | + shape=shape, |
| 113 | + data_type=dtype, |
| 114 | + chunk_grid=chunk_grid, |
| 115 | + chunk_key_encoding=chunk_key_encoding, |
| 116 | + fill_value=fill_value, |
| 117 | + # codecs=codecs, |
| 118 | + attributes=attributes, |
| 119 | + dimension_names=dimension_names, |
| 120 | + storage_transformers=storage_transformers, |
| 121 | + ) |
0 commit comments