-
Notifications
You must be signed in to change notification settings - Fork 0
/
format.py
360 lines (293 loc) · 11.1 KB
/
format.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
__author__ = "John Kirkham <kirkhamj@janelia.hhmi.org>"
__date__ = "$Dec 08, 2016 14:20:52 GMT-0500$"
import collections
import itertools
import numbers
def index_to_slice(index):
"""
Convert an index to a slice.
Note:
A single index behaves differently from a length 1 ``slice``. When
applying the former one reduces that dimension; whereas, applying
the latter results in a singleton dimension being retained. Also
if an index is out of bounds, one gets an ``IndexError``. However,
with an out of bounds length 1 ``slice``, one simply doesn't get
the requested range.
Args:
index(int): an index to convert to a slice
Returns:
(slice): a slice corresponding to the index
Examples:
>>> index_to_slice(1)
slice(1, 2, 1)
>>> index_to_slice(-1)
slice(-1, -2, -1)
"""
if not isinstance(index, numbers.Integral):
raise TypeError(
"Expected an integral type. Instead got `%s`." % str(index)
)
step = -1 if index < 0 else 1
return slice(index, index + step, step)
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)
"""
new_slice = a_slice
if (new_slice is Ellipsis) or (new_slice == tuple()):
new_slice = slice(None)
elif isinstance(a_slice, numbers.Integral):
new_slice = index_to_slice(a_slice)
elif isinstance(a_slice, collections.Sequence):
if not all(map(lambda i: isinstance(i, numbers.Integral), a_slice)):
raise ValueError(
"Arbitrary sequences not permitted."
" All elements must be of integral type."
)
# Normalize each integer in the range.
new_slice = []
for i in a_slice:
new_slice.append(reformat_slice(i, a_length))
return new_slice
elif not isinstance(a_slice, slice):
raise ValueError(
"Expected a `slice` type. Instead got `%s`." % str(a_slice)
)
if new_slice.step == 0:
raise ValueError("Slice cannot have a step size of `0`.")
start = new_slice.start
stop = new_slice.stop
step = new_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
step = 1
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
step = 1
else:
if start >= a_length:
start = a_length - 1
if stop_i and stop < -a_length:
stop = None
stop_i = False
# Catch some known empty slices.
if stop_i and (start == stop):
start = stop = 0
step = 1
elif (step > 0) and (stop == 0):
start = stop = 0
step = 1
elif (step < 0) and (stop == -1):
start = stop = 0
step = 1
elif stop_i and (start >= 0) and (stop >= 0):
if (step > 0) and (start > stop):
start = stop = 0
step = 1
elif (step < 0) and (start < stop):
start = stop = 0
step = 1
new_slice = slice(start, stop, step)
if isinstance(a_slice, numbers.Integral):
if new_slice.start == new_slice.stop == 0:
raise IndexError("Index out of range.")
new_slice = new_slice.start
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
if new_slices == tuple():
new_slices = Ellipsis
try:
len(new_slices)
except TypeError:
new_slices = (new_slices,)
new_lengths = lengths
try:
if new_lengths is not None:
len(new_lengths)
except TypeError:
new_lengths = (new_lengths,)
el_idx = None
try:
el_idx = new_slices.index(Ellipsis)
except ValueError:
pass
if new_lengths is not None and el_idx is None:
if len(new_slices) < len(new_lengths):
new_slices += (Ellipsis,)
el_idx = new_slices.index(Ellipsis)
elif len(new_slices) > len(new_lengths):
raise ValueError(
"Shape must be as large or larger than the number of slices."
)
elif new_lengths is not None:
if (len(new_slices) - 1) > len(new_lengths):
raise ValueError(
"Shape must be as large or larger than the number of slices"
" without the Ellipsis."
)
if el_idx is not None:
# Break into three cases.
#
# 1. Before the Ellipsis
# 2. The Ellipsis
# 3. After the Ellipsis
#
# Cases 1 and 3 are trivially solved as before.
# Case 2 is either a no-op or a bunch of `slice(None)`s.
#
# The result is a combination of all of these.
slices_before = new_slices[:el_idx]
slices_after = new_slices[el_idx+1:]
if Ellipsis in slices_before or Ellipsis in slices_after:
raise ValueError("Only one Ellipsis is permitted. Found multiple.")
new_lengths_before = None
new_lengths_after = None
slice_el = (Ellipsis,)
if new_lengths is not None:
pos_before = len(slices_before)
pos_after = len(new_lengths) - len(slices_after)
new_lengths_before = new_lengths[:pos_before]
new_lengths_after = new_lengths[pos_after:]
new_lengths_el = new_lengths[pos_before:pos_after]
slice_el = len(new_lengths_el) * (slice(None),)
if slice_el:
slice_el = reformat_slices(
slice_el,
new_lengths_el
)
if slices_before:
slices_before = reformat_slices(slices_before, new_lengths_before)
if slices_after:
slices_after = reformat_slices(slices_after, new_lengths_after)
new_slices = slices_before + slice_el + slices_after
else:
if new_lengths is None:
new_lengths = [None] * len(new_slices)
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)
n_seqs = sum(map(
lambda i: isinstance(i, collections.Sequence), new_slices
))
if n_seqs > 1:
raise ValueError(
"Only one integral sequence supported."
" Instead got `%s`." % str(n_seqs)
)
return(new_slices)
def split_indices(slices):
"""
Splits slices with multiple indices into multiple splices.
Support of slices with a sequence of indices is varied. Some
libraries like NumPy support them without issues. Other libraries
like h5py support them as long as they are in sequential order.
In still other libraries support is non-existent. However, in
all those cases normally a single index is permissible. This
converts slices with multiple indices into a list of slices with
a single index each. While this still leaves it up to the user
to iterate over these and combine the results in some sensible
way, it is better than just getting a failure and should extend
well to a variety of cases.
Args:
slices(tuple(slice)): a tuple of slices to split
Returns:
(list(tuple(slice))): a list of a tuple of slices
Examples:
>>> split_indices(
... (
... 3,
... Ellipsis,
... [0, 1, 2],
... slice(2, 5),
... slice(4, 6, 2)
... )
... ) # doctest: +NORMALIZE_WHITESPACE
[(3, Ellipsis, slice(0, 1, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(1, 2, 1), slice(2, 5, 1), slice(4, 6, 2)),
(3, Ellipsis, slice(2, 3, 1), slice(2, 5, 1), slice(4, 6, 2))]
"""
ref_slices = reformat_slices(slices)
mtx_slices = []
for each_dim_slice in ref_slices:
if each_dim_slice is Ellipsis:
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, numbers.Integral):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, slice):
mtx_slices.append([each_dim_slice])
elif isinstance(each_dim_slice, collections.Sequence):
new_slice = []
for i in each_dim_slice:
new_slice.append(index_to_slice(i))
mtx_slices.append(new_slice)
result_slices = []
for each_dim_slice in itertools.product(*mtx_slices):
result_slices.append(tuple(each_dim_slice))
return result_slices