-
Notifications
You must be signed in to change notification settings - Fork 259
/
metasum.py
639 lines (567 loc) · 22.1 KB
/
metasum.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
# emacs: -*- mode: python-mode; py-indent-offset: 4; indent-tabs-mode: nil -*-
# vi: set ft=python sts=4 ts=4 sw=4 et:
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
#
# See COPYING file distributed along with the NiBabel package for the
# copyright and license terms.
#
### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ### ##
'''Memory efficient tracking of meta data dicts with repeating elements
'''
from dataclasses import dataclass
from enum import IntEnum
from bitarray import bitarray, frozenbitarray
from bitarray.util import zeros
class FloatCanon:
'''Look up a canonical float that we compare equal to'''
def __init__(self, n_digits=6):
self._n_digits = n_digits
self._offset = 0.5 * (10 ** -n_digits)
self._canon_vals = set()
self._rounded = {}
def get(self, val):
'''Get a canonical value that at least compares equal to `val`'''
res = self._values.get(val)
if res is not None:
return res
lb = round(val, self._n_digits)
res = self._rounded.get(lb)
if res is not None:
return res
ub = round(val + self._offset, self._n_digits)
res = self._rounded.get(ub)
if res is not None:
return res
_NoValue = object()
# TODO: Integrate some value canonicalization filtering? Or just require the
# user to do that themselves?
class ValueIndices:
"""Track indices of values in sequence.
If values repeat frequently then memory usage can be dramatically improved.
It can be thought of as the inverse to a list.
>>> values = ['a', 'a', 'b', 'a', 'b']
>>> vidx = ValueIndices(values)
>>> vidx['a']
[0, 1, 3]
>>> vidx['b']
[2, 4]
"""
def __init__(self, values=None):
"""Initialize a ValueIndices instance.
Parameters
----------
values : sequence
The sequence of values to track indices on
"""
self._n_input = 0
# The values can be constant, unique to specific indices, or
# arbitrarily varying
self._const_val = _NoValue
self._unique_vals = {}
self._val_bitarrs = {}
if values is not None:
self.extend(values)
@property
def n_input(self):
'''The number of inputs we are indexing'''
return self._n_input
def __len__(self):
'''Number of unique values being tracked'''
if self._const_val is not _NoValue:
return 1
return len(self._unique_vals) + len(self._val_bitarrs)
def __getitem__(self, value):
'''Return list of indices for the given value'''
if self._const_val == value:
return list(range(self._n_input))
idx = self._unique_vals.get(value)
if idx is not None:
return [idx]
ba = self._val_bitarrs[value]
return list(self._extract_indices(ba))
def first(self, value):
'''Return the first index where this value appears'''
if self._const_val == value:
return 0
idx = self._unique_vals.get(value)
if idx is not None:
return idx
return self._val_bitarrs[value].index(True)
def values(self):
'''Generate each unique value that has been seen'''
if self._const_val is not _NoValue:
yield self._const_val
return
for val in self._unique_vals.keys():
yield val
for val in self._val_bitarrs.keys():
yield val
def get_mask(self, value):
'''Get bitarray mask of indices with this value'''
if self._const_val is not _NoValue:
if self._const_val != value:
raise KeyError()
res = bitarray(self._n_input)
res.setall(1)
return res
idx = self._unique_vals.get(value)
if idx is not None:
res = zeros(self._n_inpuf)
res[idx] = 1
return res
return self._val_bitarrs[value].copy()
def count(self, value, mask=None):
'''Number of indices for the given `value`'''
if mask is not None:
if len(mask) != self.n_input:
raise ValueError("Mask length must match input length")
if self._const_val is not _NoValue:
if self._const_val != value:
raise KeyError()
if mask is None:
return self._n_input
return mask.count()
unique_idx = self._unique_vals.get(value, _NoValue)
if unique_idx is not _NoValue:
if mask is not None:
if mask[unique_idx]:
return 1
return 0
return 1
if mask is not None:
return (self._val_bitarrs[value] & mask).count()
return self._val_bitarrs[value].count()
def get_value(self, idx):
'''Get the value at `idx`'''
if not 0 <= idx < self._n_input:
raise IndexError()
if self._const_val is not _NoValue:
return self._const_val
for val, vidx in self._unique_vals.items():
if vidx == idx:
return val
bit_idx = zeros(self._n_input)
bit_idx[idx] = 1
for val, ba in self._val_bitarrs.items():
if (ba & bit_idx).any():
return val
assert False
def to_list(self):
'''Convert back to a list of values'''
if self._const_val is not _NoValue:
return [self._const_val] * self._n_input
res = [_NoValue] * self._n_input
for val, idx in self._unique_vals.items():
res[idx] = val
for val, ba in self._val_bitarrs.items():
for idx in self._extract_indices(ba):
res[idx] = val
return res
def extend(self, values):
'''Add more values to the end of any existing ones'''
init_size = self._n_input
if isinstance(values, ValueIndices):
other_is_vi = True
other_size = values._n_input
else:
other_is_vi = False
other_size = len(values)
final_size = init_size + other_size
for ba in self._val_bitarrs.values():
ba.extend(zeros(other_size))
if other_is_vi:
if self._const_val is not _NoValue:
if values._const_val is not _NoValue:
self._extend_const(values)
return
else:
self._rm_const(final_size)
elif values._const_val is not _NoValue:
cval = values._const_val
other_unique = {}
other_bitarrs = {}
if values._n_input == 1:
other_unique[cval] = 0
else:
other_bitarrs[cval] = bitarray(values._n_input)
other_bitarrs[cval].setall(1)
else:
other_unique = values._unique_vals
other_bitarrs = values._val_bitarrs
for val, other_idx in other_unique.items():
self._ingest_single(val, final_size, init_size, other_idx)
for val, other_ba in other_bitarrs.items():
curr_ba = self._val_bitarrs.get(val)
if curr_ba is None:
curr_idx = self._unique_vals.get(val)
if curr_idx is None:
if init_size == 0:
new_ba = other_ba.copy()
else:
new_ba = zeros(init_size)
new_ba.extend(other_ba)
else:
new_ba = zeros(init_size)
new_ba[curr_idx] = True
new_ba.extend(other_ba)
del self._unique_vals[val]
self._val_bitarrs[val] = new_ba
else:
curr_ba[init_size:] |= other_ba
self._n_input += other_ba.count()
else:
for other_idx, val in enumerate(values):
self._ingest_single(val, final_size, init_size, other_idx)
assert self._n_input == final_size
def append(self, value):
'''Append another value as input'''
if self._const_val == value:
self._n_input += 1
return
elif self._const_val is not _NoValue:
self._rm_const(self._n_input + 1)
self._unique_vals[value] = self._n_input
self._n_input += 1
return
if self._n_input == 0:
self._const_val = value
self._n_input += 1
return
curr_size = self._n_input
found = False
for val, bitarr in self._val_bitarrs.items():
assert len(bitarr) == self._n_input
if val == value:
found = True
bitarr.append(True)
else:
bitarr.append(False)
if not found:
curr_idx = self._unique_vals.get(value)
if curr_idx is None:
self._unique_vals[value] = curr_size
else:
new_ba = zeros(curr_size + 1)
new_ba[curr_idx] = True
new_ba[curr_size] = True
self._val_bitarrs[value] = new_ba
del self._unique_vals[value]
self._n_input += 1
def reverse(self):
'''Reverse the indices in place'''
for val, idx in self._unique_vals.items():
self._unique_vals[val] = self._n_input - idx - 1
for val, bitarr in self._val_bitarrs.items():
bitarr.reverse()
def argsort(self, reverse=False):
'''Return array of indices in order that sorts the values'''
if self._const_val is not _NoValue:
return np.arange(self._n_input)
res = np.empty(self._n_input, dtype=np.int64)
vals = list(self._unique_vals.keys()) + list(self._val_bitarrs.keys())
vals.sort(reverse=reverse)
res_idx = 0
for val in vals:
idx = self._unique_vals.get(val)
if idx is not None:
res[res_idx] = idx
res_idx += 1
continue
ba = self._val_bitarrs[val]
for idx in self._extract_indices(ba):
res[res_idx] = idx
res_idx += 1
return res
def reorder(self, order):
'''Reorder the indices in place'''
if len(order) != self._n_input:
raise ValueError("The 'order' has the incorrect length")
for val, idx in self._unique_vals.items():
self._unique_vals[val] = order.index(idx)
for val, bitarr in self._val_bitarrs.items():
new_ba = zeros(self._n_input)
for idx in self._extract_indices(bitarr):
new_ba[order.index(idx)] = True
self._val_bitarrs[val] = new_ba
def is_covariant(self, other):
'''True if `other` has values that vary the same way ours do
The actual values themselves are ignored
'''
if self._n_input != other._n_input or len(self) != len(other):
return False
if self._const_val is not _NoValue:
return other._const_val is not _NoValue
if self._n_input == len(self):
return other._n_input == len(other)
self_ba_set = set(frozenbitarray(ba) for ba in self._val_bitarrs.values())
other_ba_set = set(frozenbitarray(ba) for ba in other._val_bitarrs.values())
if self_ba_set != other_ba_set:
return False
if len(self._unique_vals) != len(other._unique_vals):
return False
return True
def get_block_size(self):
'''Return size of even blocks of values, or None if values aren't "blocked"
The number of values must evenly divide the number of inputs into the block size,
with each value appearing that same number of times.
'''
block_size, rem = divmod(self._n_input, len(self))
if rem != 0:
return None
for val in self.values():
if self.count(val) != block_size:
return None
return block_size
def is_orthogonal(self, other, size=1):
'''Check our value's indices overlaps each from `other` exactly `size` times
'''
other_bas = {v: other.get_mask(v) for v in other.values()}
for val in self.values():
for other_val, other_ba in other_bas.items():
if self.count(val, mask=other_ba) != size:
return False
return True
def _extract_indices(self, ba):
'''Generate integer indices from bitarray representation'''
start = 0
while True:
try:
# TODO: Is this the most efficient approach?
curr_idx = ba.index(True, start)
except ValueError:
return
yield curr_idx
start = curr_idx + 1
def _ingest_single(self, val, final_size, init_size, other_idx):
'''Helper to ingest single value from another collection'''
if val == self._const_val:
self._n_input += 1
return
elif self._const_val is not _NoValue:
self._rm_const(final_size)
if self._n_input == 0:
self._const_val = val
self._n_input += 1
return
curr_ba = self._val_bitarrs.get(val)
if curr_ba is None:
curr_idx = self._unique_vals.get(val)
if curr_idx is None:
self._unique_vals[val] = init_size + other_idx
else:
new_ba = zeros(final_size)
new_ba[curr_idx] = True
new_ba[init_size + other_idx] = True
self._val_bitarrs[val] = new_ba
del self._unique_vals[val]
else:
curr_ba[init_size + other_idx] = True
self._n_input += 1
def _rm_const(self, final_size):
assert self._const_val is not _NoValue
if self._n_input == 1:
self._unique_vals[self._const_val] = 0
else:
self._val_bitarrs[self._const_val] = zeros(final_size)
self._val_bitarrs[self._const_val][:self._n_input] = True
self._const_val = _NoValue
def _extend_const(self, other):
if self._const_val != other._const_val:
if self._n_input == 1:
self._unique_vals[self._const_val] = 0
else:
self_ba = bitarray(self._n_input)
other_ba = bitarray(other._n_input)
self_ba.setall(1)
other_ba.setall(0)
self._val_bitarrs[self._const_val] = self_ba + other_ba
if other._n_input == 1:
self._unique_vals[other._const_val] = self._n_input
else:
self_ba = bitarray(self._n_input)
other_ba = bitarray(other._n_input)
self_ba.setall(0)
other_ba.setall(1)
self._val_bitarrs[other._const_val] = self_ba + other_ba
self._const_val = _NoValue
self._n_input += other._n_input
_MissingKey = object()
class DimTypes(IntEnum):
'''Enumerate the three types of nD dimensions'''
SLICE = 1
TIME = 2
PARAM = 3
@dataclass
class DimIndex:
'''Specify an nD index'''
dim_type: DimTypes
key: str
class NdSortError(Exception):
'''Raised when the data cannot be sorted into an nD array as specified'''
class MetaSummary:
'''Summarize a sequence of dicts, tracking how individual keys vary
The assumption is that for any key many values will be constant, or at
least repeated, and thus we can reduce memory consumption by only storing
the value once along with the indices it appears at.
'''
def __init__(self):
self._v_idxs = {}
self._n_input = 0
@property
def n_input(self):
return self._n_input
def append(self, meta):
seen = set()
for key, v_idx in self._v_idxs.items():
val = meta.get(key, _MissingKey)
v_idx.append(val)
seen.add(key)
for key, val in meta.items():
if key in seen:
continue
v_idx = ValueIndices([_MissingKey for _ in range(self._n_input)])
v_idx.append(val)
self._v_idxs[key] = v_idx
self._n_input += 1
def keys(self):
'''Generate all known keys'''
return self._v_idxs.keys()
def const_keys(self):
'''Generate keys with a constant value across all inputs'''
for key, v_idx in self._v_idxs.items():
if len(v_idx) == 1:
yield key
def unique_keys(self):
'''Generate keys with a unique value in each input'''
n_input = self._n_input
if n_input <= 1:
return
for key, v_idx in self._v_idxs.items():
if len(v_idx) == n_input:
yield key
def repeating_keys(self):
'''Generate keys that have some repeating component but are not const
'''
n_input = self._n_input
if n_input <= 1:
return
for key, v_idx in self._v_idxs.items():
if 1 < len(v_idx) < n_input:
yield key
def covariant_groups(self, keys=None, block_only=False):
'''Generate groups of keys that vary with the same pattern
'''
if keys is None:
keys = self.keys()
groups = []
for key in keys:
v_idx = self._v_idxs[key]
if len(groups) == 0:
groups.append((key, v_idx))
continue
for group in groups:
if group[0][1].is_covariant(v_idx):
group.append(key)
break
else:
groups.append((key, v_idx))
for group in groups:
group[0] = group[0][0]
return groups
def get_meta(self, idx):
'''Get the full dict at the given index'''
res = {}
for key, v_idx in self._v_idxs.items():
val = v_idx.get_value(idx)
if val is _MissingKey:
continue
res[key] = val
return res
def get_val(self, idx, key, default=None):
'''Get the value at `idx` for the `key`, or return `default``'''
res = self._v_idxs[key].get_value(idx)
if res is _MissingKey:
return default
return res
def reorder(self, order):
'''Reorder indices in place'''
for v_idx in self._v_idxs.values():
v_idx.reorder(order)
def nd_sort(self, dims):
'''Produce linear indices to fill nD array as specified by `dims`
Assumes each input corresponds to a 2D or 3D array, and the combined
array is 3D+
'''
# Make sure dims aren't completely invalid
if len(dims) == 0:
raise ValueError("At least one dimension must be specified")
last_dim = None
for dim in dims:
if last_dim is not None:
if last_dim.dim_type > dim.dim_type:
# TODO: This only allows PARAM dimensions at the end, which I guess is reasonable?
raise ValueError("Invalid dimension order")
elif last_dim.dim_type == dim.dim_type and dim.dim_type != DimTypes.PARAM:
raise ValueError("There can be at most one each of SLICE and TIME dimensions")
last_dim = dim
# Pull out info about different types of dims
n_input = self._n_input
total_vol = None
slice_dim = None
time_dim = None
param_dims = []
n_params = []
total_params = 1
shape = []
curr_size = 1
for dim in dims:
dim_vidx = self._v_idxs[dim.key]
dim_type = dim.dim_type
if dim_type is DimTypes.SLICE:
slice_dim = dim
n_slices = len(dim_vidx)
total_vol = dim_vidx.get_block_size()
if total_vol is None:
raise NdSortError("There are missing or extra slices")
shape.append(n_slices)
curr_size *= n_slices
elif dim_type is DimTypes.TIME:
time_dim = dim
elif dim_type is DimTypes.PARAM:
if dim_vidx.get_block_size() is None:
raise NdSortError(f"The parameter {dim.key} doesn't evenly divide inputs")
param_dims.append(dim)
n_param = len(dim_vidx)
n_params.append(n_param)
total_params *= n_param
if total_vol is None:
total_vol = n_input
# Size of the time dimension must be inferred from the size of the other dims
n_time = 1
prev_dim = slice_dim
if time_dim is not None:
n_time, rem = divmod(total_vol, total_params)
if rem != 0:
raise NdSortError("The combined parameters don't evenly divide inputs")
shape.append(n_time)
curr_size *= n_time
prev_dim = time_dim
# Complete the "shape", and do a more detailed check that our dims make sense
for dim, n_param in zip(param_dims, n_params):
dim_vidx = self._v_idxs[dim.key]
if dim_vidx.get_block_size() != n_input // n_param:
raise NdSortError(f"The parameter {dim.key} doesn't evenly divide inputs")
if prev_dim is not None and prev_dim.dim_type != DimTypes.TIME:
count_per = (curr_size // shape[-1]) * (n_input // (curr_size * n_param))
if not self._v_idxs[prev_dim.key].is_orthogonal(dim_vidx, count_per):
raise NdSortError("The dimensions are not orthogonal")
shape.append(n_param)
curr_size *= n_param
prev_dim = dim
# Extract dim keys for each input and do the actual sort
sort_keys = [(idx, tuple(self.get_val(idx, dim.key) for dim in reversed(dims)))
for idx in range(n_input)]
sort_keys.sort(key=lambda x: x[1])
# TODO: If we have non-singular time dimension we need to do some additional
# validation checks here after sorting.
return tuple(shape), [x[0] for x in sort_keys]