-
Notifications
You must be signed in to change notification settings - Fork 33
/
selectors_utils_test.py
253 lines (240 loc) · 7.84 KB
/
selectors_utils_test.py
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
# Copyright 2024 DeepMind Technologies Limited
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
# ==============================================================================
"""Tests for selectors_utils."""
from absl.testing import absltest
from absl.testing import parameterized
import jax.numpy as jnp
import numpy as np
import selectors_utils
class SelectorsUtilsTest(parameterized.TestCase):
@parameterized.parameters([
dict(
selectors=[[
[[3], [1], [2]],
[[0], [4, 1, 2]],
]],
expected_error=selectors_utils.SelectorsStatus.NO_ERROR,
),
dict(
selectors=[[
[[3.], [1], [2]], # Invalid: float label.
[[0], [4, 1, 2]],
]],
expected_error=selectors_utils.SelectorsStatus.INVALID_TYPE,
),
dict(
selectors=[[
[[3], [1], [2]],
[[0], [4, 1, 2]],
[], # Invalid: empty reader.
]],
expected_error=selectors_utils.SelectorsStatus.EMPTY_READER_OR_GROUP,
),
dict(
selectors=[[
[[3], [1], [2]],
[[0], [4, 1, 2]],
[[]], # Invalid: empty block/group.
]],
expected_error=selectors_utils.SelectorsStatus.EMPTY_READER_OR_GROUP,
),
dict(
selectors=[[
[[3], [1], [2]],
[[0], [4, 1, 2]],
[[0], [4, 1], [1, 3]], # Invalid: duplicate label.
]],
expected_error=selectors_utils.SelectorsStatus.DUPLICATES_IN_READER,
),
dict(
selectors=[[[269]], [[80], [135, 269]], [[80], [1, 269]], [[269]],
[[135, 269], [54, 151], [87]], [[269]], [[269]], [[80]],
[[135]]],
expected_error=selectors_utils.SelectorsStatus.NO_ERROR,
),
# Example with multiple instances: one valid, one with duplicate labels
# as this is checked last.
dict(
selectors=[
[[[3], [1], [2]]],
[[[0], [4, 1], [1, 3]]],
],
expected_error=selectors_utils.SelectorsStatus.DUPLICATES_IN_READER,
),
])
def test_check_selectors(self, selectors, expected_error):
self.assertEqual(
selectors_utils.check_selectors(selectors, 288), expected_error)
@parameterized.parameters([
dict(
selectors_expected=[[
[[2, 3, 4, 5], [6, 7, 8]],
[[0, 5], [1, 2, 3, 4]],
], [[[1, 2, 3, 4], [5, 6, 7, 8]]]],
max_group_size=4),
dict(
selectors_expected=[[
[[2, 3, 4], [5], [6, 7, 8]],
[[0, 5], [1, 2, 3], [4]],
], [[[1, 2, 3], [4, 5, 6], [7, 8]]]],
max_group_size=3),
dict(
selectors_expected=[[
[[2], [3], [4], [5], [6], [7], [8]],
[[0], [5], [1], [2], [3], [4]],
], [[[1], [2], [3], [4], [5], [6], [7], [8]]]],
max_group_size=1)
])
def test_limit_selectors_group_size(self, selectors_expected, max_group_size):
selectors = [[
[[2, 3, 4, 5], [6, 7, 8]],
[[0, 5], [1, 2, 3, 4]],
], [[[1, 2, 3, 4, 5, 6, 7, 8]]]]
selectors_returned = selectors_utils.limit_selectors_group_size(
selectors, max_group_size)
self._assert_selectors_equal(selectors_expected, selectors_returned)
def test_pad_selectors(self):
max_readers = 4
reader1 = [[3], [1], [2]]
reader2 = [[0], [4, 1, 2]]
reader3 = [[1], [5, 1]]
reader4 = [[2, 1, 5]]
reader5 = [[1, 2, 3, 4, 5]]
pad_reader = []
selectors = [
[reader1, reader2],
[reader1],
[reader1, reader2, reader3],
[reader1, reader2, reader3, reader4],
[reader1, reader2, reader3, reader4, reader5],
]
selectors_expected = [
[reader1, reader2, pad_reader, pad_reader],
[reader1, pad_reader, pad_reader, pad_reader],
[reader1, reader2, reader3, pad_reader],
[reader1, reader2, reader3, reader4],
[reader1, reader2, reader3, reader4],
]
selectors_returned = selectors_utils.pad_selectors(selectors, max_readers)
self._assert_selectors_equal(selectors_expected, selectors_returned)
@parameterized.parameters([
dict(
rankings=[
[
[0, 1, 2, 3],
[3, 0, 2, 1],
],
[
[1, 3, 0, 2],
],
],
groups=[
[
[0, -1, -1, -1],
[0, 1, -1, -1],
],
[
[0, 0, -1, -1],
],
],
),
dict(
rankings=[
[
np.array([0, 1, 2, 3]),
np.array([3, 0, 2, 1]),
],
[
np.array([1, 3, 0, 2]),
],
],
groups=[
[
np.array([0, -1, -1, -1]),
np.array([0, 1, -1, -1]),
],
[
np.array([0, 0, -1, -1]),
],
],
),
])
def test_pad_rankings(self, rankings, groups):
num_examples = len(rankings)
max_readers = 3
num_classes = 4
padded_rankings, padded_groups = selectors_utils.pad_rankings(
rankings, groups, max_readers
)
np.testing.assert_array_equal(
padded_rankings.shape, (num_examples, max_readers, num_classes)
)
np.testing.assert_array_equal(
padded_groups.shape, (num_examples, max_readers, num_classes)
)
empty_ranking = jnp.arange(num_classes)
empty_group = jnp.ones(4) * (-1)
for n in range(num_examples):
for r in range(max_readers):
if r < len(rankings[n]):
np.testing.assert_array_almost_equal(
padded_rankings[n, r], jnp.array(rankings[n][r])
)
np.testing.assert_array_almost_equal(
padded_groups[n, r], jnp.array(groups[n][r])
)
else:
np.testing.assert_array_almost_equal(
padded_rankings[n, r], empty_ranking
)
np.testing.assert_array_almost_equal(padded_groups[n, r], empty_group)
def _assert_selectors_equal(self, s1, s2):
self.assertEqual(len(s1), len(s2))
for i in range(len(s1)):
self.assertEqual(len(s1[i]), len(s2[i]))
for j in range(len(s1[i])):
self.assertEqual(len(s1[i][j]), len(s2[i][j]))
for k in range(len(s1[i][j])):
self.assertEqual(s1[i][j][k], s2[i][j][k])
def test_repeat_selectors(self):
reader1 = [[3], [1], [2]]
reader2 = [[0], [4, 1, 2]]
reader3 = [[1], [5, 1]]
selectors = [
[reader1, reader2],
[reader1],
[reader1, reader2, reader3],
]
selectors_expected = [
[reader1, reader1, reader1, reader2, reader2, reader2],
[reader1, reader1, reader1],
[
reader1,
reader1,
reader1,
reader2,
reader2,
reader2,
reader3,
reader3,
reader3,
],
]
selectors_returned = selectors_utils.repeat_selectors(selectors, 1)
self._assert_selectors_equal(selectors, selectors_returned)
selectors_returned = selectors_utils.repeat_selectors(selectors, 3)
self._assert_selectors_equal(selectors_expected, selectors_returned)
if __name__ == '__main__':
absltest.main()