Skip to content

Commit 946c762

Browse files
authored
Merge branch 'main' into feat/types-module
2 parents ce997d7 + 926a52f commit 946c762

File tree

4 files changed

+35
-0
lines changed

4 files changed

+35
-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.```

codecov.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ coverage:
33
patch:
44
default:
55
target: auto
6+
informational: true
67
project:
78
default:
89
target: auto

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
@@ -871,6 +871,30 @@ def test_write_empty_chunks_behavior(
871871
assert arr.nchunks_initialized == arr.nchunks
872872

873873

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

0 commit comments

Comments
 (0)