-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest_activepipe.py
264 lines (231 loc) · 11.6 KB
/
test_activepipe.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
254
255
256
257
258
259
260
261
262
263
import unittest
import numpy as np
import mock
from activepipe import ActivePipeline
from corpus import Corpus
from featureforge.vectorizer import Vectorizer
from scipy.sparse import csr_matrix
from sklearn.preprocessing import normalize
testing_config = {
'features': Vectorizer([lambda x : x]),
'em_adding_instances': 3,
'u_corpus_f': 'test_files/unlabeled_corpus.pickle',
'test_corpus_f': 'test_files/test_corpus.pickle',
'training_corpus_f': 'test_files/training_corpus.pickle',
'feature_corpus_f': 'test_files/feature_corpus.pickle',
'dummy_config': None,
'number_of_features': 2,
}
X = [
[0.0, 1.0, 0.0],
[1.0, 1.0, 1.0],
[0.0, 1.0, 1.0]
]
Y = [[1], [0, 1] , [0, 0]]
U_vectors = [
[1.0, 1.0, 1.0],
[1.0, 1.0, 2.0],
[0.0, 0.0, 1.0],
[1.0, 0.0, 0.0],
[2.0, 2.0, 2.0],
]
T_vectors = [
[1.0, 3.0, 5.0],
[3.0, 1.0, 0.0]
]
T_targets = [[1], [2]]
feat_corpus = [[-1, -1, 0], [1, -1, 0]]
class TestActivePipe(unittest.TestCase):
def setUp(self):
self.pipe = ActivePipeline(**testing_config)
self.instance_class_prob = np.array(
[[0.5, 0.5],
[0.25, 0.75],
[0.7, 0.3],
[0.1, 0.9],
[0.8, 0.2]]
)
self.instance_prob = np.array([0.02, 0.09, 0.01, 0.12, 0.08])
def test_em_feat_class_no_labeled(self):
"""Tests if the feature_log_prob matrix is calculated correctly.
P(fj|ck) = sum_i(P(xi) * fj(xi) * P(ck|xi))
P(f0|c0) = 0.5*0.02*1 + 0.25*0.09*1 + 0.7*0*0.01 + 0.1*1*0.12 + 0.8*2*0.08 = 0.1725
P(f0|c1) = 0.5*0.02*1 + 0.75*0.09*1 + 0.3*0*0.01 + 0.9*1*0.12 + 0.2*2*0.08 = 0.2175
P(f1|c0) = 0.5*0.02*1 + 0.25*0.09*1 + 0.7*0*0.01 + 0.1*0*0.12 + 0.8*2*0.08 = 0.1605
P(f1|c1) = 0.5*0.02*1 + 0.75*0.09*1 + 0.3*0*0.01 + 0.9*0*0.12 + 0.2*2*0.08 = 0.1095
P(f2|c0) = 0.5*0.02*1 + 0.25*0.09*2 + 0.7*1*0.01 + 0.1*0*0.12 + 0.8*2*0.08 = 0.19
P(f2|c1) = 0.5*0.02*1 + 0.75*0.09*2 + 0.3*1*0.01 + 0.9*0*0.12 + 0.2*2*0.08 = 0.18
"""
expected = np.array([[0.32982792, 0.30688337, 0.36328872],
[0.42899408, 0.21597633, 0.35502959]])
with mock.patch('featmultinomial.FeatMultinomalNB.predict_proba',
return_value=self.instance_class_prob) as mock_pred:
with mock.patch('featmultinomial.FeatMultinomalNB.instance_proba',
return_value=self.instance_prob) as mock_inst_p:
self.pipe.training_corpus = Corpus()
self.pipe._expectation_maximization()
np.testing.assert_array_almost_equal(
self.pipe.classifier.feature_log_prob_,
np.log(expected)
)
def test_em_class_no_labeled(self):
"""Tests if the class_log_prior_ matrix is calculated correctly.
P(ck) = sum_i(P(xi) * P(ck|xi))
P(c0) = 0.5*0.02 + 0.25*0.09 + 0.7*0.01 + 0.1*0.12 + 0.8*0.08 = 0.1155
P(c1) = 0.5*0.02 + 0.75*0.09 + 0.3*0.01 + 0.9*0.12 + 0.2*0.08 = 0.2045
"""
expected = np.array([0.3609375, 0.6390625])
with mock.patch('featmultinomial.FeatMultinomalNB.predict_proba',
return_value=self.instance_class_prob) as mock_pred:
with mock.patch('featmultinomial.FeatMultinomalNB.instance_proba',
return_value=self.instance_prob) as mock_inst_p:
self.pipe.training_corpus = Corpus()
self.pipe._expectation_maximization()
np.testing.assert_array_almost_equal(
self.pipe.classifier.class_log_prior_,
np.log(expected)
)
def test_em_feat_class(self):
"""
P(fj|ck) = Pu(fj|ck) * 0.1 + 0.9* sum_i(P(xl_i) * fj(xl_i) * {0,1})
P(f0|c0) = 0.1 * 0.1725 + 0.9 * (0.02*0*0 + 0.09*1*1 + 0.01*0*1) = 0.09825
P(f0|c1) = 0.1 * 0.2175 + 0.9 * (0.02*0*1 + 0.09*1*0 + 0.01*0*0) = 0.02175
P(f1|c0) = 0.1 * 0.1605 + 0.9 * (0.02*1*0 + 0.09*1*1 + 0.01*1*1) = 0.10605
P(f1|c1) = 0.1 * 0.1095 + 0.9 * (0.02*1*1 + 0.09*1*0 + 0.01*1*0) = 0.02895
P(f2|c0) = 0.1 * 0.19 + 0.9 * (0.02*0*0 + 0.09*1*1 + 0.01*1*1) = 0.109
P(f2|c1) = 0.1 * 0.18 + 0.9 * (0.02*0*1 + 0.09*1*0 + 0.01*1*0) = 0.018
"""
expected = np.array([[0.31359719, 0.3384934, 0.34790935],
[0.31659388, 0.421397379, 0.262008733]])
instance_prob_fun = lambda s, x: self.instance_prob[:x.shape[0]]
with mock.patch('featmultinomial.FeatMultinomalNB.predict_proba',
return_value=self.instance_class_prob) as mock_pred:
with mock.patch('featmultinomial.FeatMultinomalNB.instance_proba',
new=instance_prob_fun) as mock_inst_p:
self.pipe._expectation_maximization()
np.testing.assert_array_almost_equal(
self.pipe.classifier.feature_log_prob_,
np.log(expected)
)
def test_em_class(self):
"""Tests if the class_log_prior_ matrix is calculated correctly.
P(ck) = sum_i(P(xui) * P(ck|xui)) * 0.1 + sum_i(P(xli) * P(ck|xli)) * 0.9
P(c0) = 0.1155 * 0.1 + 0.9 * (0*0.02 + 1*0.09 + 1*0.01) = 0.10155
P(c1) = 0.2045 * 0.1 + 0.9 * (1*0.02 + 0*0.09 + 0*0.01) = 0.03845
"""
expected = np.array([0.725357142, 0.27464285714])
instance_prob_fun = lambda s, x: self.instance_prob[:x.shape[0]]
with mock.patch('featmultinomial.FeatMultinomalNB.predict_proba',
return_value=self.instance_class_prob) as mock_pred:
with mock.patch('featmultinomial.FeatMultinomalNB.instance_proba',
new=instance_prob_fun) as mock_inst_p:
self.pipe._expectation_maximization()
np.testing.assert_array_almost_equal(
self.pipe.classifier.class_log_prior_,
np.log(expected)
)
def test_em_sum_to_one(self):
"""Checks that both parameters estimated by the em step sums one."""
self.pipe._expectation_maximization()
self.assertAlmostEqual(
(np.exp(self.pipe.classifier.class_log_prior_)).sum(), 1
)
np.testing.assert_array_almost_equal(
(np.exp(self.pipe.classifier.feature_log_prob_)).sum(axis=1),
np.ones(2)
)
def test_get_instance_corpus(self):
"""Test the three instance corpus loaded from files."""
self.assertEqual(len(self.pipe.training_corpus), len(X))
self.assertEqual(len(self.pipe.test_corpus), len(T_vectors))
self.assertEqual(len(self.pipe.unlabeled_corpus), len(U_vectors))
self.assertEqual(len(self.pipe.user_corpus), 0)
def test_get_feature_corpus(self):
"""Test the feature corpus loaded from file."""
self.assertEqual(self.pipe.feature_corpus.shape, (2, 3))
self.assertEqual(self.pipe.feature_corpus.tolist(), feat_corpus)
def test__build_feature_boost(self):
"""Test the creation of matrices user_features and asked_features."""
self.pipe._build_feature_boost()
self.assertIsNotNone(self.pipe.user_features)
self.assertEqual(self.pipe.user_features.shape, (2, 3))
self.assertFalse(np.any(self.pipe.user_features !=
self.pipe.classifier.alpha))
self.assertIsNotNone(self.pipe.asked_features)
self.assertEqual(self.pipe.asked_features.shape, (2, 3))
self.assertFalse(np.any(self.pipe.asked_features))
def test__build_feature_boost_emulate(self):
"""The user corpus must be used to construct self.pipe.asked_features"""
self.pipe.emulate = True
self.pipe._build_feature_boost()
expected = np.array([[False, False, True], [True, False, True]])
np.testing.assert_array_almost_equal(self.pipe.asked_features, expected)
def test_set_config(self):
"""Each configuration must be set as attribute if not None."""
for key, value in testing_config.items():
if value is not None:
self.assertTrue(hasattr(self.pipe, key))
self.assertEqual(getattr(self.pipe, key), value)
else:
self.assertFalse(hasattr(self.pipe, key))
def test_get_next_features(self):
"""Tests if the features are selected in order of IG for the class."""
self.pipe.classifier.feat_information_gain = np.array([2, 0, 1])
feat_indexes = self.pipe.get_next_features(class_number=0)
self.assertEqual(feat_indexes, [2, 1])
def test_get_next_features_repeated(self):
"""If the features where labeled, don't ask for them again."""
self.pipe.classifier.feat_information_gain = np.array([2, 0, 1])
self.pipe.asked_features[0][0] = True
self.pipe.asked_features[0][2] = True
feat_indexes = self.pipe.get_next_features(class_number=0)
self.assertEqual(feat_indexes, [1])
def test_handle_feature_prediction(self):
"""Positive and negative examples must be added to aked_features.
For positive examples, the user_features must be changed.
"""
class_number = 0
self.pipe.handle_feature_prediction(class_number, full_set=[0, 1, 2],
prediction=[1])
self.assertEqual(self.pipe.user_features[0][1],
self.pipe.classifier.alpha + self.pipe.feature_boost,
msg='Bad Positive Example')
self.assertEqual(self.pipe.user_features[0][0],
self.pipe.classifier.alpha,
msg='Change in non labeled feature')
self.assertTrue(np.all(self.pipe.user_features[1] ==
self.pipe.classifier.alpha),
msg='Change in non labeled feature')
self.assertTrue(self.pipe.asked_features[class_number][0])
self.assertTrue(self.pipe.asked_features[class_number][1])
self.assertTrue(self.pipe.asked_features[class_number][2])
def test_get_next_instance(self):
"""Checks next instance selection using entropy.
-E(1) = 0.5*log(0.5) + 0.5*log(0.5) = -0.6931471805599453
-E(2) = 0.25*log(0.25) + 0.75*log(0.75) = -0.5623351446188083
-E(3) = 0.7*log(0.7) + 0.3*log(0.3) = -0.6108643020548935
-E(4) = 0.1*log(0.1) + 0.9*log(0.9) = -0.3250829733914482
-E(5) = 0.8*log(0.8) + 0.2*log(0.2) = -0.5004024235381879
"""
with mock.patch('featmultinomial.FeatMultinomalNB.predict_proba',
return_value=self.instance_class_prob) as mock_method:
indexes = []
while len(self.pipe.unlabeled_corpus) != 0:
indexes.append(self.pipe.get_next_instance())
self.pipe.unlabeled_corpus.pop_instance(indexes[-1])
rigth_order = [3, 3, 1, 1, 0]
self.assertEqual(indexes, rigth_order)
self.assertIsNone(self.pipe.get_next_instance())
def test_label_feature_corpus(self):
"""The new feature information must be saved into feature_corpus_f.
"""
self.pipe.asked_features[0] = True
self.pipe.user_features[0][0] += self.pipe.feature_boost
self.pipe.user_features[0][2] += self.pipe.feature_boost
self.pipe.feature_corpus_f = 'test_files/feature_corpus_saved.pickle'
self.pipe.label_feature_corpus()
self.pipe._get_feature_corpus()
expected = np.array([[1, 0, 1], [1, -1, 0]])
np.testing.assert_array_equal(self.pipe.feature_corpus, expected)
if __name__ == '__main__':
unittest.main()