File: test_pixel_list.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 (177 lines) | stat: -rw-r--r-- 5,220 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
175
176
177
from __future__ import annotations

import pickle
import random


def test_pickle():
    from scitbx.array_family import flex

    from dials.model.data import PixelList

    size = (100, 100)
    sf = 10
    image = flex.double(flex.grid(size))
    mask = flex.bool(flex.grid(size))
    for i in range(len(image)):
        image[i] = random.randint(0, 100)
        mask[i] = bool(random.randint(0, 1))
    pl = PixelList(sf, image, mask)
    assert pl.size() == size
    assert pl.frame() == sf

    obj = pickle.dumps(pl)
    pl2 = pickle.loads(obj)

    assert pl2.size() == size
    assert pl2.frame() == sf
    assert len(pl2) == len(pl)
    assert pl2.index().all_eq(pl.index())
    assert pl2.value().all_eq(pl.value())


def test_add_image():
    from scitbx.array_family import flex

    from dials.model.data import PixelList, PixelListLabeller

    size = (2000, 2000)
    sf = 10
    labeller = PixelListLabeller()

    count = 0
    for i in range(3):
        image = flex.random_int_gaussian_distribution(size[0] * size[1], 100, 5)
        mask = flex.random_bool(size[0] * size[1], 0.5)
        image.reshape(flex.grid(size))
        mask.reshape(flex.grid(size))
        pl = PixelList(sf + i, image, mask)
        count += len(mask.as_1d().select(mask.as_1d()))
        labeller.add(pl)
    assert len(labeller.values()) == count


def test_labels_3d():
    from scitbx.array_family import flex

    from dials.model.data import PixelList, PixelListLabeller

    size = (500, 500)
    sf = 0
    labeller = PixelListLabeller()

    count = 0
    mask_list = []
    for i in range(3):
        image = flex.random_int_gaussian_distribution(size[0] * size[1], 100, 5)
        mask = flex.random_bool(size[0] * size[1], 0.5)
        image.reshape(flex.grid(size))
        mask.reshape(flex.grid(size))
        pl = PixelList(sf + i, image, mask)
        count += len(mask.as_1d().select(mask.as_1d()))
        labeller.add(pl)
        mask_list.append(mask)

    coords = labeller.coords()
    labels = labeller.labels_3d()

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

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


def test_labels_2d():
    from scitbx.array_family import flex

    from dials.model.data import PixelList, PixelListLabeller

    size = (500, 500)
    sf = 0
    labeller = PixelListLabeller()

    count = 0
    mask_list = []
    for i in range(3):
        image = flex.random_int_gaussian_distribution(size[0] * size[1], 100, 5)
        mask = flex.random_bool(size[0] * size[1], 0.5)
        image.reshape(flex.grid(size))
        mask.reshape(flex.grid(size))
        pl = PixelList(sf + i, image, mask)
        count += len(mask.as_1d().select(mask.as_1d()))
        labeller.add(pl)
        mask_list.append(mask)

    coords = labeller.coords()
    labels = labeller.labels_2d()

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

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


def test_with_no_points():
    from scitbx.array_family import flex

    from dials.model.data import PixelList, PixelListLabeller

    size = (500, 500)
    sf = 0
    labeller = PixelListLabeller()

    count = 0
    mask_list = []
    for i in range(3):
        image = flex.random_int_gaussian_distribution(size[0] * size[1], 100, 5)
        mask = flex.bool(size[0] * size[0], False)
        image.reshape(flex.grid(size))
        mask.reshape(flex.grid(size))
        pl = PixelList(sf + i, image, mask)
        count += len(mask.as_1d().select(mask.as_1d()))
        labeller.add(pl)
        mask_list.append(mask)

    coords = labeller.coords()
    labels1 = labeller.labels_2d()
    labels2 = labeller.labels_2d()

    assert len(coords) == 0
    assert len(labels1) == 0
    assert len(labels2) == 0