Package paramz :: Package tests :: Module index_operations_tests
[hide private]
[frames] | no frames]

Source Code for Module paramz.tests.index_operations_tests

  1  # Copyright (c) 2014, Max Zwiessele 
  2  # Licensed under the BSD 3-clause license (see LICENSE.txt) 
  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
13 - def setUp(self):
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
20 - def test_clear(self):
21 self.param_index.clear() 22 self.assertDictEqual(self.param_index._properties, {})
23
24 - def test_remove(self):
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
38 - def test_shift_left(self):
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 # 0 1 2 3 4 5 6 7 8 9 10 45 # one 46 # two two 47 # three three three 48 # view: [0 1 2 3 4 5 ] 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 # 0 1 2 3 4 5 6 7 8 9 10 56 # 57 # two two 58 # three three three 59 # view: [0 1 2 3 4 5 ] 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) # Should have done nothing: 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
77 - def test_shift_right(self):
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
83 - def test_index_view(self):
84 #======================================================================= 85 # 0 1 2 3 4 5 6 7 8 9 10 86 # one one 87 # two two 88 # three three three three 89 # view: [0 1 2 3 4 5 ] 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
114 - def test_view_of_view(self):
115 #======================================================================= 116 # 0 1 2 3 4 5 6 7 8 9 10 117 # one one 118 # two two 119 # three three three three 120 # view: [0 1 2 3 4 5 ] 121 # view2: [0 1 2 3 4 5 ] 122 #======================================================================= 123 view2 = ParameterIndexOperationsView(self.view, 2, 6) 124 view2.shift_right(0, 2)
125
126 - def test_indexview_remove(self):
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
132 - def test_misc(self):
133 #py3 fix 134 #for k,v in self.param_index.copy()._properties.iteritems(): 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
140 - def test_index_conversions(self):
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
152 - def test_print(self):
153 print(self.param_index) 154 print(self.view)
155 156 if __name__ == "__main__": 157 #import sys;sys.argv = ['', 'Test.test_index_view'] 158 unittest.main() 159