Fixed database typo and removed unnecessary class identifier.

This commit is contained in:
Batuhan Berk Başoğlu 2020-10-14 10:10:37 -04:00
parent 00ad49a143
commit 45fb349a7d
5098 changed files with 952558 additions and 85 deletions

View file

@ -0,0 +1,9 @@
from ..._shared.testing import setup_test, teardown_test
def setup():
setup_test()
def teardown():
teardown_test()

File diff suppressed because it is too large Load diff

View file

@ -0,0 +1,174 @@
import numpy as np
from skimage._shared.testing import assert_array_equal, assert_allclose
from skimage.draw import ellipsoid, ellipsoid_stats, rectangle
from skimage._shared import testing
def test_ellipsoid_sign_parameters1():
with testing.raises(ValueError):
ellipsoid(-1, 2, 2)
def test_ellipsoid_sign_parameters2():
with testing.raises(ValueError):
ellipsoid(0, 2, 2)
def test_ellipsoid_sign_parameters3():
with testing.raises(ValueError):
ellipsoid(-3, -2, 2)
def test_ellipsoid_bool():
test = ellipsoid(2, 2, 2)[1:-1, 1:-1, 1:-1]
test_anisotropic = ellipsoid(2, 2, 4, spacing=(1., 1., 2.))
test_anisotropic = test_anisotropic[1:-1, 1:-1, 1:-1]
expected = np.array([[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 0, 0],
[0, 1, 1, 1, 0],
[1, 1, 1, 1, 1],
[0, 1, 1, 1, 0],
[0, 0, 1, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 1, 1, 1, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 1, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]])
assert_array_equal(test, expected.astype(bool))
assert_array_equal(test_anisotropic, expected.astype(bool))
def test_ellipsoid_levelset():
test = ellipsoid(2, 2, 2, levelset=True)[1:-1, 1:-1, 1:-1]
test_anisotropic = ellipsoid(2, 2, 4, spacing=(1., 1., 2.),
levelset=True)
test_anisotropic = test_anisotropic[1:-1, 1:-1, 1:-1]
expected = np.array([[[ 2. , 1.25, 1. , 1.25, 2. ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 1. , 0.25, 0. , 0.25, 1. ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 2. , 1.25, 1. , 1.25, 2. ]],
[[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 0.5 , -0.25, -0.5 , -0.25, 0.5 ],
[ 0.25, -0.5 , -0.75, -0.5 , 0.25],
[ 0.5 , -0.25, -0.5 , -0.25, 0.5 ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25]],
[[ 1. , 0.25, 0. , 0.25, 1. ],
[ 0.25, -0.5 , -0.75, -0.5 , 0.25],
[ 0. , -0.75, -1. , -0.75, 0. ],
[ 0.25, -0.5 , -0.75, -0.5 , 0.25],
[ 1. , 0.25, 0. , 0.25, 1. ]],
[[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 0.5 , -0.25, -0.5 , -0.25, 0.5 ],
[ 0.25, -0.5 , -0.75, -0.5 , 0.25],
[ 0.5 , -0.25, -0.5 , -0.25, 0.5 ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25]],
[[ 2. , 1.25, 1. , 1.25, 2. ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 1. , 0.25, 0. , 0.25, 1. ],
[ 1.25, 0.5 , 0.25, 0.5 , 1.25],
[ 2. , 1.25, 1. , 1.25, 2. ]]])
assert_allclose(test, expected)
assert_allclose(test_anisotropic, expected)
def test_ellipsoid_stats():
# Test comparison values generated by Wolfram Alpha
vol, surf = ellipsoid_stats(6, 10, 16)
assert_allclose(1280 * np.pi, vol, atol=1e-4)
assert_allclose(1383.28, surf, atol=1e-2)
# Test when a <= b <= c does not hold
vol, surf = ellipsoid_stats(16, 6, 10)
assert_allclose(1280 * np.pi, vol, atol=1e-4)
assert_allclose(1383.28, surf, atol=1e-2)
# Larger test to ensure reliability over broad range
vol, surf = ellipsoid_stats(17, 27, 169)
assert_allclose(103428 * np.pi, vol, atol=1e-4)
assert_allclose(37426.3, surf, atol=1e-1)
def test_rect_3d_extent():
expected = np.array([[[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 1],
[0, 0, 1, 1, 1],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]], dtype=np.uint8)
img = np.zeros((4, 5, 5), dtype=np.uint8)
start = (0, 0, 2)
extent = (5, 2, 3)
pp, rr, cc = rectangle(start, extent=extent, shape=img.shape)
img[pp, rr, cc] = 1
assert_array_equal(img, expected)
def test_rect_3d_end():
expected = np.array([[[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]],
[[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 1, 1, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]]], dtype=np.uint8)
img = np.zeros((4, 5, 5), dtype=np.uint8)
start = (1, 0, 2)
end = (3, 2, 3)
pp, rr, cc = rectangle(start, end=end, shape=img.shape)
img[pp, rr, cc] = 1
assert_array_equal(img, expected)

View file

@ -0,0 +1,18 @@
from skimage.draw import line_nd
from skimage._shared.testing import assert_equal
def test_empty_line():
coords = line_nd((1, 1, 1), (1, 1, 1))
assert len(coords) == 3
assert all(len(c) == 0 for c in coords)
def test_zero_line():
coords = line_nd((-1, -1), (2, 2))
assert_equal(coords, [[-1, 0, 1], [-1, 0, 1]])
def test_no_round():
coords = line_nd((0.5, 0), (2.5, 0), integer=False, endpoint=True)
assert_equal(coords, [[0.5, 1.5, 2.5], [0, 0, 0]])

View file

@ -0,0 +1,14 @@
import numpy as np
from skimage import draw
image_shape = (512, 512)
polygon = np.array([[80, 111, 146, 234, 407, 300, 187, 45],
[465, 438, 499, 380, 450, 287, 210, 167]]).T
def test_polygon2mask():
mask = draw.polygon2mask(image_shape, polygon)
assert mask.shape == image_shape
assert mask.sum() == 57647

View file

@ -0,0 +1,171 @@
import numpy as np
from skimage.draw import random_shapes
from skimage._shared import testing
from skimage._shared._warnings import expected_warnings
def test_generates_color_images_with_correct_shape():
image, _ = random_shapes((128, 128), max_shapes=10)
assert image.shape == (128, 128, 3)
def test_generates_gray_images_with_correct_shape():
image, _ = random_shapes(
(4567, 123), min_shapes=3, max_shapes=20, multichannel=False)
assert image.shape == (4567, 123)
def test_generates_correct_bounding_boxes_for_rectangles():
image, labels = random_shapes(
(128, 128),
max_shapes=1,
shape='rectangle',
random_seed=42)
assert len(labels) == 1
label, bbox = labels[0]
assert label == 'rectangle', label
crop = image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]]
# The crop is filled.
assert (crop >= 0).all() and (crop < 255).all()
# The crop is complete.
image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]] = 255
assert (image == 255).all()
def test_generates_correct_bounding_boxes_for_triangles():
image, labels = random_shapes(
(128, 128),
max_shapes=1,
shape='triangle',
random_seed=42)
assert len(labels) == 1
label, bbox = labels[0]
assert label == 'triangle', label
crop = image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]]
# The crop is filled.
assert (crop >= 0).any() and (crop < 255).any()
# The crop is complete.
image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]] = 255
assert (image == 255).all()
def test_generates_correct_bounding_boxes_for_circles():
image, labels = random_shapes(
(43, 44),
max_shapes=1,
min_size=20,
max_size=20,
shape='circle',
random_seed=42)
assert len(labels) == 1
label, bbox = labels[0]
assert label == 'circle', label
crop = image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]]
# The crop is filled.
assert (crop >= 0).any() and (crop < 255).any()
# The crop is complete.
image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]] = 255
assert (image == 255).all()
def test_generates_correct_bounding_boxes_for_ellipses():
image, labels = random_shapes(
(43, 44),
max_shapes=1,
min_size=20,
max_size=20,
shape='ellipse',
random_seed=42)
assert len(labels) == 1
label, bbox = labels[0]
assert label == 'ellipse', label
crop = image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]]
# The crop is filled.
assert (crop >= 0).any() and (crop < 255).any()
# The crop is complete.
image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]] = 255
assert (image == 255).all()
def test_generate_circle_throws_when_size_too_small():
with testing.raises(ValueError):
random_shapes(
(64, 128), max_shapes=1, min_size=1, max_size=1, shape='circle')
def test_generate_ellipse_throws_when_size_too_small():
with testing.raises(ValueError):
random_shapes(
(64, 128), max_shapes=1, min_size=1, max_size=1, shape='ellipse')
def test_generate_triangle_throws_when_size_too_small():
with testing.raises(ValueError):
random_shapes(
(128, 64), max_shapes=1, min_size=1, max_size=1, shape='triangle')
def test_can_generate_one_by_one_rectangle():
image, labels = random_shapes(
(50, 128),
max_shapes=1,
min_size=1,
max_size=1,
shape='rectangle')
assert len(labels) == 1
_, bbox = labels[0]
crop = image[bbox[0][0]:bbox[0][1], bbox[1][0]:bbox[1][1]]
# rgb
assert (np.shape(crop) == (1, 1, 3) and np.any(crop >= 1)
and np.any(crop < 255))
def test_throws_when_intensity_range_out_of_range():
with testing.raises(ValueError):
random_shapes((1000, 1234), max_shapes=1, multichannel=False,
intensity_range=(0, 256))
with testing.raises(ValueError):
random_shapes((2, 2), max_shapes=1,
intensity_range=((-1, 255),))
def test_returns_empty_labels_and_white_image_when_cannot_fit_shape():
# The circle will never fit this.
with expected_warnings(['Could not fit']):
image, labels = random_shapes(
(10000, 10000), max_shapes=1, min_size=10000, shape='circle')
assert len(labels) == 0
assert (image == 255).all()
def test_random_shapes_is_reproducible_with_seed():
random_seed = 42
labels = []
for _ in range(5):
_, label = random_shapes((128, 128), max_shapes=5,
random_seed=random_seed)
labels.append(label)
assert all(other == labels[0] for other in labels[1:])
def test_generates_white_image_when_intensity_range_255():
image, labels = random_shapes((128, 128), max_shapes=3,
intensity_range=((255, 255),),
random_seed=42)
assert len(labels) > 0
assert (image == 255).all()