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] 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,