def test_CompositionOperator_direct3(self):
        ig = self.ig
        data = self.data
        G = GradientOperator(domain_geometry=ig)

        Id1 = 2 * IdentityOperator(ig)
        Id2 = IdentityOperator(ig)

        d = CompositionOperator(G, Id2)

        out1 = G.direct(data)

        d_out = d.direct(data)

        d1 = Id2.direct(data)
        d2 = G.direct(d1)

        numpy.testing.assert_array_almost_equal(
            d2.get_item(0).as_array(),
            d_out.get_item(0).as_array())
        numpy.testing.assert_array_almost_equal(
            d2.get_item(1).as_array(),
            d_out.get_item(1).as_array())

        G2Id = G.compose(2 * Id2)
        d2g = G2Id.direct(data)

        numpy.testing.assert_array_almost_equal(
            d2g.get_item(0).as_array(), 2 * d_out.get_item(0).as_array())
        numpy.testing.assert_array_almost_equal(
            d2g.get_item(1).as_array(), 2 * d_out.get_item(1).as_array())
Exemplo n.º 2
0
    def test_NestedBlockDataContainer2(self):
        M, N = 2, 3
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        ag = ig
        u = ig.allocate(1)
        op1 = GradientOperator(ig)
        op2 = IdentityOperator(ig, ag)

        operator = BlockOperator(op1, op2, shape=(2, 1))

        d1 = op1.direct(u)
        d2 = op2.direct(u)

        d = operator.direct(u)

        dd = operator.domain_geometry()
        ww = operator.range_geometry()

        c1 = d + d

        c2 = 2 * d

        c3 = d / (d + 0.0001)

        c5 = d.get_item(0).power(2).sum()
Exemplo n.º 3
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)
Exemplo n.º 4
0
 def test_IdentityOperator(self):
     ig = ImageGeometry(10, 20, 30)
     img = ig.allocate()
     print(img.shape, ig.shape)
     self.assertTrue(img.shape == (30, 20, 10))
     self.assertEqual(img.sum(), 0)
     Id = IdentityOperator(ig)
     y = Id.direct(img)
     numpy.testing.assert_array_equal(y.as_array(), img.as_array())
 def test_IdentityOperator(self):
     print("test_IdentityOperator")
     ig = ImageGeometry(10, 20, 30)
     img = ig.allocate()
     # img.fill(numpy.ones((30,20,10)))
     self.assertTrue(img.shape == (30, 20, 10))
     #self.assertEqual(img.sum(), 2*float(10*20*30))
     self.assertEqual(img.sum(), 0.)
     Id = IdentityOperator(ig)
     y = Id.direct(img)
     numpy.testing.assert_array_equal(y.as_array(), img.as_array())
    def test_SumOperator(self):

        # numpy.random.seed(1)
        ig = self.ig
        data = self.data

        Id1 = 2 * IdentityOperator(ig)
        Id2 = IdentityOperator(ig)

        c = SumOperator(Id1, Id2)
        out = c.direct(data)

        numpy.testing.assert_array_almost_equal(out.as_array(),
                                                3 * data.as_array())
    def test_CompositionOperator_adjoint5(self):
        ig = self.ig
        data = self.data
        G = GradientOperator(domain_geometry=ig)

        Id1 = 3 * IdentityOperator(ig)
        Id = Id1 - IdentityOperator(ig)
        d = G.compose(Id)
        da = d.direct(data)

        out1 = G.adjoint(da)
        out2 = d.adjoint(da)

        numpy.testing.assert_array_almost_equal(out2.as_array(),
                                                2 * out1.as_array())
    def test_PDHG_strongly_convex_gamma_g(self):

        ig = ImageGeometry(3,3)
        data = ig.allocate('random')

        f = L2NormSquared(b=data)
        g = L2NormSquared()
        operator = IdentityOperator(ig)

        # sigma, tau 
        sigma = 1.0
        tau  = 1.0        

        pdhg = PDHG(f=f, g=g, operator=operator, sigma = sigma, tau=tau,
                    max_iteration=5, gamma_g=0.5)
        pdhg.run(1, verbose=0)
        self.assertAlmostEquals(pdhg.theta, 1.0/ np.sqrt(1 + 2 * pdhg.gamma_g * tau))
        self.assertAlmostEquals(pdhg.tau, tau * pdhg.theta)
        self.assertAlmostEquals(pdhg.sigma, sigma / pdhg.theta)
        pdhg.run(4, verbose=0)
        self.assertNotEqual(pdhg.sigma, sigma)
        self.assertNotEqual(pdhg.tau, tau)  

        # check negative strongly convex constant
        with self.assertRaises(ValueError):
            pdhg = PDHG(f=f, g=g, operator=operator, sigma = sigma, tau=tau,
                    max_iteration=5, gamma_g=-0.5)  
        

        # check strongly convex constant not a number
        with self.assertRaises(ValueError):
            pdhg = PDHG(f=f, g=g, operator=operator, sigma = sigma, tau=tau,
                    max_iteration=5, gamma_g="-0.5")  
Exemplo n.º 9
0
    def test_CGLS(self):
        print("Test CGLS")
        #ig = ImageGeometry(124,153,154)
        ig = ImageGeometry(10, 2)
        numpy.random.seed(2)
        initial = ig.allocate(0.)
        b = ig.allocate('random')
        # b = initial.copy()
        # fill with random numbers
        # b.fill(numpy.random.random(initial.shape))
        # b = ig.allocate()
        # bdata = numpy.reshape(numpy.asarray([i for i in range(20)]), (2,10))
        # b.fill(bdata)
        identity = IdentityOperator(ig)

        alg = CGLS(initial=initial, operator=identity, data=b)
        alg.max_iteration = 200
        alg.run(20, verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())

        alg = CGLS(initial=initial,
                   operator=identity,
                   data=b,
                   max_iteration=200,
                   update_objective_interval=2)
        self.assertTrue(alg.max_iteration == 200)
        self.assertTrue(alg.update_objective_interval == 2)
        alg.run(20, verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
    def setUp(self, *args, **kwargs):

        M, N, K = 3, 4, 5
        self.ig = ImageGeometry(M, N, K)

        self.x = self.ig.allocate('random', seed=1)
        self.b = self.ig.allocate('random', seed=2)
        self.eta = self.ig.allocate(0.1)

        self.operator = IdentityOperator(self.ig)

        scalar = 0.25
        self.f1 = L2NormSquared()
        self.f2 = L1Norm()
        self.f3 = scalar * L2NormSquared()
        self.f4 = scalar * L1Norm()
        self.f5 = scalar * L2NormSquared(b=self.b)
        self.f6 = scalar * L1Norm(b=self.b)
        self.f7 = ZeroFunction()
        self.f8 = 5 * ConstantFunction(10)
        self.f9 = LeastSquares(self.operator, self.b, c=scalar)
        self.f10 = 0.5 * KullbackLeibler(b=self.b, eta=self.eta)
        self.f11 = KullbackLeibler(b=self.b, eta=self.eta)
        self.f12 = 10

        self.list1 = [self.f1, self.f2, self.f3, self.f4, self.f5, \
                    self.f6, self.f7, self.f8, self.f9, self.f10, self.f11, self.f12]
Exemplo n.º 11
0
    def test_FISTA_Norm2Sq(self):
        print("Test FISTA Norm2Sq")
        ig = ImageGeometry(127, 139, 149)
        b = ig.allocate(ImageGeometry.RANDOM)
        # fill with random numbers
        initial = ig.allocate(ImageGeometry.RANDOM)
        identity = IdentityOperator(ig)

        #### it seems FISTA does not work with Nowm2Sq
        norm2sq = LeastSquares(identity, b)
        #norm2sq.L = 2 * norm2sq.c * identity.norm()**2
        #norm2sq = OperatorCompositionFunction(L2NormSquared(b=b), identity)
        opt = {'tol': 1e-4, 'memopt': False}
        if debug_print:
            print("initial objective", norm2sq(initial))
        alg = FISTA(initial=initial, f=norm2sq, g=ZeroFunction())
        alg.max_iteration = 2
        alg.run(20, verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())

        alg = FISTA(initial=initial,
                    f=norm2sq,
                    g=ZeroFunction(),
                    max_iteration=2,
                    update_objective_interval=3)
        self.assertTrue(alg.max_iteration == 2)
        self.assertTrue(alg.update_objective_interval == 3)

        alg.run(20, verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
Exemplo n.º 12
0
    def test_FISTA_catch_Lipschitz(self):
        if debug_print:
            print("Test FISTA catch Lipschitz")
        ig = ImageGeometry(127, 139, 149)
        initial = ImageData(geometry=ig)
        initial = ig.allocate()
        b = initial.copy()
        # fill with random numbers
        b.fill(numpy.random.random(initial.shape))
        initial = ig.allocate(ImageGeometry.RANDOM)
        identity = IdentityOperator(ig)

        #### it seems FISTA does not work with Nowm2Sq
        norm2sq = LeastSquares(identity, b)
        if debug_print:
            print('Lipschitz', norm2sq.L)
        # norm2sq.L = None
        #norm2sq.L = 2 * norm2sq.c * identity.norm()**2
        #norm2sq = OperatorCompositionFunction(L2NormSquared(b=b), identity)
        opt = {'tol': 1e-4, 'memopt': False}
        if debug_print:
            print("initial objective", norm2sq(initial))
        try:
            alg = FISTA(initial=initial, f=L1Norm(), g=ZeroFunction())
            self.assertTrue(False)
        except ValueError as ve:
            print(ve)
            self.assertTrue(True)
Exemplo n.º 13
0
    def test_ScaledBlockOperatorScalarList(self):
        ig = [ ImageGeometry(2,3) , \
               #ImageGeometry(10,20,30) , \
               ImageGeometry(2,3    ) ]
        x = [g.allocate() for g in ig]
        ops = [IdentityOperator(g) for g in ig]

        # test limit as non Scaled
        scalar = numpy.asarray([1 for _ in x])
        k = BlockOperator(*ops)
        K = scalar * k
        val = 1
        X = BlockDataContainer(*x) + val

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), (scalar * xx).sum() + zero)

        scalar = numpy.asarray([i + 1 for i, el in enumerate(x)])
        #scalar = numpy.asarray([6,0])
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + val
        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])

        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), (scalar * xx).sum() + zero)
Exemplo n.º 14
0
    def test_Function(self):

        numpy.random.seed(10)
        N = 3
        ig = ImageGeometry(N, N)
        ag = ig
        op1 = GradientOperator(ig)
        op2 = IdentityOperator(ig, ag)

        # Form Composite Operator
        operator = BlockOperator(op1, op2, shape=(2, 1))

        # Create functions
        noisy_data = ag.allocate(ImageGeometry.RANDOM)

        d = ag.allocate(ImageGeometry.RANDOM)
        alpha = 0.5
        # scaled function
        g = alpha * L2NormSquared(b=noisy_data)

        # Compare call of g
        a2 = alpha * (d - noisy_data).power(2).sum()
        #print(a2, g(d))
        self.assertEqual(a2, g(d))

        # Compare convex conjugate of g
        a3 = 0.5 * d.squared_norm() + d.dot(noisy_data)
        self.assertAlmostEqual(a3, g.convex_conjugate(d), places=7)
Exemplo n.º 15
0
    def test_ScaledBlockOperatorSingleScalar(self):
        ig = [ ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) , \
               ImageGeometry(10,20,30) ]
        x = [g.allocate() for g in ig]
        ops = [IdentityOperator(g) for g in ig]

        val = 1
        # test limit as non Scaled
        scalar = 1
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + val

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        xx = numpy.asarray([val for _ in x])
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), ((scalar * xx).sum() + zero))

        scalar = 0.5
        k = BlockOperator(*ops)
        K = scalar * k
        X = BlockDataContainer(*x) + 1

        Y = K.T.direct(X)
        self.assertTrue(Y.shape == (1, 1))
        zero = numpy.zeros(X.get_item(0).shape)
        numpy.testing.assert_array_equal(
            Y.get_item(0).as_array(), scalar * (len(x) + zero))
Exemplo n.º 16
0
    def test_GD(self):
        print("Test GD")
        ig = ImageGeometry(12, 13, 14)
        initial = ig.allocate()
        # b = initial.copy()
        # fill with random numbers
        # b.fill(numpy.random.random(initial.shape))
        b = ig.allocate('random')
        identity = IdentityOperator(ig)

        norm2sq = LeastSquares(identity, b)
        rate = norm2sq.L / 3.

        alg = GD(initial=initial,
                 objective_function=norm2sq,
                 rate=rate,
                 atol=1e-9,
                 rtol=1e-6)
        alg.max_iteration = 1000
        alg.run(verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
        alg = GD(initial=initial,
                 objective_function=norm2sq,
                 rate=rate,
                 max_iteration=20,
                 update_objective_interval=2,
                 atol=1e-9,
                 rtol=1e-6)
        alg.max_iteration = 20
        self.assertTrue(alg.max_iteration == 20)
        self.assertTrue(alg.update_objective_interval == 2)
        alg.run(20, verbose=0)
        self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
    def test_PowerMethod(self):

        # 2x2 real matrix, dominant eigenvalue = 2
        M1 = numpy.array([[1, 0], [1, 2]], dtype=float)
        M1op = MatrixOperator(M1)
        res1 = M1op.PowerMethod(M1op, 100)
        numpy.testing.assert_almost_equal(res1, 2., decimal=4)

        # Test with the norm
        res2 = M1op.norm()
        numpy.testing.assert_almost_equal(res1, res2, decimal=4)

        # 2x3 real matrix, dominant eigenvalue = 4.711479432297657
        M1 = numpy.array([[1., 0., 3], [1, 2., 3]])
        M1op = MatrixOperator(M1)
        res1 = M1op.PowerMethod(M1op, 100)
        numpy.testing.assert_almost_equal(res1, 4.711479432297657, decimal=4)

        # 2x3 complex matrix, (real eigenvalues), dominant eigenvalue = 5.417602365823937
        M1 = numpy.array([[2, 1j, 0], [2j, 5j, 0]])
        M1op = MatrixOperator(M1)
        res1 = M1op.PowerMethod(M1op, 100)
        numpy.testing.assert_almost_equal(res1, 5.417602365823937, decimal=4)

        # 3x3 complex matrix, (real+complex eigenvalue), dominant eigenvalue = 3.1624439599276974
        M1 = numpy.array([[2, 0, 0], [1, 2j, 1j], [3, 3 - 1j, 3]])
        M1op = MatrixOperator(M1)
        res1 = M1op.PowerMethod(M1op, 100)
        numpy.testing.assert_almost_equal(res1, 3.1624439599276974, decimal=4)

        # Gradient Operator (float)
        ig = ImageGeometry(30, 30)
        Grad = GradientOperator(ig)
        res1 = Grad.PowerMethod(Grad, 500, tolerance=1e-6)
        numpy.testing.assert_almost_equal(res1, numpy.sqrt(8), decimal=2)

        # Gradient Operator (complex)
        ig = ImageGeometry(30, 30, dtype=numpy.complex)
        Grad = GradientOperator(ig)
        res1 = Grad.PowerMethod(Grad, 500, tolerance=1e-6)
        numpy.testing.assert_almost_equal(res1, numpy.sqrt(8), decimal=2)

        # Identity Operator
        Id = IdentityOperator(ig)
        res1 = Id.PowerMethod(Id, 100)
        numpy.testing.assert_almost_equal(res1, 1.0, decimal=4)
 def test_ScaledOperator(self):
     print("test_ScaledOperator")
     ig = ImageGeometry(10, 20, 30)
     img = ig.allocate()
     scalar = 0.5
     sid = scalar * IdentityOperator(ig)
     numpy.testing.assert_array_equal(scalar * img.as_array(),
                                      sid.direct(img).as_array())
    def test_CompositionOperator_adjoint7(self):
        ig = self.ig
        data = self.data
        G = GradientOperator(domain_geometry=ig)

        Id1 = 2 * IdentityOperator(ig)
        Id2 = IdentityOperator(ig)

        d = CompositionOperator(G, Id1, Id2)

        out1 = G.direct(data)
        out2 = G.adjoint(out1)

        d_out = d.adjoint(out1)

        numpy.testing.assert_array_almost_equal(d_out.as_array(),
                                                2 * out2.as_array())
        numpy.testing.assert_array_almost_equal(d_out.as_array(),
                                                2 * out2.as_array())
    def test_CompositionOperator_direct1(self):
        ig = self.ig
        data = self.data
        G = GradientOperator(domain_geometry=ig)

        Id1 = 2 * IdentityOperator(ig)
        Id2 = IdentityOperator(ig)

        d = CompositionOperator(G, Id2)

        out1 = G.direct(data)
        out2 = d.direct(data)

        numpy.testing.assert_array_almost_equal(
            out2.get_item(0).as_array(),
            out1.get_item(0).as_array())
        numpy.testing.assert_array_almost_equal(
            out2.get_item(1).as_array(),
            out1.get_item(1).as_array())
Exemplo n.º 21
0
    def test_compare_with_PDHG(self):
        # Load an image from the CIL gallery.
        data = dataexample.SHAPES.get()
        ig = data.geometry
        # Add gaussian noise
        noisy_data = applynoise.gaussian(data, seed=10, var=0.005)

        # TV regularisation parameter
        alpha = 1

        # fidelity = 0.5 * L2NormSquared(b=noisy_data)
        # fidelity = L1Norm(b=noisy_data)
        fidelity = KullbackLeibler(b=noisy_data, use_numba=False)

        # Setup and run the PDHG algorithm
        F = BlockFunction(alpha * MixedL21Norm(), fidelity)
        G = ZeroFunction()
        K = BlockOperator(GradientOperator(ig), IdentityOperator(ig))

        # Compute operator Norm
        normK = K.norm()

        # Primal & dual stepsizes
        sigma = 1. / normK
        tau = 1. / normK

        pdhg = PDHG(f=F,
                    g=G,
                    operator=K,
                    tau=tau,
                    sigma=sigma,
                    max_iteration=100,
                    update_objective_interval=10)
        pdhg.run(verbose=0)

        sigma = 1
        tau = sigma / normK**2

        admm = LADMM(f=G,
                     g=F,
                     operator=K,
                     tau=tau,
                     sigma=sigma,
                     max_iteration=100,
                     update_objective_interval=10)
        admm.run(verbose=0)

        from cil.utilities.quality_measures import psnr
        if debug_print:
            print("PSNR", psnr(admm.solution, pdhg.solution))
        np.testing.assert_almost_equal(psnr(admm.solution, pdhg.solution),
                                       84.46678222768597,
                                       decimal=4)
Exemplo n.º 22
0
    def test_Norm2sq_as_OperatorCompositionFunction(self):

        print('Test for OperatorCompositionFunction')

        M, N = 50, 50
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        #numpy.random.seed(1)
        b = ig.allocate('random', seed=1)

        print('Check call with IdentityOperator operator... OK\n')
        operator = 3 * IdentityOperator(ig)

        u = ig.allocate('random', seed=50)
        f = 0.5 * L2NormSquared(b=b)
        func1 = OperatorCompositionFunction(f, operator)
        func2 = LeastSquares(operator, b, 0.5)
        print("f.L {}".format(f.L))
        print("0.5*f.L {}".format((0.5 * f).L))
        print("type func1 {}".format(type(func1)))
        print("func1.L {}".format(func1.L))
        print("func2.L {}".format(func2.L))
        print("operator.norm() {}".format(operator.norm()))

        numpy.testing.assert_almost_equal(func1(u), func2(u))

        print('Check gradient with IdentityOperator operator... OK\n')

        tmp1 = ig.allocate()
        tmp2 = ig.allocate()
        res_gradient1 = func1.gradient(u)
        res_gradient2 = func2.gradient(u)
        func1.gradient(u, out=tmp1)
        func2.gradient(u, out=tmp2)

        self.assertNumpyArrayAlmostEqual(res_gradient1.as_array(),
                                         res_gradient2.as_array())
        self.assertNumpyArrayAlmostEqual(tmp1.as_array(), tmp2.as_array())

        print('Check call with MatrixOperator... OK\n')
        mat = np.random.randn(M, N)
        operator = MatrixOperator(mat)
        vg = VectorGeometry(N)
        b = vg.allocate('random')
        u = vg.allocate('random')

        func1 = OperatorCompositionFunction(0.5 * L2NormSquared(b=b), operator)
        func2 = LeastSquares(operator, b, 0.5)

        self.assertNumpyArrayAlmostEqual(func1(u), func2(u))
        numpy.testing.assert_almost_equal(func1.L, func2.L)
 def test_exception_initial_CGLS(self):
     if debug_print:
         print ("Test CGLS")
     ig = ImageGeometry(10,2)
     numpy.random.seed(2)
     initial = ig.allocate(0.)
     b = ig.allocate('random')
     identity = IdentityOperator(ig)
     
     try:
         alg = CGLS(initial=initial, operator=identity, data=b, x_init=initial)
         assert False
     except ValueError as ve:
         assert True
    def test_PDHG_strongly_convex_both_fconj_and_g(self):

        ig = ImageGeometry(3,3)
        data = ig.allocate('random')

        f = L2NormSquared(b=data)
        g = L2NormSquared()
        operator = IdentityOperator(ig)
    
        try:
            pdhg = PDHG(f=f, g=g, operator=operator, max_iteration=10, 
                        gamma_g = 0.5, gamma_fconj=0.5)
            pdhg.run(verbose=0)
        except ValueError as err:
            print(err)
Exemplo n.º 25
0
    def test_Lipschitz(self):
        print('Test for OperatorCompositionFunction')

        M, N = 50, 50
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        b = ig.allocate('random', seed=1)

        print('Check call with IdentityOperator operator... OK\n')
        operator = 3 * IdentityOperator(ig)

        u = ig.allocate('random_int', seed=50)
        func2 = LeastSquares(operator, b, 0.5)
        assert func2.L != 2
        print(func2.L)
        func2.L = 2
        assert func2.L == 2
    def test_BlockOperatorLinearValidity(self):
        print("test_BlockOperatorLinearValidity")

        M, N = 3, 4
        ig = ImageGeometry(M, N)
        arr = ig.allocate('random', seed=1)

        G = GradientOperator(ig)
        Id = IdentityOperator(ig)

        B = BlockOperator(G, Id)
        # Nx1 case
        u = ig.allocate('random', seed=2)
        w = B.range_geometry().allocate(ImageGeometry.RANDOM, seed=3)
        w1 = B.direct(u)
        u1 = B.adjoint(w)
        self.assertAlmostEqual((w * w1).sum(), (u1 * u).sum(), places=5)
Exemplo n.º 27
0
    def test_Lipschitz3(self):
        print('Test for test_Lipschitz3')

        M, N = 50, 50
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        b = ig.allocate('random', seed=1)

        print('Check call with IdentityOperator operator... OK\n')
        operator = 3 * IdentityOperator(ig)

        u = ig.allocate('random_int', seed=50)
        # func2 = LeastSquares(operator, b, 0.5)
        func1 = ConstantFunction(0.3)
        f3 = TranslateFunction(func1, 3)
        assert f3.L != 2
        print(f3.L)
        f3.L = 2
        assert f3.L == 2
Exemplo n.º 28
0
    def test_SIRT(self):
        if debug_print:
            print("Test CGLS")
        #ig = ImageGeometry(124,153,154)
        ig = ImageGeometry(10, 2)
        numpy.random.seed(2)
        initial = ig.allocate(0.)
        b = ig.allocate('random')
        # b = initial.copy()
        # fill with random numbers
        # b.fill(numpy.random.random(initial.shape))
        # b = ig.allocate()
        # bdata = numpy.reshape(numpy.asarray([i for i in range(20)]), (2,10))
        # b.fill(bdata)
        identity = IdentityOperator(ig)

        alg = SIRT(initial=initial, operator=identity, data=b)
        alg.max_iteration = 200
        alg.run(20, verbose=0)
        np.testing.assert_array_almost_equal(alg.x.as_array(), b.as_array())

        alg2 = SIRT(initial=initial, operator=identity, data=b, upper=0.3)
        alg2.max_iteration = 200
        alg2.run(20, verbose=0)
        # equal
        try:
            numpy.testing.assert_equal(alg2.get_output().max(), 0.3)
            if debug_print:
                print("Equal OK, returning")
            return
        except AssertionError as ae:
            if debug_print:
                print("Not equal, trying almost equal")
        # almost equal to 7 digits or less
        try:
            numpy.testing.assert_almost_equal(alg2.get_output().max(), 0.3)
            if debug_print:
                print("Almost Equal OK, returning")
            return
        except AssertionError as ae:
            if debug_print:
                print("Not almost equal, trying less")
        numpy.testing.assert_array_less(alg2.get_output().max(), 0.3)
Exemplo n.º 29
0
    def test_exception_initial_GD(self):
        print("Test FISTA")
        ig = ImageGeometry(127, 139, 149)
        initial = ig.allocate()
        b = initial.copy()
        # fill with random numbers
        b.fill(numpy.random.random(initial.shape))
        initial = ig.allocate(ImageGeometry.RANDOM)
        identity = IdentityOperator(ig)

        norm2sq = OperatorCompositionFunction(L2NormSquared(b=b), identity)
        opt = {'tol': 1e-4, 'memopt': False}
        print("initial objective", norm2sq(initial))
        try:
            alg = GD(initial=initial,
                     objective_function=norm2sq,
                     x_init=initial)
            assert False
        except ValueError as ve:
            assert True
Exemplo n.º 30
0
    def test_Lipschitz4(self):
        print('Test for test_Lipschitz4')

        M, N = 50, 50
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        b = ig.allocate('random', seed=1)

        print('Check call with IdentityOperator operator... OK\n')
        operator = 3 * IdentityOperator(ig)

        u = ig.allocate('random_int', seed=50)
        # func2 = LeastSquares(operator, b, 0.5)
        func1 = ConstantFunction(0.3)
        f3 = func1 + 3
        assert f3.L == 0
        print("OK")
        print(f3.L)
        f3.L = 2
        assert f3.L == 2
        print("OK")
        assert func1.L == 0
        print("OK")
        try:
            func1.L = 2
            assert False
        except AttributeError as ve:
            assert True
        print("OK")
        f2 = LeastSquares(operator, b, 0.5)
        f4 = 2 * f2
        assert f4.L == 2 * f2.L

        print("OK")
        f4.L = 10
        assert f4.L != 2 * f2.L
        print("OK")

        f4 = -2 * f2
        assert f4.L == 2 * f2.L