Test examples

The following documents a number of tests covering essentially all functions and features contained in the package. These functions serve the double purpose of showing that the package is indeed working as desired (via pytest or nosetests, see Installation), and illustrating to users how to set various parameters of the gratopy toolbox and what their effect are (cf. the source code for the tests).

The tests are also able to produce plots of the results. To turn on plotting, the environment variable GRATOPY_TEST_PLOT needs to be set, e.g. the command

GRATOPY_TEST_PLOT=true pytest

can be issued in the gratopy directory.

Radon transform

tests.test_radon.test_projection()[source]

Basic projection test. Simply computes forward and backprojection of the Radon transform for two test images in order to visually confirm the correctness of the method. This projection is repeated 10 times to estimate the required time per execution.

tests.test_radon.test_types_contiguity()[source]

Types and contiguity test. Runs forward and backprojections for parallel beam geometry for different precision and contiguity settings, checking that they all lead to the same results.

tests.test_radon.test_weighting()[source]

Mass preservation test. Check whether the total mass of an image (square with side length 4/3 and pixel values, i.e. density, 1) is correctly transported into the total mass of a projection, i.e., the scaling is adequate.

tests.test_radon.test_adjointness()[source]

Adjointness test. Creates random images and sinograms to check whether forward and backprojection are indeed adjoint to one another (by comparing the corresponding dual pairings). This comparison is carried out for 100 experiments to affirm adjointness with some certainty.

tests.test_radon.test_nonquadratic()[source]

Non-quadratic image test. Tests and illustrates the projection operator for non-quadratic images.

tests.test_radon.test_limited_angles()[source]

Limited angle test. Tests and illustrates how to set the angles in case of limited angle situation, in particular showing artifacts resulting from the incorrect use for the limited angle setting (leading to undesired angle_weights). This can be achieved through the format of the angles parameter or by setting the angle_weights directly as shown in the test.

tests.test_radon.test_angle_input_variants()[source]

Angle parameter input test. Illustrates all possibilities to specify projection angles, checks the resulting angles and angle_weights as well as tests the possibility to set the angle_weights manually.

tests.test_radon.test_midpoint_shift()[source]

Shifted midpoint test. Tests and illustrates how the sinogram changes if the midpoint of an images is shifted away from the center of rotation.

tests.test_radon.test_create_sparse_matrix()[source]

Tests the create_sparse_matrix method to create a sparse matrix associated with the transform, and tests it by applying forward and backprojection via matrix multiplication.

Fanbeam transform

tests.test_fanbeam.test_projection()[source]

Basic projection test. Computes the forward and backprojection of the fanbeam transform for two test images to visually confirm the correctness of the method. This projection is repeated 10 times to estimate the required time per execution.

tests.test_fanbeam.test_types_contiguity()[source]

Types and contiguity test. Types and contiguity test. Runs forward and backprojections for fanbeam geometry for different precision and contiguity settings, checking that they all lead to the same results.

tests.test_fanbeam.test_weighting()[source]

Mass preservation test. Checks whether the total mass of an image is correctly transported into the total mass of a projection. Due to the fan geometry, the width of a projected object on the detector is wider than the original object was, as the width of the fan grows linearly with the distance it travels. Consequently, also the total mass on the detector is roughly the multiplication of the total mass in the object by the ratio R to RE. This estimate is verified numerically.

tests.test_fanbeam.test_adjointness()[source]

Adjointness test. Creates random images and sinograms to check whether forward and backprojection are indeed adjoint to one another (by comparing the corresponding dual pairings). This comparison is carried out for 100 experiments to affirm adjointness with some certainty.

tests.test_fanbeam.test_nonquadratic()[source]

Non-quadratic image test. Tests and illustrates the projection operator for non-quadratic images.

tests.test_fanbeam.test_limited_angles()[source]

Limited angle test. Tests and illustrates how to set the angles in case of limited angle situation, in particular showing artifacts resulting from the incorrect use for the limited angle setting (leading to undesired angle_weights). This can be achieved through the format of the angles parameter or by setting the angle_weights directly as shown in the test.

tests.test_fanbeam.test_midpoint_shift()[source]

Shifted midpoint test. Tests and illustrates how the sinogram changes if the midpoint of an images is shifted away from the center of rotation.

tests.test_fanbeam.test_geometric_orientation()[source]

Geometric orientation test. Considers projections with parallel and fanbeam geometry for very simple images in different shifted geometries to illustrate how the geometry of the projection work and that they indeed behave analogously for parallel and fanbeam setting. Note that the axes of the images shown by matplotlib.pyplot.imshow() are always rotated by 90 degrees compared to the standard (x, y)-axes.

tests.test_fanbeam.test_range_check_walnut()[source]

The walnut data set from [HHKKNS2015] is considered for testing the implementation. This test observes that with suitable parameters, the data is well-explained by the model defined by gratopy’s operators. In particular, one can observe that there is a slight imperfection in the data set as the detector is not perfectly centered. Indeed, the total mass of the upper detector-half theoretically needs to coincide with the lower detector-half’s total mass (up to numerical precision), but these values differ significantly. Moreover, this test serves to verify the validity of the conjugate gradients (CG) method. It is well-known that the CG algorithm approximates the minimal-norm least squares solution to the data, and in particular, the forward projection of this solution corresponds to the projection of data onto the range of the operator. As depicted in the plots of the residual data shown by this test, the walnut projection data admit, after detector_shift correction, only slight intensity variations as systematic error.

[HHKKNS2015] (1,2,3,4)

Keijo Hämäläinen and Lauri Harhanen and Aki Kallonen and Antti Kujanpää and Esa Niemi and Samuli Siltanen. “Tomographic X-ray data of a walnut”. https://arxiv.org/abs/1502.04064

tests.test_fanbeam.test_landweber()[source]

Landweber reconstruction test. Performs the Landweber iteration to compute a reconstruction from a sinogram contained in the walnut data set of [HHKKNS2015], testing the implementation.

tests.test_fanbeam.test_conjugate_gradients()[source]

Conjugate gradients reconstruction test. Performs the conjugate gradients iteration to compute a reconstruction from a sinogram contained in the walnut data set of [HHKKNS2015], testing the implementation.

tests.test_fanbeam.test_total_variation()[source]

Total variation reconstruction test. Performs the toolbox’s total-variation-based approach to compute a reconstruction from a sinogram contained in the walnut data set of [HHKKNS2015], testing the implementation.

tests.test_fanbeam.test_create_sparse_matrix()[source]

Tests the create_sparse_matrix method to create a sparse matrix associated with the transform, and tests it by applying forward and backprojection via matrix multiplication.