def test_CGLS(self): print ("Test CGLS") #ig = ImageGeometry(124,153,154) ig = ImageGeometry(10,2) numpy.random.seed(2) x_init = ig.allocate(0.) b = ig.allocate('random') # b = x_init.copy() # fill with random numbers # b.fill(numpy.random.random(x_init.shape)) # b = ig.allocate() # bdata = numpy.reshape(numpy.asarray([i for i in range(20)]), (2,10)) # b.fill(bdata) identity = Identity(ig) alg = CGLS(x_init=x_init, operator=identity, data=b) alg.max_iteration = 200 alg.run(20, verbose=True) self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array()) alg = CGLS(x_init=x_init, 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=True) self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
def test_CGLS(self): print ("Test CGLS") ig = ImageGeometry(124,153,154) x_init = ImageData(geometry=ig) b = x_init.copy() # fill with random numbers b.fill(numpy.random.random(x_init.shape)) identity = TomoIdentity(geometry=ig) alg = CGLS(x_init=x_init, operator=identity, data=b) alg.max_iteration = 1 alg.run(20, verbose=True) self.assertNumpyArrayAlmostEqual(alg.x.as_array(), b.as_array())
Aop = AstraProjectorSimple(ig, ag, dev) sin = Aop.direct(data) noisy_data = AcquisitionData(sin.as_array() + np.random.normal(0, 3, ig.shape)) # Setup and run the CGLS algorithm alpha = 50 Grad = Gradient(ig) # Form Tikhonov as a Block CGLS structure op_CGLS = BlockOperator(Aop, alpha * Grad, shape=(2, 1)) block_data = BlockDataContainer(noisy_data, Grad.range_geometry().allocate()) x_init = ig.allocate() cgls = CGLS(x_init=x_init, operator=op_CGLS, data=block_data) cgls.max_iteration = 1000 cgls.update_objective_interval = 200 cgls.run(1000, verbose=False) #Setup and run the PDHG algorithm # Create BlockOperator op_PDHG = BlockOperator(Grad, Aop, shape=(2, 1)) # Create functions f1 = 0.5 * alpha**2 * L2NormSquared() f2 = 0.5 * L2NormSquared(b=noisy_data) f = BlockFunction(f1, f2) g = ZeroFunction() ## Compute operator Norm
plt.colorbar() plt.subplot(2, 1, 2) plt.imshow(noisy_data.as_array()) plt.title('Noisy Data') plt.colorbar() plt.show() # Setup and run the regularised CGLS algorithm (Tikhonov with Gradient) x_init = ig.allocate() alpha = 2 op = Gradient(ig) block_op = BlockOperator(Identity(ig), alpha * op, shape=(2, 1)) block_data = BlockDataContainer(noisy_data, op.range_geometry().allocate()) cgls = CGLS(x_init=x_init, operator=block_op, data=block_data) cgls.max_iteration = 200 cgls.update_objective_interval = 5 cgls.run(200, verbose=True) # Show results plt.figure(figsize=(20, 10)) plt.subplot(3, 1, 1) plt.imshow(data.as_array()) plt.title('Ground Truth') plt.subplot(3, 1, 2) plt.imshow(noisy_data.as_array()) plt.title('Noisy') plt.subplot(3, 1, 3) plt.imshow(cgls.get_output().as_array()) plt.title('Regularised GGLS with Gradient')
im2 = axs[1].imshow(tmp[channel, :, axis[1], :], cmap='inferno') axs[1].set_title('coronal view') fig.colorbar(im2, ax=axs[1]) im3 = axs[2].imshow(tmp[channel, :, :, axis[2]], cmap='inferno') axs[2].set_title('sagittal view') fig.colorbar(im3, ax=axs[2]) fig.suptitle(title + ': Channel {}'.format(channel), fontsize=20) plt.show() #%% CGLS reconstruction on the 4D volume = 3D + channels x_init = A3D_chan.volume_geometry.allocate() cgls = CGLS(x_init=x_init, operator=A3D_chan, data=data) cgls.max_iteration = 100 cgls.update_objective_interval = 10 cgls.run(50, verbose=True, callback=show_data_4D) show_4D_channel_slice(cgls.get_output(), 5, 'CGLS reconstruction') show_4D_channel_slice(cgls.get_output(), 10, 'CGLS reconstruction') show_4D_channel_slice(cgls.get_output(), 15, 'CGLS reconstruction') #%% TV reconstuction channel-wise. # Basically for every energy - channel we apply TV reconstruction with the same parameter # which is not the best # Setup Astra Projector for the 3D volume A3D = A3D_chan.A3D
plt.title('Simulated data') plt.show() plt.imshow(z.array) plt.title('Backprojected data') plt.colorbar() plt.show() # Using the test data b, different reconstruction methods can now be set up as # demonstrated in the rest of this file. In general all methods need an initial # guess and some algorithm options to be set: x_init = ig.allocate() opt = {'tol': 1e-4, 'iter': 100} # First a CGLS reconstruction can be done: CGLS_alg = CGLS(x_init=x_init, operator=Aop, data=b) CGLS_alg.max_iteration = 2000 CGLS_alg.run(opt['iter']) x_CGLS = CGLS_alg.get_output() plt.figure() plt.imshow(x_CGLS.array) plt.title('CGLS') plt.show() plt.figure() plt.semilogy(CGLS_alg.objective) plt.title('CGLS criterion') plt.show()
# Test backprojection and projection z1 = Cop.adjoint(padded_data) z2 = Cop.direct(z1) plt.imshow(z1.subset(horizontal_x=68).array) plt.show() # Set initial guess for reconstruction algorithms. print ("Initial guess") x_init = ImageData(geometry=ig) # Set tolerance and number of iterations for reconstruction algorithms. opt = {'tol': 1e-4, 'iter': 100} # First a CGLS reconstruction can be done: CGLS_alg = CGLS() CGLS_alg.set_up(x_init, Cop, padded_data) CGLS_alg.max_iteration = 2000 CGLS_alg.update_objective_interval = 10 CGLS_alg.run(opt['iter']) x_CGLS = CGLS_alg.get_output() # Fix color and slices for display v1 = -0.01 v2 = 0.13 hx=80 hy=80 v=68 # Display ortho slices of reconstruction
# Show Ground Truth and Noisy Data plt.figure(figsize=(10, 10)) plt.subplot(2, 1, 1) plt.imshow(data.as_array()) plt.title('Ground Truth') plt.colorbar() plt.subplot(2, 1, 2) plt.imshow(noisy_data.as_array()) plt.title('Noisy Data') plt.colorbar() plt.show() # Setup and run the simple CGLS algorithm x_init = ig.allocate() cgls1 = CGLS(x_init=x_init, operator=Aop, data=noisy_data) cgls1.max_iteration = 20 cgls1.update_objective_interval = 5 cgls1.run(20, verbose=True) # Setup and run the regularised CGLS algorithm (Tikhonov with Identity) x_init = ig.allocate() alpha1 = 50 op1 = Identity(ig) block_op1 = BlockOperator(Aop, alpha1 * op1, shape=(2, 1)) block_data1 = BlockDataContainer(noisy_data, op1.range_geometry().allocate()) cgls2 = CGLS(x_init=x_init, operator=block_op1, data=block_data1) cgls2.max_iteration = 200
# Create the algorithm object from the configuration structure alg_id = astra.algorithm.create(cgls_astra) astra.algorithm.run(alg_id, 500) recon_cgls_astra = ImageData(astra.data2d.get(rec_id)) #%% ############################################################################### # # Setup and run the simple CGLS algorithm print("Running CGLS reconstruction") x_init = ig.allocate() cgls = CGLS() cgls.set_up(x_init=x_init, operator=Aop, data=sinogram) cgls.max_iteration = 500 cgls.update_objective_interval = 100 cgls.run(500, verbose=True) #%% ############################################################################### # Setup and run the PDHG algorithm print("Running PDHG reconstruction") operator = Aop f = L2NormSquared(b=sinogram) g = ZeroFunction()
# Show Ground Truth and Noisy Data plt.figure(figsize=(10, 10)) plt.subplot(2, 1, 1) plt.imshow(data.as_array()) plt.title('Ground Truth') plt.colorbar() plt.subplot(2, 1, 2) plt.imshow(projection_data.as_array()) plt.title('Projection Data') plt.colorbar() plt.show() # Setup and run the CGLS algorithm x_init = ig.allocate() cgls = CGLS(x_init=x_init, operator=Aop, data=projection_data) cgls.max_iteration = 5000 cgls.update_objective_interval = 100 cgls.run(1000, verbose=True) plt.figure(figsize=(5, 5)) plt.imshow(cgls.get_output().as_array()) plt.title('CGLS reconstruction') plt.colorbar() plt.show() #%% # Check with CVX solution print('If N > {} : CVX solution will take some time '.format(128))