Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use implicit fill values for zarr v2 #2274

Merged
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 16 additions & 2 deletions src/zarr/codecs/pipeline.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
from zarr.core.common import ChunkCoords, concurrent_map
from zarr.core.config import config
from zarr.core.indexing import SelectorTuple, is_scalar, is_total_slice
from zarr.core.metadata.v2 import default_fill_value
from zarr.registry import register_pipeline

if TYPE_CHECKING:
Expand Down Expand Up @@ -247,7 +248,17 @@ async def read_batch(
if chunk_array is not None:
out[out_selection] = chunk_array
else:
out[out_selection] = chunk_spec.fill_value
fill_value = chunk_spec.fill_value

if fill_value is None:
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Something we may consider in the future is to parameterize the CodecPipeline class with the zarr_format of the calling Array. This would allow us feel more confident that workarounds like this one only apply to v2 data.

# Zarr V2 allowed `fill_value` to be null in the metadata.
# Zarr V3 requires it to be set. This has already been
# validated when decoding the metadata, but we support reading
# Zarr V2 data and need to support the case where fill_value
# is None.
fill_value = default_fill_value(dtype=chunk_spec.dtype)

out[out_selection] = fill_value
else:
chunk_bytes_batch = await concurrent_map(
[
Expand All @@ -274,7 +285,10 @@ async def read_batch(
tmp = tmp.squeeze(axis=drop_axes)
out[out_selection] = tmp
else:
out[out_selection] = chunk_spec.fill_value
fill_value = chunk_spec.fill_value
if fill_value is None:
fill_value = default_fill_value(dtype=chunk_spec.dtype)
out[out_selection] = fill_value

def _merge_chunk_array(
self,
Expand Down
2 changes: 1 addition & 1 deletion src/zarr/core/array.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,7 +376,7 @@ async def _create_v2(
chunks=chunks,
order=order,
dimension_separator=dimension_separator,
fill_value=0 if fill_value is None else fill_value,
fill_value=fill_value,
compressor=compressor,
filters=filters,
attributes=attributes,
Expand Down
16 changes: 16 additions & 0 deletions src/zarr/core/metadata/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -284,3 +284,19 @@ def parse_fill_value(fill_value: object, dtype: np.dtype[Any]) -> Any:
raise ValueError(msg) from e

return fill_value


def default_fill_value(dtype: np.dtype[Any]) -> Any:
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
"""
Get the default fill value for a type.

Notes
-----
This differs from :func:`parse_fill_value`, which parses a fill value
stored in the Array metadata into an in-memory value. This only gives
the default fill value for some type.

This is useful for reading Zarr V2 arrays, which allow the fill
value to be unspecified.
"""
return dtype.type(0)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
3 changes: 2 additions & 1 deletion src/zarr/testing/strategies.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,8 @@ def arrays(
)

assert isinstance(a, Array)
assert a.fill_value is not None
if a.metadata.zarr_format == 3:
assert a.fill_value is not None
assert isinstance(root[array_path], Array)
assert nparray.shape == a.shape
assert chunks == a.chunks
Expand Down
2 changes: 1 addition & 1 deletion tests/v3/test_metadata/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def test_parse_zarr_format_invalid(data: Any) -> None:
@pytest.mark.parametrize("attributes", [None, {"foo": "bar"}])
@pytest.mark.parametrize("filters", [None, (), (numcodecs.GZip(),)])
@pytest.mark.parametrize("compressor", [None, numcodecs.GZip()])
@pytest.mark.parametrize("fill_value", [0, 1])
@pytest.mark.parametrize("fill_value", [None, 0, 1])
@pytest.mark.parametrize("order", ["C", "F"])
@pytest.mark.parametrize("dimension_separator", [".", "/", None])
def test_metadata_to_dict(
Expand Down
9 changes: 9 additions & 0 deletions tests/v3/test_v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,15 @@ def test_simple(store: StorePath) -> None:
assert np.array_equal(data, a[:, :])


def test_implicit_fill_value(store: StorePath) -> None:
arr = zarr.open_array(store=store, shape=(4,), fill_value=None, zarr_format=2)
TomAugspurger marked this conversation as resolved.
Show resolved Hide resolved
assert arr.metadata.fill_value is None
assert arr.metadata.to_dict()["fill_value"] is None
result = arr[:]
expected = np.zeros(arr.shape, dtype=arr.dtype)
np.testing.assert_array_equal(result, expected)


def test_codec_pipeline() -> None:
# https://github.com/zarr-developers/zarr-python/issues/2243
store = MemoryStore(mode="w")
Expand Down