From 03c03ea81068b62c95882c769230bf8c4c63337b Mon Sep 17 00:00:00 2001 From: Edoardo Pasca Date: Sat, 16 Feb 2019 22:06:03 +0000 Subject: First implementation of CompositeOperator/DataContainer --- Wrappers/Python/ccpi/framework.py | 3 ++ .../optimisation/operators/CompositeOperator.py | 39 ++++++++-------------- 2 files changed, 17 insertions(+), 25 deletions(-) diff --git a/Wrappers/Python/ccpi/framework.py b/Wrappers/Python/ccpi/framework.py index 9938fb7..6af7a97 100644 --- a/Wrappers/Python/ccpi/framework.py +++ b/Wrappers/Python/ccpi/framework.py @@ -558,6 +558,8 @@ class DataContainer(object): # __isub__ def __idiv__(self, other): + return self.__itruediv__(other) + def __itruediv__(self, other): if isinstance(other, (int, float)) : numpy.divide(self.array, other, out=self.array) elif issubclass(type(other), DataContainer): @@ -721,6 +723,7 @@ class DataContainer(object): def sum(self, out=None, *args, **kwargs): return self.as_array().sum(*args, **kwargs) + class ImageData(DataContainer): '''DataContainer for holding 2D or 3D DataContainer''' def __init__(self, diff --git a/Wrappers/Python/ccpi/optimisation/operators/CompositeOperator.py b/Wrappers/Python/ccpi/optimisation/operators/CompositeOperator.py index 06c0ca8..6a14262 100755 --- a/Wrappers/Python/ccpi/optimisation/operators/CompositeOperator.py +++ b/Wrappers/Python/ccpi/optimisation/operators/CompositeOperator.py @@ -219,26 +219,19 @@ class CompositeDataContainer(object): for el,ot in zip(self.containers, other): el *= ot return self - # __rmul__ + # __imul__ def __idiv__(self, other): if isinstance (other, CompositeDataContainer): - for i,el,ot in enumerate(zip(self.containers, other.containers)): - print ('__idiv__', i, el.as_array()[0][0][0], ot.as_array()[0][0][0]) + for el,ot in zip(self.containers, other.containers): el /= ot - print ("fatto", el.as_array()[0][0][0]) elif isinstance(other, Number): for el in self.containers: - print ("prima", el) - print ('__idiv__', el.as_array()[0][0][0], other) el /= other - print ("fatto", el.as_array()[0][0][0]) elif isinstance(other, list) or isinstance(other, numpy.ndarray): assert self.is_compatible(other) for el,ot in zip(self.containers, other): - print ('__idiv__', el.as_array()[0][0][0], ot[0][0][0]) el /= ot - print ("fatto", el.as_array()[0][0][0]) return self # __rdiv__ def __itruediv__(self, other): @@ -486,24 +479,20 @@ if __name__ == '__main__': numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , 0.5, decimal = 5) cp2 += 1 - print ("cp2" , cp2[0].as_array()[0][0][0],cp2[1].as_array()[0][0][0]) - print ("cp1" , cp1[0].as_array()[0][0][0],cp1[1].as_array()[0][0][0]) - #cp2 /= cp1 + cp2 /= cp1 # TODO fix inplace division - cp2 /= 0.5 - print (cp2[0].as_array()[0][0][0],cp2[1].as_array()[0][0][0]) - - numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , 1./0.5 , decimal=5) - numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , .2, decimal = 5) + + numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , 1./2 , decimal=5) + numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , 1.5/3., decimal = 5) cp2 /= 1 - numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , 0. , decimal=5) - numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , 0.5/3., decimal = 5) + numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , 0.5 , decimal=5) + numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , 0.5, decimal = 5) cp2 /= [-2,-1] - numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , 0. , decimal=5) - numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , -0.5/3., decimal = 5) - + numpy.testing.assert_almost_equal(cp2[0].as_array()[0][0][0] , -0.5/2. , decimal=5) + numpy.testing.assert_almost_equal(cp2[1].as_array()[0][0][0] , -0.5, decimal = 5) + #### cp2 = cp0.power(cp1) assert (cp2[0].as_array()[0][0][0] == 0.) @@ -656,6 +645,6 @@ if __name__ == '__main__': # # for _ in gd: # pass - - - \ No newline at end of file +# +# +# \ No newline at end of file -- cgit v1.2.3