Skip to content
Merged
Show file tree
Hide file tree
Changes from all 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
1 change: 1 addition & 0 deletions packages/zarr-indexing/changes/4603.misc.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Writes whose indices land on a chunk in sorted order no longer sort them to drop repeated indices, so deduplication is linear in the number of points; writes without repeats also skip a gather copy.
18 changes: 16 additions & 2 deletions packages/zarr-indexing/src/zarr_indexing/writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -167,8 +167,22 @@ def _domain_cells(shape: tuple[int, ...]) -> np.ndarray[Any, np.dtype[np.intp]]:
return np.indices(shape, dtype=np.intp).reshape(len(shape), -1).T


def _last_occurrences(flat: np.ndarray[Any, Any]) -> np.ndarray[Any, np.dtype[np.intp]]:
"""Positions to keep so that repeated flat indices take their last value."""
def _last_occurrences(
flat: np.ndarray[Any, Any],
) -> slice | np.ndarray[Any, np.dtype[np.intp]]:
"""Positions to keep so that repeated flat indices take their last value.

A full slice means every position is kept, so callers index without a copy.
"""
if flat.size < 2:
return slice(None)
if (flat[1:] >= flat[:-1]).all():
# Non-decreasing input, the usual C-order case: repeats are adjacent,
# so no sort is needed.
steps = flat[1:] != flat[:-1]
if steps.all():
return slice(None)
return np.flatnonzero(np.append(steps, True))
_, first_in_reversed = np.unique(flat[::-1], return_index=True)
return np.asarray(len(flat) - 1 - first_in_reversed, dtype=np.intp)

Expand Down
28 changes: 28 additions & 0 deletions packages/zarr-indexing/tests/test_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -460,3 +460,31 @@ def read_into(self, source: Any, context: ReadContext, out: Any, /) -> None:
view.write([7, 8, 9])
np.testing.assert_array_equal(source, [0, 7, 8, 9, 4, 5])
np.testing.assert_array_equal(view.result(), [1, 2, 3])


@pytest.mark.parametrize(
"flat",
[
[],
[3],
[0, 2, 5, 7],
[0, 0, 2, 5, 5, 5, 7],
[5, 0, 5, 2, 0, 7],
[7, 5, 5, 2, 0],
],
ids=["empty", "single", "sorted", "sorted_repeats", "unsorted_repeats", "reversed"],
)
def test_last_occurrences_matches_sequential_assignment(flat: list[int]) -> None:
from zarr_indexing.writer import _last_occurrences

indices = np.asarray(flat, dtype=np.intp)
values = np.arange(1, indices.size + 1)
expected = np.zeros(8, dtype=values.dtype)
for index, value in zip(indices, values, strict=True):
expected[index] = value
keep = _last_occurrences(indices)
kept = indices[keep]
assert np.unique(kept).size == kept.size
actual = np.zeros(8, dtype=values.dtype)
actual[kept] = values[keep]
np.testing.assert_array_equal(actual, expected)
Loading