scikit-image
181 строка · 5.2 Кб
1import numpy as np2from skimage.measure import find_contours3
4from skimage._shared.testing import assert_array_equal5import pytest6
7
8a = np.ones((8, 8), dtype=np.float32)9a[1:-1, 1] = 010a[1, 1:-1] = 011
12x, y = np.mgrid[-1:1:5j, -1:1:5j]13r = np.sqrt(x**2 + y**2)14
15
16def test_binary():17ref = [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
45contours = find_contours(a, 0.5, positive_orientation='high')46assert len(contours) == 147assert_array_equal(contours[0][::-1], ref)48
49
50# target contour for mask tests
51mask_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
77mask = np.ones((8, 8), dtype=bool)78# Some missing data that should result in a hole in the contour:
79mask[7, 0:3] = False80
81
82@pytest.mark.parametrize("level", [0.5, None])83def test_nodata(level):84# Test missing data via NaNs in input array85b = np.copy(a)86b[~mask] = np.nan87contours = find_contours(b, level, positive_orientation='high')88assert len(contours) == 189assert_array_equal(contours[0], mask_contour)90
91
92@pytest.mark.parametrize("level", [0.5, None])93def test_mask(level):94# Test missing data via explicit masking95contours = find_contours(a, level, positive_orientation='high', mask=mask)96assert len(contours) == 197assert_array_equal(contours[0], mask_contour)98
99
100@pytest.mark.parametrize("level", [0, None])101def test_mask_shape(level):102bad_mask = np.ones((8, 7), dtype=bool)103with pytest.raises(ValueError, match='shape'):104find_contours(a, level, mask=bad_mask)105
106
107@pytest.mark.parametrize("level", [0, None])108def test_mask_dtype(level):109bad_mask = np.ones((8, 8), dtype=np.uint8)110with pytest.raises(TypeError, match='binary'):111find_contours(a, level, mask=bad_mask)112
113
114def test_float():115contours = find_contours(r, 0.5)116assert len(contours) == 1117assert_array_equal(118contours[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])123def test_memory_order(level):124contours = find_contours(np.ascontiguousarray(r), level)125assert len(contours) == 1126
127contours = find_contours(np.asfortranarray(r), level)128assert len(contours) == 1129
130
131def test_invalid_input():132with pytest.raises(ValueError):133find_contours(r, 0.5, 'foo', 'bar')134with pytest.raises(ValueError):135find_contours(r[..., None], 0.5)136
137
138def test_level_default():139# image with range [0.9, 0.91]140image = np.random.random((100, 100)) * 0.01 + 0.9141contours = find_contours(image) # use default level142# many contours should be found143assert len(contours) > 1144
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)
179def test_keyerror_fix(image):180"""Failing samples from issue #4830"""181find_contours(np.array(image, np.float32), 0)182