scikit-image

Форк
0
/
test_random_shapes.py 
178 строк · 5.4 Кб
1
import numpy as np
2
import pytest
3

4
from skimage._shared import testing
5
from skimage._shared._warnings import expected_warnings
6
from skimage.draw import random_shapes
7

8

9
def test_generates_color_images_with_correct_shape():
10
    image, _ = random_shapes((128, 128), max_shapes=10)
11
    assert image.shape == (128, 128, 3)
12

13

14
def test_generates_gray_images_with_correct_shape():
15
    image, _ = random_shapes(
16
        (4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
17
    )
18
    assert image.shape == (4567, 123)
19

20

21
def test_generates_gray_images_with_correct_shape_deprecated_multichannel():
22
    image, _ = random_shapes(
23
        (4567, 123), min_shapes=3, max_shapes=20, channel_axis=None
24
    )
25
    assert image.shape == (4567, 123)
26

27

28
@pytest.mark.parametrize('channel_axis', [None, 0, 1, 2])
29
def test_generated_shape_for_channel_axis(channel_axis):
30
    shape = (128, 64)
31
    num_channels = 5
32

33
    image, _ = random_shapes(
34
        shape,
35
        num_channels=num_channels,
36
        min_shapes=3,
37
        max_shapes=10,
38
        channel_axis=channel_axis,
39
    )
40

41
    if channel_axis is None:
42
        expected_shape = shape
43
    else:
44
        expected_shape = tuple(np.insert(shape, channel_axis, num_channels))
45

46
    assert image.shape == expected_shape
47

48

49
def test_generates_correct_bounding_boxes_for_rectangles():
50
    image, labels = random_shapes((128, 128), max_shapes=1, shape='rectangle', rng=42)
51

52
    assert len(labels) == 1
53
    label, bbox = labels[0]
54
    assert label == 'rectangle', label
55

56
    crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
57

58
    # The crop is filled.
59
    assert (crop >= 0).all() and (crop < 255).all()
60

61
    # The crop is complete.
62
    image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
63
    assert (image == 255).all()
64

65

66
def test_generates_correct_bounding_boxes_for_triangles():
67
    image, labels = random_shapes((128, 128), max_shapes=1, shape='triangle', rng=42)
68
    assert len(labels) == 1
69
    label, bbox = labels[0]
70
    assert label == 'triangle', label
71

72
    crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
73

74
    # The crop is filled.
75
    assert (crop >= 0).any() and (crop < 255).any()
76

77
    # The crop is complete.
78
    image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
79
    assert (image == 255).all()
80

81

82
def test_generates_correct_bounding_boxes_for_circles():
83
    image, labels = random_shapes(
84
        (43, 44), max_shapes=1, min_size=20, max_size=20, shape='circle', rng=42
85
    )
86
    assert len(labels) == 1
87
    label, bbox = labels[0]
88
    assert label == 'circle', label
89

90
    crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
91

92
    # The crop is filled.
93
    assert (crop >= 0).any() and (crop < 255).any()
94

95
    # The crop is complete.
96
    image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
97
    assert (image == 255).all()
98

99

100
def test_generates_correct_bounding_boxes_for_ellipses():
101
    image, labels = random_shapes(
102
        (43, 44), max_shapes=1, min_size=20, max_size=20, shape='ellipse', rng=42
103
    )
104
    assert len(labels) == 1
105
    label, bbox = labels[0]
106
    assert label == 'ellipse', label
107

108
    crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
109

110
    # The crop is filled.
111
    assert (crop >= 0).any() and (crop < 255).any()
112

113
    # The crop is complete.
114
    image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]] = 255
115
    assert (image == 255).all()
116

117

118
def test_generate_circle_throws_when_size_too_small():
119
    with testing.raises(ValueError):
120
        random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='circle')
121

122

123
def test_generate_ellipse_throws_when_size_too_small():
124
    with testing.raises(ValueError):
125
        random_shapes((64, 128), max_shapes=1, min_size=1, max_size=1, shape='ellipse')
126

127

128
def test_generate_triangle_throws_when_size_too_small():
129
    with testing.raises(ValueError):
130
        random_shapes((128, 64), max_shapes=1, min_size=1, max_size=1, shape='triangle')
131

132

133
def test_can_generate_one_by_one_rectangle():
134
    image, labels = random_shapes(
135
        (50, 128), max_shapes=1, min_size=1, max_size=1, shape='rectangle'
136
    )
137
    assert len(labels) == 1
138
    _, bbox = labels[0]
139
    crop = image[bbox[0][0] : bbox[0][1], bbox[1][0] : bbox[1][1]]
140

141
    # rgb
142
    assert np.shape(crop) == (1, 1, 3) and np.any(crop >= 1) and np.any(crop < 255)
143

144

145
def test_throws_when_intensity_range_out_of_range():
146
    with testing.raises(ValueError):
147
        random_shapes(
148
            (1000, 1234), max_shapes=1, channel_axis=None, intensity_range=(0, 256)
149
        )
150
    with testing.raises(ValueError):
151
        random_shapes((2, 2), max_shapes=1, intensity_range=((-1, 255),))
152

153

154
def test_returns_empty_labels_and_white_image_when_cannot_fit_shape():
155
    # The circle will never fit this.
156
    with expected_warnings(['Could not fit']):
157
        image, labels = random_shapes(
158
            (10000, 10000), max_shapes=1, min_size=10000, shape='circle'
159
        )
160
    assert len(labels) == 0
161
    assert (image == 255).all()
162

163

164
def test_random_shapes_is_reproducible_with_seed():
165
    random_seed = 42
166
    labels = []
167
    for _ in range(5):
168
        _, label = random_shapes((128, 128), max_shapes=5, rng=random_seed)
169
        labels.append(label)
170
    assert all(other == labels[0] for other in labels[1:])
171

172

173
def test_generates_white_image_when_intensity_range_255():
174
    image, labels = random_shapes(
175
        (128, 128), max_shapes=3, intensity_range=((255, 255),), rng=42
176
    )
177
    assert len(labels) > 0
178
    assert (image == 255).all()
179

Использование cookies

Мы используем файлы cookie в соответствии с Политикой конфиденциальности и Политикой использования cookies.

Нажимая кнопку «Принимаю», Вы даете АО «СберТех» согласие на обработку Ваших персональных данных в целях совершенствования нашего веб-сайта и Сервиса GitVerse, а также повышения удобства их использования.

Запретить использование cookies Вы можете самостоятельно в настройках Вашего браузера.