|
1
|
|
|
#!/usr/bin/env python |
|
2
|
|
|
# -*- coding: utf-8 -*- |
|
3
|
|
|
|
|
4
|
|
|
""" |
|
5
|
|
|
Unit tests for general utility functions. |
|
6
|
|
|
""" |
|
7
|
|
|
|
|
8
|
|
|
import unittest |
|
9
|
|
|
from AnniesLasso import utils |
|
10
|
|
|
|
|
11
|
|
|
|
|
12
|
|
|
class TestShortHash(unittest.TestCase): |
|
13
|
|
|
|
|
14
|
|
|
def test_different(self): |
|
15
|
|
|
self.assertNotEqual(utils.short_hash(True), utils.short_hash(False)) |
|
16
|
|
|
|
|
17
|
|
|
def test_hashing(self): |
|
18
|
|
|
# So that it does not change with time and break old hashes. |
|
19
|
|
|
self.assertEqual("7fc56270e79d5ed678fe0d61f8370cf623e75af33a3ea00cfc", |
|
20
|
|
|
utils.short_hash("ABCDE")) |
|
21
|
|
|
|
|
22
|
|
|
|
|
23
|
|
|
class TestIsStructuredLabelVector(unittest.TestCase): |
|
24
|
|
|
|
|
25
|
|
|
def test_fail_input_string(self): |
|
26
|
|
|
self.assertFalse(utils.is_structured_label_vector("")) |
|
27
|
|
|
|
|
28
|
|
|
def test_fail_input_none(self): |
|
29
|
|
|
self.assertFalse(utils.is_structured_label_vector(None)) |
|
30
|
|
|
|
|
31
|
|
|
def test_fail_input_bool(self): |
|
32
|
|
|
self.assertFalse(utils.is_structured_label_vector(True)) |
|
33
|
|
|
self.assertFalse(utils.is_structured_label_vector(False)) |
|
34
|
|
|
|
|
35
|
|
|
def test_fail_input_int(self): |
|
36
|
|
|
self.assertFalse(utils.is_structured_label_vector(1)) |
|
37
|
|
|
|
|
38
|
|
|
def test_fail_input_float(self): |
|
39
|
|
|
self.assertFalse(utils.is_structured_label_vector(0.0)) |
|
40
|
|
|
|
|
41
|
|
|
def test_fail_input_empty_iterables(self): |
|
42
|
|
|
self.assertFalse(utils.is_structured_label_vector(())) |
|
43
|
|
|
self.assertFalse(utils.is_structured_label_vector(set())) |
|
44
|
|
|
self.assertFalse(utils.is_structured_label_vector([])) |
|
45
|
|
|
self.assertFalse(utils.is_structured_label_vector({})) |
|
46
|
|
|
|
|
47
|
|
|
def test_fail_input_nested_empty_iterables(self): |
|
48
|
|
|
self.assertFalse(utils.is_structured_label_vector([{}])) |
|
49
|
|
|
self.assertFalse(utils.is_structured_label_vector([()])) |
|
50
|
|
|
self.assertFalse(utils.is_structured_label_vector([[()]])) |
|
51
|
|
|
|
|
52
|
|
|
def test_failed_input_term_lengths(self): |
|
53
|
|
|
self.assertFalse(utils.is_structured_label_vector([[("A", 0, 1)]])) |
|
54
|
|
|
self.assertFalse(utils.is_structured_label_vector( |
|
55
|
|
|
[[("A", 1)], [("B", 0, 1)]])) |
|
56
|
|
|
|
|
57
|
|
|
def test_valid_input_term_int_power(self): |
|
58
|
|
|
self.assertTrue(utils.is_structured_label_vector([[("A", 1)]])) |
|
59
|
|
|
|
|
60
|
|
|
def test_valid_input_term_float_power(self): |
|
61
|
|
|
self.assertTrue(utils.is_structured_label_vector([[("A", 1.0)]])) |
|
62
|
|
|
|
|
63
|
|
|
def test_valid_input_term_form(self): |
|
64
|
|
|
self.assertFalse(utils.is_structured_label_vector([[(1, "A")]])) |
|
65
|
|
|
|
|
66
|
|
|
|
|
67
|
|
|
|
|
68
|
|
|
class TestLabelVectorParser(unittest.TestCase): |
|
69
|
|
|
|
|
70
|
|
|
def test_single_term(self): |
|
71
|
|
|
self.assertEquals([[("A", 1)]], utils.parse_label_vector("A")) |
|
72
|
|
|
|
|
73
|
|
|
def test_single_term_explicit(self): |
|
74
|
|
|
self.assertEquals([[("A", 1)]], utils.parse_label_vector("A^1")) |
|
75
|
|
|
|
|
76
|
|
|
def test_single_term_explicit_float(self): |
|
77
|
|
|
self.assertEquals([[("A", 1.2)]], utils.parse_label_vector("A^1.2")) |
|
78
|
|
|
|
|
79
|
|
|
def test_single_term_explicit_float_negative(self): |
|
80
|
|
|
self.assertEquals([[("A", -1.5)]], utils.parse_label_vector("A^-1.5")) |
|
81
|
|
|
|
|
82
|
|
|
def test_no_genuine_term(self): |
|
83
|
|
|
with self.assertRaises(ValueError): |
|
84
|
|
|
utils.parse_label_vector("A^0") |
|
85
|
|
|
|
|
86
|
|
|
def test_remove_irrelevant_terms(self): |
|
87
|
|
|
self.assertEquals([[("A", 2)]], utils.parse_label_vector("A^2 + B^0")) |
|
88
|
|
|
|
|
89
|
|
|
def test_sum_terms(self): |
|
90
|
|
|
self.assertEquals([[("A", 5)]], utils.parse_label_vector("A^3 * A^2")) |
|
91
|
|
|
|
|
92
|
|
|
def test_sum_negative_terms(self): |
|
93
|
|
|
self.assertEquals([[("A", -7)]], utils.parse_label_vector("A^-3 * A^-4")) |
|
94
|
|
|
|
|
95
|
|
|
def test_ignore_irrelevant_terms(self): |
|
96
|
|
|
self.assertEquals([[("A", 3)]], utils.parse_label_vector("A^3 * B^0")) |
|
97
|
|
|
|
|
98
|
|
|
def test_parse_mixed_cross_terms(self): |
|
99
|
|
|
self.assertEquals([[("A", 2.0), ("B", -4.23), ("C", 1)]], |
|
100
|
|
|
utils.parse_label_vector("A^2.0 * B^-4.23 * C")) |
|
101
|
|
|
|
|
102
|
|
|
def test_invalid_powers(self): |
|
103
|
|
|
with self.assertRaises(ValueError): |
|
104
|
|
|
utils.parse_label_vector("A^B") |
|
105
|
|
|
|
|
106
|
|
|
def test_infinte_powers(self): # Ha! |
|
107
|
|
|
with self.assertRaises(ValueError): |
|
108
|
|
|
utils.parse_label_vector("A^inf") |
|
109
|
|
|
|
|
110
|
|
|
with self.assertRaises(ValueError): |
|
111
|
|
|
utils.parse_label_vector("A^-inf") |
|
112
|
|
|
|
|
113
|
|
|
def test_nan_powers(self): |
|
114
|
|
|
with self.assertRaises(ValueError): |
|
115
|
|
|
utils.parse_label_vector("A^nan") |
|
116
|
|
|
|
|
117
|
|
|
def test_complex_parse(self): |
|
118
|
|
|
self.assertEquals(utils.parse_label_vector( |
|
119
|
|
|
"A^4 + B^4*C^3 + D^2 *E * F^1 + G^4.3*H + J^0*G^6 * G^-6"), |
|
120
|
|
|
[ |
|
121
|
|
|
[("A", 4)], |
|
122
|
|
|
[("B", 4), ("C", 3)], |
|
123
|
|
|
[("D", 2), ("E", 1), ("F", 1)], |
|
124
|
|
|
[("G", 4.3), ("H", 1)] |
|
125
|
|
|
] |
|
126
|
|
|
) |
|
127
|
|
|
|
|
128
|
|
|
def test_valid_label_vector(self): |
|
129
|
|
|
label_vector = [ |
|
130
|
|
|
[("A", 4)], |
|
131
|
|
|
[("B", 4), ("C", 3)], |
|
132
|
|
|
[("D", 2), ("E", 1), ("F", 1)], |
|
133
|
|
|
[("G", 4.3), ("H", 1)] |
|
134
|
|
|
] |
|
135
|
|
|
self.assertTrue(utils.is_structured_label_vector(label_vector)) |
|
136
|
|
|
self.assertEquals(label_vector, utils.parse_label_vector(label_vector)) |
|
137
|
|
|
|
|
138
|
|
|
def test_parsing_with_columns(self): |
|
139
|
|
|
self.assertEquals(utils.parse_label_vector("A + B^2 + C^3 + A*B", |
|
140
|
|
|
columns=["C", "A", "B"]), |
|
141
|
|
|
[ |
|
142
|
|
|
[(1, 1)], |
|
143
|
|
|
[(2, 2)], |
|
144
|
|
|
[(0, 3)], |
|
145
|
|
|
[(1, 1), (2, 1)] |
|
146
|
|
|
]) |
|
147
|
|
|
|
|
148
|
|
|
|
|
149
|
|
|
class TestHumanReadableLabelVector(unittest.TestCase): |
|
150
|
|
|
|
|
151
|
|
|
def test_none_supplied(self): |
|
152
|
|
|
with self.assertRaises(TypeError): |
|
153
|
|
|
utils.human_readable_label_vector(None) |
|
154
|
|
|
|
|
155
|
|
|
def test_bool_supplied(self): |
|
156
|
|
|
for each in (True, False): |
|
157
|
|
|
with self.assertRaises(TypeError): |
|
158
|
|
|
utils.human_readable_label_vector(each) |
|
159
|
|
|
|
|
160
|
|
|
def test_str_supplied(self): |
|
161
|
|
|
with self.assertRaises(TypeError): |
|
162
|
|
|
utils.human_readable_label_vector("") |
|
163
|
|
|
|
|
164
|
|
|
def test_complex_parse(self): |
|
165
|
|
|
self.assertEqual( |
|
166
|
|
|
utils.human_readable_label_vector([ |
|
167
|
|
|
[("A", 4)], |
|
168
|
|
|
[("B", 4), ("C", 3)], |
|
169
|
|
|
[("D", 2), ("E", 1), ("F", 1)], |
|
170
|
|
|
[("G", 4.3), ("H", 1)] |
|
171
|
|
|
]), |
|
172
|
|
|
"1 + A^4 + (B^4 * C^3) + (D^2 * E * F) + (G^4.3 * H)" |
|
173
|
|
|
) |
|
174
|
|
|
|
|
175
|
|
|
|
|
176
|
|
|
class TestProgressBar(unittest.TestCase): |
|
177
|
|
|
def test_iterable(self): |
|
178
|
|
|
for i in utils.progressbar(range(1000), size=100): |
|
179
|
|
|
None |
|
180
|
|
|
|
|
181
|
|
|
def test_nosize(self): |
|
182
|
|
|
for i in utils.progressbar(range(100)): |
|
183
|
|
|
None |
|
184
|
|
|
|
|
185
|
|
|
def test_message(self): |
|
186
|
|
|
for i in utils.progressbar(range(1000), size=100, message="Hi"): |
|
187
|
|
|
None |
|
188
|
|
|
|
|
189
|
|
|
def test_neg_size(self): |
|
190
|
|
|
for i in utils.progressbar(range(1000), size=-1): |
|
191
|
|
|
None |
|
192
|
|
|
|
|
193
|
|
|
|
|
194
|
|
|
class TestBuildLabelVector(unittest.TestCase): |
|
195
|
|
|
def test_invalid_labels(self): |
|
196
|
|
|
self.assertEquals("", utils.build_label_vector("", 8, 1)) |
|
197
|
|
|
|
|
198
|
|
|
def test_single_labels(self): |
|
199
|
|
|
self.assertEquals( |
|
200
|
|
|
"A + B + C + D + E", |
|
201
|
|
|
utils.build_label_vector("ABCDE", 1)) |
|
202
|
|
|
|
|
203
|
|
|
def test_high_order_labels(self): |
|
204
|
|
|
self.assertEquals( |
|
205
|
|
|
"A + B + C + A^2 + B^2 + C^2 + A^3 + B^3 + C^3", |
|
206
|
|
|
utils.build_label_vector("ABC", 3)) |
|
207
|
|
|
|
|
208
|
|
|
def test_single_cross_term(self): |
|
209
|
|
|
self.assertEquals( |
|
210
|
|
|
"A + B + A*B", |
|
211
|
|
|
utils.build_label_vector("AB", 1, 1)) |
|
212
|
|
|
|
|
213
|
|
|
def test_high_order_cross_term(self): |
|
214
|
|
|
self.assertEquals( |
|
215
|
|
|
utils.build_label_vector("ABC", 3, 3), |
|
216
|
|
|
"A + B + C + A^2 + A*B + A*C + B^2 + B*C + C^2 + A^3 + A^2*B + " |
|
217
|
|
|
"A^2*C + A*B^2 + A*B*C + A*C^2 + B^3 + B^2*C + B*C^2 + C^3 + " |
|
218
|
|
|
"A^3*B + A^3*C + A^2*B^2 + A^2*B*C + A^2*C^2 + A*B^3 + A*B^2*C + " |
|
219
|
|
|
"A*B*C^2 + A*C^3 + B^3*C + B^2*C^2 + B*C^3" |
|
220
|
|
|
) |
|
221
|
|
|
|
|
222
|
|
|
def test_different_order_cross_terms(self): |
|
223
|
|
|
self.assertEquals( |
|
224
|
|
|
utils.build_label_vector("ABC", 3, 2), |
|
225
|
|
|
"A + B + C + A^2 + A*B + A*C + B^2 + B*C + C^2 + A^3 + A^2*B + " |
|
226
|
|
|
"A^2*C + A*B^2 + A*B*C + A*C^2 + B^3 + B^2*C + B*C^2 + C^3") |
|
227
|
|
|
|
|
228
|
|
|
|