Skip to content
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
1 change: 1 addition & 0 deletions changes/3144.bugfix.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
For arrays with ``config={"write_empty_chunks: False}`` and ``fill_value=0.0`` (default), chunks containing negative zeroes are written out.
6 changes: 6 additions & 0 deletions src/zarr/core/buffer/core.py
Original file line number Diff line number Diff line change
Expand Up @@ -523,6 +523,12 @@ def all_equal(self, other: Any, equal_nan: bool = True) -> bool:
if other is None:
# Handle None fill_value for Zarr V2
return False
if other == 0.0 and self._data.dtype.kind not in ("U", "S", "T", "O", "V"):
# Handle positive and negative zero
if np.any(self._data): # Check for any truthy value
return False
# Check signs:
return np.array_equiv(np.signbit(self._data), np.signbit(other))
# use array_equal to obtain equal_nan=True functionality
# Since fill-value is a scalar, isn't there a faster path than allocating a new array for fill value
# every single time we have to write data?
Expand Down
24 changes: 24 additions & 0 deletions tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -904,6 +904,30 @@ def test_write_empty_chunks_behavior(
assert arr.nchunks_initialized == arr.nchunks


@pytest.mark.parametrize("store", ["memory"], indirect=True)
@pytest.mark.parametrize("fill_value", [0.0, -0.0])
def test_write_empty_chunks_negative_zero(
zarr_format: ZarrFormat, store: MemoryStore, fill_value: float
) -> None:
# regression test for https://github.com/zarr-developers/zarr-python/issues/3144

arr = zarr.create_array(
store=store,
shape=(2,),
zarr_format=zarr_format,
dtype="f4",
fill_value=fill_value,
chunks=(1,),
config={"write_empty_chunks": False},
)

assert arr.nchunks_initialized == 0

# initialize the with the negated fill value (-0.0 for +0.0, +0.0 for -0.0)
arr[:] = -fill_value
assert arr.nchunks_initialized == arr.nchunks
Copy link
Contributor

Choose a reason for hiding this comment

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

this test is fine but ideally we would be testing the altered function explicitly, instead of indirectly via array creation + chunk writing. this is not a blocker for this PR, just something to sort out down the road

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That test is basically copied from test_write_empty_chunks_behavior right above it. But yeah, it might be worth to have both a unit and an integration test in this case (:



@pytest.mark.parametrize(
("fill_value", "expected"),
[
Expand Down
Loading