Skip to content

Use implicit fill values for zarr v2 #2274

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

Merged
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
14 changes: 12 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,13 @@ 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

# this should maybe be version specific?
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.

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 +281,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
9 changes: 8 additions & 1 deletion src/zarr/core/metadata/v2.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,10 @@ def __init__(
order_parsed = parse_indexing_order(order)
dimension_separator_parsed = parse_separator(dimension_separator)
filters_parsed = parse_filters(filters)
fill_value_parsed = parse_fill_value(fill_value, dtype=data_type_parsed)
if fill_value is None:
fill_value_parsed = None
else:
fill_value_parsed = parse_fill_value(fill_value, dtype=data_type_parsed)
attributes_parsed = parse_attributes(attributes)

object.__setattr__(self, "shape", shape_parsed)
Expand Down Expand Up @@ -273,3 +276,7 @@ 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:
return dtype.type(0)
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
11 changes: 11 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)
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 All @@ -46,3 +55,5 @@ def test_codec_pipeline() -> None:
result = array[:]
expected = np.ones(1)
np.testing.assert_array_equal(result, expected)


Loading