.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "demos\02_algorithms\d08_Algorithms03.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note :ref:`Go to the end ` to download the full example code. .. rst-class:: sphx-glr-example-title .. _sphx_glr_demos_02_algorithms_d08_Algorithms03.py: Krylov subspace methods ======================= CGLS, LSQR, LSMR, hybrid LSQR and the GMRES variants. Source: `Python/demos/d08_Algorithms03.py `__ ยท `MATLAB/Demos/d08_Algorithms03.m `__ .. dropdown:: MATLAB version of this demo :icon: code .. literalinclude:: /../MATLAB/Demos/d08_Algorithms03.m :language: matlab .. GENERATED FROM PYTHON SOURCE LINES 17-19 Initialize ---------- .. GENERATED FROM PYTHON SOURCE LINES 19-27 .. code-block:: Python import tigre import numpy as np from tigre.utilities import sample_loader from tigre.utilities import CTnoise import tigre.algorithms as algs from matplotlib import pyplot as plt .. GENERATED FROM PYTHON SOURCE LINES 28-30 Geometry -------- .. GENERATED FROM PYTHON SOURCE LINES 30-33 .. code-block:: Python geo = tigre.geometry_default(high_resolution=False) .. GENERATED FROM PYTHON SOURCE LINES 34-36 Load data and generate projections ---------------------------------- .. GENERATED FROM PYTHON SOURCE LINES 36-46 .. code-block:: Python # define angles angles = np.linspace(0, 2 * np.pi, 100, endpoint=False) # Load thorax phantom data head = sample_loader.load_head_phantom(geo.nVoxel) # generate projections projections = tigre.Ax(head, geo, angles) # add noise noise_projections = CTnoise.add(projections, Poisson=1e5, Gaussian=np.array([0, 10])) .. GENERATED FROM PYTHON SOURCE LINES 47-49 Usage CGLS ---------- .. GENERATED FROM PYTHON SOURCE LINES 49-91 .. code-block:: Python # # # CGLS has the common 4 inputs for iterative algorithms in TIGRE: # # Projections, geometry, angles, and number of iterations # # Additionally it contains optional initialization techniques, but we # recommend not using them. CGLS is already quite fast and using them may # lead to divergence. # The options are: # 'init' Describes different initialization techniques. # - 'none' : Initializes the image to zeros (default) # - 'FDK' : initializes image to FDK reconstruction # - 'multigrid': Initializes image by solving the problem in # small scale and increasing it when relative # convergence is reached. # - 'image' : Initialization using a user specified # image. Not recommended unless you really # know what you are doing. # 'initimg' an image for the 'image' initialization. Avoid. # # use CGLS imgCGLS, normL2CGLS = algs.cgls(noise_projections, geo, angles, 30, computel2=True) # use LSQR imgLSQR, normL2LSQR = algs.lsqr(noise_projections, geo, angles, 30, computel2=True) # use LSMR imgLSMR, normL2LSMR = algs.lsmr(noise_projections, geo, angles, 30, computel2=True,lmbda=0) imgLSMR2, normL2LSMR2 = algs.lsmr(noise_projections, geo, angles, 30, computel2=True,lmbda=30) # use hybrid LSQR imghLSQR, normhL2LSQR = algs.hybrid_lsqr(noise_projections, geo, angles, 30, computel2=True) # AB/BA-GMRES imgabgmres, normhabgmres = algs.ab_gmres(noise_projections, geo, angles, 30, computel2=True) imgbagmres, normhbagmres = algs.ba_gmres(noise_projections, geo, angles, 30, computel2=True) # # AB/BA-GMRES with FDK backprojector imgabgmresfdk, normhabgmresfdk = algs.ab_gmres(noise_projections, geo, angles, 30, computel2=True,backprojector="FDK") imgbagmresfdk, normhbagmresfdk = algs.ba_gmres(noise_projections, geo, angles, 30, computel2=True,backprojector="FDK") # SIRT for comparison. imgSIRT, normL2SIRT = algs.sirt(noise_projections, geo, angles, 60, computel2=True) .. rst-class:: sphx-glr-script-out .. code-block:: none CGLS algorithm in progress. Estimated time until completion : 00:00:00 Algorithm will finish at : 16:49:53 re-initilization of CGLS called at iteration:21 Average time taken for each iteration for CGLS:0.008052303224817038(s) LSQR algorithm in progress. Estimated time until completion : 00:00:00 Algorithm will finish at : 16:49:54 LSMR algorithm in progress. Estimated time until completion : 00:00:00 Algorithm will finish at : 16:49:55 LSMR algorithm in progress. Estimated time until completion : 00:00:00 Algorithm will finish at : 16:49:56 HYBRID_LSQR algorithm in progress. Estimated time until completion : 00:00:01 Algorithm will finish at : 16:49:57 AB_GMRES algorithm in progress. Estimated time until completion : 00:00:02 Algorithm will finish at : 16:50:01 BA_GMRES algorithm in progress. Estimated time until completion : 00:00:00 Algorithm will finish at : 16:50:08 AB_GMRES algorithm in progress. Estimated time until completion : 00:00:02 Algorithm will finish at : 16:50:12 BA_GMRES algorithm in progress. Estimated time until completion : 00:00:02 Algorithm will finish at : 16:50:34 BA-GMRES exited due to divergence at iteration 13 SIRT algorithm in progress. Estimated time until completion : 00:00:01 Algorithm will finish at : 16:50:34 .. GENERATED FROM PYTHON SOURCE LINES 92-94 plot results ------------ .. GENERATED FROM PYTHON SOURCE LINES 94-109 .. code-block:: Python # # We can see that CGLS gets to the same L2 error in less amount of # iterations. plt.plot(np.vstack((normL2CGLS[0, :], normL2SIRT[0, 0:30],normL2LSMR[0, :],normL2LSMR2[0, :],normhL2LSQR[0, :],normhabgmres[0,:],normhbagmres[0,:],normhabgmresfdk[0,:],normhbagmresfdk[0,:])).T) plt.title("L2 error") plt.xlabel("Iteration") plt.ylabel("$ |Ax-b| $") plt.gca().legend(("CGLS","SIRT","LSMR lambda=0","LSMR lambda=30","hybrid LSQR","AB-GMRES","BA-GMRES","AB-GMRES FDK","BA-GMRES FDK")) plt.show() # plot images tigre.plotimg(np.concatenate([np.concatenate([imgCGLS, imgSIRT, imgLSQR,imgabgmres,imgabgmresfdk],axis=1),np.concatenate([imgLSMR, imgLSMR2, imghLSQR, imgbagmres, imgbagmresfdk], axis=1)], axis=2), dim="z", step=2, clims=[0, 2]) # plot errors tigre.plotimg(np.concatenate([np.concatenate([head-imgCGLS, head-imgSIRT, head-imgLSQR, head-imgabgmres, head-imgabgmresfdk],axis=1),np.concatenate([head-imgLSMR, head-imgLSMR2, head-imghLSQR, head-imgbagmres, head-imgbagmresfdk], axis=1)], axis=2), dim="z", slice=32) .. image-sg:: /demos/02_algorithms/images/sphx_glr_d08_Algorithms03_001.gif :alt: d08 Algorithms03 :srcset: /demos/02_algorithms/images/sphx_glr_d08_Algorithms03_001.gif :class: sphx-glr-single-img .. image-sg:: /demos/02_algorithms/images/sphx_glr_d08_Algorithms03_002.png :alt: L2 error :srcset: /demos/02_algorithms/images/sphx_glr_d08_Algorithms03_002.png :class: sphx-glr-single-img .. rst-class:: sphx-glr-timing **Total running time of the script:** (0 minutes 44.372 seconds) .. _sphx_glr_download_demos_02_algorithms_d08_Algorithms03.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: d08_Algorithms03.ipynb ` .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: d08_Algorithms03.py ` .. container:: sphx-glr-download sphx-glr-download-zip :download:`Download zipped: d08_Algorithms03.zip ` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_