-
Notifications
You must be signed in to change notification settings - Fork 0
/
my_unittests.py
executable file
·202 lines (169 loc) · 7.86 KB
/
my_unittests.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
#!/bin/env python3
import pandas as pd
import unittest
import classifiers
import exceptions
import load_data
from exceptions import InsufficientDataException
from process_data import compute_evaluation_scores
from my_statistics import DataGraph, Point, DataLine
class TestLoadData(unittest.TestCase):
def test_sample(self):
# create dummy data
sample = load_data.Sample()
data = \
[pd.Series({'a': 'b', 'classification': 'useful'}),
pd.Series({'a': 'c', 'classification': 'not-useful'})]
sample.add_chunk([data[0]])
sample.add_chunk([data[1]])
self.assertEqual(
sample.get_data(load_data.SampleTypeEnum.TRAIN),
None
)
# testing all sets are properly set
sample.start_iter()
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TEST)[0]
.equals(pd.Series({'a': 'b', 'classification': 'useful'})))
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TRAIN)[0]
.equals(pd.Series({'a': 'c', 'classification': 'not-useful'})))
# more dummy data
data = \
[pd.Series({'a': 'd', 'classification': 'funny'}),
pd.Series({'a': 'e', 'classification': 'not-funny'})]
sample.add_chunk([data[0]])
sample.add_chunk([data[1]])
sample.start_iter()
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TEST)[0]
.equals(pd.Series({'a': 'b', 'classification': 'useful'})))
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TRAIN)[0]
.equals(pd.Series({'a': 'c', 'classification': 'not-useful'})))
self.assertEqual(
len(sample.get_data(load_data.SampleTypeEnum.TRAIN)),
3)
sample.next_iter()
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TEST)[0]
.equals(pd.Series({'a': 'c', 'classification': 'not-useful'})))
self.assertTrue(sample.get_data(load_data.SampleTypeEnum.TRAIN)[0]
.equals(pd.Series({'a': 'b', 'classification': 'useful'})))
self.assertEqual(
len(sample.get_data(load_data.SampleTypeEnum.TRAIN)),
3)
# test if data are not changed by sideeffects
identical = True
for a, b in zip(sample.get_data(load_data.SampleTypeEnum.TRAIN),
sample.get_data(load_data.SampleTypeEnum.TEST)):
if not a.equals(b):
identical = False
break
self.assertFalse(identical)
self.assertRaises(IndexError, lambda: sample.limit_train_size(4))
sample.limit_train_size(1)
self.assertTrue(len(sample.get_data(load_data.SampleTypeEnum.TRAIN)) == 1)
self.assertEqual(sample.next_iter(), True)
self.assertEqual(sample.next_iter(), True)
self.assertEqual(sample.next_iter(), False)
def test_load_data(self):
# warning data has been tampered for testing purposes
self.assertRaises(exceptions.DataMismatchException,
lambda: load_data.Data('unittests/data_unit_mismatch.json',
'unittests/geneea_unit_mismatch.json'))
# warning data has been tampered for testing purposes
data = load_data.Data('unittests/data_unit.json', 'unittests/geneea_unit.json')
# returns size of training set
self.assertEqual(data.generate_sample(10, load_data.LikeTypeEnum.USEFUL),
20)
# test returned samples
self.assertEqual(data.get_feature_dict(load_data.SampleTypeEnum.TRAIN, set())[0][0],
{})
# only review len features
self.assertEqual(data.generate_sample(10, load_data.LikeTypeEnum.USEFUL),
20)
self.assertEqual(len(data.get_feature_dict(load_data.SampleTypeEnum.TRAIN, set())),
18)
self.assertTrue('review_length' in
data.get_feature_dict(load_data.SampleTypeEnum.TRAIN, {load_data.FeatureSetEnum.REVIEWLEN})[0][
0])
# number of instances
self.assertEqual(len(data.get_feature_dict(load_data.SampleTypeEnum.TRAIN, set())),
18)
data.limit_train_size(10)
self.assertEqual(len(data.get_feature_dict(load_data.SampleTypeEnum.TRAIN, set())),
10)
# test insufficient data exception
self.assertRaises(IndexError, lambda: data.limit_train_size(1000))
# test add n-grams
data.used_ngrams = {'a', 'b'}
fs = {'c': 2}
data.add_ngram(fs, ['b', 'b', 'c', 'a'], 2)
self.assertEqual(fs,
{'c': 2,
'contains(b&b&)': 'Yes',
})
class TestStatistics(unittest.TestCase):
def test_data_graph(self):
# create dummy data
dg = DataGraph('name', 'xl', 'yl')
dg.add_points(5, 'n', {'a': 2, 'b': 3})
dg.clear_data()
dg.add_points(1, 'n', {'a': 2, 'b': 3})
dg.add_points(2, 'n', {'b': 3, 'd': 4})
dg.add_points(2, 'n', {'c': 3})
dg.set_view({'n': {'a', 'c', 'd'}})
dg.set_fmt('n', 'a', 'ro')
# test string properties
self.assertEqual(dg.name, 'name')
self.assertEqual(dg.xlabel, 'xl')
self.assertEqual(dg.ylabel, 'yl')
# test data with the set view
# not applicable anymore
# self.assertEqual(dg.get_data(),
# {'n': {
# 'a': DataLine({1: Point()}, 'ro'),
# 'c': DataLine({2: Point()}, ''),
# 'd': DataLine({2: Point()}, '')
# }})
def test_statistics(self):
# for testing statistics, run statistics as main, it'll produce graphs
pass
class TestClassifier(unittest.TestCase):
def test_naive_bayes(self):
nb = classifiers.naivebayes.Classifier({})
nb.train([({'a': 1, 'b': 1}, 'a'),
({'a': 1, 'b': 2}, 'b')])
self.assertEqual(nb.classify({'a': 1, 'b': 1}), 'a')
self.assertEqual(nb.classify({'a': 1, 'b': 2}), 'b')
class TestProcessData(unittest.TestCase):
def test_evaluation(self):
nb = classifiers.naivebayes.Classifier({})
nb.train([({'a': 1, 'b': 1}, 'useful'),
({'a': 1, 'b': 2}, 'not-useful'),
({'a': 1, 'b': 3}, 'not-useful')])
test_set = [({'a': 1, 'b': 1}, 'useful'), # TP
({'a': 1, 'b': 2}, 'not-useful'), # TN
({'a': 1, 'b': 2}, 'not-useful'), # TN
({'a': 1, 'b': 3}, 'useful'), # FN
({'a': 1, 'b': 3}, 'useful'), # FN
({'a': 1, 'b': 3}, 'useful'), # FN
({'a': 1, 'b': 1}, 'not-useful'), # FP
({'a': 1, 'b': 1}, 'not-useful'), # FP
({'a': 1, 'b': 1}, 'not-useful'), # FP
({'a': 1, 'b': 1}, 'not-useful')] # FP
# TP 1
# TN 2
# FP 4
# FN 3
metrics: dict = compute_evaluation_scores(nb, test_set, load_data.LikeTypeEnum.USEFUL)
expected_out: dict = {'accuracy': 3 / 10,
'precision': 1 / (1 + 4),
'recall': 1 / (1 + 3),
'f_measure': 2 * 1 / 5 * 1 / 4 / (1 / 5 + 1 / 4),
'tp': 1 / 10,
'tn': 2 / 10,
'fp': 4 / 10,
'fn': 3 / 10}
for k in expected_out.keys():
self.assertEqual(round(metrics[k], 3),
round(expected_out[k], 3),
msg=f'metrics {k} failed.')
if __name__ == "__main__":
unittest.main()