Skip to content

Commit

Permalink
Add python sample scripts for ART, SART, SIRT, CGLS
Browse files Browse the repository at this point in the history
  • Loading branch information
wjp committed Jul 15, 2024
1 parent 30961ed commit 811ae29
Show file tree
Hide file tree
Showing 4 changed files with 171 additions and 4 deletions.
46 changes: 45 additions & 1 deletion docs/algs/ART.rst
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,51 @@ cfg.option.RayOrderList optional Required if option.RayOrder = 'custom', ignor
Example
-------

.. code-block:: matlab
.. tabs::
.. group-tab:: Python
.. code-block:: python
import astra
import matplotlib.pyplot as plt
import numpy
# create geometries and projector
proj_geom = astra.create_proj_geom('parallel', 1.0, 256, numpy.linspace(0, numpy.pi, 180, endpoint=False))
vol_geom = astra.create_vol_geom(256,256)
proj_id = astra.create_projector('linear', proj_geom, vol_geom)
# generate phantom image
V_exact_id, V_exact = astra.data2d.shepp_logan(vol_geom)
# create forward projection
sinogram_id, sinogram = astra.create_sino(V_exact, proj_id)
# reconstruct
recon_id = astra.data2d.create('-vol', vol_geom, 0)
cfg = astra.astra_dict('ART')
cfg['ProjectorId'] = proj_id
cfg['ProjectionDataId'] = sinogram_id
cfg['ReconstructionDataId'] = recon_id
cfg['option'] = { }
cfg['option']['RayOrder'] = 'custom'
fullRayList = numpy.array(numpy.meshgrid(range(180), range(256))).reshape(2,-1).T
cfg['option']['RayOrderList'] = numpy.random.permutation(fullRayList).reshape(-1)
art_id = astra.algorithm.create(cfg)
astra.algorithm.run(art_id, 3*180*256)
V = astra.data2d.get(recon_id)
plt.gray()
plt.imshow(V)
plt.show()
# garbage disposal
astra.data2d.delete([sinogram_id, recon_id, V_exact_id])
astra.projector.delete(proj_id)
astra.algorithm.delete(art_id)
.. group-tab:: Matlab
.. code-block:: matlab
%% create phantom
V_exact = phantom(256);
Expand Down
41 changes: 40 additions & 1 deletion docs/algs/CGLS.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,46 @@ cfg.option.ReconstructionMaskId optional If specified, the astra_mex_data2d ID
Example
-------

.. code-block:: matlab
.. tabs::
.. group-tab:: Python
.. code-block:: python
import astra
import matplotlib.pyplot as plt
import numpy
# create geometries and projector
proj_geom = astra.create_proj_geom('parallel', 1.0, 256, numpy.linspace(0, numpy.pi, 180, endpoint=False))
vol_geom = astra.create_vol_geom(256,256)
proj_id = astra.create_projector('linear', proj_geom, vol_geom)
# generate phantom image
V_exact_id, V_exact = astra.data2d.shepp_logan(vol_geom)
# create forward projection
sinogram_id, sinogram = astra.create_sino(V_exact, proj_id)
# reconstruct
recon_id = astra.data2d.create('-vol', vol_geom, 0)
cfg = astra.astra_dict('CGLS')
cfg['ProjectorId'] = proj_id
cfg['ProjectionDataId'] = sinogram_id
cfg['ReconstructionDataId'] = recon_id
cgls_id = astra.algorithm.create(cfg)
astra.algorithm.run(cgls_id, 100)
V = astra.data2d.get(recon_id)
plt.gray()
plt.imshow(V)
plt.show()
# garbage disposal
astra.data2d.delete([sinogram_id, recon_id, V_exact_id])
astra.projector.delete(proj_id)
astra.algorithm.delete(cgls_id)
.. group-tab:: Matlab
.. code-block:: matlab
%% create phantom
V_exact = phantom(256);
Expand Down
46 changes: 45 additions & 1 deletion docs/algs/SART.rst
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,51 @@ cfg.option.ProjectionOrderList optional Required if option.ProjectionOrder = '
Example
-------

.. code-block:: matlab
.. tabs::
.. group-tab:: Python
.. code-block:: python
import astra
import matplotlib.pyplot as plt
import numpy
# create geometries and projector
proj_geom = astra.create_proj_geom('parallel', 1.0, 256, numpy.linspace(0, numpy.pi, 180, endpoint=False))
vol_geom = astra.create_vol_geom(256,256)
proj_id = astra.create_projector('linear', proj_geom, vol_geom)
# generate phantom image
V_exact_id, V_exact = astra.data2d.shepp_logan(vol_geom)
# create forward projection
sinogram_id, sinogram = astra.create_sino(V_exact, proj_id)
# reconstruct
recon_id = astra.data2d.create('-vol', vol_geom, 0)
cfg = astra.astra_dict('SART')
cfg['ProjectorId'] = proj_id
cfg['ProjectionDataId'] = sinogram_id
cfg['ReconstructionDataId'] = recon_id
cfg['option'] = { }
cfg['option']['ProjectionOrder'] = 'custom'
# set projection order to 0, 5, 10, ..., 175, 1, 6, 11, ...., 176, 2, 7, .....
cfg['option']['ProjectionOrderList'] = numpy.array(range(180)).reshape(-1,5).T.reshape(-1)
sart_id = astra.algorithm.create(cfg)
astra.algorithm.run(sart_id, 10*180)
V = astra.data2d.get(recon_id)
plt.gray()
plt.imshow(V)
plt.show()
# garbage disposal
astra.data2d.delete([sinogram_id, recon_id, V_exact_id])
astra.projector.delete(proj_id)
astra.algorithm.delete(sart_id)
.. group-tab:: Matlab
.. code-block:: matlab
%% create phantom
V_exact = phantom(256);
Expand Down
42 changes: 41 additions & 1 deletion docs/algs/SIRT.rst
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,47 @@ cfg.option.MaxConstraint optional If specified, all values above MaxConstraint
Example
-------

.. code-block:: matlab
.. tabs::
.. group-tab:: Python
.. code-block:: python
import astra
import matplotlib.pyplot as plt
import numpy
# create geometries and projector
proj_geom = astra.create_proj_geom('parallel', 1.0, 256, numpy.linspace(0, numpy.pi, 180, endpoint=False))
vol_geom = astra.create_vol_geom(256,256)
proj_id = astra.create_projector('linear', proj_geom, vol_geom)
# generate phantom image
V_exact_id, V_exact = astra.data2d.shepp_logan(vol_geom)
# create forward projection
sinogram_id, sinogram = astra.create_sino(V_exact, proj_id)
# reconstruct
recon_id = astra.data2d.create('-vol', vol_geom, 0)
cfg = astra.astra_dict('SIRT')
cfg['ProjectorId'] = proj_id
cfg['ProjectionDataId'] = sinogram_id
cfg['ReconstructionDataId'] = recon_id
cfg['option'] = { 'MinConstraint': 0, 'MaxConstraint': 1 }
sirt_id = astra.algorithm.create(cfg)
astra.algorithm.run(sirt_id, 100)
V = astra.data2d.get(recon_id)
plt.gray()
plt.imshow(V)
plt.show()
# garbage disposal
astra.data2d.delete([sinogram_id, recon_id, V_exact_id])
astra.projector.delete(proj_id)
astra.algorithm.delete(sirt_id)
.. group-tab:: Matlab
.. code-block:: matlab
%% create phantom
V_exact = phantom(256);
Expand Down

0 comments on commit 811ae29

Please sign in to comment.