Merge pull request #1054 from blink1073/add_test_seeds

Add random seeds to tests per #1044

Additionally, change all `np.random.random` to `np.random.rand` calls, as the former are not affected by `np.random.seed`.
This commit is contained in:
Juan Nunez-Iglesias
2014-07-20 07:48:49 -05:00
21 changed files with 78 additions and 53 deletions
+4 -2
View File
@@ -43,6 +43,8 @@ from skimage import data_dir, data
import colorsys
np.random.seed(0)
def test_guess_spatial_dimensions():
im1 = np.zeros((5, 5))
@@ -223,7 +225,7 @@ class TestColorconv(TestCase):
assert_equal(g.shape, (1, 1))
def test_rgb2grey_on_grey(self):
rgb2grey(np.random.random((5, 5)))
rgb2grey(np.random.rand(5, 5))
# test matrices for xyz2lab and lab2xyz generated using http://www.easyrgb.com/index.php?X=CALC
# Note: easyrgb website displays xyz*100
@@ -349,7 +351,7 @@ def test_gray2rgb():
def test_gray2rgb_rgb():
x = np.random.random((5, 5, 4))
x = np.random.rand(5, 5, 4)
y = gray2rgb(x)
assert_equal(x, y)
+3 -2
View File
@@ -5,12 +5,13 @@ from skimage.feature import CENSURE
img = moon()
np.random.seed(0)
def test_censure_on_rectangular_images():
"""Censure feature detector should work on 2D image of any shape."""
rect_image = np.random.random((300, 200))
square_image = np.random.random((200, 200))
rect_image = np.random.rand(300, 200)
square_image = np.random.rand(200, 200)
CENSURE().detect((square_image))
CENSURE().detect((rect_image))
+4 -5
View File
@@ -5,6 +5,9 @@ import scipy.ndimage
from skimage.feature import peak
np.random.seed(21)
def test_trivial_case():
trivial = np.zeros((25, 25))
peak_indices = peak.peak_local_max(trivial, min_distance=1, indices=True)
@@ -17,7 +20,7 @@ def test_noisy_peaks():
peak_locations = [(7, 7), (7, 13), (13, 7), (13, 13)]
# image with noise of amplitude 0.8 and peaks of amplitude 1
image = 0.8 * np.random.random((20, 20))
image = 0.8 * np.random.rand(20, 20)
for r, c in peak_locations:
image[r, c] = 1
@@ -80,7 +83,6 @@ def test_num_peaks():
def test_reorder_labels():
np.random.seed(21)
image = np.random.uniform(size=(40, 60))
i, j = np.mgrid[0:40, 0:60]
labels = 1 + (i >= 20) + (j >= 30) * 2
@@ -100,7 +102,6 @@ def test_reorder_labels():
def test_indices_with_labels():
np.random.seed(21)
image = np.random.uniform(size=(40, 60))
i, j = np.mgrid[0:40, 0:60]
labels = 1 + (i >= 20) + (j >= 30) * 2
@@ -233,7 +234,6 @@ def test_adjacent_different_objects():
def test_four_quadrants():
np.random.seed(21)
image = np.random.uniform(size=(40, 60))
i, j = np.mgrid[0:40, 0:60]
labels = 1 + (i >= 20) + (j >= 30) * 2
@@ -255,7 +255,6 @@ def test_disk():
'''regression test of img-1194, footprint = [1]
Test peak.peak_local_max when every point is a local maximum
'''
np.random.seed(31)
image = np.random.uniform(size=(10, 20))
footprint = np.array([[1]])
result = peak.peak_local_max(image, labels=np.ones((10, 20)),
+1 -1
View File
@@ -199,7 +199,7 @@ class TestLBP():
np.random.seed(13141516)
# Create random image with known variance.
image = np.random.random((500, 500))
image = np.random.rand(500, 500)
target_std = 0.3
image = image / image.std() * target_std
+1 -1
View File
@@ -112,7 +112,7 @@ def canny(image, sigma=1., low_threshold=None, high_threshold=None, mask=None):
>>> # Generate noisy image of a square
>>> im = np.zeros((256, 256))
>>> im[64:-64, 64:-64] = 1
>>> im += 0.2 * np.random.random(im.shape)
>>> im += 0.2 * np.random.rand(*im.shape)
>>> # First trial with the Canny filter, with the default smoothing
>>> edges1 = filter.canny(im)
>>> # Increase the smoothing for better results
+5 -3
View File
@@ -6,6 +6,8 @@ from skimage import data, util
from skimage.morphology import cmorph, disk
from skimage.filter import rank
np.random.seed(0)
def test_random_sizes():
# make sure the size is not a problem
@@ -44,7 +46,7 @@ def test_random_sizes():
def test_compare_with_cmorph_dilate():
# compare the result of maximum filter with dilate
image = (np.random.random((100, 100)) * 256).astype(np.uint8)
image = (np.random.rand(100, 100) * 256).astype(np.uint8)
out = np.empty_like(image)
mask = np.ones(image.shape, dtype=np.uint8)
@@ -58,7 +60,7 @@ def test_compare_with_cmorph_dilate():
def test_compare_with_cmorph_erode():
# compare the result of maximum filter with erode
image = (np.random.random((100, 100)) * 256).astype(np.uint8)
image = (np.random.rand(100, 100) * 256).astype(np.uint8)
out = np.empty_like(image)
mask = np.ones(image.shape, dtype=np.uint8)
@@ -143,7 +145,7 @@ def test_inplace_output():
# rank filters are not supposed to filter inplace
selem = disk(20)
image = (np.random.random((500, 500)) * 256).astype(np.uint8)
image = (np.random.rand(500, 500) * 256).astype(np.uint8)
out = image
assert_raises(NotImplementedError, rank.mean, image, selem, out=out)
+5 -5
View File
@@ -5,6 +5,7 @@ from numpy.testing import (assert_array_equal,
import skimage.graph.mcp as mcp
np.random.seed(0)
a = np.ones((8, 8), dtype=np.float32)
a[1:-1, 1] = 0
a[1, 1:-1] = 0
@@ -133,15 +134,14 @@ def test_crashing():
def _test_random(shape):
# Just tests for crashing -- not for correctness.
np.random.seed(0)
a = np.random.random(shape).astype(np.float32)
a = np.random.rand(*shape).astype(np.float32)
starts = [[0] * len(shape), [-1] * len(shape),
(np.random.random(len(shape)) * shape).astype(int)]
ends = [(np.random.random(len(shape)) * shape).astype(int)
(np.random.rand(len(shape)) * shape).astype(int)]
ends = [(np.random.rand(len(shape)) * shape).astype(int)
for i in range(4)]
m = mcp.MCP(a, fully_connected=True)
costs, offsets = m.find_costs(starts)
for point in [(np.random.random(len(shape)) * shape).astype(int)
for point in [(np.random.rand(len(shape)) * shape).astype(int)
for i in range(4)]:
m.traceback(point)
m._reset()
+1 -1
View File
@@ -193,7 +193,7 @@ def show():
>>> import skimage.io as io
>>> for i in range(4):
... io.imshow(np.random.random((50, 50)))
... io.imshow(np.random.rand(50, 50))
>>> io.show() # doctest: +SKIP
'''
+4 -2
View File
@@ -14,6 +14,8 @@ try:
except RuntimeError:
FI_available = False
np.random.seed(0)
def setup_module(self):
"""The effect of the `plugin.use` call may be overridden by later imports.
@@ -62,7 +64,7 @@ def test_imread_uint16_big_endian():
@skipif(not FI_available)
def test_write_multipage():
shape = (64, 64, 64)
x = np.ones(shape, dtype=np.uint8) * np.random.random(shape) * 255
x = np.ones(shape, dtype=np.uint8) * np.random.rand(*shape) * 255
x = x.astype(np.uint8)
f = NamedTemporaryFile(suffix='.tif')
fname = f.name
@@ -91,7 +93,7 @@ class TestSave:
]:
tests = [(d, f) for d in dtype for f in format]
for d, f in tests:
x = np.ones(shape, dtype=d) * np.random.random(shape)
x = np.ones(shape, dtype=d) * np.random.rand(*shape)
if not np.issubdtype(d, float):
x = (x * 255).astype(d)
yield self.roundtrip, d, x, f
+3 -1
View File
@@ -16,6 +16,8 @@ except ImportError:
else:
imread_available = True
np.random.seed(0)
def teardown():
reset_plugins()
@@ -66,7 +68,7 @@ class TestSave:
def test_imsave_roundtrip(self):
dtype = np.uint8
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
x = np.ones(shape, dtype=dtype) * np.random.random(shape)
x = np.ones(shape, dtype=dtype) * np.random.rand(*shape)
if np.issubdtype(dtype, float):
yield self.roundtrip, x, 255
+3 -1
View File
@@ -21,6 +21,8 @@ except ImportError:
else:
PIL_available = True
np.random.seed(0)
def teardown():
reset_plugins()
@@ -124,7 +126,7 @@ class TestSave:
def test_imsave_roundtrip(self):
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
for dtype in (np.uint8, np.uint16, np.float32, np.float64):
x = np.ones(shape, dtype=dtype) * np.random.random(shape)
x = np.ones(shape, dtype=dtype) * np.random.rand(*shape)
if np.issubdtype(dtype, float):
yield self.roundtrip, dtype, x, 255
+8 -6
View File
@@ -3,13 +3,15 @@ from skimage.io._plugins.util import prepare_for_display, WindowManager
from numpy.testing import *
import numpy as np
np.random.seed(0)
class TestPrepareForDisplay:
def test_basic(self):
prepare_for_display(np.random.random((10, 10)))
prepare_for_display(np.random.rand(10, 10))
def test_dtype(self):
x = prepare_for_display(np.random.random((10, 15)))
x = prepare_for_display(np.random.rand(10, 15))
assert x.dtype == np.dtype(np.uint8)
def test_grey(self):
@@ -19,18 +21,18 @@ class TestPrepareForDisplay:
assert x[3, 2, 0] == 255
def test_colour(self):
prepare_for_display(np.random.random((10, 10, 3)))
prepare_for_display(np.random.rand(10, 10, 3))
def test_alpha(self):
prepare_for_display(np.random.random((10, 10, 4)))
prepare_for_display(np.random.rand(10, 10, 4))
@raises(ValueError)
def test_wrong_dimensionality(self):
prepare_for_display(np.random.random((10, 10, 1, 1)))
prepare_for_display(np.random.rand(10, 10, 1, 1))
@raises(ValueError)
def test_wrong_depth(self):
prepare_for_display(np.random.random((10, 10, 5)))
prepare_for_display(np.random.rand(10, 10, 5))
class TestWindowManager:
+3 -1
View File
@@ -15,6 +15,8 @@ except ImportError:
else:
sitk_available = True
np.random.seed(0)
def teardown():
reset_plugins()
@@ -86,7 +88,7 @@ class TestSave:
def test_imsave_roundtrip(self):
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
for dtype in (np.uint8, np.uint16, np.float32, np.float64):
x = np.ones(shape, dtype=dtype) * np.random.random(shape)
x = np.ones(shape, dtype=dtype) * np.random.rand(*shape)
if np.issubdtype(dtype, float):
yield self.roundtrip, dtype, x
+3 -1
View File
@@ -15,6 +15,8 @@ try:
except ImportError:
TF_available = False
np.random.seed(0)
def teardown():
sio.reset_plugins()
@@ -49,7 +51,7 @@ class TestSave:
def test_imsave_roundtrip(self):
for shape in [(10, 10), (10, 10, 3), (10, 10, 4)]:
for dtype in (np.uint8, np.uint16, np.float32, np.float64):
x = np.ones(shape, dtype=dtype) * np.random.random(shape)
x = np.ones(shape, dtype=dtype) * np.random.rand(*shape)
if not np.issubdtype(dtype, float):
x = (x * 255).astype(dtype)
+2 -1
View File
@@ -586,7 +586,8 @@ def ransac(data, model_class, min_samples, residual_threshold,
Robustly estimate geometric transformation:
>>> from skimage.transform import SimilarityTransform
>>> src = 100 * np.random.random((50, 2))
>>> np.random.seed(0)
>>> src = 100 * np.random.rand(50, 2)
>>> model0 = SimilarityTransform(scale=0.5, rotation=1,
... translation=(10, 20))
>>> dst = model0(src)
@@ -9,8 +9,8 @@ np.random.seed(1234)
def test_ssim_patch_range():
N = 51
X = (np.random.random((N, N)) * 255).astype(np.uint8)
Y = (np.random.random((N, N)) * 255).astype(np.uint8)
X = (np.random.rand(N, N) * 255).astype(np.uint8)
Y = (np.random.rand(N, N) * 255).astype(np.uint8)
assert(ssim(X, Y, win_size=N) < 0.1)
assert_equal(ssim(X, X, win_size=N), 1)
@@ -18,8 +18,8 @@ def test_ssim_patch_range():
def test_ssim_image():
N = 100
X = (np.random.random((N, N)) * 255).astype(np.uint8)
Y = (np.random.random((N, N)) * 255).astype(np.uint8)
X = (np.random.rand(N, N) * 255).astype(np.uint8)
Y = (np.random.rand(N, N) * 255).astype(np.uint8)
S0 = ssim(X, X, win_size=3)
assert_equal(S0, 1)
@@ -31,8 +31,8 @@ def test_ssim_image():
# NOTE: This test is known to randomly fail on some systems (Mac OS X 10.6)
def test_ssim_grad():
N = 30
X = np.random.random((N, N)) * 255
Y = np.random.random((N, N)) * 255
X = np.random.rand(N, N) * 255
Y = np.random.rand(N, N) * 255
f = ssim(X, Y, dynamic_range=255)
g = ssim(X, Y, dynamic_range=255, gradient=True)
@@ -44,8 +44,8 @@ def test_ssim_grad():
def test_ssim_dtype():
N = 30
X = np.random.random((N, N))
Y = np.random.random((N, N))
X = np.random.rand(N, N)
Y = np.random.rand(N, N)
S1 = ssim(X, Y)
+4 -1
View File
@@ -5,6 +5,9 @@ from skimage.morphology import label
from warnings import catch_warnings
from skimage._shared.utils import skimage_deprecation
np.random.seed(0)
class TestConnectedComponents:
def setup(self):
self.x = np.array([[0, 0, 3, 2, 1, 9],
@@ -24,7 +27,7 @@ class TestConnectedComponents:
assert self.x[0, 2] == 3
def test_random(self):
x = (np.random.random((20, 30)) * 5).astype(np.int)
x = (np.random.rand(20, 30) * 5).astype(np.int)
with catch_warnings():
labels = label(x)
+7 -7
View File
@@ -15,7 +15,7 @@ def test_denoise_tv_chambolle_2d():
# lena image
img = lena_gray
# add noise to lena
img += 0.5 * img.std() * np.random.random(img.shape)
img += 0.5 * img.std() * np.random.rand(*img.shape)
# clip noise so that it does not exceed allowed range for float images.
img = np.clip(img, 0, 1)
# denoise
@@ -57,7 +57,7 @@ def test_denoise_tv_chambolle_3d():
mask = (x - 22)**2 + (y - 20)**2 + (z - 17)**2 < 8**2
mask = 100 * mask.astype(np.float)
mask += 60
mask += 20 * np.random.random(mask.shape)
mask += 20 * np.random.rand(*mask.shape)
mask[mask < 0] = 0
mask[mask > 255] = 255
res = restoration.denoise_tv_chambolle(mask.astype(np.uint8), weight=100)
@@ -66,13 +66,13 @@ def test_denoise_tv_chambolle_3d():
# test wrong number of dimensions
assert_raises(ValueError, restoration.denoise_tv_chambolle,
np.random.random((8, 8, 8, 8)))
np.random.rand(8, 8, 8, 8))
def test_denoise_tv_bregman_2d():
img = lena_gray
# add some random noise
img += 0.5 * img.std() * np.random.random(img.shape)
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_tv_bregman(img, weight=10)
@@ -98,7 +98,7 @@ def test_denoise_tv_bregman_float_result_range():
def test_denoise_tv_bregman_3d():
img = lena
# add some random noise
img += 0.5 * img.std() * np.random.random(img.shape)
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_tv_bregman(img, weight=10)
@@ -112,7 +112,7 @@ def test_denoise_tv_bregman_3d():
def test_denoise_bilateral_2d():
img = lena_gray
# add some random noise
img += 0.5 * img.std() * np.random.random(img.shape)
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_bilateral(img, sigma_range=0.1,
@@ -128,7 +128,7 @@ def test_denoise_bilateral_2d():
def test_denoise_bilateral_3d():
img = lena
# add some random noise
img += 0.5 * img.std() * np.random.random(img.shape)
img += 0.5 * img.std() * np.random.rand(*img.shape)
img = np.clip(img, 0, 1)
out1 = restoration.denoise_bilateral(img, sigma_range=0.1,
@@ -316,7 +316,8 @@ def random_walker(data, labels, beta=130, mode='bf', tol=1.e-3, copy=True,
Examples
--------
>>> a = np.zeros((10, 10)) + 0.2 * np.random.random((10, 10))
>>> np.random.seed(0)
>>> a = np.zeros((10, 10)) + 0.2 * np.random.rand(10, 10)
>>> a[5:8, 5:8] += 1
>>> b = np.zeros_like(a)
>>> b[3, 3] = 1 # Marker for first phase
+3 -2
View File
@@ -3,14 +3,15 @@ from numpy.testing import assert_equal
from skimage.transform import integral_image, integrate
x = (np.random.random((50, 50)) * 255).astype(np.uint8)
np.random.seed(0)
x = (np.random.rand(50, 50) * 255).astype(np.uint8)
s = integral_image(x)
def test_validity():
y = np.arange(12).reshape((4, 3))
y = (np.random.random((50, 50)) * 255).astype(np.uint8)
y = (np.random.rand(50, 50) * 255).astype(np.uint8)
assert_equal(integral_image(y)[-1, -1],
y.sum())
+4 -1
View File
@@ -12,6 +12,9 @@ from skimage import transform as tf, data, img_as_float
from skimage.color import rgb2gray
np.random.seed(0)
def test_warp_tform():
x = np.zeros((5, 5), dtype=np.double)
x[2, 2] = 1
@@ -249,7 +252,7 @@ def test_inverse():
def test_slow_warp_nonint_oshape():
image = np.random.random((5, 5))
image = np.random.rand(5, 5)
assert_raises(ValueError, warp, image, lambda xy: xy,
output_shape=(13.1, 19.5))