Skip to content

Commit

Permalink
Fix off-by-one error in overlap_tuple
Browse files Browse the repository at this point in the history
  • Loading branch information
peytondmurray committed Apr 14, 2024
1 parent c014024 commit 99d010a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 4 deletions.
6 changes: 3 additions & 3 deletions versioned_hdf5/backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -1323,7 +1323,7 @@ def write_to_dataset(
written by this function.
"""
raw_data: Dataset = f["_version_data"][name]["raw_data"]
chunk_size = tuple(raw_data.attrs["chunks"])[0]
chunk_size = raw_data.chunks[0]

if isinstance(data, np.ndarray) and raw_data.dtype != data.dtype:
raise ValueError(
Expand All @@ -1334,8 +1334,8 @@ def write_to_dataset(
slices: Dict[Tuple, Tuple] = {}
with Hashtable(f, name) as hashtable:
for data_slice, vchunk in zip(
partition(data, chunk_size),
partition(virtual_slice, chunk_size),
partition(data, raw_data.chunks),
partition(virtual_slice, raw_data.chunks),
strict=True,
):
arr = data[data_slice.raw]
Expand Down
2 changes: 1 addition & 1 deletion versioned_hdf5/slicetools.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ def overlap(index1: Slice, index2: Slice) -> Optional[Slice]:
overlapping = Slice(max(index1.start, index2.start), min(index1.stop, index2.stop))
# If the slices don't overlap, the start index will be greater than
# the stop index
if overlapping.start > overlapping.stop:
if overlapping.start >= overlapping.stop:
return None
return overlapping

Expand Down
10 changes: 10 additions & 0 deletions versioned_hdf5/tests/test_backend.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
DEFAULT_CHUNK_SIZE,
create_base_dataset,
create_virtual_dataset,
overlap_tuple,
write_dataset,
write_dataset_chunks,
)
Expand Down Expand Up @@ -800,3 +801,12 @@ def test_create_empty_virtual_dataset(setup_vfile):
assert_equal(ds, np.array([]))
assert ds.shape == (0,)
assert ds.size == 0


def test_overlap_tuple():
"""Test that multidimensional indices which don't overlap have no overlap_tuple."""
overlapping = overlap_tuple(
Tuple(slice(0, 3, 1), slice(15100, 15200, 1), slice(0, 2, 1)),
Tuple(slice(0, 2, 1), slice(15200, 15220, 1), slice(0, 2, 1)),
)
assert overlapping is None

0 comments on commit 99d010a

Please sign in to comment.