Skip to content

Instantly share code, notes, and snippets.

@cpelley
Created September 26, 2014 12:24
Show Gist options
  • Save cpelley/1f1cee06eeac82981e1e to your computer and use it in GitHub Desktop.
Save cpelley/1f1cee06eeac82981e1e to your computer and use it in GitHub Desktop.
Generator of slices which represent a mosaic indexing of an iterable object
def mosaic(iterable_shape, subdivided_shape):
"""
Return a generator of slices that represent a subdivided indexing of an iterable of specified shape.
"""
shape = subdivided_shape
if len(iterable_shape) != len(shape):
raise ValueError('Array shape and resulting shape mismatch, broadcasting not supported.')
# iterate through this array with shape (shape)
subdomain = [[slice(itt*shape[dim], itt*shape[dim] + shape[dim]) for itt in
range(int(math.ceil(iterable_shape[dim]/float(shape[dim]))))] for
dim in range(len(shape))]
return itertools.product(*subdomain)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment