File: test_connected_components.py

package info (click to toggle)
dials 3.25.0%2Bdfsg3-3
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 20,112 kB
  • sloc: python: 134,740; cpp: 34,526; makefile: 160; sh: 142
file content (174 lines) | stat: -rw-r--r-- 6,310 bytes parent folder | download
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
from __future__ import annotations


class Test2d:
    def setup_class(self):
        from dials.algorithms.image.connected_components import LabelImageStack2d

        self.size = (500, 500)
        self.label_images = LabelImageStack2d(self.size)

        from scitbx.array_family import flex

        self.data_list = []
        self.mask_list = []
        for i in range(10):
            data = flex.random_int_gaussian_distribution(
                self.size[0] * self.size[1], 100, 10
            )
            data.reshape(flex.grid(self.size))
            mask = flex.random_bool(self.size[0] * self.size[1], 0.1)
            mask.reshape(flex.grid(self.size))
            self.data_list.append(data)
            self.mask_list.append(mask)

        for i in range(10):
            self.label_images.add_image(self.data_list[i], self.mask_list[i])

        self.labels = self.label_images.labels()
        self.coords = self.label_images.coords()
        self.values = list(self.label_images.values())

        assert len(self.labels) > 0
        assert len(self.labels) == len(self.coords)
        assert len(self.labels) == len(self.values)

    def test_coords_are_valid(self):
        # Ensure that the values are all ok and in the right order
        vi = 0
        for k in range(10):
            ind = 0
            for j in range(self.size[0]):
                for i in range(self.size[1]):
                    m = self.mask_list[k][ind]
                    if m:
                        c1 = (k, j, i)
                        c2 = self.coords[vi]
                        vi += 1
                        assert c1 == c2
                    ind += 1

    def test_values_are_valid(self):
        # Ensure that the values are all ok and in the right order
        vi = 0
        for k in range(10):
            for d, m in zip(self.data_list[k], self.mask_list[k]):
                if m:
                    v1 = d
                    v2 = self.values[vi]
                    vi += 1
                    assert v1 == v2

    def test_labels_are_valid(self):
        from scitbx.array_family import flex

        # Create a map of labels
        label_map = flex.int(flex.grid(10, self.size[0], self.size[1]))
        for c, l in zip(self.coords, self.labels):
            assert c[0] >= 0 and c[0] < 10
            assert c[1] >= 0 and c[1] < self.size[0]
            assert c[2] >= 0 and c[2] < self.size[1]
            label_map[c] = l

        # Ensure all labels are correct
        vi = 0
        for k in range(10):
            for j in range(self.size[0]):
                for i in range(self.size[1]):
                    if self.mask_list[k][j, i]:
                        l1 = self.labels[vi]
                        if k > 0 and self.mask_list[k - 1][j, i]:
                            l2 = label_map[k - 1, j, i]
                            assert l2 != l1
                        if j > 0 and self.mask_list[k][j - 1, i]:
                            l2 = label_map[k, j - 1, i]
                            assert l2 == l1
                        if i > 0 and self.mask_list[k][j, i - 1]:
                            l2 = label_map[k, j, i - 1]
                            assert l2 == l1
                        vi += 1


class Test3d:
    def setup_class(self):
        from dials.algorithms.image.connected_components import LabelImageStack3d

        self.size = (500, 500)
        self.label_images = LabelImageStack3d(self.size)

        from scitbx.array_family import flex

        self.data_list = []
        self.mask_list = []
        for i in range(10):
            data = flex.random_int_gaussian_distribution(
                self.size[0] * self.size[1], 100, 10
            )
            data.reshape(flex.grid(self.size))
            mask = flex.random_bool(self.size[0] * self.size[1], 0.1)
            mask.reshape(flex.grid(self.size))
            self.data_list.append(data)
            self.mask_list.append(mask)

        for i in range(10):
            self.label_images.add_image(self.data_list[i], self.mask_list[i])

        self.labels = self.label_images.labels()
        self.coords = self.label_images.coords()
        self.values = list(self.label_images.values())

        assert len(self.labels) > 0
        assert len(self.labels) == len(self.coords)
        assert len(self.labels) == len(self.values)

    def test_coords_are_valid(self):
        # Ensure that the values are all ok and in the right order
        vi = 0
        for k in range(10):
            ind = 0
            for j in range(self.size[0]):
                for i in range(self.size[1]):
                    m = self.mask_list[k][ind]
                    if m:
                        c1 = (k, j, i)
                        c2 = self.coords[vi]
                        vi += 1
                        assert c1 == c2
                    ind += 1

    def test_values_are_valid(self):
        # Ensure that the values are all ok and in the right order
        vi = 0
        for k in range(10):
            for d, m in zip(self.data_list[k], self.mask_list[k]):
                if m:
                    v1 = d
                    v2 = self.values[vi]
                    vi += 1
                    assert v1 == v2

    def test_labels_are_valid(self):
        from scitbx.array_family import flex

        # Create a map of labels
        label_map = flex.int(flex.grid(10, self.size[0], self.size[1]))
        for c, l in zip(self.coords, self.labels):
            label_map[c] = l

        # Ensure all labels are correct
        vi = 0
        for k in range(10):
            for j in range(self.size[0]):
                for i in range(self.size[1]):
                    if self.mask_list[k][j, i]:
                        l1 = self.labels[vi]
                        if k > 0 and self.mask_list[k - 1][j, i]:
                            l2 = label_map[k - 1, j, i]
                            assert l2 == l1
                        if j > 0 and self.mask_list[k][j - 1, i]:
                            l2 = label_map[k, j - 1, i]
                            assert l2 == l1
                        if i > 0 and self.mask_list[k][j, i - 1]:
                            l2 = label_map[k, j, i - 1]
                            assert l2 == l1
                        vi += 1