diff options
author | Edoardo Pasca <edo.paskino@gmail.com> | 2019-04-12 12:09:39 +0100 |
---|---|---|
committer | Edoardo Pasca <edo.paskino@gmail.com> | 2019-04-12 12:09:39 +0100 |
commit | 4049917fa20353da6f88ff03b7c5b11d67743a00 (patch) | |
tree | efea0009e8fc6dcc90539f1c88e5e0c941a4e9a2 | |
parent | 4129fe6a81d20b5f7d05554222d3a78f18f014f0 (diff) | |
download | framework-4049917fa20353da6f88ff03b7c5b11d67743a00.tar.gz framework-4049917fa20353da6f88ff03b7c5b11d67743a00.tar.bz2 framework-4049917fa20353da6f88ff03b7c5b11d67743a00.tar.xz framework-4049917fa20353da6f88ff03b7c5b11d67743a00.zip |
fix BlockDataContainer algebra
closes #242
-rwxr-xr-x | Wrappers/Python/ccpi/framework/BlockDataContainer.py | 152 |
1 files changed, 86 insertions, 66 deletions
diff --git a/Wrappers/Python/ccpi/framework/BlockDataContainer.py b/Wrappers/Python/ccpi/framework/BlockDataContainer.py index fee0cda..529a1ce 100755 --- a/Wrappers/Python/ccpi/framework/BlockDataContainer.py +++ b/Wrappers/Python/ccpi/framework/BlockDataContainer.py @@ -111,79 +111,98 @@ class BlockDataContainer(object): def __getitem__(self, row): return self.get_item(row) +# def add(self, other, *args, **kwargs): +# if not self.is_compatible(other): +# raise ValueError('Incompatible for add') +# out = kwargs.get('out', None) +# #print ("args" , *args) +# if isinstance(other, Number): +# return type(self)(*[ el.add(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# elif isinstance(other, list) or isinstance(other, numpy.ndarray): +# return type(self)(*[ el.add(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) +# elif issubclass(other.__class__, DataContainer): +# # try to do algebra with one DataContainer. Will raise error if not compatible +# return type(self)(*[ el.add(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# +# return type(self)( +# *[ el.add(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], +# shape=self.shape) +# +# def subtract(self, other, *args, **kwargs): +# if not self.is_compatible(other): +# raise ValueError('Incompatible for subtract') +# out = kwargs.get('out', None) +# if isinstance(other, Number): +# return type(self)(*[ el.subtract(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# elif isinstance(other, list) or isinstance(other, numpy.ndarray): +# return type(self)(*[ el.subtract(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) +# elif issubclass(other.__class__, DataContainer): +# # try to do algebra with one DataContainer. Will raise error if not compatible +# return type(self)(*[ el.subtract(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# return type(self)(*[ el.subtract(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], +# shape=self.shape) +# +# def multiply(self, other, *args, **kwargs): +# if not self.is_compatible(other): +# raise ValueError('{} Incompatible for multiply'.format(other)) +# out = kwargs.get('out', None) +# if isinstance(other, Number): +# return type(self)(*[ el.multiply(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# elif isinstance(other, list): +# return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) +# elif isinstance(other, numpy.ndarray): +# return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) +# elif issubclass(other.__class__, DataContainer): +# # try to do algebra with one DataContainer. Will raise error if not compatible +# return type(self)(*[ el.multiply(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], +# shape=self.shape) +# +# def divide_old(self, other, *args, **kwargs): +# if not self.is_compatible(other): +# raise ValueError('Incompatible for divide') +# out = kwargs.get('out', None) +# if isinstance(other, Number): +# return type(self)(*[ el.divide(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# elif isinstance(other, list) or isinstance(other, numpy.ndarray): +# return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) +# elif issubclass(other.__class__, DataContainer): +# # try to do algebra with one DataContainer. Will raise error if not compatible +# if out is not None: +# kw = kwargs.copy() +# for i,el in enumerate(self.containers): +# kw['out'] = out.get_item(i) +# el.divide(other, *args, **kw) +# return +# else: +# return type(self)(*[ el.divide(other, *args, **kwargs) for el in self.containers], shape=self.shape) +# return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], +# shape=self.shape) def add(self, other, *args, **kwargs): - if not self.is_compatible(other): - raise ValueError('Incompatible for add') out = kwargs.get('out', None) - #print ("args" , *args) - if isinstance(other, Number): - return type(self)(*[ el.add(other, *args, **kwargs) for el in self.containers], shape=self.shape) - elif isinstance(other, list) or isinstance(other, numpy.ndarray): - return type(self)(*[ el.add(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif issubclass(other.__class__, DataContainer): - # try to do algebra with one DataContainer. Will raise error if not compatible - return type(self)(*[ el.add(other, *args, **kwargs) for el in self.containers], shape=self.shape) - - return type(self)( - *[ el.add(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], - shape=self.shape) - + 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): - if not self.is_compatible(other): - raise ValueError('Incompatible for subtract') out = kwargs.get('out', None) - if isinstance(other, Number): - return type(self)(*[ el.subtract(other, *args, **kwargs) for el in self.containers], shape=self.shape) - elif isinstance(other, list) or isinstance(other, numpy.ndarray): - return type(self)(*[ el.subtract(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif issubclass(other.__class__, DataContainer): - # try to do algebra with one DataContainer. Will raise error if not compatible - return type(self)(*[ el.subtract(other, *args, **kwargs) for el in self.containers], shape=self.shape) - return type(self)(*[ el.subtract(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], - shape=self.shape) - + 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): - if not self.is_compatible(other): - raise ValueError('{} Incompatible for multiply'.format(other)) out = kwargs.get('out', None) - if isinstance(other, Number): - return type(self)(*[ el.multiply(other, *args, **kwargs) for el in self.containers], shape=self.shape) - elif isinstance(other, list): - return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif isinstance(other, numpy.ndarray): - return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif issubclass(other.__class__, DataContainer): - # try to do algebra with one DataContainer. Will raise error if not compatible - return type(self)(*[ el.multiply(other, *args, **kwargs) for el in self.containers], shape=self.shape) - return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], - shape=self.shape) - - def divide_old(self, other, *args, **kwargs): - if not self.is_compatible(other): - raise ValueError('Incompatible for divide') - out = kwargs.get('out', None) - if isinstance(other, Number): - return type(self)(*[ el.divide(other, *args, **kwargs) for el in self.containers], shape=self.shape) - elif isinstance(other, list) or isinstance(other, numpy.ndarray): - return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif issubclass(other.__class__, DataContainer): - # try to do algebra with one DataContainer. Will raise error if not compatible - if out is not None: - kw = kwargs.copy() - for i,el in enumerate(self.containers): - kw['out'] = out.get_item(i) - el.divide(other, *args, **kw) - return - else: - return type(self)(*[ el.divide(other, *args, **kwargs) for el in self.containers], shape=self.shape) - return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], - shape=self.shape) + 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): 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 binary_operations(self, operation, other, *args, **kwargs): if not self.is_compatible(other): @@ -215,11 +234,15 @@ class BlockDataContainer(object): return else: return type(self)(*res, shape=self.shape) - elif isinstance(other, (list, numpy.ndarray)): + elif isinstance(other, (list, numpy.ndarray, BlockDataContainer)): # try to do algebra with one DataContainer. Will raise error if not compatible kw = kwargs.copy() res = [] - for i,zel in enumerate(zip ( self.containers, other) ): + 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: @@ -244,9 +267,6 @@ class BlockDataContainer(object): else: return type(self)(*res, shape=self.shape) return type(self)(*[ operation(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - elif isinstance(other, BlockDataContainer): - return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], - shape=self.shape) else: raise ValueError('Incompatible type {}'.format(type(other))) |