Skip to content

Instantly share code, notes, and snippets.

@GenevieveBuckley
Last active November 10, 2021 00:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save GenevieveBuckley/decd23c22ee3417f7d78e87f791bc081 to your computer and use it in GitHub Desktop.
Save GenevieveBuckley/decd23c22ee3417f7d78e87f791bc081 to your computer and use it in GitHub Desktop.
slices_from_chunks_overlap
# Proposed slices_from_chunks_overlap function
# Mofified from slices_from_chunks from dask.array.core
from itertools import product
from dask.array.slicing import cached_cumsum
def slices_from_chunks_overlap(chunks, array_shape, depth=1):
"""Translate chunks tuple to a set of slices in product order
Parameters
----------
chunks : tuple
The chunks of the corresponding dask array.
array_shape : tuple
Shape of the corresponding dask array.
depth : int
The number of pixels to overlap, providing we're not at the array edge.
Example
-------
>>> slices_from_chunks_overlap(((4,), (7, 7)), (4, 14), depth=1) # doctest: +NORMALIZE_WHITESPACE
[(slice(0, 5, None), slice(0, 8, None)),
(slice(0, 5, None), slice(6, 15, None))]
"""
cumdims = [cached_cumsum(bds, initial_zero=True) for bds in chunks]
slices = []
for starts, shapes, maxshape in zip(cumdims, chunks, array_shape):
inner_slices = []
for s, dim in zip(starts, shapes):
slice_start = max(0, s - depth)
slice_stop = min(s + dim + depth, maxshape)
inner_slices.append(slice(slice_start, slice_stop))
slices.append(inner_slices)
return list(product(*slices))
@GenevieveBuckley
Copy link
Author

Fixed bug with Juan, 2021-11-10

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment