Merge pull request #351 from ahojnnes/test-coverage

Test coverage
This commit is contained in:
Tony S Yu
2012-10-09 14:51:23 -07:00
14 changed files with 183 additions and 73 deletions
+17 -1
View File
@@ -15,9 +15,25 @@ def test_camera():
def test_checkerboard():
""" Test that checkerboard image can be loaded. """
""" Test that "checkerboard" image can be loaded. """
data.checkerboard()
def test_text():
""" Test that "text" image can be loaded. """
data.text()
def test_moon():
""" Test that "moon" image can be loaded. """
data.moon()
def test_page():
""" Test that "page" image can be loaded. """
data.page()
if __name__ == "__main__":
from numpy.testing import run_module_suite
run_module_suite()
+1 -1
View File
@@ -75,7 +75,7 @@ class LPIFilter2D(object):
>>> filter = LPIFilter2D(filt_func)
"""
if impulse_response is None:
if not callable(impulse_response):
raise ValueError("Impulse response must be a callable.")
self.impulse_response = impulse_response
+5
View File
@@ -61,3 +61,8 @@ class TestCanny(unittest.TestCase):
def test_image_shape(self):
self.assertRaises(TypeError, F.canny, np.zeros((20, 20, 20)), 4, 0, 0)
def test_mask_none(self):
result1 = F.canny(np.zeros((20, 20)), 4, 0, 0, np.ones((20, 20), bool))
result2 = F.canny(np.zeros((20, 20)), 4, 0, 0)
self.assertTrue(np.all(result1 == result2))
+6
View File
@@ -117,5 +117,11 @@ def test_insufficient_size():
median_filter(img, radius=1)
@raises(TypeError)
def test_wrong_shape():
img = np.empty((10, 10, 3))
median_filter(img)
if __name__ == "__main__":
np.testing.run_module_suite()
+6 -1
View File
@@ -8,7 +8,8 @@ from skimage.io import *
from skimage.filter import *
class TestLPIFilter2D():
class TestLPIFilter2D(object):
img = imread(os.path.join(data_dir, 'camera.png'),
flatten=True)[:50, :50]
@@ -55,5 +56,9 @@ class TestLPIFilter2D():
g1 = wiener(F[::-1, ::-1], self.filt_func)
assert ((g - g1[::-1, ::-1]).sum() < 1)
def test_non_callable(self):
assert_raises(ValueError, LPIFilter2D, None)
if __name__ == "__main__":
run_module_suite()
+1 -1
View File
@@ -28,7 +28,7 @@ def approximate_polygon(coords, tolerance):
----------
.. [1] http://en.wikipedia.org/wiki/Ramer-Douglas-Peucker_algorithm
"""
if tolerance == 0:
if tolerance <= 0:
return coords
chain = np.zeros(coords.shape[0], 'bool')
+1 -1
View File
@@ -98,7 +98,7 @@ def find_contours(array, level,
"""
array = np.asarray(array, dtype=np.double)
if array.ndim != 2:
raise RuntimeError('Only 2D arrays are supported.')
raise ValueError('Only 2D arrays are supported.')
level = float(level)
if (fully_connected not in _param_options or
positive_orientation not in _param_options):
+35 -27
View File
@@ -21,34 +21,37 @@ r = np.sqrt(x**2 + y**2)
def test_binary():
contours = find_contours(a, 0.5)
ref = [[6. , 1.5],
[5. , 1.5],
[4. , 1.5],
[3. , 1.5],
[2. , 1.5],
[1.5, 2. ],
[1.5, 3. ],
[1.5, 4. ],
[1.5, 5. ],
[1.5, 6. ],
[1. , 6.5],
[0.5, 6. ],
[0.5, 5. ],
[0.5, 4. ],
[0.5, 3. ],
[0.5, 2. ],
[0.5, 1. ],
[1. , 0.5],
[2. , 0.5],
[3. , 0.5],
[4. , 0.5],
[5. , 0.5],
[6. , 0.5],
[6.5, 1. ],
[6. , 1.5]]
contours = find_contours(a, 0.5, positive_orientation='high')
assert len(contours) == 1
assert_array_equal(contours[0],
[[6. , 1.5],
[5. , 1.5],
[4. , 1.5],
[3. , 1.5],
[2. , 1.5],
[1.5, 2. ],
[1.5, 3. ],
[1.5, 4. ],
[1.5, 5. ],
[1.5, 6. ],
[1. , 6.5],
[0.5, 6. ],
[0.5, 5. ],
[0.5, 4. ],
[0.5, 3. ],
[0.5, 2. ],
[0.5, 1. ],
[1. , 0.5],
[2. , 0.5],
[3. , 0.5],
[4. , 0.5],
[5. , 0.5],
[6. , 0.5],
[6.5, 1. ],
[6. , 1.5]])
assert_array_equal(contours[0][::-1], ref)
def test_float():
@@ -70,6 +73,11 @@ def test_memory_order():
assert len(contours) == 1
def test_invalid_input():
assert_raises(ValueError, find_contours, r, 0.5, 'foo', 'bar')
assert_raises(ValueError, find_contours, r[..., None], 0.5)
if __name__ == '__main__':
from numpy.testing import run_module_suite
run_module_suite()
+6
View File
@@ -22,6 +22,8 @@ def test_approximate_polygon():
out = approximate_polygon(square, -1)
np.testing.assert_array_equal(out, square)
out = approximate_polygon(square, 0)
np.testing.assert_array_equal(out, square)
def test_subdivide_polygon():
@@ -51,6 +53,10 @@ def test_subdivide_polygon():
np.testing.assert_equal(new_square3.shape[0],
2 * (square3.shape[0] - mask_len + 2))
# not supported B-Spline degree
np.testing.assert_raises(ValueError, subdivide_polygon, square, 0)
np.testing.assert_raises(ValueError, subdivide_polygon, square, 8)
if __name__ == "__main__":
np.testing.run_module_suite()
+36 -4
View File
@@ -1,9 +1,9 @@
from numpy.testing import assert_array_equal, assert_almost_equal, \
assert_array_almost_equal
assert_array_almost_equal, assert_raises
import numpy as np
import math
from skimage.measure import regionprops
from skimage.measure._regionprops import regionprops, PROPS, perimeter
SAMPLE = np.array(
@@ -22,6 +22,16 @@ INTENSITY_SAMPLE = SAMPLE.copy()
INTENSITY_SAMPLE[1, 9:11] = 2
def test_unsupported_dtype():
assert_raises(TypeError, regionprops, np.zeros((10, 10), dtype=np.double))
def test_all_props():
props = regionprops(SAMPLE, 'all', INTENSITY_SAMPLE)[0]
for prop in PROPS:
assert prop in props
def test_area():
area = regionprops(SAMPLE, ['Area'])[0]['Area']
assert area == np.sum(SAMPLE)
@@ -36,6 +46,7 @@ def test_bbox():
bbox = regionprops(SAMPLE_mod, ['BoundingBox'])[0]['BoundingBox']
assert_array_almost_equal(bbox, (0, 0, SAMPLE.shape[0], SAMPLE.shape[1]-1))
def test_central_moments():
mu = regionprops(SAMPLE, ['CentralMoments'])[0]['CentralMoments']
#: determined with OpenCV
@@ -48,6 +59,7 @@ def test_central_moments():
assert_almost_equal(mu[2,1], 2000.296296296291)
assert_almost_equal(mu[3,0], -760.0246913580195)
def test_centroid():
centroid = regionprops(SAMPLE, ['Centroid'])[0]['Centroid']
# determined with MATLAB
@@ -90,6 +102,11 @@ def test_eccentricity():
eps = regionprops(SAMPLE, ['Eccentricity'])[0]['Eccentricity']
assert_almost_equal(eps, 0.814629313427)
img = np.zeros((5, 5), dtype=np.int)
img[2, 2] = 1
eps = regionprops(img, ['Eccentricity'])[0]['Eccentricity']
assert_almost_equal(eps, 0)
def test_equiv_diameter():
diameter = regionprops(SAMPLE, ['EquivDiameter'])[0]['EquivDiameter']
@@ -142,6 +159,11 @@ def test_filled_area():
assert area == np.sum(SAMPLE)
def test_filled_image():
img = regionprops(SAMPLE, ['FilledImage'])[0]['FilledImage']
assert_array_equal(img, SAMPLE)
def test_major_axis_length():
length = regionprops(SAMPLE, ['MajorAxisLength'])[0]['MajorAxisLength']
# MATLAB has different interpretation of ellipse than found in literature,
@@ -188,6 +210,7 @@ def test_moments():
assert_almost_equal(m[2,1], 43882.0)
assert_almost_equal(m[3,0], 95588.0)
def test_normalized_moments():
nu = regionprops(SAMPLE, ['NormalizedMoments'])[0]['NormalizedMoments']
#: determined with OpenCV
@@ -198,6 +221,7 @@ def test_normalized_moments():
assert_almost_equal(nu[2,1], 0.045473992910668816)
assert_almost_equal(nu[3,0], -0.017278118992041805)
def test_orientation():
orientation = regionprops(SAMPLE, ['Orientation'])[0]['Orientation']
# determined with MATLAB
@@ -219,15 +243,21 @@ def test_orientation():
)[0]['Orientation']
assert_almost_equal(orientation_diag, -math.pi / 4)
def test_perimeter():
perimeter = regionprops(SAMPLE, ['Perimeter'])[0]['Perimeter']
assert_almost_equal(perimeter, 59.2132034355964)
per = regionprops(SAMPLE, ['Perimeter'])[0]['Perimeter']
assert_almost_equal(per, 59.2132034355964)
per = perimeter(SAMPLE, neighbourhood=8)
assert_almost_equal(per, 43.1213203436)
def test_solidity():
solidity = regionprops(SAMPLE, ['Solidity'])[0]['Solidity']
# determined with MATLAB
assert_almost_equal(solidity, 0.580645161290323)
def test_weighted_central_moments():
wmu = regionprops(SAMPLE, ['WeightedCentralMoments'], INTENSITY_SAMPLE
)[0]['WeightedCentralMoments']
@@ -281,6 +311,7 @@ def test_weighted_moments():
)
assert_array_almost_equal(wm, ref)
def test_weighted_normalized_moments():
wnu = regionprops(SAMPLE, ['WeightedNormalizedMoments'], INTENSITY_SAMPLE
)[0]['WeightedNormalizedMoments']
@@ -292,6 +323,7 @@ def test_weighted_normalized_moments():
)
assert_array_almost_equal(wnu, ref)
if __name__ == "__main__":
from numpy.testing import run_module_suite
run_module_suite()
@@ -1,5 +1,5 @@
import numpy as np
from numpy.testing import assert_equal
from numpy.testing import assert_equal, assert_raises
from skimage.measure import structural_similarity as ssim
@@ -24,20 +24,19 @@ def test_ssim_image():
S1 = ssim(X, Y, win_size=3)
assert(S1 < 0.3)
## Come up with a better way of testing the gradient
##
## def test_ssim_grad():
## N = 30
## X = np.random.random((N, N)) * 255
## Y = np.random.random((N, N)) * 255
## def func(Y):
## return ssim(X, Y, dynamic_range=255)
# 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
## def grad(Y):
## return ssim(X, Y, dynamic_range=255, gradient=True)[1]
f = ssim(X, Y, dynamic_range=255)
g = ssim(X, Y, dynamic_range=255, gradient=True)
## assert(np.all(opt.check_grad(func, grad, Y) < 0.05))
assert f < 0.05
assert g[0] < 0.05
assert np.all(g[1] < 0.05)
def test_ssim_dtype():
@@ -56,5 +55,15 @@ def test_ssim_dtype():
assert S2 < 0.1
def test_invalid_input():
X = np.zeros((3, 3), dtype=np.double)
Y = np.zeros((3, 3), dtype=np.int)
assert_raises(ValueError, ssim, X, Y)
Y = np.zeros((4, 4), dtype=np.double)
assert_raises(ValueError, ssim, X, Y)
assert_raises(ValueError, ssim, X, X, win_size=8)
if __name__ == "__main__":
np.testing.run_module_suite()
-9
View File
@@ -1,10 +1,3 @@
"""
:author: Damian Eads, 2009
:license: modified BSD
"""
__docformat__ = 'restructuredtext en'
import warnings
from skimage import img_as_ubyte
@@ -266,7 +259,6 @@ def white_tophat(image, selem, out=None):
"""
if image is out:
raise NotImplementedError("Cannot perform white top hat in place.")
image = img_as_ubyte(image)
out = opening(image, selem, out=out)
out = image - out
@@ -317,7 +309,6 @@ def black_tophat(image, selem, out=None):
if image is out:
raise NotImplementedError("Cannot perform white top hat in place.")
image = img_as_ubyte(image)
out = closing(image, selem, out=out)
out = out - image
+4 -3
View File
@@ -1015,7 +1015,8 @@ def warp(image, inverse_map=None, map_args={}, output_shape=None, order=1,
if mode == 'constant' and not (0 <= cval <= 1):
clipped[out == cval] = cval
if clipped.shape[0] == 1 or clipped.shape[1] == 1:
if clipped.ndim == 3 and orig_ndim == 2:
# remove singleton dim introduced by atleast_3d
return clipped[..., 0]
else:
return clipped
else: # remove singleton dim introduced by atleast_3d
return clipped.squeeze()
+44 -13
View File
@@ -1,41 +1,72 @@
from numpy.testing import assert_array_equal, run_module_suite
from numpy.testing import assert_array_equal, assert_raises, run_module_suite
from skimage import data
from skimage.transform import (pyramid_reduce, pyramid_expand,
pyramid_gaussian, pyramid_laplacian)
from skimage.transform import pyramids
image = data.lena()
image_gray = image[..., 0]
def test_pyramid_reduce():
def test_pyramid_reduce_rgb():
rows, cols, dim = image.shape
out = pyramid_reduce(image, downscale=2)
out = pyramids.pyramid_reduce(image, downscale=2)
assert_array_equal(out.shape, (rows / 2, cols / 2, dim))
def test_pyramid_expand():
def test_pyramid_reduce_gray():
rows, cols = image_gray.shape
out = pyramids.pyramid_reduce(image_gray, downscale=2)
assert_array_equal(out.shape, (rows / 2, cols / 2))
def test_pyramid_expand_rgb():
rows, cols, dim = image.shape
out = pyramid_expand(image, upscale=2)
out = pyramids.pyramid_expand(image, upscale=2)
assert_array_equal(out.shape, (rows * 2, cols * 2, dim))
def test_build_gaussian_pyramid():
rows, cols, dim = image.shape
pyramid = pyramid_gaussian(image, downscale=2)
def test_pyramid_expand_gray():
rows, cols = image_gray.shape
out = pyramids.pyramid_expand(image_gray, upscale=2)
assert_array_equal(out.shape, (rows * 2, cols * 2))
def test_build_gaussian_pyramid_rgb():
rows, cols, dim = image.shape
pyramid = pyramids.pyramid_gaussian(image, downscale=2)
for layer, out in enumerate(pyramid):
layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim)
assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid():
rows, cols, dim = image.shape
pyramid = pyramid_laplacian(image, downscale=2)
def test_build_gaussian_pyramid_gray():
rows, cols = image_gray.shape
pyramid = pyramids.pyramid_gaussian(image_gray, downscale=2)
for layer, out in enumerate(pyramid):
layer_shape = (rows / 2 ** layer, cols / 2 ** layer)
assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_rgb():
rows, cols, dim = image.shape
pyramid = pyramids.pyramid_laplacian(image, downscale=2)
for layer, out in enumerate(pyramid):
layer_shape = (rows / 2 ** layer, cols / 2 ** layer, dim)
assert_array_equal(out.shape, layer_shape)
def test_build_laplacian_pyramid_gray():
rows, cols = image_gray.shape
pyramid = pyramids.pyramid_laplacian(image_gray, downscale=2)
for layer, out in enumerate(pyramid):
layer_shape = (rows / 2 ** layer, cols / 2 ** layer)
assert_array_equal(out.shape, layer_shape)
def test_check_factor():
assert_raises(ValueError, pyramids._check_factor, 0.99)
assert_raises(ValueError, pyramids._check_factor, - 2)
if __name__ == "__main__":
run_module_suite()