Skip to content

Commit 61873e5

Browse files
Backport PR #3216: Write chunks with negative zero values and a zero fill value (#3349)
Co-authored-by: Bojidar Marinov <[email protected]>
1 parent 98111f5 commit 61873e5

File tree

3 files changed

+34
-0
lines changed

3 files changed

+34
-0
lines changed

changes/3144.bugfix.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Ensure that -0.0 is not considered equal to 0.0 when checking if all the values in a chunk are equal to an array's fill value.```

src/zarr/core/buffer/core.py

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -523,6 +523,15 @@ def all_equal(self, other: Any, equal_nan: bool = True) -> bool:
523523
if other is None:
524524
# Handle None fill_value for Zarr V2
525525
return False
526+
# Handle positive and negative zero by comparing bit patterns:
527+
if (
528+
np.asarray(other).dtype.kind == "f"
529+
and other == 0.0
530+
and self._data.dtype.kind not in ("U", "S", "T", "O", "V")
531+
):
532+
_data, other = np.broadcast_arrays(self._data, np.asarray(other, self._data.dtype))
533+
void_dtype = "V" + str(_data.dtype.itemsize)
534+
return np.array_equal(_data.view(void_dtype), other.view(void_dtype))
526535
# use array_equal to obtain equal_nan=True functionality
527536
# Since fill-value is a scalar, isn't there a faster path than allocating a new array for fill value
528537
# every single time we have to write data?

tests/test_array.py

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -866,6 +866,30 @@ def test_write_empty_chunks_behavior(
866866
assert arr.nchunks_initialized == arr.nchunks
867867

868868

869+
@pytest.mark.parametrize("store", ["memory"], indirect=True)
870+
@pytest.mark.parametrize("fill_value", [0.0, -0.0])
871+
@pytest.mark.parametrize("dtype", ["f4", "f2"])
872+
def test_write_empty_chunks_negative_zero(
873+
zarr_format: ZarrFormat, store: MemoryStore, fill_value: float, dtype: str
874+
) -> None:
875+
# regression test for https://github.com/zarr-developers/zarr-python/issues/3144
876+
877+
arr = zarr.create_array(
878+
store=store,
879+
shape=(2,),
880+
zarr_format=zarr_format,
881+
dtype=dtype,
882+
fill_value=fill_value,
883+
chunks=(1,),
884+
config={"write_empty_chunks": False},
885+
)
886+
assert arr.nchunks_initialized == 0
887+
888+
# initialize the with the negated fill value (-0.0 for +0.0, +0.0 for -0.0)
889+
arr[:] = -fill_value
890+
assert arr.nchunks_initialized == arr.nchunks
891+
892+
869893
@pytest.mark.parametrize(
870894
("fill_value", "expected"),
871895
[

0 commit comments

Comments
 (0)