Exemplo n.º 1
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()
    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)
    def test_timedifference(self):

        print("test_timedifference")
        M, N, W = 100, 512, 512
        ig = ImageGeometry(M, N, W)
        arr = ig.allocate('random')

        G = GradientOperator(ig, backend='numpy')
        Id = IdentityOperator(ig)

        B = BlockOperator(G, Id)

        # Nx1 case
        u = ig.allocate('random')
        steps = [timer()]
        i = 0
        n = 10.
        t1 = t2 = 0
        res = B.range_geometry().allocate()

        while (i < n):
            print("i ", i)
            steps.append(timer())
            z1 = B.direct(u)
            steps.append(timer())
            t = dt(steps)
            #print ("B.direct(u) " ,t)
            t1 += t / n

            steps.append(timer())
            B.direct(u, out=res)
            steps.append(timer())
            t = dt(steps)
            #print ("B.direct(u, out=res) " ,t)
            t2 += t / n
            i += 1

        print("Time difference ", t1, t2)
        self.assertGreater(t1, t2)

        steps = [timer()]
        i = 0
        #n = 50.
        t1 = t2 = 0
        resd = B.domain_geometry().allocate()
        z1 = B.direct(u)
        #B.adjoint(z1, out=resd)

        print(type(res))
        while (i < n):
            print("i ", i)
            steps.append(timer())
            w1 = B.adjoint(z1)
            steps.append(timer())
            t = dt(steps)
            #print ("B.adjoint(z1) " ,t)
            t1 += t / n

            steps.append(timer())
            B.adjoint(z1, out=resd)
            steps.append(timer())
            t = dt(steps)
            #print ("B.adjoint(z1, out=res) " ,t)
            t2 += t / n
            i += 1

        print("Time difference ", t1, t2)
    def test_BlockOperator(self):
        print("test_BlockOperator")

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

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

        B = BlockOperator(G, Id)
        # Nx1 case
        u = ig.allocate('random')
        z1 = B.direct(u)

        res = B.range_geometry().allocate()
        #res = z1.copy()
        B.direct(u, out=res)

        print(type(z1), type(res))
        print(z1.shape)
        print(z1[0][0].as_array())
        print(res[0][0].as_array())
        self.assertBlockDataContainerEqual(z1, res)
        # for col in range(z1.shape[0]):
        #     a = z1.get_item(col)
        #     b = res.get_item(col)
        #     if isinstance(a, BlockDataContainer):
        #         for col2 in range(a.shape[0]):
        #             self.assertNumpyArrayEqual(
        #                 a.get_item(col2).as_array(),
        #                 b.get_item(col2).as_array()
        #                 )
        #     else:
        #         self.assertNumpyArrayEqual(
        #             a.as_array(),
        #             b.as_array()
        #             )
        z1 = B.range_geometry().allocate(ImageGeometry.RANDOM)

        res1 = B.adjoint(z1)
        res2 = B.domain_geometry().allocate()
        B.adjoint(z1, out=res2)

        self.assertNumpyArrayEqual(res1.as_array(), res2.as_array())

        BB = BlockOperator(Id, 2 * Id)
        B = BlockOperator(BB, Id)
        v = B.domain_geometry().allocate()
        B.adjoint(res, out=v)
        vv = B.adjoint(res)
        el1 = B.get_item(0,0).adjoint(z1.get_item(0)) +\
              B.get_item(1,0).adjoint(z1.get_item(1))
        print("el1", el1.as_array())
        print("vv", vv.as_array())
        print("v", v.as_array())

        self.assertNumpyArrayEqual(v.as_array(), vv.as_array())
        # test adjoint
        print("############ 2x1 #############")

        BB = BlockOperator(Id, 2 * Id)
        u = ig.allocate(1)
        z1 = BB.direct(u)
        print("z1 shape {} one\n{} two\n{}".format(z1.shape,
                                                   z1.get_item(0).as_array(),
                                                   z1.get_item(1).as_array()))
        res = BB.range_geometry().allocate(0)
        BB.direct(u, out=res)
        print("res shape {} one\n{} two\n{}".format(
            res.shape,
            res.get_item(0).as_array(),
            res.get_item(1).as_array()))

        self.assertNumpyArrayEqual(z1.get_item(0).as_array(), u.as_array())
        self.assertNumpyArrayEqual(z1.get_item(1).as_array(), 2 * u.as_array())
        self.assertNumpyArrayEqual(res.get_item(0).as_array(), u.as_array())
        self.assertNumpyArrayEqual(
            res.get_item(1).as_array(), 2 * u.as_array())

        x1 = BB.adjoint(z1)
        print("adjoint x1\n", x1.as_array())

        res1 = BB.domain_geometry().allocate()
        BB.adjoint(z1, out=res1)
        print("res1\n", res1.as_array())
        self.assertNumpyArrayEqual(x1.as_array(), res1.as_array())

        self.assertNumpyArrayEqual(x1.as_array(), 5 * u.as_array())
        self.assertNumpyArrayEqual(res1.as_array(), 5 * u.as_array())
        #################################################

        print("############ 2x2 #############")
        BB = BlockOperator(Id, 2 * Id, 3 * Id, Id, shape=(2, 2))
        B = BB
        u = ig.allocate(1)
        U = BlockDataContainer(u, u)
        z1 = B.direct(U)

        print("z1 shape {} one\n{} two\n{}".format(z1.shape,
                                                   z1.get_item(0).as_array(),
                                                   z1.get_item(1).as_array()))
        self.assertNumpyArrayEqual(z1.get_item(0).as_array(), 3 * u.as_array())
        self.assertNumpyArrayEqual(z1.get_item(1).as_array(), 4 * u.as_array())
        res = B.range_geometry().allocate()
        B.direct(U, out=res)
        self.assertNumpyArrayEqual(
            res.get_item(0).as_array(), 3 * u.as_array())
        self.assertNumpyArrayEqual(
            res.get_item(1).as_array(), 4 * u.as_array())

        x1 = B.adjoint(z1)
        # this should be [15 u, 10 u]
        el1 = B.get_item(0, 0).adjoint(z1.get_item(0)) + B.get_item(
            1, 0).adjoint(z1.get_item(1))
        el2 = B.get_item(0, 1).adjoint(z1.get_item(0)) + B.get_item(
            1, 1).adjoint(z1.get_item(1))

        shape = B.get_output_shape(z1.shape, adjoint=True)
        print("shape ", shape)
        out = B.domain_geometry().allocate()

        for col in range(B.shape[1]):
            for row in range(B.shape[0]):
                if row == 0:
                    el = B.get_item(row, col).adjoint(z1.get_item(row))
                else:
                    el += B.get_item(row, col).adjoint(z1.get_item(row))
            out.get_item(col).fill(el)

        print("el1 ", el1.as_array())
        print("el2 ", el2.as_array())
        print("out shape {} one\n{} two\n{}".format(
            out.shape,
            out.get_item(0).as_array(),
            out.get_item(1).as_array()))

        self.assertNumpyArrayEqual(
            out.get_item(0).as_array(), 15 * u.as_array())
        self.assertNumpyArrayEqual(
            out.get_item(1).as_array(), 10 * u.as_array())

        res2 = B.domain_geometry().allocate()
        #print (res2, res2.as_array())
        B.adjoint(z1, out=res2)

        #print ("adjoint",x1.as_array(),"\n",res2.as_array())
        self.assertNumpyArrayEqual(
            out.get_item(0).as_array(),
            res2.get_item(0).as_array())
        self.assertNumpyArrayEqual(
            out.get_item(1).as_array(),
            res2.get_item(1).as_array())

        if True:
            #B1 = BlockOperator(Id, Id, Id, Id, shape=(2,2))
            B1 = BlockOperator(G, Id)
            U = ig.allocate(ImageGeometry.RANDOM)
            #U = BlockDataContainer(u,u)
            RES1 = B1.range_geometry().allocate()

            Z1 = B1.direct(U)
            B1.direct(U, out=RES1)

            self.assertBlockDataContainerEqual(Z1, RES1)

            print("U", U.as_array())
            print("Z1", Z1[0][0].as_array())
            print("RES1", RES1[0][0].as_array())
            print("Z1", Z1[0][1].as_array())
            print("RES1", RES1[0][1].as_array())