From bfa7d487866ad5196466b8f4f6975a369961c8cd Mon Sep 17 00:00:00 2001 From: Edoardo Pasca Date: Mon, 18 Mar 2019 13:14:51 +0000 Subject: fix algebra for BlockDataContainer and add test --- .../Python/ccpi/framework/BlockDataContainer.py | 35 +++++++-------- Wrappers/Python/test/test_BlockDataContainer.py | 50 +++++++++++++++++++++- 2 files changed, 67 insertions(+), 18 deletions(-) (limited to 'Wrappers') diff --git a/Wrappers/Python/ccpi/framework/BlockDataContainer.py b/Wrappers/Python/ccpi/framework/BlockDataContainer.py index b9f5c5f..358ba2d 100755 --- a/Wrappers/Python/ccpi/framework/BlockDataContainer.py +++ b/Wrappers/Python/ccpi/framework/BlockDataContainer.py @@ -77,8 +77,9 @@ class BlockDataContainer(object): def add(self, other, *args, **kwargs): assert self.is_compatible(other) out = kwargs.get('out', None) + #print ("args" , *args) if isinstance(other, Number): - return type(self)(*[ el.add(other, out, *args, **kwargs) for el in self.containers], shape=self.shape) + 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, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) return type(self)( @@ -99,52 +100,52 @@ class BlockDataContainer(object): self.is_compatible(other) out = kwargs.get('out', None) if isinstance(other, Number): - return type(self)(*[ el.multiply(other, out, *args, **kwargs) for el in self.containers], shape=self.shape) + 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, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) + 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, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - return type(self)(*[ el.multiply(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], + return type(self)(*[ el.multiply(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], 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(self, other, *args, **kwargs): self.is_compatible(other) out = kwargs.get('out', None) if isinstance(other, Number): - return type(self)(*[ el.divide(other, out, *args, **kwargs) for el in self.containers], shape=self.shape) + 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, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - return type(self)(*[ el.divide(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], + return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) + return type(self)(*[ el.divide(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], shape=self.shape) def power(self, other, *args, **kwargs): assert self.is_compatible(other) out = kwargs.get('out', None) if isinstance(other, Number): - return type(self)(*[ el.power(other, out, *args, **kwargs) for el in self.containers], shape=self.shape) + return type(self)(*[ el.power(other, *args, **kwargs) for el in self.containers], shape=self.shape) elif isinstance(other, list) or isinstance(other, numpy.ndarray): - return type(self)(*[ el.power(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - return type(self)(*[ el.power(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], shape=self.shape) + return type(self)(*[ el.power(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) + return type(self)(*[ el.power(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], shape=self.shape) def maximum(self,other, *args, **kwargs): assert self.is_compatible(other) out = kwargs.get('out', None) if isinstance(other, Number): - return type(self)(*[ el.maximum(other, out, *args, **kwargs) for el in self.containers], shape=self.shape) + return type(self)(*[ el.maximum(other, *args, **kwargs) for el in self.containers], shape=self.shape) elif isinstance(other, list) or isinstance(other, numpy.ndarray): - return type(self)(*[ el.maximum(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) - return type(self)(*[ el.maximum(ot, out, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], shape=self.shape) + return type(self)(*[ el.maximum(ot, *args, **kwargs) for el,ot in zip(self.containers,other)], shape=self.shape) + return type(self)(*[ el.maximum(ot, *args, **kwargs) for el,ot in zip(self.containers,other.containers)], shape=self.shape) ## unary operations def abs(self, *args, **kwargs): out = kwargs.get('out', None) - return type(self)(*[ el.abs(out, *args, **kwargs) for el in self.containers], shape=self.shape) + return type(self)(*[ el.abs(*args, **kwargs) for el in self.containers], shape=self.shape) def sign(self, *args, **kwargs): out = kwargs.get('out', None) - return type(self)(*[ el.sign(out, *args, **kwargs) for el in self.containers], shape=self.shape) + return type(self)(*[ el.sign(*args, **kwargs) for el in self.containers], shape=self.shape) def sqrt(self, *args, **kwargs): out = kwargs.get('out', None) - return type(self)(*[ el.sqrt(out, *args, **kwargs) for el in self.containers], shape=self.shape) + 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) diff --git a/Wrappers/Python/test/test_BlockDataContainer.py b/Wrappers/Python/test/test_BlockDataContainer.py index ef11a82..ec69225 100755 --- a/Wrappers/Python/test/test_BlockDataContainer.py +++ b/Wrappers/Python/test/test_BlockDataContainer.py @@ -281,4 +281,52 @@ class TestBlockDataContainer(unittest.TestCase): s1 *= i #numpy.testing.assert_almost_equal(s[1], cp0.get_item(0,0).as_array()[0][0][0]*s0 +cp0.get_item(1,0).as_array()[0][0][0]*s1, decimal=4) - \ No newline at end of file + def test_Nested_BlockDataContainer(self): + print ("test_Nested_BlockDataContainer") + ig0 = ImageGeometry(2,3,4) + ig1 = ImageGeometry(2,3,4) + + data0 = ImageData(geometry=ig0) + data1 = ImageData(geometry=ig1) + 1 + + data2 = ImageData(geometry=ig0) + 2 + data3 = ImageData(geometry=ig1) + 3 + + cp0 = BlockDataContainer(data0,data1) + cp1 = BlockDataContainer(data2,data3) + + nbdc = BlockDataContainer(cp0, cp1) + nbdc2 = nbdc + 2 + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(0).as_array()[0][0][0] , 2. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(1).as_array()[0][0][0] , 3. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 4. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 5. , decimal=5) + + nbdc2 = 2 + nbdc + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(0).as_array()[0][0][0] , 2. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(1).as_array()[0][0][0] , 3. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 4. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 5. , decimal=5) + + + nbdc2 = nbdc * 2 + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(0).as_array()[0][0][0] , 0. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(1).as_array()[0][0][0] , 2. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 4. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 6. , decimal=5) + + nbdc2 = 2 * nbdc + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(0).as_array()[0][0][0] , 0. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(1).as_array()[0][0][0] , 2. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 4. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 6. , decimal=5) + + nbdc2 = nbdc / 2 + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(0).as_array()[0][0][0] , 0. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(0).get_item(1).as_array()[0][0][0] , .5 , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(0).as_array()[0][0][0] , 1. , decimal=5) + numpy.testing.assert_almost_equal(nbdc2.get_item(1).get_item(1).as_array()[0][0][0] , 3./2 , decimal=5) + + + print ("test_Nested_BlockDataContainer OK") + -- cgit v1.2.3