-
Notifications
You must be signed in to change notification settings - Fork 8
/
test_arff_helper.py
63 lines (46 loc) · 1.85 KB
/
test_arff_helper.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
#!/usr/bin/env python
import unittest
import os
import numpy as np
import arff
from sp_tool.arff_helper import ArffHelper
class ArffHelperTest(unittest.TestCase):
def test_load(self):
a = ArffHelper.load(open('test_data/arff_data_example.arff'))
self.assertEqual(a['metadata']['distance_mm'], 450.0)
del(a['metadata'])
b = arff.load(open('test_data/arff_data_example.arff'))
del (a['description'])
del (b['description'])
np.testing.assert_almost_equal(a['data'].tolist(), b['data'], 2)
# tested data equality, can now delete it
del (a['data'])
del (b['data'])
self.assertEqual(a, b)
def test_dump(self):
a = ArffHelper.load(open('test_data/arff_data_example.arff'))
# close the returned file handle after dump has been completed
ArffHelper.dump(a, open('test_data/test_dump.arff', 'w')).close()
b = arff.load(open('test_data/test_dump.arff'))
c = arff.load(open('test_data/arff_data_example.arff'))
del (a['metadata'])
del (a['description'])
del (b['description'])
del (c['description'])
np.testing.assert_almost_equal(a['data'].tolist(), b['data'], 2)
np.testing.assert_almost_equal(a['data'].tolist(), c['data'], 2)
del (a['data'])
del (b['data'])
del (c['data'])
self.assertEqual(a, b)
self.assertEqual(a, c)
os.remove('test_data/test_dump.arff')
def test_add_column(self):
name = 'EM_type'
dtype = ['UNKNOWN', 'FIX', 'SACCADE', 'SP', 'NOISE']
a = ArffHelper.load(open('test_data/arff_data_example.arff'))
b = ArffHelper.add_column(a, name, dtype, 'UNKNOWN')
a['attributes'].append((name, dtype))
self.assertEqual(a['attributes'], b['attributes'])
if __name__ == '__main__':
unittest.main()