- Notifications
You must be signed in to change notification settings - Fork 228
/
Copy pathtest_constraints.py
188 lines (149 loc) · 8.08 KB
/
test_constraints.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
importpytest
importnumpyasnp
fromsklearn.utilsimportshuffle
frommetric_learn.constraintsimportConstraints
fromsklearn.datasetsimportmake_blobs
SEED=42
defgen_labels_for_chunks(n_chunks, chunk_size,
n_classes=10, n_unknown_labels=5):
"""Generates n_chunks*chunk_size labels that split in n_chunks chunks,
that are homogeneous in the label."""
assertmin(n_chunks, chunk_size) >0
classes=shuffle(np.arange(n_classes), random_state=SEED)
n_per_class=chunk_size* (n_chunks//n_classes)
n_maj_class=chunk_size*n_chunks-n_per_class* (n_classes-1)
first_labels=classes[0] *np.ones(n_maj_class, dtype=int)
remaining_labels=np.concatenate([k*np.ones(n_per_class, dtype=int)
forkinclasses[1:]])
unknown_labels=-1*np.ones(n_unknown_labels, dtype=int)
labels=np.concatenate([first_labels, remaining_labels, unknown_labels])
returnshuffle(labels, random_state=SEED)
@pytest.mark.parametrize("n_chunks, chunk_size", [(5, 10), (10, 50)])
deftest_exact_num_points_for_chunks(n_chunks, chunk_size):
"""Checks that the chunk generation works well with just enough points."""
labels=gen_labels_for_chunks(n_chunks, chunk_size)
constraints=Constraints(labels)
chunks=constraints.chunks(n_chunks=n_chunks, chunk_size=chunk_size,
random_state=SEED)
chunk_no, size_each_chunk=np.unique(chunks[chunks>=0],
return_counts=True)
np.testing.assert_array_equal(size_each_chunk, chunk_size)
assertchunk_no.shape[0] ==n_chunks
@pytest.mark.parametrize("n_chunks, chunk_size", [(5, 10), (10, 50)])
deftest_chunk_case_one_miss_point(n_chunks, chunk_size):
"""Checks that the chunk generation breaks when one point is missing."""
labels=gen_labels_for_chunks(n_chunks, chunk_size)
assertlen(labels) >=1
constraints=Constraints(labels[1:])
withpytest.raises(ValueError) ase:
constraints.chunks(n_chunks=n_chunks, chunk_size=chunk_size,
random_state=SEED)
expected_message= (('Not enough possible chunks of %d elements in each'
' class to form expected %d chunks - maximum number'
' of chunks is %d'
) % (chunk_size, n_chunks, n_chunks-1))
assertstr(e.value) ==expected_message
@pytest.mark.parametrize("n_chunks, chunk_size", [(5, 10), (10, 50)])
deftest_unknown_labels_not_in_chunks(n_chunks, chunk_size):
"""Checks that unknown labels are not assigned to any chunk."""
labels=gen_labels_for_chunks(n_chunks, chunk_size)
constraints=Constraints(labels)
chunks=constraints.chunks(n_chunks=n_chunks, chunk_size=chunk_size,
random_state=SEED)
assertnp.all(chunks[labels<0] <0)
@pytest.mark.parametrize("k_genuine, k_impostor, T_test",
[(2, 2,
[[0, 1, 3], [0, 1, 4], [0, 2, 3], [0, 2, 4],
[1, 0, 3], [1, 0, 4], [1, 2, 3], [1, 2, 4],
[2, 0, 3], [2, 0, 4], [2, 1, 3], [2, 1, 4],
[3, 4, 1], [3, 4, 2], [3, 5, 1], [3, 5, 2],
[4, 3, 1], [4, 3, 2], [4, 5, 1], [4, 5, 2],
[5, 3, 1], [5, 3, 2], [5, 4, 1], [5, 4, 2]]),
(1, 3,
[[0, 1, 3], [0, 1, 4], [0, 1, 5], [1, 0, 3],
[1, 0, 4], [1, 0, 5], [2, 1, 3], [2, 1, 4],
[2, 1, 5], [3, 4, 0], [3, 4, 1], [3, 4, 2],
[4, 3, 0], [4, 3, 1], [4, 3, 2], [5, 4, 0],
[5, 4, 1], [5, 4, 2]]),
(1, 2,
[[0, 1, 3], [0, 1, 4], [1, 0, 3], [1, 0, 4],
[2, 1, 3], [2, 1, 4], [3, 4, 1], [3, 4, 2],
[4, 3, 1], [4, 3, 2], [5, 4, 1], [5, 4, 2]])])
deftest_generate_knntriplets_under_edge(k_genuine, k_impostor, T_test):
"""Checks under the edge cases of knn triplet construction with enough
neighbors"""
X=np.array([[0, 0], [2, 2], [4, 4], [8, 8], [16, 16], [32, 32], [33, 33]])
y=np.array([1, 1, 1, 2, 2, 2, -1])
T=Constraints(y).generate_knntriplets(X, k_genuine, k_impostor)
assertnp.array_equal(sorted(T.tolist()), T_test)
@pytest.mark.parametrize("k_genuine, k_impostor,",
[(3, 3), (2, 4), (3, 4), (10, 9), (144, 33)])
deftest_generate_knntriplets(k_genuine, k_impostor):
"""Checks edge and over the edge cases of knn triplet construction with not
enough neighbors"""
T_test= [[0, 1, 3], [0, 1, 4], [0, 1, 5], [0, 2, 3], [0, 2, 4], [0, 2, 5],
[1, 0, 3], [1, 0, 4], [1, 0, 5], [1, 2, 3], [1, 2, 4], [1, 2, 5],
[2, 0, 3], [2, 0, 4], [2, 0, 5], [2, 1, 3], [2, 1, 4], [2, 1, 5],
[3, 4, 0], [3, 4, 1], [3, 4, 2], [3, 5, 0], [3, 5, 1], [3, 5, 2],
[4, 3, 0], [4, 3, 1], [4, 3, 2], [4, 5, 0], [4, 5, 1], [4, 5, 2],
[5, 3, 0], [5, 3, 1], [5, 3, 2], [5, 4, 0], [5, 4, 1], [5, 4, 2]]
X=np.array([[0, 0], [2, 2], [4, 4], [8, 8], [16, 16], [32, 32], [33, 33]])
y=np.array([1, 1, 1, 2, 2, 2, -1])
msg1= ("The class 1 has 3 elements, which is not sufficient to "
f"generate {k_genuine+1} genuine neighbors "
"as specified by k_genuine")
msg2= ("The class 2 has 3 elements, which is not sufficient to "
f"generate {k_genuine+1} genuine neighbors "
"as specified by k_genuine")
msg3= ("The class 1 has 3 elements of other classes, which is "
f"not sufficient to generate {k_impostor} impostor "
"neighbors as specified by k_impostor")
msg4= ("The class 2 has 3 elements of other classes, which is "
f"not sufficient to generate {k_impostor} impostor "
"neighbors as specified by k_impostor")
msgs= [msg1, msg2, msg3, msg4]
withpytest.warns(UserWarning) asuser_warning:
T=Constraints(y).generate_knntriplets(X, k_genuine, k_impostor)
assertany([[msginstr(warn.message) formsginmsgs]
forwarninuser_warning])
assertnp.array_equal(sorted(T.tolist()), T_test)
deftest_generate_knntriplets_k_genuine():
"""Checks the correct error raised when k_genuine is too big """
X, y=shuffle(*make_blobs(random_state=SEED),
random_state=SEED)
label, labels_count=np.unique(y, return_counts=True)
labels_count_min=np.min(labels_count)
idx_smallest_label, =np.where(labels_count==labels_count_min)
k_genuine=labels_count_min
warn_msgs= []
foridxinidx_smallest_label:
warn_msgs.append("The class {} has {} elements, which is not sufficient "
"to generate {} genuine neighbors as specified by "
"k_genuine. Will generate {} genuine neighbors instead."
"\n"
.format(label[idx], k_genuine, k_genuine+1, k_genuine-1))
withpytest.warns(UserWarning) asraised_warning:
Constraints(y).generate_knntriplets(X, k_genuine, 1)
forwarninraised_warning:
assertstr(warn.message) inwarn_msgs
deftest_generate_knntriplets_k_impostor():
"""Checks the correct error raised when k_impostor is too big """
X, y=shuffle(*make_blobs(random_state=SEED),
random_state=SEED)
length=len(y)
label, labels_count=np.unique(y, return_counts=True)
labels_count_max=np.max(labels_count)
idx_biggest_label, =np.where(labels_count==labels_count_max)
k_impostor=length-labels_count_max+1
warn_msgs= []
foridxinidx_biggest_label:
warn_msgs.append("The class {} has {} elements of other classes, which is"
" not sufficient to generate {} impostor neighbors as "
"specified by k_impostor. Will generate {} impostor "
"neighbors instead.\n"
.format(label[idx], k_impostor-1, k_impostor,
k_impostor-1))
withpytest.warns(UserWarning) asraised_warning:
Constraints(y).generate_knntriplets(X, 1, k_impostor)
forwarninraised_warning:
assertstr(warn.message) inwarn_msgs