From 1860fc4a3f67794238cb967235f7771afd6185c3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20B=C3=B8=20Fl=C3=B8ystad?= Date: Sun, 2 Jun 2013 20:06:39 +0200 Subject: [PATCH 1/2] Reduce code duplication in test_radon_transform Although the line count is increased by these changes, future tests can be written in fewer lines. --- .../transform/tests/test_radon_transform.py | 23 +++++++++++-------- 1 file changed, 14 insertions(+), 9 deletions(-) diff --git a/skimage/transform/tests/test_radon_transform.py b/skimage/transform/tests/test_radon_transform.py index 9b9dc2b0..064b6b73 100644 --- a/skimage/transform/tests/test_radon_transform.py +++ b/skimage/transform/tests/test_radon_transform.py @@ -106,6 +106,17 @@ def test_reconstruct_with_wrong_angles(): assert_raises(ValueError, iradon, p, theta=[0, 1, 2, 3]) +def _random_circle(shape): + # Synthetic random data, zero outside reconstruction circle + np.random.seed(98312871) + image = np.random.rand(*shape) + c0, c1 = np.ogrid[0:shape[0], 0:shape[1]] + r = np.sqrt((c0 - shape[0] // 2)**2 + (c1 - shape[1] // 2)**2) + radius = min(shape) // 2 + image[r >= radius] = 0. + return image + + def test_radon_circle(): a = np.ones((10, 10)) assert_raises(ValueError, radon, a, circle=True) @@ -122,9 +133,7 @@ def test_radon_circle(): assert np.all(sinogram.std(axis=1) < 1e-2) # Synthetic data, random - np.random.seed(98312871) - image = np.random.rand(*shape) - image[r >= radius] = 0. + image = _random_circle(shape) sinogram = radon(image, theta=angles, circle=True) mass = sinogram.sum(axis=0) average_mass = mass.mean() @@ -135,8 +144,8 @@ def test_radon_circle(): def test_radon_iradon_circle(): shape = (61, 79) - # Synthetic random data, zero outside reconstruction circle - image = np.random.rand(*shape) + radius = min(shape) // 2 + image = _random_circle(shape) interpolations = ('nearest', 'linear') output_sizes = (None, min(shape), max(shape), 97) @@ -144,10 +153,6 @@ def test_radon_iradon_circle(): output_sizes): print('interpolation =', interpolation) print('output_size =', output_size) - c0, c1 = np.ogrid[0:shape[0], 0:shape[1]] - r = np.sqrt((c0 - shape[0] // 2)**2 + (c1 - shape[1] // 2)**2) - radius = min(shape) // 2 - image[r >= radius] = 0. # Forward and inverse radon on synthetic data sinogram_rectangle = radon(image, circle=False) reconstruction_rectangle = iradon(sinogram_rectangle, From f533f5351bd08dd40f1198501318610efdf5a4f0 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Jostein=20B=C3=B8=20Fl=C3=B8ystad?= Date: Sun, 2 Jun 2013 20:08:34 +0200 Subject: [PATCH 2/2] PEP8 fixes for test_radon_transform --- skimage/transform/tests/test_radon_transform.py | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/skimage/transform/tests/test_radon_transform.py b/skimage/transform/tests/test_radon_transform.py index 064b6b73..c0fc883d 100644 --- a/skimage/transform/tests/test_radon_transform.py +++ b/skimage/transform/tests/test_radon_transform.py @@ -35,12 +35,14 @@ def test_radon_iradon(): assert delta < 0.05 - reconstructed = iradon(radon(image), filter="ramp", interpolation="nearest") + reconstructed = iradon(radon(image), filter="ramp", + interpolation="nearest") delta = np.mean(abs(image - reconstructed)) assert delta < 0.05 size = 20 image = np.tri(size) + np.tri(size)[::-1] - reconstructed = iradon(radon(image), filter="ramp", interpolation="nearest") + reconstructed = iradon(radon(image), filter="ramp", + interpolation="nearest") def test_iradon_angles(): @@ -53,14 +55,14 @@ def test_iradon_angles(): # Large number of projections: a good quality is expected nb_angles = 200 radon_image_200 = radon(image, theta=np.linspace(0, 180, nb_angles, - endpoint=False)) + endpoint=False)) reconstructed = iradon(radon_image_200) delta_200 = np.mean(abs(rescale(image) - rescale(reconstructed))) assert delta_200 < 0.03 # Lower number of projections nb_angles = 80 radon_image_80 = radon(image, theta=np.linspace(0, 180, nb_angles, - endpoint=False)) + endpoint=False)) # Test whether the sum of all projections is approximately the same s = radon_image_80.sum(axis=0) assert np.allclose(s, s[0], rtol=0.01)