-
Notifications
You must be signed in to change notification settings - Fork 0
/
kenjutsu.py
434 lines (335 loc) · 13.3 KB
/
kenjutsu.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
# -*- coding: utf-8 -*-
"""
The module ``kenjutsu`` provides support for working with ``slice``\ s.
===============================================================================
Overview
===============================================================================
The module ``kenjutsu`` provides several functions that are useful for working
with a Python ``slice`` or ``tuple`` of ``slice``\ s. This is of particular
value when working with NumPy_.
.. _NumPy: http://www.numpy.org/
===============================================================================
API
===============================================================================
"""
__author__ = "John Kirkham <kirkhamj@janelia.hhmi.org>"
__date__ = "$Sep 08, 2016 15:46:46 EDT$"
import itertools
import operator
import math
import warnings
def reformat_slice(a_slice, a_length=None):
"""
Takes a slice and reformats it to fill in as many undefined values as
possible.
Args:
a_slice(slice): a slice to reformat.
a_length(int): a length to fill for stopping if not
provided.
Returns:
(slice): a new slice with as many values filled in as
possible.
Examples:
>>> reformat_slice(slice(2, -1, None))
slice(2, -1, 1)
>>> reformat_slice(slice(2, -1, None), 10)
slice(2, 9, 1)
"""
assert (a_slice is not None), "err"
assert (a_slice.step != 0), "err"
start = a_slice.start
stop = a_slice.stop
step = a_slice.step
# Fill unknown values.
if step is None:
step = 1
if start is None:
if step > 0:
start = 0
elif step < 0:
start = -1
if (stop is None) and (step > 0):
stop = a_length
stop_i = stop is not None
# Make adjustments for length
if a_length is not None:
# Normalize out-of-bound step sizes.
if step < -a_length:
step = -a_length
elif step > a_length:
step = a_length
# Normalize bounded negative values.
if -a_length <= start < 0:
start += a_length
if stop_i and (-a_length <= stop < 0):
stop += a_length
# Handle out-of-bound limits.
if step > 0:
if (start > a_length) or (stop < -a_length):
start = stop = 0
else:
if start < -a_length:
start = 0
if stop > a_length:
stop = a_length
elif step < 0:
if (start < -a_length) or (stop_i and stop >= (a_length - 1)):
start = stop = 0
else:
if start >= a_length:
start = a_length - 1
if stop_i and stop < -a_length:
stop = None
stop_i = True
# Catch some known empty slices.
if (step > 0) and (stop == 0):
start = stop = 0
elif stop_i and (start >= 0) and (stop >= 0):
if (step > 0) and (start > stop):
start = stop = 0
elif (step < 0) and (start < stop):
start = stop = 0
new_slice = slice(start, stop, step)
return(new_slice)
def reformat_slices(slices, lengths=None):
"""
Takes a tuple of slices and reformats them to fill in as many undefined
values as possible.
Args:
slices(tuple(slice)): a tuple of slices to reformat.
lengths(tuple(int)): a tuple of lengths to fill.
Returns:
(slice): a tuple of slices with all default
values filled if possible.
Examples:
>>> reformat_slices(
... (
... slice(None),
... slice(3, None),
... slice(None, 5),
... slice(None, None, 2)
... ),
... (10, 13, 15, 20)
... )
(slice(0, 10, 1), slice(3, 13, 1), slice(0, 5, 1), slice(0, 20, 2))
"""
new_slices = slices
try:
len(new_slices)
except TypeError:
new_slices = (new_slices,)
new_lengths = lengths
if new_lengths is None:
new_lengths = [None] * len(new_slices)
try:
len(new_lengths)
except TypeError:
new_lengths = (new_lengths,)
assert (len(new_slices) == len(new_lengths))
new_slices = list(new_slices)
for i, each_length in enumerate(new_lengths):
new_slices[i] = reformat_slice(new_slices[i], each_length)
new_slices = tuple(new_slices)
return(new_slices)
class UnknownSliceLengthException(Exception):
"""
Raised if a slice does not have a known length.
"""
pass
def len_slice(a_slice, a_length=None):
"""
Determines how many elements a slice will contain.
Raises:
UnknownSliceLengthException: Will raise an exception if
a_slice.stop and a_length is None.
Args:
a_slice(slice): a slice to reformat.
a_length(int): a length to fill for stopping if not
provided.
Returns:
(slice): a new slice with as many values filled in as
possible.
Examples:
>>> len_slice(slice(2, None), 10)
8
>>> len_slice(slice(2, 6))
4
"""
new_slice = reformat_slice(a_slice, a_length)
if new_slice.stop is None:
raise UnknownSliceLengthException(
"Cannot determine slice length without a defined end point. " +
"The reformatted slice was " + repr(new_slice) + "."
)
new_slice_diff = float(new_slice.stop - new_slice.start)
new_slice_size = int(math.ceil(new_slice_diff / new_slice.step))
return(new_slice_size)
def len_slices(slices, lengths=None):
"""
Takes a tuple of slices and reformats them to fill in as many undefined
values as possible.
Args:
slices(tuple(slice)): a tuple of slices to reformat.
lengths(tuple(int)): a tuple of lengths to fill.
Returns:
(slice): a tuple of slices with all default
values filled if possible.
Examples:
>>> len_slices(
... (
... slice(None),
... slice(3, None),
... slice(None, 5),
... slice(None, None, 2)
... ),
... (10, 13, 15, 20)
... )
(10, 10, 5, 10)
"""
new_slices = reformat_slices(slices, lengths)
lens = []
for each_slice in new_slices:
lens.append(len_slice(each_slice))
lens = tuple(lens)
return(lens)
def split_blocks(space_shape, block_shape, block_halo=None):
"""
Return a list of slicings to cut each block out of an array or other.
Takes an array with ``space_shape`` and ``block_shape`` for every
dimension and a ``block_halo`` to extend each block on each side. From
this, it can compute slicings to use for cutting each block out from
the original array, HDF5 dataset or other.
Note:
Blocks on the boundary that cannot extend the full range will
be truncated to the largest block that will fit. This will raise
a warning, which can be converted to an exception, if needed.
Args:
space_shape(tuple): Shape of array to slice
block_shape(tuple): Size of each block to take
block_halo(tuple): Halo to tack on to each block
Returns:
collections.Sequence of \
tuples of slices: Provides tuples of slices for \
retrieving blocks.
Examples:
>>> split_blocks(
... (2, 3,), (1, 1,), (1, 1,)
... ) #doctest: +NORMALIZE_WHITESPACE
([(slice(0, 1, 1), slice(0, 1, 1)),
(slice(0, 1, 1), slice(1, 2, 1)),
(slice(0, 1, 1), slice(2, 3, 1)),
(slice(1, 2, 1), slice(0, 1, 1)),
(slice(1, 2, 1), slice(1, 2, 1)),
(slice(1, 2, 1), slice(2, 3, 1))],
<BLANKLINE>
[(slice(0, 2, 1), slice(0, 2, 1)),
(slice(0, 2, 1), slice(0, 3, 1)),
(slice(0, 2, 1), slice(1, 3, 1)),
(slice(0, 2, 1), slice(0, 2, 1)),
(slice(0, 2, 1), slice(0, 3, 1)),
(slice(0, 2, 1), slice(1, 3, 1))],
<BLANKLINE>
[(slice(0, 1, 1), slice(0, 1, 1)),
(slice(0, 1, 1), slice(1, 2, 1)),
(slice(0, 1, 1), slice(1, 2, 1)),
(slice(1, 2, 1), slice(0, 1, 1)),
(slice(1, 2, 1), slice(1, 2, 1)),
(slice(1, 2, 1), slice(1, 2, 1))])
"""
try:
irange = xrange
except NameError:
irange = range
try:
from itertools import ifilter, imap
except ImportError:
ifilter, imap = filter, map
if block_halo is not None:
assert (len(space_shape) == len(block_shape) == len(block_halo)), \
"The dimensions of `space_shape`, `block_shape`, and " + \
"`block_halo` should be the same."
else:
assert (len(space_shape) == len(block_shape)), \
"The dimensions of `space_shape` and `block_shape` " + \
"should be the same."
block_halo = tuple()
for i in irange(len(space_shape)):
block_halo += (0,)
vec_add = lambda a, b: imap(operator.add, a, b)
vec_sub = lambda a, b: imap(operator.sub, a, b)
vec_mul = lambda a, b: imap(operator.mul, a, b)
vec_mod = lambda a, b: imap(operator.mod, a, b)
vec_nonzero = lambda a: \
imap(lambda _: _[0], ifilter(lambda _: _[1], enumerate(a)))
vec_str = lambda a: imap(str, a)
vec_clip_floor = lambda a, a_min: \
imap(lambda _: _ if _ >= a_min else a_min, a)
vec_clip_ceil = lambda a, a_max: \
imap(lambda _: _ if _ <= a_max else a_max, a)
vec_clip = lambda a, a_min, a_max: \
vec_clip_ceil(vec_clip_floor(a, a_min), a_max)
uneven_block_division = tuple(vec_mod(space_shape, block_shape))
if any(uneven_block_division):
uneven_block_division_str = vec_nonzero(uneven_block_division)
uneven_block_division_str = vec_str(uneven_block_division_str)
uneven_block_division_str = ", ".join(uneven_block_division_str)
warnings.warn(
"Blocks will not evenly divide the array." +
" The following dimensions will be unevenly divided: %s." %
uneven_block_division_str,
RuntimeWarning
)
ranges_per_dim = []
haloed_ranges_per_dim = []
trimmed_halos_per_dim = []
for each_dim in irange(len(space_shape)):
# Construct each block using the block size given. Allow to spill over.
if block_shape[each_dim] == -1:
block_shape = (block_shape[:each_dim] +
space_shape[each_dim:each_dim+1] +
block_shape[each_dim+1:])
# Generate block ranges.
a_range = []
for i in irange(2):
offset = i * block_shape[each_dim]
this_range = irange(
offset,
offset + space_shape[each_dim],
block_shape[each_dim]
)
a_range.append(list(this_range))
# Add the halo to each block on both sides.
a_range_haloed = []
for i in irange(2):
sign = 2 * i - 1
haloed = vec_mul(
itertools.repeat(sign, len(a_range[i])),
itertools.repeat(block_halo[each_dim], len(a_range[i])),
)
haloed = vec_add(a_range[i], haloed)
haloed = vec_clip(haloed, 0, space_shape[each_dim])
a_range_haloed.append(list(haloed))
# Compute how to trim the halo off of each block.
# Clip each block to the boundaries.
a_trimmed_halo = []
for i in irange(2):
trimmed = vec_sub(a_range[i], a_range_haloed[0])
a_trimmed_halo.append(list(trimmed))
a_range[i] = list(vec_clip(a_range[i], 0, space_shape[each_dim]))
# Convert all ranges to slices for easier use.
a_range = tuple(imap(slice, *a_range))
a_range_haloed = tuple(imap(slice, *a_range_haloed))
a_trimmed_halo = tuple(imap(slice, *a_trimmed_halo))
# Format all slices.
a_range = reformat_slices(a_range)
a_range_haloed = reformat_slices(a_range_haloed)
a_trimmed_halo = reformat_slices(a_trimmed_halo)
# Collect all blocks
ranges_per_dim.append(a_range)
haloed_ranges_per_dim.append(a_range_haloed)
trimmed_halos_per_dim.append(a_trimmed_halo)
# Take all combinations of all ranges to get blocks.
blocks = list(itertools.product(*ranges_per_dim))
haloed_blocks = list(itertools.product(*haloed_ranges_per_dim))
trimmed_halos = list(itertools.product(*trimmed_halos_per_dim))
return(blocks, haloed_blocks, trimmed_halos)