-
Notifications
You must be signed in to change notification settings - Fork 41
/
BlockDataContainer.py
648 lines (562 loc) · 25.5 KB
/
BlockDataContainer.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
640
641
642
643
644
645
646
647
648
# -*- coding: utf-8 -*-
# Copyright 2019 United Kingdom Research and Innovation
# Copyright 2019 The University of Manchester
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
# Authors:
# CIL Developers, listed at: https://github.com/TomographicImaging/CIL/blob/master/NOTICE.txt
import numpy
from numbers import Number
import functools
from cil.utilities.multiprocessing import NUM_THREADS
class BlockDataContainer(object):
'''Class to hold DataContainers as column vector
Provides basic algebra between BlockDataContainer's, DataContainer's and
subclasses and Numbers
1) algebra between `BlockDataContainer`s will be element-wise, only if
the shape of the 2 `BlockDataContainer`s is the same, otherwise it
will fail
2) algebra between `BlockDataContainer`s and `list` or `numpy array` will
work as long as the number of `rows` and element of the arrays match,
independently on the fact that the `BlockDataContainer` could be nested
3) algebra between `BlockDataContainer` and one `DataContainer` is possible.
It will require all the `DataContainers` in the block to be
compatible with the `DataContainer` we want to operate with.
4) algebra between `BlockDataContainer` and a `Number` is possible and it
will be done with each element of the `BlockDataContainer` even if nested
A = [ [B,C] , D]
A * 3 = [ 3 * [B,C] , 3* D] = [ [ 3*B, 3*C] , 3*D ]
'''
ADD = 'add'
SUBTRACT = 'subtract'
MULTIPLY = 'multiply'
DIVIDE = 'divide'
POWER = 'power'
SAPYB = 'sapyb'
MAXIMUM = 'maximum'
MINIMUM = 'minimum'
ABS = 'abs'
SIGN = 'sign'
SQRT = 'sqrt'
CONJUGATE = 'conjugate'
__array_priority__ = 1
__container_priority__ = 2
@property
def dtype(self):
return tuple(i.dtype for i in self.containers)
def __init__(self, *args, **kwargs):
''''''
self.containers = args
self.index = 0
self.geometry = None
#if len(set([i.shape for i in self.containers])):
# self.geometry = self.containers[0].geometry
shape = kwargs.get('shape', None)
if shape is None:
shape = (len(args),1)
# shape = (len(args),1)
self.shape = shape
n_elements = functools.reduce(lambda x,y: x*y, shape, 1)
if len(args) != n_elements:
raise ValueError(
'Dimension and size do not match: expected {} got {}'
.format(n_elements, len(args)))
def __iter__(self):
'''BlockDataContainer is Iterable'''
return self
def next(self):
'''python2 backwards compatibility'''
return self.__next__()
def __next__(self):
try:
out = self[self.index]
except IndexError as ie:
raise StopIteration()
self.index+=1
return out
def is_compatible(self, other):
'''basic check if the size of the 2 objects fit'''
if isinstance(other, Number):
return True
elif isinstance(other, (list, tuple, numpy.ndarray)) :
for ot in other:
if not isinstance(ot, Number):
raise ValueError('List/ numpy array can only contain numbers {}'\
.format(type(ot)))
return len(self.containers) == len(other)
elif isinstance(other, BlockDataContainer):
return len(self.containers) == len(other.containers)
else:
# this should work for other as DataContainers and children
ret = True
for i, el in enumerate(self.containers):
if isinstance(el, BlockDataContainer):
a = el.is_compatible(other)
else:
a = el.shape == other.shape
ret = ret and a
# probably will raise
return ret
def get_item(self, row):
if row > self.shape[0]:
raise ValueError('Requested row {} > max {}'.format(row, self.shape[0]))
return self.containers[row]
def __getitem__(self, row):
return self.get_item(row)
def add(self, other, *args, **kwargs):
'''Algebra: add method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer
:param: out (optional): provides a placehold for the resul.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.ADD, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.ADD, other, *args, **kwargs)
def subtract(self, other, *args, **kwargs):
'''Algebra: subtract method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.SUBTRACT, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.SUBTRACT, other, *args, **kwargs)
def multiply(self, other, *args, **kwargs):
'''Algebra: multiply method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer)
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.MULTIPLY, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.MULTIPLY, other, *args, **kwargs)
def divide(self, other, *args, **kwargs):
'''Algebra: divide method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer)
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.DIVIDE, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.DIVIDE, other, *args, **kwargs)
def power(self, other, *args, **kwargs):
'''Algebra: power method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.POWER, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.POWER, other, *args, **kwargs)
def maximum(self, other, *args, **kwargs):
'''Algebra: power method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer)
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.MAXIMUM, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.MAXIMUM, other, *args, **kwargs)
def minimum(self, other, *args, **kwargs):
'''Algebra: power method of BlockDataContainer with number/DataContainer or BlockDataContainer
:param: other (number, DataContainer or subclasses or BlockDataContainer)
:param: out (optional): provides a placeholder for the result.
'''
out = kwargs.get('out', None)
if out is not None:
self.binary_operations(BlockDataContainer.MINIMUM, other, *args, **kwargs)
else:
return self.binary_operations(BlockDataContainer.MINIMUM, other, *args, **kwargs)
def sapyb(self, a, y, b, out, num_threads = NUM_THREADS):
r'''performs axpby element-wise on the BlockDataContainer containers
Does the operation .. math:: a*x+b*y and stores the result in out, where x is self
:param a: scalar
:param b: scalar
:param y: compatible (Block)DataContainer
:param out: (Block)DataContainer to store the result
Example:
--------
>>> a = 2
>>> b = 3
>>> ig = ImageGeometry(10,11)
>>> x = ig.allocate(1)
>>> y = ig.allocate(2)
>>> bdc1 = BlockDataContainer(2*x, y)
>>> bdc2 = BlockDataContainer(x, 2*y)
>>> out = bdc1.sapyb(a,bdc2,b)
'''
if out is None:
raise ValueError("out container cannot be None")
kwargs = {'a':a, 'b':b, 'out':out, 'num_threads': NUM_THREADS}
self.binary_operations(BlockDataContainer.SAPYB, y, **kwargs)
def axpby(self, a, b, y, out, dtype=numpy.float32, num_threads = NUM_THREADS):
'''Deprecated method. Alias of sapyb'''
return self.sapyb(a,y,b,out,num_threads)
def binary_operations(self, operation, other, *args, **kwargs):
'''Algebra: generic method of algebric operation with BlockDataContainer with number/DataContainer or BlockDataContainer
Provides commutativity with DataContainer and subclasses, i.e. this
class's reverse algebraic methods take precedence w.r.t. direct algebraic
methods of DataContainer and subclasses.
This method is not to be used directly
'''
if not self.is_compatible(other):
raise ValueError('Incompatible for operation {}'.format(operation))
out = kwargs.get('out', None)
if isinstance(other, Number):
# try to do algebra with one DataContainer. Will raise error if not compatible
kw = kwargs.copy()
res = []
for i,el in enumerate(self.containers):
if operation == BlockDataContainer.ADD:
op = el.add
elif operation == BlockDataContainer.SUBTRACT:
op = el.subtract
elif operation == BlockDataContainer.MULTIPLY:
op = el.multiply
elif operation == BlockDataContainer.DIVIDE:
op = el.divide
elif operation == BlockDataContainer.POWER:
op = el.power
elif operation == BlockDataContainer.MAXIMUM:
op = el.maximum
elif operation == BlockDataContainer.MINIMUM:
op = el.minimum
else:
raise ValueError('Unsupported operation', operation)
if out is not None:
kw['out'] = out.get_item(i)
op(other, *args, **kw)
else:
res.append(op(other, *args, **kw))
if out is not None:
return
else:
return type(self)(*res, shape=self.shape)
elif isinstance(other, (list, tuple, numpy.ndarray, BlockDataContainer)):
kw = kwargs.copy()
res = []
if isinstance(other, BlockDataContainer):
the_other = other.containers
else:
the_other = other
for i,zel in enumerate(zip ( self.containers, the_other) ):
el = zel[0]
ot = zel[1]
if operation == BlockDataContainer.ADD:
op = el.add
elif operation == BlockDataContainer.SUBTRACT:
op = el.subtract
elif operation == BlockDataContainer.MULTIPLY:
op = el.multiply
elif operation == BlockDataContainer.DIVIDE:
op = el.divide
elif operation == BlockDataContainer.POWER:
op = el.power
elif operation == BlockDataContainer.MAXIMUM:
op = el.maximum
elif operation == BlockDataContainer.MINIMUM:
op = el.minimum
elif operation == BlockDataContainer.SAPYB:
if not isinstance(other, BlockDataContainer):
raise ValueError("{} cannot handle {}".format(operation, type(other)))
op = el.sapyb
else:
raise ValueError('Unsupported operation', operation)
if out is not None:
if operation == BlockDataContainer.SAPYB:
if isinstance(kw['a'], BlockDataContainer):
a = kw['a'].get_item(i)
else:
a = kw['a']
if isinstance(kw['b'], BlockDataContainer):
b = kw['b'].get_item(i)
else:
b = kw['b']
el.sapyb(a, ot, b, out.get_item(i), num_threads=kw['num_threads'])
else:
kw['out'] = out.get_item(i)
op(ot, *args, **kw)
else:
res.append(op(ot, *args, **kw))
if out is not None:
return
else:
return type(self)(*res, shape=self.shape)
else:
# try to do algebra with one DataContainer. Will raise error if not compatible
kw = kwargs.copy()
if operation != BlockDataContainer.SAPYB:
# remove keyworded argument related to SAPYB
for k in ['a','b','y', 'num_threads', 'dtype']:
if k in kw.keys():
kw.pop(k)
res = []
for i,el in enumerate(self.containers):
if operation == BlockDataContainer.ADD:
op = el.add
elif operation == BlockDataContainer.SUBTRACT:
op = el.subtract
elif operation == BlockDataContainer.MULTIPLY:
op = el.multiply
elif operation == BlockDataContainer.DIVIDE:
op = el.divide
elif operation == BlockDataContainer.POWER:
op = el.power
elif operation == BlockDataContainer.MAXIMUM:
op = el.maximum
elif operation == BlockDataContainer.MINIMUM:
op = el.minimum
elif operation == BlockDataContainer.SAPYB:
if isinstance(kw['a'], BlockDataContainer):
a = kw['a'].get_item(i)
else:
a = kw['a']
if isinstance(kw['b'], BlockDataContainer):
b = kw['b'].get_item(i)
else:
b = kw['b']
el.sapyb(a, other, b, out.get_item(i), kw['num_threads'])
# As axpyb cannot return anything we `continue` to skip the rest of the code block
continue
else:
raise ValueError('Unsupported operation', operation)
if out is not None:
kw['out'] = out.get_item(i)
op(other, *args, **kw)
else:
res.append(op(other, *args, **kw))
if out is not None:
return
else:
return type(self)(*res, shape=self.shape)
## unary operations
def unary_operations(self, operation, *args, **kwargs ):
'''Unary operation on BlockDataContainer:
generic method of unary operation with BlockDataContainer: abs, sign, sqrt and conjugate
This method is not to be used directly
'''
out = kwargs.get('out', None)
kw = kwargs.copy()
if out is None:
res = []
for el in self.containers:
if operation == BlockDataContainer.ABS:
op = el.abs
elif operation == BlockDataContainer.SIGN:
op = el.sign
elif operation == BlockDataContainer.SQRT:
op = el.sqrt
elif operation == BlockDataContainer.CONJUGATE:
op = el.conjugate
res.append(op(*args, **kw))
return BlockDataContainer(*res)
else:
kw.pop('out')
for el,elout in zip(self.containers, out.containers):
if operation == BlockDataContainer.ABS:
op = el.abs
elif operation == BlockDataContainer.SIGN:
op = el.sign
elif operation == BlockDataContainer.SQRT:
op = el.sqrt
elif operation == BlockDataContainer.CONJUGATE:
op = el.conjugate
kw['out'] = elout
op(*args, **kw)
def abs(self, *args, **kwargs):
return self.unary_operations(BlockDataContainer.ABS, *args, **kwargs)
def sign(self, *args, **kwargs):
return self.unary_operations(BlockDataContainer.SIGN, *args, **kwargs)
def sqrt(self, *args, **kwargs):
return self.unary_operations(BlockDataContainer.SQRT, *args, **kwargs)
def conjugate(self, *args, **kwargs):
return self.unary_operations(BlockDataContainer.CONJUGATE, *args, **kwargs)
# def abs(self, *args, **kwargs):
# return type(self)(*[ el.abs(*args, **kwargs) for el in self.containers], shape=self.shape)
# def sign(self, *args, **kwargs):
# return type(self)(*[ el.sign(*args, **kwargs) for el in self.containers], shape=self.shape)
# def sqrt(self, *args, **kwargs):
# return type(self)(*[ el.sqrt(*args, **kwargs) for el in self.containers], shape=self.shape)
# def conjugate(self, out=None):
# return type(self)(*[el.conjugate() for el in self.containers], shape=self.shape)
## reductions
def sum(self, *args, **kwargs):
return numpy.sum([ el.sum(*args, **kwargs) for el in self.containers])
def squared_norm(self):
y = numpy.asarray([el.squared_norm() for el in self.containers])
return y.sum()
def norm(self):
return numpy.sqrt(self.squared_norm())
def pnorm(self, p=2):
if p==1:
return sum(self.abs())
elif p==2:
tmp = functools.reduce(lambda a,b: a + b.conjugate()*b, self.containers, self.get_item(0) * 0 ).sqrt()
return tmp
else:
return ValueError('Not implemented')
def copy(self):
'''alias of clone'''
return self.clone()
def clone(self):
return type(self)(*[el.copy() for el in self.containers], shape=self.shape)
def fill(self, other):
if isinstance (other, BlockDataContainer):
if not self.is_compatible(other):
raise ValueError('Incompatible containers')
for el,ot in zip(self.containers, other.containers):
el.fill(ot)
else:
return ValueError('Cannot fill with object provided {}'.format(type(other)))
def __add__(self, other):
return self.add( other )
# __radd__
def __sub__(self, other):
return self.subtract( other )
# __rsub__
def __mul__(self, other):
return self.multiply(other)
# __rmul__
def __div__(self, other):
return self.divide(other)
# __rdiv__
def __truediv__(self, other):
return self.divide(other)
def __pow__(self, other):
return self.power(other)
# reverse operand
def __radd__(self, other):
'''Reverse addition
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return self + other
# __radd__
def __rsub__(self, other):
'''Reverse subtraction
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return (-1 * self) + other
# __rsub__
def __rmul__(self, other):
'''Reverse multiplication
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return self * other
# __rmul__
def __rdiv__(self, other):
'''Reverse division
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return pow(self / other, -1)
# __rdiv__
def __rtruediv__(self, other):
'''Reverse truedivision
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return self.__rdiv__(other)
def __rpow__(self, other):
'''Reverse power
to make sure that this method is called rather than the __mul__ of a numpy array
the class constant __array_priority__ must be set > 0
https://docs.scipy.org/doc/numpy-1.15.1/reference/arrays.classes.html#numpy.class.__array_priority__
'''
return other.power(self)
def __iadd__(self, other):
'''Inline addition'''
if isinstance (other, BlockDataContainer):
for el,ot in zip(self.containers, other.containers):
el += ot
elif isinstance(other, Number):
for el in self.containers:
el += other
elif isinstance(other, list) or isinstance(other, numpy.ndarray):
if not self.is_compatible(other):
raise ValueError('Incompatible for __iadd__')
for el,ot in zip(self.containers, other):
el += ot
return self
# __iadd__
def __isub__(self, other):
'''Inline subtraction'''
if isinstance (other, BlockDataContainer):
for el,ot in zip(self.containers, other.containers):
el -= ot
elif isinstance(other, Number):
for el in self.containers:
el -= other
elif isinstance(other, list) or isinstance(other, numpy.ndarray):
if not self.is_compatible(other):
raise ValueError('Incompatible for __isub__')
for el,ot in zip(self.containers, other):
el -= ot
return self
# __isub__
def __imul__(self, other):
'''Inline multiplication'''
if isinstance (other, BlockDataContainer):
for el,ot in zip(self.containers, other.containers):
el *= ot
elif isinstance(other, Number):
for el in self.containers:
el *= other
elif isinstance(other, list) or isinstance(other, numpy.ndarray):
if not self.is_compatible(other):
raise ValueError('Incompatible for __imul__')
for el,ot in zip(self.containers, other):
el *= ot
return self
# __imul__
def __idiv__(self, other):
'''Inline division'''
if isinstance (other, BlockDataContainer):
for el,ot in zip(self.containers, other.containers):
el /= ot
elif isinstance(other, Number):
for el in self.containers:
el /= other
elif isinstance(other, list) or isinstance(other, numpy.ndarray):
if not self.is_compatible(other):
raise ValueError('Incompatible for __idiv__')
for el,ot in zip(self.containers, other):
el /= ot
return self
# __rdiv__
def __itruediv__(self, other):
'''Inline truedivision'''
return self.__idiv__(other)
def __neg__(self):
""" Return - self """
return -1 * self
def dot(self, other):
#
tmp = [ self.containers[i].dot(other.containers[i]) for i in range(self.shape[0])]
return sum(tmp)
def __len__(self):
return self.shape[0]