-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGrid.py
More file actions
622 lines (514 loc) · 22.1 KB
/
Copy pathGrid.py
File metadata and controls
622 lines (514 loc) · 22.1 KB
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
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
from typing import Tuple, Sequence, Optional, List, Generator, Dict
import bisect
import abc
from ._subset import _is_single_subset_noop
class AbstractGrid(abc.ABC):
"""
Abstract base class for array grids. Each grid subdivides an array to
determine how it should be iterated over; this is useful for ensuring that
iteration respects the physical layout of an array.
Subclasses should define the ``shape``, ``boundaries`` and ``cost``
properties, as well as the ``subset``, ``transpose`` and ``iterate``
methods; see the :py:class:`~SimpleGrid` and :py:class:`~CompositeGrid`
subclasses for examples.
"""
@property
@abc.abstractmethod
def shape(self) -> Tuple[int, ...]:
pass
@property
@abc.abstractmethod
def cost(self) -> int:
pass
@property
@abc.abstractmethod
def boundaries(self) -> Tuple[Sequence[int], ...]:
pass
@abc.abstractmethod
def transpose(self, perm: Tuple[int, ...]) -> "AbstractGrid":
pass
@abc.abstractmethod
def subset(self, subset: Tuple[Sequence[int], ...]) -> "AbstractGrid":
pass
@abc.abstractmethod
def iterate(self, dimensions: Tuple[int, ...], buffer_elements: int = 1e6) -> Generator[Tuple, None, None]:
pass
############################################################
############################################################
class SimpleGrid(AbstractGrid):
"""
A simple grid to subdivide an array, involving arbitrary boundaries on each
dimension. Each grid element is defined by boundaries on each dimension.
"""
def __init__(self, boundaries: Tuple[Sequence[int], ...], cost_factor: float, internals: Optional[Dict] = None):
"""
Args:
boundaries:
Tuple of length equal to the number of dimensions. Each entry
should be a strictly increasing sequence of integers specifying
the position of the grid boundaries; the last element should be
equal to the extent of the dimension for the array. A tuple
entry may also be an empty list for a zero-extent dimension.
cost_factor:
Positive number representing the cost of iteration over each
element of the grid's array. The actual cost is defined by the
product of the cost factor by the array size. This is used to
choose between iteration schemes; as a reference, extraction
from an in-memory NumPy array has a cost factor of 1.
internals:
Internal use only.
"""
self._boundaries = boundaries
if internals is not None and "shape" in internals:
shape = internals["shape"]
else:
new_shape = []
for bounds in boundaries:
if len(bounds):
new_shape.append(bounds[-1])
else:
new_shape.append(0)
shape = (*new_shape,)
self._shape = shape
if internals is not None and "maxgap" in internals:
maxgap = internals["maxgap"]
else:
maxgap = (*(SimpleGrid._define_maxgap(b) for b in boundaries),)
self._maxgap = maxgap
if internals is not None and "cost" in internals:
cost = internals["cost"]
else:
cost = cost_factor
for s in shape:
cost *= s
self._cost = cost
self._cost_factor = cost_factor
@staticmethod
def _define_maxgap(bounds):
last = 0
curmax = 0
for d in bounds:
gap = d - last
if gap > curmax:
curmax = gap
last = d
return curmax
@property
def shape(self) -> Tuple[int, ...]:
"""
Returns:
Shape of the grid, equivalent to the array's shape.
"""
return self._shape
@property
def boundaries(self) -> Tuple[Sequence[int], ...]:
"""
Returns:
Boundaries on each dimension of the grid.
"""
return self._boundaries
@property
def cost(self) -> float:
"""
Returns:
Cost of iteration over the underlying array.
"""
return self._cost
def transpose(self, perm: Tuple[int, ...]) -> "SimpleGrid":
"""
Transpose a grid to reflect the same operation on the associated array.
Args:
perm:
Tuple of length equal to the dimensionality of the array,
containing the permutation of dimensions.
Returns:
A new ``SimpleGrid`` object.
"""
return SimpleGrid(
(*(self._boundaries[p] for p in perm),),
self._cost_factor,
internals = { # Save ourselves the trouble of recomputing these.
"shape": (*(self._shape[p] for p in perm),),
"maxgap": (*(self._maxgap[p] for p in perm),),
"cost": self._cost,
}
)
def subset(self, subset: Tuple[Sequence[int], ...]) -> "SimpleGrid":
"""
Subset a grid to reflect the same operation on the associated array.
For any given dimension, consecutive elements in the subset are only
placed in the same grid interval in the subsetted grid if they belong
to the same grid interval in the original grid.
Args:
subset:
Tuple of length equal to the number of grid dimensions. Each
entry should be a (possibly unsorted) sequence of integers,
specifying the subset to apply to each dimension of the grid.
Returns:
A new ``SimpleGrid`` object.
"""
if len(subset) != len(self._shape):
raise ValueError("'shape' and 'subset' should have the same length")
new_boundaries = []
new_shape = []
new_maxgap = []
for i, bounds in enumerate(self._boundaries):
cursub = subset[i]
if _is_single_subset_noop(self._shape[i], cursub):
new_boundaries.append(bounds)
new_shape.append(self._shape[i])
new_maxgap.append(self._maxgap[i])
else:
my_boundaries = []
counter = 0
if len(bounds):
last_chunk = -1
for y in cursub:
cur_chunk = bisect.bisect_right(bounds, y)
if cur_chunk != last_chunk:
if counter > 0:
my_boundaries.append(counter)
last_chunk = cur_chunk
counter += 1
my_boundaries.append(counter)
new_boundaries.append(my_boundaries)
new_shape.append(counter)
new_maxgap.append(SimpleGrid._define_maxgap(my_boundaries))
return SimpleGrid(
(*new_boundaries,),
self._cost_factor,
internals = {
"shape": (*new_shape,),
"maxgap": (*new_maxgap,),
}
)
def _recursive_iterate(self, dimension: int, used: List[bool], starts: List[int], ends: List[int], buffer_elements: int, prescale: List[int]):
bounds = self._boundaries[dimension]
full_end = self._shape[dimension]
if used[dimension]:
# 'prescale' holds the worst-case minimum block sizes of the
# remaining dimensions (i.e., in the subsequent recursion levels).
# These are the worst-case because we assume that all remaining
# dimensions are using their maximum gap for a single grid interval
# to form a block, hence it's a conservative estimate of the space
# we have left to play with on the current dimenion. Note that this
# should only differ from buffer_elements when dimension > 0.
conservative_buffer_elements = max(1, buffer_elements // prescale[dimension])
start = 0
pos = 0
nb = len(bounds)
while True:
if pos == nb:
# Wrapping up the last block, if the grid-breaking code
# has not already iterated to the end of the dimension.
if start != full_end:
starts[dimension] = start
ends[dimension] = full_end
if dimension == 0:
yield (*zip(starts, ends),)
else:
yield from self._recursive_iterate(dimension - 1, used, starts, ends, buffer_elements // (full_end - start), prescale)
break
# Check if we can keep going to make a larger block.
current_end = bounds[pos]
if current_end - start <= conservative_buffer_elements:
pos += 1
continue
if pos:
previous_end = bounds[pos - 1]
else:
previous_end = 0
# Break grid intervals to force compliance with the buffer element limit.
if previous_end == start:
while start < current_end:
starts[dimension] = start
breaking_end = min(current_end, start + conservative_buffer_elements)
ends[dimension] = breaking_end
if dimension == 0:
yield (*zip(starts, ends),)
else:
# Next level of recursion uses buffer_elements, not its
# conservative counterpart, as the next level actually has
# knowledge of the boundaries for that dimension.
yield from self._recursive_iterate(dimension - 1, used, starts, ends, buffer_elements // (breaking_end - start), prescale)
start = breaking_end
pos += 1
continue
# Falling back to the last boundary that fit in the buffer limit.
starts[dimension] = start
ends[dimension] = previous_end
if dimension == 0:
yield (*zip(starts, ends),)
else:
yield from self._recursive_iterate(dimension - 1, used, starts, ends, buffer_elements // (previous_end - start), prescale)
start = previous_end
else:
# If this dimension is not used, we return its entire extent.
starts[dimension] = 0
ends[dimension] = full_end
if dimension == 0:
yield (*zip(starts, ends),)
else:
yield from self._recursive_iterate(dimension - 1, used, starts, ends, buffer_elements // full_end, prescale)
def iterate(self, dimensions: Tuple[int, ...], buffer_elements: int = 1e6) -> Generator[Tuple, None, None]:
"""
Iterate over an array grid. This assembles blocks of contiguous grid
intervals to reduce the number of iterations (and associated overhead)
at the cost of increased memory usage during data extraction.
Args:
dimensions:
Dimensions over which to perform the iteration. Any dimensions
not listed here are extracted in their entirety, i.e., each
block consists of the full extent of unlisted dimensions.
buffer_elements:
Total number of elements in each block. Larger values increase
the block size and reduce the number of iterations, at the cost
of increased memory usage at each iteration.
Returns:
A generator that returns a tuple of length equal to the number of
dimensions. Each element contains the start and end of the block
on its corresponding dimension.
"""
if 0 in self._shape:
return
ndim = len(self._shape)
used = [False] * ndim
for d in dimensions:
used[d] = True
# See comments above about 'prescale'. For each dimension, this
# contains the worst-case minimum block size from the remaining
# dimensions. For used dimensions, this is computed from the max gap;
# for unused dimensions, the full extent is applied.
prescale = [1]
for i in range(ndim - 1):
last = prescale[-1]
if used[i]:
last *= self._maxgap[i]
else:
last *= self._shape[i]
prescale.append(last)
starts = [0] * ndim
ends = [0] * ndim
yield from self._recursive_iterate(ndim - 1, used, starts, ends, buffer_elements, prescale)
############################################################
############################################################
class CompositeGrid(AbstractGrid):
"""
A grid to subdivide an array, constructed by combining component grids
along a specified dimension. This aims to mirror the same combining
operation for the arrays associated with the component grids.
"""
def __init__(self, components: Tuple[AbstractGrid, ...], along: int, internals: Optional[Dict] = None):
"""
Args:
components:
Component grids to be combined to form the composite grid.
Each grid should have the same dimension extents, except for
the ``along`` dimension.
along:
Dimension over which to combine entries of ``components``.
internals:
Internal use only.
"""
self._components = components
self._along = along
if internals is not None and "shape" in internals:
shape = internals["shape"]
else:
first = components[0]
new_shape = list(first.shape)
for i in range(1, len(components)):
current = components[i]
for j, d in enumerate(current.shape):
if j == along:
new_shape[j] += d
elif new_shape[j] != d:
raise ValueError("entries of 'components' should have the same shape on all dimensions except 'along'")
shape = (*new_shape,)
self._shape = shape
if internals is not None and "boundaries" in internals:
boundaries = internals["boundaries"]
else:
boundaries = None
self._boundaries = boundaries
if internals is not None and "cost" in internals:
cost = internals["cost"]
else:
cost = 0
for comp in self._components:
cost += comp.cost
self._cost = cost
@property
def shape(self) -> Tuple[int, ...]:
"""
Returns:
Shape of the grid, equivalent to the array's shape.
"""
return self._shape
@property
def boundaries(self) -> Tuple[Sequence[int], ...]:
"""
Returns:
Boundaries on each dimension of the grid. For the ``along``
dimension, this is a concatenation of the boundaries for the
component grids. For all other dimensions, the boundaries are
set to those of the most costly component grid.
"""
if self._boundaries is None: # Lazy evaluation
chosen, maxcost = self._maxcost()
new_boundaries = list(self._components[chosen].boundaries)
replacement = []
offset = 0
for i, comp in enumerate(self._components):
if i == chosen:
addition = new_boundaries[self._along]
else:
addition = comp.boundaries[self._along]
for a in addition:
replacement.append(a + offset)
offset += comp.shape[self._along]
new_boundaries[self._along] = replacement
self._boundaries = (*new_boundaries,)
return self._boundaries
@property
def cost(self) -> float:
"""
Returns:
Cost of iteration over the underlying array. This is defined
as the sum of the costs of the component arrays.
"""
return self._cost
def _maxcost(self) -> Tuple[int, float]:
chosen = 0
maxcost = 0
for i, comp in enumerate(self._components):
if isinstance(comp, CompositeGrid):
tmp, curcost = comp._maxcost()
else:
curcost = comp.cost
if curcost > maxcost:
maxcost = curcost
chosen = i
return chosen, maxcost
def transpose(self, perm: Tuple[int, ...]) -> "CompositeGrid":
"""
Transpose a grid to reflect the same operation on the associated array.
Args:
perm:
Tuple of length equal to the dimensionality of the array,
containing the permutation of dimensions.
Returns:
A new ``CompositeGrid`` object.
"""
new_components = [grid.transpose(perm) for grid in self._components]
new_along = 0
for i, p in enumerate(perm):
if p == self._along:
new_along = i
internals = { "cost": self._cost }
if self._boundaries is not None:
internals["boundaries"] = (*(self._boundaries[p] for p in perm),)
return CompositeGrid(
new_components,
new_along,
internals = internals,
)
def subset(self, subset: Tuple[Sequence[int], ...]) -> "CompositeGrid":
"""
Subset a grid to reflect the same operation on the associated array.
This splits up the subset sequence for the ``along`` dimension and
distributes it to each of the component grids.
Args:
subset:
Tuple of length equal to the number of grid dimensions. Each
entry should be a (possibly unsorted) sequence of integers,
specifying the subset to apply to each dimension of the grid.
Returns:
A new ``CompositeGrid`` object.
"""
if len(subset) != len(self._shape):
raise ValueError("'shape' and 'subset' should have the same length")
if _is_single_subset_noop(self._shape[self._along], subset[self._along]):
new_components = []
new_subset = list(subset)
for grid in self._components:
new_subset[self._along] = range(grid.shape[self._along])
new_components.append(grid.subset((*new_subset,)))
return CompositeGrid(new_components, self._along)
component_limits = []
counter = 0
for y in self._components:
counter += y.shape[self._along]
component_limits.append(counter)
last_choice = -1
new_components = []
sofar = []
raw_subset = list(subset)
for s in subset[self._along]:
choice = bisect.bisect_left(component_limits, s)
if choice != last_choice:
if len(sofar):
raw_subset[self._along] = sofar
new_components.append(self._components[last_choice].subset((*raw_subset,)))
sofar = []
last_choice = choice
if choice:
sofar.append(s - component_limits[choice - 1])
else:
sofar.append(s)
if len(sofar):
raw_subset[self._along] = sofar
new_components.append(self._components[last_choice].subset((*raw_subset,)))
new_shape = []
for i, s in enumerate(subset):
if s is None:
new_shape.append(self._shape[i])
else:
new_shape.append(len(s))
return CompositeGrid(
new_components,
self._along,
internals = {
"shape": (*new_shape,),
}
)
def iterate(self, dimensions: Tuple[int, ...], buffer_elements: int = 1e6) -> Generator[Tuple, None, None]:
"""
Iterate over an array grid. This assembles blocks of contiguous grid
intervals to reduce the number of iterations (and associated overhead)
at the cost of increased memory usage during data extraction. For any
iteration over the ``along`` dimension (i.e., ``along`` is in
``dimensions``), this function dispatches to the component grids;
otherwise the iteration is performed based on :py:meth:`~boundaries`.
Args:
dimensions:
Dimensions over which to perform the iteration. Any dimensions
not listed here are extracted in their entirety, i.e., each
block consists of the full extent of unlisted dimensions.
buffer_elements:
Total number of elements in each block. Larger values increase
the block size and reduce the number of iterations, at the cost
of increased memory usage at each iteration.
Returns:
A generator that returns a tuple of length equal to the number of
dimensions. Each element contains the start and end of the block
on its corresponding dimension.
"""
if 0 in self._shape:
return
if self._along in dimensions:
first = True
offset = 0
for grid in self._components:
for block in grid.iterate(dimensions=dimensions, buffer_elements=buffer_elements):
if offset == 0:
yield block
else:
copy = list(block)
comp = copy[self._along]
copy[self._along] = (comp[0] + offset, comp[1] + offset)
yield (*copy,)
offset += grid.shape[self._along]
return
temp = SimpleGrid(self.boundaries, cost_factor=1)
yield from temp.iterate(dimensions=dimensions, buffer_elements=buffer_elements)