summaryrefslogtreecommitdiff
path: root/silx/image/marchingsquares/test/test_mergeimpl.py
blob: 1c14f338ec4ea4aa982d1a71f2c200bb34811429 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
# -*- coding: utf-8 -*-
#
#    Project: silx
#             https://github.com/silx-kit/silx
#
#    Copyright (C) 2012-2016  European Synchrotron Radiation Facility, Grenoble, France
#
# Permission is hereby granted, free of charge, to any person obtaining a copy
# of this software and associated documentation files (the "Software"), to deal
# in the Software without restriction, including without limitation the rights
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
# copies of the Software, and to permit persons to whom the Software is
# furnished to do so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in
# all copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
# THE SOFTWARE.

__authors__ = ["V. Valls"]
__license__ = "MIT"
__date__ = "18/04/2018"

import unittest
import numpy
from .._mergeimpl import MarchingSquaresMergeImpl


class TestMergeImplApi(unittest.TestCase):

    def test_image_not_an_array(self):
        bad_image = 1
        self.assertRaises(ValueError, MarchingSquaresMergeImpl, bad_image)

    def test_image_bad_dim(self):
        bad_image = numpy.array([[[1.0]]])
        self.assertRaises(ValueError, MarchingSquaresMergeImpl, bad_image)

    def test_image_not_big_enough(self):
        bad_image = numpy.array([[1.0, 1.0, 1.0, 1.0]])
        self.assertRaises(ValueError, MarchingSquaresMergeImpl, bad_image)

    def test_mask_not_an_array(self):
        image = numpy.array([[1.0, 1.0], [1.0, 1.0]])
        bad_mask = 1
        self.assertRaises(ValueError, MarchingSquaresMergeImpl, image, bad_mask)

    def test_mask_not_match(self):
        image = numpy.array([[1.0, 1.0], [1.0, 1.0]])
        bad_mask = numpy.array([[1.0, 1.0]])
        self.assertRaises(ValueError, MarchingSquaresMergeImpl, image, bad_mask)

    def test_ok_anyway_bad_type(self):
        image = numpy.array([[1.0, 1.0], [1.0, 1.0]], dtype=numpy.int32)
        mask = numpy.array([[1.0, 1.0], [1.0, 1.0]], dtype=numpy.float32)
        MarchingSquaresMergeImpl(image, mask)

    def test_find_contours_result(self):
        image = numpy.zeros((2, 2))
        image[0, 0] = 1
        ms = MarchingSquaresMergeImpl(image)
        polygons = ms.find_contours(0.5)
        self.assertIsInstance(polygons, list)
        self.assertTrue(len(polygons), 1)
        self.assertIsInstance(polygons[0], numpy.ndarray)
        self.assertEqual(polygons[0].shape[1], 2)
        self.assertEqual(polygons[0].dtype.kind, "f")

    def test_find_pixels_result(self):
        image = numpy.zeros((2, 2))
        image[0, 0] = 1
        ms = MarchingSquaresMergeImpl(image)
        pixels = ms.find_pixels(0.5)
        self.assertIsInstance(pixels, numpy.ndarray)
        self.assertEqual(pixels.shape[1], 2)
        self.assertEqual(pixels.dtype.kind, "i")

    def test_find_contours_empty_result(self):
        image = numpy.zeros((2, 2))
        ms = MarchingSquaresMergeImpl(image)
        polygons = ms.find_contours(0.5)
        self.assertIsInstance(polygons, list)
        self.assertEqual(len(polygons), 0)

    def test_find_pixels_empty_result(self):
        image = numpy.zeros((2, 2))
        ms = MarchingSquaresMergeImpl(image)
        pixels = ms.find_pixels(0.5)
        self.assertIsInstance(pixels, numpy.ndarray)
        self.assertEqual(pixels.shape[1], 2)
        self.assertEqual(pixels.shape[0], 0)
        self.assertEqual(pixels.dtype.kind, "i")

    def test_find_contours_yx_result(self):
        image = numpy.zeros((2, 2))
        image[1, 0] = 1
        ms = MarchingSquaresMergeImpl(image)
        polygons = ms.find_contours(0.5)
        polygon = polygons[0]
        self.assertTrue((polygon == (0.5, 0)).any())
        self.assertTrue((polygon == (1, 0.5)).any())

    def test_find_pixels_yx_result(self):
        image = numpy.zeros((2, 2))
        image[1, 0] = 1
        ms = MarchingSquaresMergeImpl(image)
        pixels = ms.find_pixels(0.5)
        self.assertTrue((pixels == (1, 0)).any())


class TestMergeImplContours(unittest.TestCase):

    def test_merge_segments(self):
        image = numpy.zeros((4, 4))
        image[(2, 3), :] = 1
        ms = MarchingSquaresMergeImpl(image)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 1)

    def test_merge_segments_2(self):
        image = numpy.zeros((4, 4))
        image[(2, 3), :] = 1
        image[2, 2] = 0
        ms = MarchingSquaresMergeImpl(image)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 1)

    def test_merge_tiles(self):
        image = numpy.zeros((4, 4))
        image[(2, 3), :] = 1
        ms = MarchingSquaresMergeImpl(image, group_size=2)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 1)

    def test_fully_masked(self):
        image = numpy.zeros((5, 5))
        image[(2, 3), :] = 1
        mask = numpy.ones((5, 5))
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 0)

    def test_fully_masked_minmax(self):
        """This invalidates all the tiles. The route is not the same."""
        image = numpy.zeros((5, 5))
        image[(2, 3), :] = 1
        mask = numpy.ones((5, 5))
        ms = MarchingSquaresMergeImpl(image, mask, group_size=2, use_minmax_cache=True)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 0)

    def test_masked_segments(self):
        image = numpy.zeros((5, 5))
        image[(2, 3, 4), :] = 1
        mask = numpy.zeros((5, 5))
        mask[:, 2] = 1
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 2)

    def test_closed_polygon(self):
        image = numpy.zeros((5, 5))
        image[2, 2] = 1
        image[1, 2] = 1
        image[3, 2] = 1
        image[2, 1] = 1
        image[2, 3] = 1
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.9)
        self.assertEqual(len(polygons), 1)
        self.assertEqual(list(polygons[0][0]), list(polygons[0][-1]))

    def test_closed_polygon_between_tiles(self):
        image = numpy.zeros((5, 5))
        image[2, 2] = 1
        image[1, 2] = 1
        image[3, 2] = 1
        image[2, 1] = 1
        image[2, 3] = 1
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask, group_size=2)
        polygons = ms.find_contours(0.9)
        self.assertEqual(len(polygons), 1)
        self.assertEqual(list(polygons[0][0]), list(polygons[0][-1]))

    def test_open_polygon(self):
        image = numpy.zeros((5, 5))
        image[2, 2] = 1
        image[1, 2] = 1
        image[3, 2] = 1
        image[2, 1] = 1
        image[2, 3] = 1
        mask = numpy.zeros((5, 5))
        mask[1, 1] = 1
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.9)
        self.assertEqual(len(polygons), 1)
        self.assertNotEqual(list(polygons[0][0]), list(polygons[0][-1]))

    def test_ambiguous_pattern(self):
        image = numpy.zeros((6, 8))
        image[(3, 4), :] = 1
        image[:, (0, -1)] = 0
        image[3, 3] = -0.001
        image[4, 4] = 0.0
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 2)

    def test_ambiguous_pattern_2(self):
        image = numpy.zeros((6, 8))
        image[(3, 4), :] = 1
        image[:, (0, -1)] = 0
        image[3, 3] = +0.001
        image[4, 4] = 0.0
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 1)

    def count_closed_polygons(self, polygons):
        closed = 0
        for polygon in polygons:
            if list(polygon[0]) == list(polygon[-1]):
                closed += 1
        return closed

    def test_image(self):
        # example from skimage
        x, y = numpy.ogrid[-numpy.pi:numpy.pi:100j, -numpy.pi:numpy.pi:100j]
        image = numpy.sin(numpy.exp((numpy.sin(x)**3 + numpy.cos(y)**2)))
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 11)
        self.assertEqual(self.count_closed_polygons(polygons), 3)

    def test_image_tiled(self):
        # example from skimage
        x, y = numpy.ogrid[-numpy.pi:numpy.pi:100j, -numpy.pi:numpy.pi:100j]
        image = numpy.sin(numpy.exp((numpy.sin(x)**3 + numpy.cos(y)**2)))
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask, group_size=50)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 11)
        self.assertEqual(self.count_closed_polygons(polygons), 3)

    def test_image_tiled_minmax(self):
        # example from skimage
        x, y = numpy.ogrid[-numpy.pi:numpy.pi:100j, -numpy.pi:numpy.pi:100j]
        image = numpy.sin(numpy.exp((numpy.sin(x)**3 + numpy.cos(y)**2)))
        mask = None
        ms = MarchingSquaresMergeImpl(image, mask, group_size=50, use_minmax_cache=True)
        polygons = ms.find_contours(0.5)
        self.assertEqual(len(polygons), 11)
        self.assertEqual(self.count_closed_polygons(polygons), 3)


def suite():
    test_suite = unittest.TestSuite()
    loadTests = unittest.defaultTestLoader.loadTestsFromTestCase
    test_suite.addTest(loadTests(TestMergeImplApi))
    test_suite.addTest(loadTests(TestMergeImplContours))
    return test_suite