Created
September 26, 2014 12:24
-
-
Save cpelley/1f1cee06eeac82981e1e to your computer and use it in GitHub Desktop.
Generator of slices which represent a mosaic indexing of an iterable object
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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