Beispiel #1
0
    def test_CompositionOperator_direct3(self):
        ig = self.ig
        data = self.data
        G = Gradient(domain_geometry=ig)
        

        Id1 = 2 * Identity(ig)
        Id2 = Identity(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())
Beispiel #2
0
    def stest_NestedBlockDataContainer2(self):
        M, N = 2, 3
        ig = ImageGeometry(voxel_num_x=M, voxel_num_y=N)
        ag = ig
        u = ig.allocate(1)
        op1 = Gradient(ig)
        op2 = Identity(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()

        print(d.get_item(0).get_item(0).as_array())
        print(d.get_item(0).get_item(1).as_array())
        print(d.get_item(1).as_array())

        c1 = d + d

        c2 = 2 * d

        c3 = d / (d + 0.0001)

        c5 = d.get_item(0).power(2).sum()
Beispiel #3
0
    def test_FiniteDifference(self):
        print ("test FiniteDifference")
        ##
        N, M = 2, 3

        ig = ImageGeometry(N, M)
        Id = Identity(ig)

        FD = FiniteDiff(ig, direction = 0, bnd_cond = 'Neumann')
        u = FD.domain_geometry().allocate('random_int')
        
        
        res = FD.domain_geometry().allocate(ImageGeometry.RANDOM_INT)
        FD.adjoint(u, out=res)
        w = FD.adjoint(u)

        self.assertNumpyArrayEqual(res.as_array(), w.as_array())
        
        res = Id.domain_geometry().allocate(ImageGeometry.RANDOM_INT)
        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 = Gradient(ig)

        u = G.range_geometry().allocate(ImageGeometry.RANDOM_INT)
        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_INT)
        res = G.range_geometry().allocate()
        G.direct(u, out=res)
        w = G.direct(u)
        self.assertBlockDataContainerEqual(res, w)
Beispiel #4
0
    def stest_CompositionOperator_direct4(self):
        ig = self.ig
        data = self.data
        G = Gradient(domain_geometry=ig)
        

        sym = SymmetrizedGradient(domain_geometry=ig)
        Id2 = Identity(ig)
        
        d = CompositionOperator(sym, 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())
Beispiel #5
0
    def test_CompositionOperator_adjoint7(self):
        ig = self.ig
        data = self.data
        G = Gradient(domain_geometry=ig)
        

        Id1 = 2 * Identity(ig)
        Id2 = Identity(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())
Beispiel #6
0
    def test_Gradient(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)
        ig2 = ImageGeometry(voxel_num_x=M, voxel_num_y=N, voxel_num_z=K)
        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 = Gradient(ig1, correlation='Space', backend='numpy')
        print(G1.range_geometry().shape, '2D no channels')

        G4 = Gradient(ig3, correlation='SpaceChannels', backend='numpy')
        print(G4.range_geometry().shape, '2D with channels corr')
        G5 = Gradient(ig3, correlation='Space', backend='numpy')
        print(G5.range_geometry().shape, '2D with channels no corr')

        G6 = Gradient(ig4, correlation='Space', backend='numpy')
        print(G6.range_geometry().shape, '3D with channels no corr')
        G7 = Gradient(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_INT)

        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 = Gradient(ig_channel,
                                correlation='Space',
                                backend='numpy')
        G_channel = Gradient(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 = Gradient(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)
        arr = ig.allocate('random_int')

        # check direct of Gradient and sparse matrix
        G = Gradient(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 = Gradient(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 #7
0
    def test_Gradient_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 = Gradient(ig,
                         bnd_cond='Neumann',
                         correlation='SpaceChannels',
                         backend='numpy')
        gold_direct = grad1.direct(data)
        gold_adjoint = grad1.adjoint(gold_direct)

        grad2 = Gradient(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("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())

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

        grad2 = Gradient(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 #8
0
    shape.set_origin((10, 60, 0))

    # add the shape to the image
    image.add_shape(shape, scale=1)

    # add another shape
    shape.set_radii((30, 30))
    shape.set_origin((10, -30, 60))
    image.add_shape(shape, scale=1.5)

    # add another shape
    shape.set_origin((10, -30, -60))
    image.add_shape(shape, scale=0.75)

    G = Gradient(image, backend='c', correlation='SpaceChannel')
    out = G.direct(image)

    plotter2D([
        image.as_array()[70],
        out.get_item(0).as_array()[70],
        out.get_item(1).as_array()[70],
        out.get_item(2).as_array()[70]
    ],
              cmap='inferno',
              titles=['ImageData', 'grad_x', 'grad_y', 'grad_z'])

    raise RuntimeError('Stop it')
else:
    nxny = 127
    image = acq_data.create_uniform_image(0.0, (nxny, nxny))