-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_feature_extraction.py
356 lines (262 loc) · 12.7 KB
/
test_feature_extraction.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
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
'''
Unit tests for feature_extraction.py
'''
import unittest
from ast import literal_eval
import numpy as np
from helpers import extract_images, get_black_white
from feature_extraction import \
density, \
horizontal_symmetry, \
horizontal_intersections, \
vertical_intersections, \
number_of_loops, \
horizontally_split_symmetry, \
vertically_split_symmetry, \
get_image_features
# pylint: disable=invalid-name
# pylint: disable=unused-variable
def generate_all_images() -> list[np.ndarray]:
'''Returns a list of images.'''
folder = 'input_files/training_data'
images = []
for digit in range(NUM_FILES := 10):
FILENAME = f'{folder}/handwritten_samples_{digit}.csv'
IMAGES, _ = extract_images(file=FILENAME, has_label=True)
BINARY_IMAGES = [get_black_white(image) for image in IMAGES]
for image in BINARY_IMAGES:
images.append(image)
assert isinstance(images, list)
assert [isinstance(image, np.ndarray) for image in images]
assert len(images) == 9990, \
f'Expected 9990 images, got {len(images)}'
return images
def load_expected(filename: str) -> list[float]:
'''Loads expected values from a file'''
FOLDER = 'output_files/feature_values'
VALID_FILENAMES = [
f'{FOLDER}/density.expected',
f'{FOLDER}/horizontal_symmetry.expected',
f'{FOLDER}/horizontal_intersections.expected',
f'{FOLDER}/vertical_intersections.expected',
f'{FOLDER}/number_of_loops.expected',
f'{FOLDER}/horizontally_split_symmetry.expected',
f'{FOLDER}/vertically_split_symmetry.expected',
]
assert filename in VALID_FILENAMES, \
f'Invalid filename: {filename}'
with open(filename, 'r', encoding='utf-8') as f:
if filename.endswith('horizontal_intersections.expected') or \
filename.endswith('vertical_intersections.expected'):
expected_values = [literal_eval(value) for value in f.readlines()]
else:
expected_values = [float(value) for value in f.readlines()]
# asert assert assert
assert isinstance(expected_values, list)
if filename.endswith('horizontal_intersections.expected') or \
filename.endswith('vertical_intersections.expected'):
for value in expected_values:
assert isinstance(value, tuple)
assert len(value) == 2, f'Expected length 2, got {len(value)}'
assert [isinstance(v, float) for v in value]
else:
assert [isinstance(value, float) for value in expected_values]
assert len(expected_values) == 9990, \
f'Expected 9990 values, got {len(expected_values)}'
return expected_values
class TestFeatureExtraction(unittest.TestCase):
'''Test class for feature_extraction.py'''
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self._images = generate_all_images()
FOLDER = 'output_files/feature_values'
self._densities = load_expected(
filename=f'{FOLDER}/density.expected')
self._horizontal_symmetries = load_expected(
filename=f'{FOLDER}/horizontal_symmetry.expected')
self._vertical_intersections = load_expected(
filename=f'{FOLDER}/vertical_intersections.expected')
self._horizontal_intersections = load_expected(
filename=f'{FOLDER}/horizontal_intersections.expected')
self._number_of_loops = load_expected(
filename=f'{FOLDER}/number_of_loops.expected')
self._horizontally_split_symmetries = load_expected(
filename=f'{FOLDER}/horizontally_split_symmetry.expected')
self._vertically_split_symmetries = load_expected(
filename=f'{FOLDER}/vertically_split_symmetry.expected')
def test_density(self):
'''Tests the density function'''
# test edge cases
self.assertEqual(density(np.zeros(shape=(3, 3))), 0.0)
self.assertEqual(density(np.eye(3)), 0.3333333333333333)
self.assertEqual(density(np.ones(shape=(3, 3))), 1.0)
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(density(image), self._densities[i])
def test_horizontal_symmetry(self):
'''Tests the horizontal_symmetry function'''
# test edge cases
IMAGE = np.array([[0, 1, 0], [1, 0, 1], [0, 1, 0]])
IDENTITY = np.eye(3, dtype=np.uint8)
self.assertEqual(horizontal_symmetry(np.zeros((3, 3), np.uint8)), 0.0)
self.assertEqual(horizontal_symmetry(np.ones((3, 3), np.uint8)), 0.0)
self.assertEqual(horizontal_symmetry(IMAGE), 0.0)
self.assertEqual(horizontal_symmetry(IDENTITY), 0.4444444444444444)
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(horizontal_symmetry(image),
self._horizontal_symmetries[i])
def test_vertical_intersections(self):
'''Tests the vertical_intersections function'''
# test edge cases
image = np.zeros(shape=(3, 3), dtype=np.uint8)
self.assertEqual(vertical_intersections(image), (0.0, 0.0))
image = np.array([[1, 1, 1], [0, 0, 0], [0, 0, 0]])
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.array([[0, 0, 0], [1, 1, 1], [0, 0, 0]])
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.array([[0, 0, 0], [0, 0, 0], [1, 1, 1]])
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.array([[1, 1, 1], [1, 1, 1], [0, 0, 0]])
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.array([[1, 1, 1], [0, 0, 0], [1, 1, 1]])
self.assertEqual(vertical_intersections(image), (2.0, 2.0))
image = np.array([[0, 0, 0], [1, 1, 1], [1, 1, 1]])
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.ones(shape=(3, 3), dtype=np.uint8)
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.eye(3, dtype=np.uint8)
self.assertEqual(vertical_intersections(image), (1.0, 1.0))
image = np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0]])
self.assertEqual(vertical_intersections(
image), (1.0, 0.3333333333333333))
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(vertical_intersections(image),
self._vertical_intersections[i])
def test_horizontal_intersections(self):
'''Tests the horizontal_intersections function'''
# test edge cases
image = np.zeros(shape=(3, 3), dtype=np.uint8)
self.assertEqual(horizontal_intersections(image), (0.0, 0.0))
image = np.array([[1, 0, 0], [1, 0, 0], [1, 0, 0]])
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.array([[0, 1, 0], [0, 1, 0], [0, 1, 0]])
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.array([[0, 0, 1], [0, 0, 1], [0, 0, 1]])
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.array([[1, 1, 0], [1, 1, 0], [1, 1, 0]])
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.array([[1, 0, 1], [1, 0, 1], [1, 0, 1]])
self.assertEqual(horizontal_intersections(image), (2.0, 2.0))
image = np.array([[0, 1, 1], [0, 1, 1], [0, 1, 1]])
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.ones(shape=(3, 3), dtype=np.uint8)
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
image = np.eye(3, dtype=np.uint8)
self.assertEqual(horizontal_intersections(image), (1.0, 1.0))
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(horizontal_intersections(image),
self._horizontal_intersections[i])
def test_number_of_loops(self):
'''Tests the number_of_loops function'''
# test edge cases
self.assertEqual(number_of_loops(np.zeros((28, 28), np.uint8)), 0.0)
self.assertEqual(number_of_loops(np.ones((28, 28), np.uint8)), -1.0,
'Expect -1.0 for all 1 since we assume a background.')
# test custom expected values
for digit, image in enumerate(self._images):
img_copy = image.copy()
self.assertEqual(number_of_loops(image),
self._number_of_loops[digit])
self.assertTrue(np.array_equal(image, img_copy),
'expected immutable image')
def test_horizontally_split_symmetry(self):
'''Tests the horizontally_split_symmetry function'''
# test edge cases
image = np.zeros(shape=(4, 4), dtype=np.uint8)
self.assertEqual(horizontally_split_symmetry(image), 0.0)
image = np.ones(shape=(4, 4), dtype=np.uint8)
self.assertEqual(horizontally_split_symmetry(image), 0.0)
image = np.eye(4, dtype=np.uint8)
self.assertEqual(horizontally_split_symmetry(image), 0.5)
image = np.array([[1, 0],
[0, 0]])
self.assertEqual(horizontally_split_symmetry(image), 0.5)
image = np.array([[0, 1],
[0, 0]])
self.assertEqual(horizontally_split_symmetry(image), 0.5)
image = np.array([[0, 0],
[1, 0]])
self.assertEqual(horizontally_split_symmetry(image), 0.5)
image = np.array([[0, 0],
[0, 1]])
self.assertEqual(horizontally_split_symmetry(image), 0.5)
image = np.array([[1, 0],
[0, 1]])
self.assertEqual(horizontally_split_symmetry(image), 1.0)
image = np.array([[0, 1],
[1, 0]])
self.assertEqual(horizontally_split_symmetry(image), 1.0)
image = np.array([[1, 1],
[0, 0]])
self.assertEqual(horizontally_split_symmetry(image), 0.0)
image = np.array([[0, 0],
[1, 1]])
self.assertEqual(horizontally_split_symmetry(image), 0.0)
image = np.array([[1, 0, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 0],
[1, 0, 0, 1]])
self.assertEqual(horizontally_split_symmetry(image), 1.0)
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(horizontally_split_symmetry(image),
self._horizontally_split_symmetries[i])
def test_vertically_split_symmetry(self):
'''Tests the vertically_split_symmetry function'''
# test edge cases
image = np.zeros(shape=(4, 4), dtype=np.uint8)
self.assertEqual(vertically_split_symmetry(image), 0.0)
image = np.ones(shape=(4, 4), dtype=np.uint8)
self.assertEqual(vertically_split_symmetry(image), 0.0)
image = np.eye(4, dtype=np.uint8)
self.assertEqual(vertically_split_symmetry(image), 0.5)
image = np.array([[1, 0],
[0, 0]])
self.assertEqual(vertically_split_symmetry(image), 0.5)
image = np.array([[0, 1],
[0, 0]])
self.assertEqual(vertically_split_symmetry(image), 0.5)
image = np.array([[0, 0],
[1, 0]])
self.assertEqual(vertically_split_symmetry(image), 0.5)
image = np.array([[0, 0],
[0, 1]])
self.assertEqual(vertically_split_symmetry(image), 0.5)
image = np.array([[1, 0],
[0, 1]])
self.assertEqual(vertically_split_symmetry(image), 1.0)
image = np.array([[0, 1],
[1, 0]])
self.assertEqual(vertically_split_symmetry(image), 1.0)
image = np.array([[1, 0],
[1, 0]])
self.assertEqual(vertically_split_symmetry(image), 0.0)
image = np.array([[0, 1],
[0, 1]])
self.assertEqual(vertically_split_symmetry(image), 0.0)
image = np.array([[1, 0, 0, 1],
[0, 1, 1, 0],
[0, 1, 1, 0],
[1, 0, 0, 1]])
self.assertEqual(vertically_split_symmetry(image), 1.0)
# test custom expected values
for i, image in enumerate(self._images):
self.assertEqual(vertically_split_symmetry(image),
self._vertically_split_symmetries[i])
def test_get_image_features(self):
'''Tests the get_image_features function'''
if __name__ == '__main__':
unittest.main()