scikit-image

Форк
0
/
test_find_contours.py 
181 строка · 5.2 Кб
1
import numpy as np
2
from skimage.measure import find_contours
3

4
from skimage._shared.testing import assert_array_equal
5
import pytest
6

7

8
a = np.ones((8, 8), dtype=np.float32)
9
a[1:-1, 1] = 0
10
a[1, 1:-1] = 0
11

12
x, y = np.mgrid[-1:1:5j, -1:1:5j]
13
r = np.sqrt(x**2 + y**2)
14

15

16
def test_binary():
17
    ref = [
18
        [6.0, 1.5],
19
        [5.0, 1.5],
20
        [4.0, 1.5],
21
        [3.0, 1.5],
22
        [2.0, 1.5],
23
        [1.5, 2.0],
24
        [1.5, 3.0],
25
        [1.5, 4.0],
26
        [1.5, 5.0],
27
        [1.5, 6.0],
28
        [1.0, 6.5],
29
        [0.5, 6.0],
30
        [0.5, 5.0],
31
        [0.5, 4.0],
32
        [0.5, 3.0],
33
        [0.5, 2.0],
34
        [0.5, 1.0],
35
        [1.0, 0.5],
36
        [2.0, 0.5],
37
        [3.0, 0.5],
38
        [4.0, 0.5],
39
        [5.0, 0.5],
40
        [6.0, 0.5],
41
        [6.5, 1.0],
42
        [6.0, 1.5],
43
    ]
44

45
    contours = find_contours(a, 0.5, positive_orientation='high')
46
    assert len(contours) == 1
47
    assert_array_equal(contours[0][::-1], ref)
48

49

50
# target contour for mask tests
51
mask_contour = [
52
    [6.0, 0.5],
53
    [5.0, 0.5],
54
    [4.0, 0.5],
55
    [3.0, 0.5],
56
    [2.0, 0.5],
57
    [1.0, 0.5],
58
    [0.5, 1.0],
59
    [0.5, 2.0],
60
    [0.5, 3.0],
61
    [0.5, 4.0],
62
    [0.5, 5.0],
63
    [0.5, 6.0],
64
    [1.0, 6.5],
65
    [1.5, 6.0],
66
    [1.5, 5.0],
67
    [1.5, 4.0],
68
    [1.5, 3.0],
69
    [1.5, 2.0],
70
    [2.0, 1.5],
71
    [3.0, 1.5],
72
    [4.0, 1.5],
73
    [5.0, 1.5],
74
    [6.0, 1.5],
75
]
76

77
mask = np.ones((8, 8), dtype=bool)
78
# Some missing data that should result in a hole in the contour:
79
mask[7, 0:3] = False
80

81

82
@pytest.mark.parametrize("level", [0.5, None])
83
def test_nodata(level):
84
    # Test missing data via NaNs in input array
85
    b = np.copy(a)
86
    b[~mask] = np.nan
87
    contours = find_contours(b, level, positive_orientation='high')
88
    assert len(contours) == 1
89
    assert_array_equal(contours[0], mask_contour)
90

91

92
@pytest.mark.parametrize("level", [0.5, None])
93
def test_mask(level):
94
    # Test missing data via explicit masking
95
    contours = find_contours(a, level, positive_orientation='high', mask=mask)
96
    assert len(contours) == 1
97
    assert_array_equal(contours[0], mask_contour)
98

99

100
@pytest.mark.parametrize("level", [0, None])
101
def test_mask_shape(level):
102
    bad_mask = np.ones((8, 7), dtype=bool)
103
    with pytest.raises(ValueError, match='shape'):
104
        find_contours(a, level, mask=bad_mask)
105

106

107
@pytest.mark.parametrize("level", [0, None])
108
def test_mask_dtype(level):
109
    bad_mask = np.ones((8, 8), dtype=np.uint8)
110
    with pytest.raises(TypeError, match='binary'):
111
        find_contours(a, level, mask=bad_mask)
112

113

114
def test_float():
115
    contours = find_contours(r, 0.5)
116
    assert len(contours) == 1
117
    assert_array_equal(
118
        contours[0], [[2.0, 3.0], [1.0, 2.0], [2.0, 1.0], [3.0, 2.0], [2.0, 3.0]]
119
    )
120

121

122
@pytest.mark.parametrize("level", [0.5, None])
123
def test_memory_order(level):
124
    contours = find_contours(np.ascontiguousarray(r), level)
125
    assert len(contours) == 1
126

127
    contours = find_contours(np.asfortranarray(r), level)
128
    assert len(contours) == 1
129

130

131
def test_invalid_input():
132
    with pytest.raises(ValueError):
133
        find_contours(r, 0.5, 'foo', 'bar')
134
    with pytest.raises(ValueError):
135
        find_contours(r[..., None], 0.5)
136

137

138
def test_level_default():
139
    # image with range [0.9, 0.91]
140
    image = np.random.random((100, 100)) * 0.01 + 0.9
141
    contours = find_contours(image)  # use default level
142
    # many contours should be found
143
    assert len(contours) > 1
144

145

146
@pytest.mark.parametrize(
147
    "image",
148
    [
149
        [
150
            [0.13680, 0.11220, 0.0, 0.0, 0.0, 0.19417, 0.19417, 0.33701],
151
            [0.0, 0.15140, 0.10267, 0.0, np.nan, 0.14908, 0.18158, 0.19178],
152
            [0.0, 0.06949, 0.0, 0.0, 0.0, 0.0, 0.0, 0.01860],
153
            [0.0, 0.06949, 0.0, 0.17852, 0.08469, 0.02135, 0.08198, np.nan],
154
            [0.0, 0.08244, np.nan, np.nan, np.nan, np.nan, np.nan, np.nan],
155
            [0.12342, 0.21330, 0.0, np.nan, 0.01301, 0.04335, 0.0, 0.0],
156
        ],
157
        [
158
            [0.08, -0.03, -0.17, -0.08, 0.24, 0.06, 0.17, -0.02],
159
            [0.12, 0.0, np.nan, 0.24, 0.0, -0.53, 0.26, 0.16],
160
            [0.39, 0.0, 0.0, 0.0, 0.0, -0.02, -0.3, 0.01],
161
            [0.28, -0.04, -0.03, 0.16, 0.12, 0.01, -0.87, 0.16],
162
            [0.26, 0.08, 0.08, 0.08, 0.12, 0.13, 0.11, 0.19],
163
            [0.27, 0.24, 0.0, 0.25, 0.32, 0.19, 0.26, 0.22],
164
        ],
165
        [
166
            [-0.18, np.nan, np.nan, 0.22, -0.14, -0.23, -0.2, -0.17, -0.19, -0.24],
167
            [0.0, np.nan, np.nan, np.nan, -0.1, -0.24, -0.15, -0.02, -0.09, -0.21],
168
            [0.43, 0.19, np.nan, np.nan, -0.01, -0.2, -0.22, -0.18, -0.16, -0.07],
169
            [0.23, 0.0, np.nan, -0.06, -0.07, -0.21, -0.24, -0.25, -0.23, -0.13],
170
            [-0.05, -0.11, 0.0, 0.1, -0.19, -0.23, -0.23, -0.18, -0.19, -0.16],
171
            [-0.19, -0.05, 0.13, -0.08, -0.22, -0.23, -0.26, -0.15, -0.12, -0.13],
172
            [-0.2, -0.11, -0.11, -0.24, -0.29, -0.27, -0.35, -0.36, -0.27, -0.13],
173
            [-0.28, -0.33, -0.31, -0.36, -0.39, -0.37, -0.38, -0.32, -0.34, -0.2],
174
            [-0.28, -0.33, -0.39, -0.4, -0.42, -0.38, -0.35, -0.39, -0.35, -0.34],
175
            [-0.38, -0.35, -0.41, -0.42, -0.39, -0.36, -0.34, -0.36, -0.28, -0.34],
176
        ],
177
    ],
178
)
179
def test_keyerror_fix(image):
180
    """Failing samples from issue #4830"""
181
    find_contours(np.array(image, np.float32), 0)
182

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

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

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

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