def test_PowerMethod(self): print("test_BlockOperator") N, M = 200, 300 niter = 10 ig = ImageGeometry(N, M) Id = IdentityOperator(ig) G = GradientOperator(ig) uid = Id.domain_geometry().allocate(ImageGeometry.RANDOM, seed=1) a = LinearOperator.PowerMethod(Id, niter, uid) #b = LinearOperator.PowerMethodNonsquare(Id, niter, uid) b = LinearOperator.PowerMethod(Id, niter) print("Edo impl", a[0]) print("None impl", b[0]) #self.assertAlmostEqual(a[0], b[0]) self.assertNumpyArrayAlmostEqual(a[0], b[0], decimal=6) a = LinearOperator.PowerMethod(G, niter, uid) b = LinearOperator.PowerMethod(G, niter) #b = LinearOperator.PowerMethodNonsquare(G, niter, uid) print("Edo impl", a[0]) #print ("old impl", b[0]) self.assertNumpyArrayAlmostEqual(a[0], b[0], decimal=2)
def test_SymmetrisedGradientOperator2a(self): ########################################################################### # 2D geometry with channels # ig2 = ImageGeometry(N, M, channels = C) Grad2 = GradientOperator(self.ig2, correlation='Space') E2 = SymmetrisedGradientOperator(Grad2.range_geometry()) norm = LinearOperator.PowerMethod(E2, max_iterations=self.iterations) numpy.testing.assert_almost_equal(norm, numpy.sqrt(8), decimal=self.decimal)
def test_SymmetrisedGradientOperator3a(self): ########################################################################### # 3D geometry no channels #ig3 = ImageGeometry(N, M, K) Grad3 = GradientOperator(self.ig3, correlation='Space') E3 = SymmetrisedGradientOperator(Grad3.range_geometry()) norm = LinearOperator.PowerMethod(E3, max_iteration=100, tolerance=0) numpy.testing.assert_almost_equal(norm, numpy.sqrt(12), decimal=self.decimal)
def test_SymmetrisedGradientOperator1a(self): ########################################################################### ## Symmetrized Gradient Tests print("Test SymmetrisedGradientOperator") ########################################################################### # 2D geometry no channels # ig = ImageGeometry(N, M) Grad = GradientOperator(self.ig) E1 = SymmetrisedGradientOperator(Grad.range_geometry()) norm = LinearOperator.PowerMethod(E1, max_iteration=self.iterations) numpy.testing.assert_almost_equal(norm, numpy.sqrt(8), decimal=self.decimal)
def calculate_Lipschitz(self): # Compute the Lipschitz parameter from the operator if possible # Leave it initialised to None otherwise try: self._L = 2.0*self.c*(self.A.norm()**2) except AttributeError as ae: if self.A.is_linear(): Anorm = LinearOperator.PowerMethod(self.A, 10)[0] self._L = 2.0 * self.c * (Anorm*Anorm) else: warnings.warn('{} could not calculate Lipschitz Constant. {}'.format( self.__class__.__name__, ae)) except NotImplementedError as noe: warnings.warn('{} could not calculate Lipschitz Constant. {}'.format( self.__class__.__name__, noe)) if self.weight is not None: self._L *= self.weight_norm
def norm(self, **kwargs): '''Returns the norm of the BlockOperator if the operator in the block do not have method norm defined, i.e. they are SIRF AcquisitionModel's we use PowerMethod if applicable, otherwise we raise an Error ''' norm = [] for op in self.operators: if hasattr(op, 'norm'): norm.append(op.norm(**kwargs)**2.) else: # use Power method if op.is_linear(): norm.append(LinearOperator.PowerMethod(op, 20)[0]) else: raise TypeError( 'Operator {} does not have a norm method and is not linear' .format(op)) return numpy.sqrt(sum(norm))