scikit-image
178 строк · 5.4 Кб
1import numpy as np
2import pytest
3
4from skimage._shared import testing
5from skimage._shared._warnings import expected_warnings
6from skimage.draw import random_shapes
7
8
9def test_generates_color_images_with_correct_shape():
10image, _ = random_shapes((128, 128), max_shapes=10)
11assert image.shape == (128, 128, 3)
12
13
14def test_generates_gray_images_with_correct_shape():
15image, _ = random_shapes(
16(4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
17)
18assert image.shape == (4567, 123)
19
20
21def test_generates_gray_images_with_correct_shape_deprecated_multichannel():
22image, _ = random_shapes(
23(4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
24)
25assert image.shape == (4567, 123)
26
27
28@pytest.mark.parametrize('channel_axis', [None, 0, 1, 2])
29def test_generated_shape_for_channel_axis(channel_axis):
30shape = (128, 64)
31num_channels = 5
32
33image, _ = random_shapes(
34shape,
35num_channels=num_channels,
36min_shapes=3,
37max_shapes=10,
38channel_axis=channel_axis,
39)
40
41if channel_axis is None:
42expected_shape = shape
43else:
44expected_shape = tuple(np.insert(shape, channel_axis, num_channels))
45
46assert image.shape == expected_shape
47
48
49def test_generates_correct_bounding_boxes_for_rectangles():
50image, labels = random_shapes((128, 128), max_shapes=1, shape='rectangle', rng=42)
51
52assert len(labels) == 1
53label, bbox = labels[0]
54assert label == 'rectangle', label
55
56crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
57
58# The crop is filled.
59assert (crop >= 0).all() and (crop < 255).all()
60
61# The crop is complete.
62image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
63assert (image == 255).all()
64
65
66def test_generates_correct_bounding_boxes_for_triangles():
67image, labels = random_shapes((128, 128), max_shapes=1, shape='triangle', rng=42)
68assert len(labels) == 1
69label, bbox = labels[0]
70assert label == 'triangle', label
71
72crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
73
74# The crop is filled.
75assert (crop >= 0).any() and (crop < 255).any()
76
77# The crop is complete.
78image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
79assert (image == 255).all()
80
81
82def test_generates_correct_bounding_boxes_for_circles():
83image, labels = random_shapes(
84(43, 44), max_shapes=1, min_size=20, max_size=20, shape='circle', rng=42
85)
86assert len(labels) == 1
87label, bbox = labels[0]
88assert label == 'circle', label
89
90crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
91
92# The crop is filled.
93assert (crop >= 0).any() and (crop < 255).any()
94
95# The crop is complete.
96image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
97assert (image == 255).all()
98
99
100def test_generates_correct_bounding_boxes_for_ellipses():
101image, labels = random_shapes(
102(43, 44), max_shapes=1, min_size=20, max_size=20, shape='ellipse', rng=42
103)
104assert len(labels) == 1
105label, bbox = labels[0]
106assert label == 'ellipse', label
107
108crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
109
110# The crop is filled.
111assert (crop >= 0).any() and (crop < 255).any()
112
113# The crop is complete.
114image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
115assert (image == 255).all()
116
117
118def test_generate_circle_throws_when_size_too_small():
119with testing.raises(ValueError):
120random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='circle')
121
122
123def test_generate_ellipse_throws_when_size_too_small():
124with testing.raises(ValueError):
125random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='ellipse')
126
127
128def test_generate_triangle_throws_when_size_too_small():
129with testing.raises(ValueError):
130random_shapes((128, 64), max_shapes=1, min_size=1, max_size=1, shape='triangle')
131
132
133def test_can_generate_one_by_one_rectangle():
134image, labels = random_shapes(
135(50, 128), max_shapes=1, min_size=1, max_size=1, shape='rectangle'
136)
137assert len(labels) == 1
138_, bbox = labels[0]
139crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
140
141# rgb
142assert np.shape(crop) == (1, 1, 3) and np.any(crop >= 1) and np.any(crop < 255)
143
144
145def test_throws_when_intensity_range_out_of_range():
146with testing.raises(ValueError):
147random_shapes(
148(1000, 1234), max_shapes=1, channel_axis=None, intensity_range=(0, 256)
149)
150with testing.raises(ValueError):
151random_shapes((2, 2), max_shapes=1, intensity_range=((-1, 255),))
152
153
154def test_returns_empty_labels_and_white_image_when_cannot_fit_shape():
155# The circle will never fit this.
156with expected_warnings(['Could not fit']):
157image, labels = random_shapes(
158(10000, 10000), max_shapes=1, min_size=10000, shape='circle'
159)
160assert len(labels) == 0
161assert (image == 255).all()
162
163
164def test_random_shapes_is_reproducible_with_seed():
165random_seed = 42
166labels = []
167for _ in range(5):
168_, label = random_shapes((128, 128), max_shapes=5, rng=random_seed)
169labels.append(label)
170assert all(other == labels[0] for other in labels[1:])
171
172
173def test_generates_white_image_when_intensity_range_255():
174image, labels = random_shapes(
175(128, 128), max_shapes=3, intensity_range=((255, 255),), rng=42
176)
177assert len(labels) > 0
178assert (image == 255).all()
179