Esempio n. 1
0
    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_dot_test(self):
        Grad3 = GradientOperator(self.ig3,
                                 correlation='Space',
                                 backend='numpy')

        # self.assertAlmostEqual(lhs3, rhs3)
        self.assertTrue(LinearOperator.dot_test(Grad3, verbose=True,
                                                decimal=4))
        self.assertTrue(LinearOperator.dot_test(Grad3, verbose=True,
                                                decimal=4))
Esempio n. 3
0
    def test_GradientOperator_linearity(self):

        nc, nz, ny, nx = 3, 4, 5, 6
        ig = ImageGeometry(voxel_num_x=nx, voxel_num_y=ny, voxel_num_z=nz, channels=nc)

        grad = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='c')   
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Periodic', correlation='SpaceChannels', backend='c')
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='numpy')
        self.assertTrue(LinearOperator.dot_test(grad))

        grad = GradientOperator(ig, bnd_cond='Periodic', correlation='SpaceChannels', backend='numpy')
        self.assertTrue(LinearOperator.dot_test(grad))
Esempio n. 4
0
    def test_SymmetrisedGradientOperator3b(self):
        ###########################################################################
        # 3D geometry no channels
        #ig3 = ImageGeometry(N, M, K)
        Grad3 = GradientOperator(self.ig3, correlation='Space')

        E3 = SymmetrisedGradientOperator(Grad3.range_geometry())
        numpy.random.seed(1)
        u3 = E3.domain_geometry().allocate('random')
        w3 = E3.range_geometry().allocate('random', symmetry=True)
        #
        lhs3 = E3.direct(u3).dot(w3)
        rhs3 = u3.dot(E3.adjoint(w3))
        # with numpy 1.11 and py 3.5 decimal = 3
        decimal = 4
        npv = version.parse(numpy.version.version)
        if npv.major == 1 and npv.minor == 11:
            print("########## SETTING decimal to 3 ###########")
            decimal = 3
        numpy.testing.assert_almost_equal(lhs3, rhs3, decimal=decimal)
        # self.assertAlmostEqual(lhs3, rhs3,  )
        print("*******", lhs3, rhs3, abs((rhs3 - lhs3) / rhs3), 1.5 * 10**(-4),
              abs((rhs3 - lhs3) / rhs3) < 1.5 * 10**(-4))
        self.assertTrue(
            LinearOperator.dot_test(E3,
                                    range_init=w3,
                                    domain_init=u3,
                                    decimal=decimal))
    def test_dot_test2(self):
        Grad3 = GradientOperator(self.ig3,
                                 correlation='SpaceChannel',
                                 backend='c')

        # self.assertAlmostEqual(lhs3, rhs3)
        # self.assertTrue( LinearOperator.dot_test(Grad3 , verbose=True))
        self.assertTrue(LinearOperator.dot_test(Grad3, decimal=4,
                                                verbose=True))
    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)
Esempio n. 9
0
 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
Esempio n. 10
0
    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))
Esempio n. 11
0
    def test_GradientOperator_complex_data(self):

        # make complex dtype
        self.ig_2D.dtype = numpy.complex
        x = self.ig_2D.allocate('random')

        Grad = GradientOperator(domain_geometry=self.ig_2D)

        res1 = Grad.direct(x)
        res2 = Grad.range.allocate()
        Grad.direct(x, out=res2)

        numpy.testing.assert_array_almost_equal(res1[0].as_array(),
                                                res2[0].as_array())
        numpy.testing.assert_array_almost_equal(res1[1].as_array(),
                                                res2[1].as_array())

        # check dot_test
        for sd in [5, 10, 15]:
            self.assertTrue(LinearOperator.dot_test(Grad, seed=sd))
Esempio n. 12
0
    def test_GradientOperator_linearity(self):

        for geom in self.list_geometries:
            for bnd in self.bconditions:
                for backend in self.backend:
                    for corr in self.correlation:
                        for method in self.method:

                            Grad = GradientOperator(geom,
                                                    bnd_cond=bnd,
                                                    backend=backend,
                                                    correlation=corr,
                                                    method=method)
                            try:
                                for sd in [5, 10, 15]:
                                    self.assertTrue(
                                        LinearOperator.dot_test(Grad, seed=sd))
                            except:
                                self.print_assertion_info(
                                    geom, bnd, backend, corr, method, None)
                                raise
    def test_SymmetrisedGradientOperator3b(self):
        ###########################################################################
        # 3D geometry no channels
        #ig3 = ImageGeometry(N, M, K)
        Grad3 = GradientOperator(self.ig3, correlation='Space')

        E3 = SymmetrisedGradientOperator(Grad3.range_geometry())
        numpy.random.seed(1)
        u3 = E3.domain_geometry().allocate('random')
        w3 = E3.range_geometry().allocate('random', symmetry=True)
        #
        lhs3 = E3.direct(u3).dot(w3)
        rhs3 = u3.dot(E3.adjoint(w3))

        numpy.testing.assert_almost_equal(lhs3, rhs3, decimal=3)
        print("*******", lhs3, rhs3, abs((rhs3 - lhs3) / rhs3), 1.5 * 10**(-4),
              abs((rhs3 - lhs3) / rhs3) < 1.5 * 10**(-4))
        self.assertTrue(
            LinearOperator.dot_test(E3,
                                    range_init=w3,
                                    domain_init=u3,
                                    decimal=3))
Esempio n. 14
0
    def test_GradientOperator_for_pseudo_2D_geometries(self):

        numpy.random.seed(1)
        # ImageGeometry shape (5,5,1)
        ig1 = ImageGeometry(voxel_num_x=1,
                            voxel_num_y=5,
                            voxel_num_z=5,
                            voxel_size_x=0.4,
                            voxel_size_y=0.2,
                            voxel_size_z=0.6)
        # ImageGeometry shape (1,5,5)
        ig2 = ImageGeometry(voxel_num_x=5,
                            voxel_num_y=5,
                            voxel_num_z=1,
                            voxel_size_x=0.1,
                            voxel_size_y=0.2,
                            voxel_size_z=0.4)
        # ImageGeometry shape (5,1,5)
        ig3 = ImageGeometry(voxel_num_x=5,
                            voxel_num_y=1,
                            voxel_num_z=5,
                            voxel_size_x=0.6,
                            voxel_size_y=0.4,
                            voxel_size_z=0.3)

        data1 = ig1.allocate('random')
        data2 = ig2.allocate('random')
        data3 = ig3.allocate('random')

        data = [data1, data2, data3]
        ig = [ig1, ig2, ig3]

        for i in range(3):

            ########################################
            ##### Test Gradient numpy backend  #####
            ########################################
            Grad_numpy = GradientOperator(ig[i], backend='numpy')
            res1 = Grad_numpy.direct(data[i])
            res2 = Grad_numpy.range_geometry().allocate()
            Grad_numpy.direct(data[i], out=res2)

            # test direct with and without out
            numpy.testing.assert_array_almost_equal(res1[0].as_array(),
                                                    res2[0].as_array())
            numpy.testing.assert_array_almost_equal(res1[1].as_array(),
                                                    res2[1].as_array())

            # test adjoint with and without out
            res3 = Grad_numpy.adjoint(res1)
            res4 = Grad_numpy.domain_geometry().allocate()
            Grad_numpy.adjoint(res2, out=res4)
            numpy.testing.assert_array_almost_equal(res3.as_array(),
                                                    res4.as_array())

            # test dot_test
            for sd in [5, 10, 15]:
                self.assertTrue(LinearOperator.dot_test(Grad_numpy, seed=sd))

            # test shape of output of direct
            self.assertEqual(res1[0].shape, ig[i].shape)
            self.assertEqual(res1.shape, (2, 1))

            ########################################
            ##### Test Gradient c backend  #####
            ########################################
            Grad_c = GradientOperator(ig[i], backend='c')

            # test direct with and without out
            res5 = Grad_c.direct(data[i])
            res6 = Grad_c.range_geometry().allocate() * 0.
            Grad_c.direct(data[i], out=res6)

            numpy.testing.assert_array_almost_equal(res5[0].as_array(),
                                                    res6[0].as_array())
            numpy.testing.assert_array_almost_equal(res5[1].as_array(),
                                                    res6[1].as_array())

            # test adjoint
            res7 = Grad_c.adjoint(res5)
            res8 = Grad_c.domain_geometry().allocate() * 0.
            Grad_c.adjoint(res5, out=res8)
            numpy.testing.assert_array_almost_equal(res7.as_array(),
                                                    res8.as_array())

            # test dot_test
            for sd in [5, 10, 15]:
                self.assertTrue(LinearOperator.dot_test(Grad_c, seed=sd))

            # test direct numpy vs direct c backends (with and without out)
            numpy.testing.assert_array_almost_equal(res5[0].as_array(),
                                                    res1[0].as_array())
            numpy.testing.assert_array_almost_equal(res6[1].as_array(),
                                                    res2[1].as_array())