This repository has been archived by the owner on Feb 5, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathunittests.py
288 lines (214 loc) · 8.67 KB
/
unittests.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
import sys
sys.path.append("libs")
import os
from math import *
try:
from collections import OrderedDict
except ImportError:
print "Python 2.7+ OrderedDict collection not available"
try:
from ordereddict import OrderedDict
print "Using backported OrderedDict implementation"
except ImportError:
print "Backported OrderedDict implementation not available"
import itertools
import argparse
import unittest
if sys.version_info[0] == 2 and sys.version_info[1] < 7:
import unittest2
else:
unittest2 = unittest
# probably won't work w/ Python 3.0 / 3.1, tested on Python 3.2 and 2.7
import unittest
from definitions import *
from mapclass import Map2
sys.path.append("old")
# Make sets of strings to compare them because oldMapClass might
# return less elements than MapClass2
#
# We use strings and not floats because the accuracy for floats is not
# very good and there are some discrepancies between the two versions
# after the 10th decimal
class TestExtended:
xyzd = XYZD[:-2]
sf = "%.8e"
compare = False
correlation = False
gaussian = False
### Example values
betx = 66.14532014
bety = 17.92472388
gamma = 3e6
ex = 68e-8
ey = 2e-8
sigmaFFS = [sqrt(ex*betx/gamma), sqrt(ex/betx/gamma), sqrt(ey*bety/gamma), sqrt(ey/bety/gamma), 0.01]
###
def assertAlmostEq(self, a, b):
self.assertEqual(self.sf % a, self.sf % b)
def testEqual(self):
res1 = set([self.sf % e[()] for e in self.m(self.vals).values()])
res2 = set([self.sf % e for e in self.mm.f(self.vals.values())])
self.assertTrue(res1.issuperset(res2))
def testSigma(self):
for i in self.xyzd:
self.assertAlmostEq(self.m.sigma(i,self.sigmaFFS,self.gaussian), self.mm.sigma(i,self.sigmaFFS))
def testOffset(self):
for i in self.xyzd:
self.assertAlmostEq(self.m.offset(i,self.sigmaFFS,self.gaussian), self.mm.offset(i,self.sigmaFFS))
def testCorrelation(self):
if self.correlation:
for [v1, v2] in itertools.combinations(self.xyzd, 2):
self.assertAlmostEq(self.m.correlation(v1,v2,self.sigmaFFS,self.gaussian), self.mm.correlation(v1,v2,self.sigmaFFS))
@unittest2.skipUnless('-s' in sys.argv, "Slow test")
def testCorrelation3(self):
if self.correlation:
for [v1, v2, v3] in itertools.combinations(self.xyzd, 3):
self.assertAlmostEq(self.m.correlation3(v1,v2,v3,self.sigmaFFS,self.gaussian), self.mm.correlation3(v1,v2,v3,self.sigmaFFS))
def testComp(self):
if self.compare:
self.assertAlmostEq(self.m.comp(self.m2,v=self.xyzd), self.mm.comp(self.mm2))
def testCompc(self):
if self.compare:
self.assertAlmostEq(self.m.compc(self.m2,v=self.xyzd), self.mm.compc(self.mm2))
def testGenList(self):
for v in self.xyzd:
self.mm.generatelist(v, self.sigmaFFS)
for (l1,l2) in zip(self.m.generatelist(v, self.sigmaFFS, self.gaussian), getattr(self.mm, 'list' + v)):
self.assertAlmostEq(l1[0],l2[0])
# Because the order in which the indices are stored isn't the
# same the result isn't equal but equivalent.
# E.g.
# l1[2:] = (1,2,3,0,1,0) equal l2[2:] = (1,2,3,0,1,0)
# l1[2:] = (1,2,3,0,1,0) equivalent l2[2:] = (0,1,0,1,2,3)
ind1 = l1[2:]
ind2 = l2[2:]
l = len(ind1)/2
self.assertTrue((ind1[:l] == ind2[:l] or ind1[:l] == ind2[l:]) and (ind1[l:] == ind2[:l] or ind1[l:] == ind2[l:]))
################
## Test cases ##
################
class Test5var6order(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclass25 import Map
o = 6
f = 'assets/fort.18'
ff = 'assets/5fort.18'
self.compare = True
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
self.m2 = Map2(order=o, filename=ff)
self.mm2 = Map(order=o, filename=ff)
class Test5var6orderGaussian(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclassGaussianDelta25 import Map
o = 6
f = 'assets/fort.18'
self.gaussian = True
self.correlation = True
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
class Test5var10order(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclass25 import Map
o = 10
f = 'assets/fort.18'
ff = 'assets/5fort.18'
self.compare = True
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
self.m2 = Map2(order=o, filename=ff)
self.mm2 = Map(order=o, filename=ff)
class Test5var10orderGaussian(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclassGaussianDelta25 import Map
o = 10
f = 'assets/fort.18'
self.gaussian = True
self.correlation = True
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',0)])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
class Test6var6order(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclass25_6var import Map
self.sigmaFFS = [0.000227, 9.306e-5, 5.02e-4, 4.21e-5, 0.00666, 0.002]
o = 6
f = 'assets/6Dfort.18'
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',self.sigmaFFS[5])])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
@unittest2.skip("Unnecessary")
def testGenList(self):
# generatelist isn't clear in mapclass25_6var so override this
# test to avoid running it
return
class Test6var10order(unittest2.TestCase, TestExtended):
def setUp(self):
from mapclass25_6var import Map
self.sigmaFFS = [0.000227, 9.306e-5, 5.02e-4, 4.21e-5, 0.00666, 0.002]
o = 10
f = 'assets/6Dfort.18'
self.vals = OrderedDict([('x',self.sigmaFFS[0]),('px',self.sigmaFFS[1]),('y',self.sigmaFFS[2]),('py',self.sigmaFFS[3]),('d',self.sigmaFFS[4]),('s',self.sigmaFFS[5])])
self.m = Map2(order=o, filename=f)
self.mm = Map(order=o, filename=f)
@unittest2.skip("Unnecessary")
def testGenList(self):
# generatelist isn't clear in mapclass25_6var so override this
# test to avoid running it
return
########################
## Twiss
########################
class TwissExtended:
xyzd = XYZD[:-2]
def compareFortTwiss(self):
self.assertTrue(self.m.compc(self.mm, self.xyzd).real < 0.1)
def testBeta(self):
for i in range(len(self.t.elems)):
e = self.t.elems[i]
if e.L != 0:
self.assertEqual(e.BETX, self.t.getBeta(i, e.L).BETX)
self.assertEqual(e.BETY, self.t.getBeta(i, e.L).BETY)
self.assertEqual(e.ALFX, self.t.getBeta(i, e.L).ALFX)
self.assertEqual(e.ALFY, self.t.getBeta(i, e.L).ALFY)
def testDisp(self):
for i in range(len(self.t.elems)):
e = self.t.elems[i]
self.assertEqual(e.DX, self.t.getDisp(i, e.L).DX)
self.assertEqual(e.DY, self.t.getDisp(i, e.L).DY)
self.assertEqual(e.DPX, self.t.getDisp(i, e.L).DPX)
self.assertEqual(e.DPY, self.t.getDisp(i, e.L).DPY)
class TestElems(unittest2.TestCase, TwissExtended):
def setUp(self):
from metaclass2 import twiss2
for root, subFolders, files in os.walk('assets/twiss/'):
if 'twiss' in files and 'fort.18' in files:
print "\nTesting: ", root
twissfile = os.path.join(root, "twiss")
fortfile = os.path.join(root, "fort.18")
self.t = twiss2(twissfile)
self.m = Map2(self.t)
self.mm = Map2(filename=fortfile)
def twissSuite():
suite = unittest2.TestSuite()
suite.addTest(TestElems("compareFortTwiss"))
suite.addTest(TestElems("testBeta"))
suite.addTest(TestElems("testDisp"))
return suite
####################
## Main
####################
if __name__ == '__main__':
parser = argparse.ArgumentParser(description='Tester for MapClass')
parser.add_argument('-s', help='Run the slow tests as well', action='store_true', dest='slow')
parser.add_argument('-t', help="Test the import from Twiss and compare with fort.18. It doesn't run any of the other tests.", action='store_true', dest='twiss')
args = parser.parse_args()
if args.twiss:
runner = unittest2.TextTestRunner(verbosity=2)
test_suite = twissSuite()
runner.run(test_suite)
else:
unittest2.main(verbosity=2, argv=sys.argv[:1])