Skip to content

Commit

Permalink
(chore): clean up offsets creation
Browse files Browse the repository at this point in the history
  • Loading branch information
ilan-gold committed Dec 10, 2024
1 parent d252557 commit e45a05f
Showing 1 changed file with 8 additions and 13 deletions.
21 changes: 8 additions & 13 deletions src/anndata/_core/sparse_dataset.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,28 +269,23 @@ def get_compressed_vectors_for_slices(
x: BackedSparseMatrix, slices: Iterable[slice]
) -> tuple[Sequence, Sequence, Sequence]:
indptr_indices = [x.indptr[slice(s.start, s.stop + 1)] for s in slices]
indptr_limits = [(i[0], i[-1]) for i in indptr_indices]
indptr_limits = [slice(i[0], i[-1]) for i in indptr_indices]
# HDF5 cannot handle out-of-order integer indexing
if isinstance(x.data, ZarrArray):
indptr_int = np.concatenate(
[np.arange(start, stop) for start, stop in indptr_limits]
)
indptr_int = np.concatenate([np.arange(s.start, s.stop) for s in indptr_limits])
data = x.data[indptr_int]
indices = x.indices[indptr_int]
else:
data = np.concatenate([x.data[start:stop] for start, stop in indptr_limits])
indices = np.concatenate(
[x.indices[start:stop] for start, stop in indptr_limits]
)
data = np.concatenate([x.data[s] for s in indptr_limits])
indices = np.concatenate([x.indices[s] for s in indptr_limits])
# Need to track the size of the gaps in the slices to each indptr subselection
offsets = [indptr_indices[0][0]]
for (_, stop0), (start1, _) in pairwise(indptr_limits):
offsets.append((start1 - stop0) + offsets[-1])
start_indptr = indptr_indices[0] - offsets[0]
gaps = (s1.start - s0.stop for s0, s1 in pairwise(indptr_limits))
offsets = accumulate(chain([indptr_limits[0].start], gaps))
start_indptr = indptr_indices[0] - next(offsets)
if len(slices) < 2: # there is only one slice so no need to concatenate
return data, indices, start_indptr
end_indptr = np.concatenate(
[s[1:] - offsets[i + 1] for i, s in enumerate(indptr_indices[1:])]
[s[1:] - o for s, o in zip(indptr_indices[1:], offsets)]
)
indptr = np.concatenate([start_indptr, end_indptr])
return data, indices, indptr
Expand Down

0 comments on commit e45a05f

Please sign in to comment.