def test_GradientOperator_4D_allocate(self):

        nc, nz, ny, nx = 3, 4, 5, 6
        size = nc * nz * ny * nx
        dim = [nc, nz, ny, nx]

        ig = ImageGeometry(voxel_num_x=nx, voxel_num_y=ny, voxel_num_z=nz, channels=nc)

        arr = numpy.arange(size).reshape(dim).astype(numpy.float32)**2

        data = ig.allocate()
        data.fill(arr)

        #numpy
        grad1 = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='numpy')
        gold_direct = grad1.direct(data)
        gold_adjoint = grad1.adjoint(gold_direct)

        grad2 = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='numpy')
        out_direct = grad2.range_geometry().allocate()
        out_adjoint = grad2.domain_geometry().allocate()
        grad2.direct(data, out=out_direct)
        grad2.adjoint(out_direct, out=out_adjoint)

        #print("GradientOperatorOperator, 4D, bnd_cond='Neumann', direct")
        numpy.testing.assert_array_equal(out_direct.get_item(0).as_array(), gold_direct.get_item(0).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(1).as_array(), gold_direct.get_item(1).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(2).as_array(), gold_direct.get_item(2).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(3).as_array(), gold_direct.get_item(3).as_array())

        #print("GradientOperator, 4D, bnd_cond='Neumann', adjoint")
        numpy.testing.assert_array_equal(out_adjoint.as_array(), gold_adjoint.as_array())

        #c
        grad1 = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='c')
        gold_direct = grad1.direct(data)
        gold_adjoint = grad1.adjoint(gold_direct)

        grad2 = GradientOperator(ig, bnd_cond='Neumann', correlation='SpaceChannels', backend='c')
        out_direct = grad2.range_geometry().allocate()
        out_adjoint = grad2.domain_geometry().allocate()
        grad2.direct(data, out=out_direct)
        grad2.adjoint(out_direct, out=out_adjoint)

        #print("GradientOperator, 4D, bnd_cond='Neumann', direct")
        numpy.testing.assert_array_equal(out_direct.get_item(0).as_array(), gold_direct.get_item(0).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(1).as_array(), gold_direct.get_item(1).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(2).as_array(), gold_direct.get_item(2).as_array())
        numpy.testing.assert_array_equal(out_direct.get_item(3).as_array(), gold_direct.get_item(3).as_array())

        #print("GradientOperator, 4D, bnd_cond='Neumann', adjoint")
        numpy.testing.assert_array_equal(out_adjoint.as_array(), gold_adjoint.as_array())
Beispiel #2
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))
Beispiel #3
0
    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())
        numpy.testing.assert_almost_equal(E2.norm(iterations=self.iterations),
                                          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)
Beispiel #5
0
    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())
        numpy.testing.assert_almost_equal(E1.norm(iterations=self.iterations),
                                          numpy.sqrt(8),
                                          decimal=self.decimal)
Beispiel #6
0
    def test_SymmetrisedGradientOperator3a(self):
        ###########################################################################
        # 3D geometry no channels
        #ig3 = ImageGeometry(N, M, K)
        Grad3 = GradientOperator(self.ig3, correlation='Space')

        E3 = SymmetrisedGradientOperator(Grad3.range_geometry())

        norm1 = E3.norm()
        norm2 = E3.calculate_norm(iterations=100)
        print(norm1, norm2)
        numpy.testing.assert_almost_equal(norm2,
                                          numpy.sqrt(12),
                                          decimal=self.decimal)
    def test_SymmetrisedGradientOperator2(self):
        ###########################################################################
        # 2D geometry with channels
        # ig2 = ImageGeometry(N, M, channels = C)
        Grad2 = GradientOperator(self.ig2, correlation='Space')

        E2 = SymmetrisedGradientOperator(Grad2.range_geometry())
        numpy.random.seed(1)
        u2 = E2.domain_geometry().allocate('random')
        w2 = E2.range_geometry().allocate('random', symmetry=True)
        #
        lhs2 = E2.direct(u2).dot(w2)
        rhs2 = u2.dot(E2.adjoint(w2))

        numpy.testing.assert_allclose(lhs2, rhs2, rtol=1e-3)
    def test_TranslateFunction_MixedL21Norm(self):

        print("Test for TranslateFunction for MixedL21Norm")

        ig = ImageGeometry(4, 4)

        Grad = GradientOperator(ig)
        b = Grad.range_geometry().allocate('random', seed=10)

        alpha = 0.4
        f1 = alpha * MixedL21Norm()
        fun = TranslateFunction(f1, b)

        tmp_x = Grad.range_geometry().allocate('random', seed=10)

        res1 = fun(tmp_x)
        res2 = f1(tmp_x - b)
        self.assertAlmostEqual(res1, res2)
        print("Check call...OK")

        res1 = f1.convex_conjugate(tmp_x) - b.dot(tmp_x)
        res2 = fun.convex_conjugate(tmp_x)
        self.assertAlmostEqual(res1, res2)
        print("Check convex conjugate...OK (maybe inf=inf)")

        tau = 0.4
        res1 = fun.proximal(tmp_x, tau)
        res2 = f1.proximal(tmp_x - b, tau) + b

        self.assertNumpyArrayAlmostEqual(
            res1.get_item(0).as_array(),
            res2.get_item(0).as_array())
        self.assertNumpyArrayAlmostEqual(
            res1.get_item(1).as_array(),
            res2.get_item(1).as_array())
        print("Check prox...OK ")
    def test_SymmetrisedGradientOperator1b(self):
        ###########################################################################
        ## Symmetrized GradientOperator Tests
        print("Test SymmetrisedGradientOperator")
        ###########################################################################
        # 2D geometry no channels
        # ig = ImageGeometry(N, M)
        Grad = GradientOperator(self.ig)

        E1 = SymmetrisedGradientOperator(Grad.range_geometry())
        numpy.random.seed(1)
        u1 = E1.domain_geometry().allocate('random')
        w1 = E1.range_geometry().allocate('random', symmetry=True)

        lhs = E1.direct(u1).dot(w1)
        rhs = u1.dot(E1.adjoint(w1))
        print("lhs {} rhs {}".format(lhs, rhs))
        # self.assertAlmostEqual(lhs, rhs)
        numpy.testing.assert_allclose(lhs, rhs, rtol=1e-3)
    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))
Beispiel #11
0
    ###########################################################################
    ## Symmetrized GradientOperator Tests
    from cil.framework import ImageGeometry
    from cil.optimisation.operators import GradientOperator
    import numpy as np

    N, M = 2, 3
    K = 2
    C = 2

    ###########################################################################
    # 2D geometry no channels
    ig = ImageGeometry(N, M)
    Grad = GradientOperator(ig)

    E1 = SymmetrizedGradientOperator(Grad.range_geometry())
    np.testing.assert_almost_equal(E1.norm(), np.sqrt(8), 1e-5)

    print(E1.domain_geometry().shape, E1.range_geometry().shape)
    u1 = E1.domain_geometry().allocate('random_int')
    w1 = E1.range_geometry().allocate('random_int', symmetry=True)

    lhs = E1.direct(u1).dot(w1)
    rhs = u1.dot(E1.adjoint(w1))
    np.testing.assert_almost_equal(lhs, rhs)

    ###########################################################################
    # 2D geometry with channels
    ig2 = ImageGeometry(N, M, channels=C)
    Grad2 = GradientOperator(ig2, correlation='Space')
    def test_FiniteDifference(self):
        print("test FiniteDifference")
        ##
        N, M = 2, 3
        numpy.random.seed(1)
        ig = ImageGeometry(N, M)
        Id = IdentityOperator(ig)

        FD = FiniteDifferenceOperator(ig, direction=0, bnd_cond='Neumann')
        u = FD.domain_geometry().allocate('random')

        res = FD.domain_geometry().allocate(ImageGeometry.RANDOM)
        FD.adjoint(u, out=res)
        w = FD.adjoint(u)

        self.assertNumpyArrayEqual(res.as_array(), w.as_array())

        res = Id.domain_geometry().allocate(ImageGeometry.RANDOM)
        Id.adjoint(u, out=res)
        w = Id.adjoint(u)

        self.assertNumpyArrayEqual(res.as_array(), w.as_array())
        self.assertNumpyArrayEqual(u.as_array(), w.as_array())

        G = GradientOperator(ig)

        u = G.range_geometry().allocate(ImageGeometry.RANDOM)
        res = G.domain_geometry().allocate()
        G.adjoint(u, out=res)
        w = G.adjoint(u)

        self.assertNumpyArrayEqual(res.as_array(), w.as_array())

        u = G.domain_geometry().allocate(ImageGeometry.RANDOM)
        res = G.range_geometry().allocate()
        G.direct(u, out=res)
        w = G.direct(u)
        self.assertBlockDataContainerEqual(res, w)

        # 2D
        M, N = 2, 3
        ig = ImageGeometry(voxel_num_x=M,
                           voxel_num_y=N,
                           voxel_size_x=0.1,
                           voxel_size_y=0.4)
        x = ig.allocate('random')

        labels = ["horizontal_y", "horizontal_x"]

        for i, dir in enumerate(labels):
            FD1 = FiniteDifferenceOperator(ig, direction=i)
            res1 = FD1.direct(x)
            res1b = FD1.adjoint(x)

            FD2 = FiniteDifferenceOperator(ig, direction=labels[i])
            res2 = FD2.direct(x)
            res2b = FD2.adjoint(x)

            numpy.testing.assert_almost_equal(res1.as_array(), res2.as_array())
            numpy.testing.assert_almost_equal(res1b.as_array(),
                                              res2b.as_array())
            print("Check 2D FiniteDiff for label {}".format(labels[i]))

        # 2D  + chan
        M, N, K = 2, 3, 4
        ig1 = ImageGeometry(voxel_num_x=M,
                            voxel_num_y=N,
                            channels=K,
                            voxel_size_x=0.1,
                            voxel_size_y=0.4)
        print(ig1.dimension_labels)
        x = ig1.allocate('random')

        labels = ["channel", "horizontal_y", "horizontal_x"]

        for i, dir in enumerate(labels):
            FD1 = FiniteDifferenceOperator(ig1, direction=i)
            res1 = FD1.direct(x)
            res1b = FD1.adjoint(x)

            FD2 = FiniteDifferenceOperator(ig1, direction=labels[i])
            res2 = FD2.direct(x)
            res2b = FD2.adjoint(x)

            numpy.testing.assert_almost_equal(res1.as_array(), res2.as_array())
            numpy.testing.assert_almost_equal(res1b.as_array(),
                                              res2b.as_array())
            print("Check for 2D chan for FiniteDiff label {}".format(
                labels[i]))
Beispiel #13
0
    def test_GradientOperator(self): 
        N, M, K = 20, 30, 40
        channels = 10
        
        numpy.random.seed(1)
        
        # check range geometry, examples
        
        ig1 = ImageGeometry(voxel_num_x = M, voxel_num_y = N) 
        ig3 = ImageGeometry(voxel_num_x = M, voxel_num_y = N, channels = channels) 
        ig4 = ImageGeometry(voxel_num_x = M, voxel_num_y = N, channels = channels, voxel_num_z= K) 
        
        G1 = GradientOperator(ig1, correlation = 'Space', backend='numpy') 
        print(G1.range_geometry().shape, '2D no channels')
            
        G4 = GradientOperator(ig3, correlation = 'SpaceChannels', backend='numpy')
        print(G4.range_geometry().shape, '2D with channels corr')  
        G5 = GradientOperator(ig3, correlation = 'Space', backend='numpy')
        print(G5.range_geometry().shape, '2D with channels no corr')
        
        G6 = GradientOperator(ig4, correlation = 'Space', backend='numpy')
        print(G6.range_geometry().shape, '3D with channels no corr')
        G7 = GradientOperator(ig4, correlation = 'SpaceChannels', backend='numpy')
        print(G7.range_geometry().shape, '3D with channels with corr')
        
        u = ig1.allocate(ImageGeometry.RANDOM)
        w = G1.range_geometry().allocate(ImageGeometry.RANDOM)
        
        LHS = (G1.direct(u)*w).sum()
        RHS = (u * G1.adjoint(w)).sum()
        numpy.testing.assert_approx_equal(LHS, RHS, significant = 1)
        numpy.testing.assert_approx_equal(G1.norm(), numpy.sqrt(2*4), significant = 1)
                    
        u1 = ig3.allocate('random')
        w1 = G4.range_geometry().allocate('random')
        LHS1 = (G4.direct(u1) * w1).sum()
        RHS1 = (u1 * G4.adjoint(w1)).sum() 
        numpy.testing.assert_approx_equal(LHS1, RHS1, significant=1)
        numpy.testing.assert_almost_equal(G4.norm(), numpy.sqrt(3*4), decimal = 0)
        
        u2 = ig4.allocate('random')
        w2 = G7.range_geometry().allocate('random')
        LHS2 = (G7.direct(u2) * w2).sum()
        RHS2 = (u2 * G7.adjoint(w2)).sum() 
        numpy.testing.assert_approx_equal(LHS2, RHS2, significant = 3)
        numpy.testing.assert_approx_equal(G7.norm(), numpy.sqrt(3*4), significant = 1)
        
        
        #check direct/adjoint for space/channels correlation
        
        ig_channel = ImageGeometry(voxel_num_x = 2, voxel_num_y = 3, channels = 2)
        G_no_channel = GradientOperator(ig_channel, correlation = 'Space', backend='numpy')
        G_channel = GradientOperator(ig_channel, correlation = 'SpaceChannels', backend='numpy')
        
        u3 = ig_channel.allocate('random_int')
        res_no_channel = G_no_channel.direct(u3)
        res_channel = G_channel.direct(u3)
        
        print(" Derivative for 3 directions, first is wrt Channel direction\n")
        print(res_channel[0].as_array())
        print(res_channel[1].as_array())
        print(res_channel[2].as_array())
        
        print(" Derivative for 2 directions, no Channel direction\n")
        print(res_no_channel[0].as_array())
        print(res_no_channel[1].as_array())  
        
        ig2D = ImageGeometry(voxel_num_x = 2, voxel_num_y = 3)
        u4 = ig2D.allocate('random_int')
        G2D = GradientOperator(ig2D, backend='numpy')
        res = G2D.direct(u4)  
        print(res[0].as_array())
        print(res[1].as_array())

        M, N = 20, 30
        ig = ImageGeometry(M, N)
        
        # check direct of GradientOperator and sparse matrix
        G = GradientOperator(ig, backend='numpy')
        norm1 = G.norm(iterations=300)
        print ("should be sqrt(8) {} {}".format(numpy.sqrt(8), norm1))
        numpy.testing.assert_almost_equal(norm1, numpy.sqrt(8), decimal=1)
        ig4 = ImageGeometry(M,N, channels=3)
        G4 = GradientOperator(ig4, correlation="SpaceChannels", backend='numpy')
        norm4 = G4.norm(iterations=300)
        print("should be sqrt(12) {} {}".format(numpy.sqrt(12), norm4))
        self.assertTrue((norm4 - numpy.sqrt(12))/norm4 < 0.2)
Beispiel #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())
Beispiel #15
0
    u = ig.allocate('random_int')

    z1 = B.direct(u)
    res = B.range_geometry().allocate()

    B.direct(u, out=res)

    ###########################################################################
    # Block Operator for TGV reconstruction

    M, N = 2, 3
    ig = ImageGeometry(M, N)
    ag = ig

    op11 = GradientOperator(ig)
    op12 = IdentityOperator(op11.range_geometry())

    op22 = SymmetrisedGradientOperator(op11.domain_geometry())

    op21 = ZeroOperator(ig, op22.range_geometry())

    op31 = IdentityOperator(ig, ag)
    op32 = ZeroOperator(op22.domain_geometry(), ag)

    operator = BlockOperator(op11,
                             -1 * op12,
                             op21,
                             op22,
                             op31,
                             op32,
                             shape=(3, 2))