-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtest_array_data.py
More file actions
73 lines (51 loc) · 2.1 KB
/
test_array_data.py
File metadata and controls
73 lines (51 loc) · 2.1 KB
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
import os
import unittest
import pickle
import numpy as np
import py_utils.array_data as array_data
_path = os.path.dirname(os.path.realpath(__file__))
class TestArray(unittest.TestCase):
_data = array_data.Array(10)
def test_array(self):
self.assertEqual(len(self._data), 10)
self.assertTrue(np.all(self._data.index == np.arange(10)))
def test_getitem(self):
# integer indexing
sub_data = self._data[3]
self.assertEqual(len(sub_data), 1)
self.assertTrue(np.all(sub_data.index == [3]))
# slice indexing
sub_data = self._data[3:5]
self.assertEqual(len(sub_data), 2)
self.assertTrue(np.all(sub_data.index == [3, 4]))
# boolean indexing
mask = np.array([1, 0, 1, 0, 0, 0, 0, 0, 0, 0], dtype=bool)
sub_data = self._data[mask]
self.assertEqual(len(sub_data), 2)
self.assertTrue(np.all(sub_data.index == [0, 2]))
# list indexing (unique)
sub_data = self._data[[3, 5, 7]]
self.assertEqual(len(sub_data), 3)
self.assertTrue(np.all(sub_data.index == [3, 5, 7]))
# list indexing (repeated)
sub_data = self._data[[7, 5, 3, 3, 3]]
self.assertEqual(len(sub_data), 3)
self.assertTrue(np.all(sub_data.index == [3, 5, 7]))
# numpy indexing (unique)
sub_data = self._data[np.array([3, 5, 7])]
self.assertEqual(len(sub_data), 3)
self.assertTrue(np.all(sub_data.index == [3, 5, 7]))
# numpy indexing (repeated)
sub_data = self._data[np.array([7, 5, 3, 3, 3])]
self.assertEqual(len(sub_data), 3)
self.assertTrue(np.all(sub_data.index == [3, 5, 7]))
def test_pickle(self):
with open(os.path.join(_path, "test.pkl"), "wb") as f:
pickle.dump(self._data, f)
with open(os.path.join(_path, "test.pkl"), "rb") as f:
foo = pickle.load(f)
self.assertEqual(len(foo), len(self._data))
self.assertTrue(np.all(foo.index == self._data.index))
os.remove(os.path.join(_path, "test.pkl"))
if __name__ == "__main__":
unittest.main()