1
2
3
4 import unittest
5 import numpy as np
6 from ..core.index_operations import ParameterIndexOperations,\
7 ParameterIndexOperationsView
8
9 one, two, three = 'one', 'two', 'three'
10
11 -class Test(unittest.TestCase):
12
14 self.param_index = ParameterIndexOperations()
15 self.param_index.add(one, [3,9])
16 self.param_index.add(two, [0,5])
17 self.param_index.add(three, [2,4,7,10])
18 self.view = ParameterIndexOperationsView(self.param_index, 2, 6)
19
21 self.param_index.clear()
22 self.assertDictEqual(self.param_index._properties, {})
23
25 removed = self.param_index.remove(three, np.r_[3:13])
26 self.assertListEqual(removed.tolist(), [4,7,10])
27 self.assertListEqual(self.param_index[three].tolist(), [2])
28 removed = self.param_index.remove(one, [1])
29 self.assertListEqual(removed.tolist(), [])
30 self.assertListEqual(self.param_index[one].tolist(), [3,9])
31 self.assertListEqual(self.param_index.remove('not in there', []).tolist(), [])
32 removed = self.param_index.remove(one, [9])
33 self.assertListEqual(removed.tolist(), [9])
34 self.assertListEqual(self.param_index[one].tolist(), [3])
35 self.assertListEqual(self.param_index.remove('not in there', [2,3,4]).tolist(), [])
36 self.assertListEqual(self.view.remove('not in there', [2,3,4]).tolist(), [])
37
39 self.view.shift_left(0, 2)
40 self.assertListEqual(self.param_index[three].tolist(), [2,5,8])
41 self.assertListEqual(self.param_index[two].tolist(), [0,3])
42 self.assertListEqual(self.param_index[one].tolist(), [7])
43
44
45
46
47
48
49
50 self.assertListEqual(self.view[three].tolist(), [0,3])
51 self.assertListEqual(self.view[two].tolist(), [1])
52 self.assertListEqual(self.view[one].tolist(), [5])
53 self.param_index.shift_left(7, 1)
54
55
56
57
58
59
60
61 self.assertListEqual(self.param_index[three].tolist(), [2,5,7])
62 self.assertListEqual(self.param_index[two].tolist(), [0,3])
63 self.assertListEqual(self.param_index[one].tolist(), [])
64 self.assertListEqual(self.view[three].tolist(), [0,3,5])
65 self.assertListEqual(self.view[two].tolist(), [1])
66 self.assertListEqual(self.view[one].tolist(), [])
67
68 self.param_index.shift_left(9, 1)
69 self.assertListEqual(self.param_index[three].tolist(), [2,5,7])
70 self.assertListEqual(self.param_index[two].tolist(), [0,3])
71 self.assertListEqual(self.param_index[one].tolist(), [])
72 self.assertListEqual(self.view[three].tolist(), [0,3,5])
73 self.assertListEqual(self.view[two].tolist(), [1])
74 self.assertListEqual(self.view[one].tolist(), [])
75
76
78 self.view.shift_right(3, 2)
79 self.assertListEqual(self.param_index[three].tolist(), [2,4,9,12])
80 self.assertListEqual(self.param_index[two].tolist(), [0,7])
81 self.assertListEqual(self.param_index[one].tolist(), [3,11])
82
84
85
86
87
88
89
90
91 self.view = ParameterIndexOperationsView(self.param_index, 2, 6)
92 self.assertSetEqual(set(self.view.properties()), set([one, two, three]))
93 for v,p in zip(self.view.properties_for(np.r_[:6]), self.param_index.properties_for(np.r_[2:2+6])):
94 self.assertEqual(v, p)
95 self.assertSetEqual(set(self.view[two]), set([3]))
96 self.assertSetEqual(set(self.param_index[two]), set([0, 5]))
97 self.view.add(two, np.array([0]))
98 self.assertSetEqual(set(self.view[two]), set([0,3]))
99 self.assertSetEqual(set(self.param_index[two]), set([0, 2, 5]))
100 self.view.clear()
101 for v,p in zip(self.view.properties_for(np.r_[:6]), self.param_index.properties_for(np.r_[2:2+6])):
102 self.assertEqual(v, p)
103 self.assertEqual(v, [])
104 param_index = ParameterIndexOperations()
105 param_index.add(one, [3,9])
106 param_index.add(two, [0,5])
107 param_index.add(three, [2,4,7,10])
108 view2 = ParameterIndexOperationsView(param_index, 2, 8)
109 self.view.update(view2)
110 for [i,v],[i2,v2] in zip(sorted(param_index.items()), sorted(self.param_index.items())):
111 self.assertEqual(i, i2)
112 np.testing.assert_equal(v, v2)
113
125
127 removed = self.view.remove(two, [3])
128 self.assertListEqual(removed.tolist(), [3])
129 removed = self.view.remove(three, np.r_[:5])
130 self.assertListEqual(removed.tolist(), [0, 2])
131
133
134
135 for k,v in self.param_index.copy()._properties.items():
136 self.assertListEqual(self.param_index[k].tolist(), v.tolist())
137 self.assertEqual(self.param_index.size, 8)
138 self.assertEqual(self.view.size, 5)
139
141 self.param_index.add(two, [1,10])
142 self.assertDictEqual(
143 dict([(k, d.tolist()) for k, d in self.param_index.properties_dict_for([1,3,5,10]).items()]),
144 {one:[3], two:[1,5,10], three:[10]}
145 )
146 view = ParameterIndexOperationsView(self.param_index, 2, 8)
147 self.assertDictEqual(
148 dict([(k, d.tolist()) for k, d in view.properties_dict_for([1,3,5]).items()]),
149 {one:[1], two:[3], three:[5]}
150 )
151
153 print(self.param_index)
154 print(self.view)
155
156 if __name__ == "__main__":
157
158 unittest.main()
159