~madteam/mg5amcnlo/series2.0

« back to all changes in this revision

Viewing changes to tests/unit_tests/various/test_banner.py

  • Committer: olivier Mattelaer
  • Date: 2015-03-05 00:14:16 UTC
  • mfrom: (258.1.9 2.3)
  • mto: (258.8.1 2.3)
  • mto: This revision was merged to the branch mainline in revision 259.
  • Revision ID: olivier.mattelaer@uclouvain.be-20150305001416-y9mzeykfzwnl9t0j
partial merge

Show diffs side-by-side

added added

removed removed

Lines of Context:
17
17
import unittest
18
18
import tempfile
19
19
import madgraph.various.banner as bannermod
 
20
import madgraph.various.misc as misc
20
21
import os
21
22
import models
 
23
from madgraph import MG5DIR
 
24
 
 
25
import StringIO
22
26
 
23
27
_file_path = os.path.split(os.path.dirname(os.path.realpath(__file__)))[0]
24
28
 
67
71
        # access element of the card
68
72
        self.assertRaises(KeyError, mybanner.get, 'param_card', 'mt')
69
73
        self.assertEqual(mybanner.get('param_card', 'mass', 6).value, 175.0)
70
 
        self.assertEqual(mybanner.get('run_card', 'lpp1'), '0')
71
 
        
72
 
        
73
 
        
 
74
        self.assertEqual(mybanner.get('run_card', 'lpp1'), 0)
 
75
        
 
76
 
 
77
 
 
78
class TestConfigFileCase(unittest.TestCase):
 
79
    """ A class to test the TestConfig functionality """
 
80
    # a lot of the funtionality are actually already tested in the child
 
81
    # TESTMadLoopParam and are not repeated here.
 
82
     
 
83
    def setUp(self):
 
84
        
 
85
        self.config = bannermod.ConfigFile()
 
86
        self.config.add_param('lower', 1)
 
87
        self.config.add_param('UPPER', 1)
 
88
        assert self.config.__dict__
 
89
   
 
90
    def test_sum_object(self):
 
91
        """ check for the case handling only #more test in TESTMadLoopParam """
 
92
        
 
93
        self.assertEqual(self.config.lower_to_case, {"lower":"lower", "upper":"UPPER"})
 
94
 
 
95
        # add a dictionary
 
96
        a = {'lower2':2, 'UPPER2':2, 'Mixed':2} 
 
97
        config2 = self.config + a
 
98
        
 
99
        #ensure that config is not change
 
100
        self.assertEqual(len(self.config),2)
 
101
        self.assertEqual(self.config.lower_to_case, {"lower":"lower", "upper":"UPPER"})
 
102
 
 
103
        self.assertEqual(type(config2), bannermod.ConfigFile)
 
104
        self.assertFalse(dict.__contains__(config2, 'UPPER2'))
 
105
        self.assertTrue('UPPER2' in config2)
 
106
        
 
107
        # from a dictionary add a config file
 
108
        config3 = a + self.config
 
109
        self.assertTrue(not hasattr(config3, 'lower_to_dict'))
 
110
        self.assertEqual(type(config3), dict)
 
111
        self.assertTrue(dict.__contains__(config3, 'UPPER2'))
 
112
        self.assertTrue(config3.__contains__('UPPER2'))        
 
113
        self.assertTrue(dict.__contains__(config3, 'UPPER'))
 
114
        self.assertTrue(config3.__contains__('UPPER'))
 
115
          
 
116
        
 
117
    def test_for_loop(self):
 
118
        """ check correct handling of case"""
 
119
    
 
120
        keys = []
 
121
        for key in self.config:
 
122
            keys.append(key)
 
123
        self.assertEqual(set(keys), set(self.config.keys()))
 
124
        self.assertTrue('upper' not in keys)
 
125
        self.assertTrue('UPPER' in keys)
 
126
    
 
127
#    def test_in(self):
 
128
#        """actually tested in sum_object"""
 
129
#       
 
130
#    def test_update(self):
 
131
#        """actually tested in sum_object"""
 
132
 
 
133
 
 
134
class TestRunCard(unittest.TestCase):
 
135
    """ A class to test the TestConfig functionality """
 
136
    # a lot of the funtionality are actually already tested in the child
 
137
    # TESTMadLoopParam and are not repeated here.
 
138
     
 
139
        
 
140
    def test_basic(self):
 
141
        """ """
 
142
        
 
143
        # check the class factory works        
 
144
        run_card = bannermod.RunCard()
 
145
        self.assertTrue(isinstance(run_card, bannermod.RunCard))
 
146
        self.assertTrue(isinstance(run_card, bannermod.RunCardLO))
 
147
        self.assertFalse(isinstance(run_card, bannermod.RunCardNLO))
 
148
        
 
149
        path = pjoin(_file_path, '..', 'input_files', 'run_card_matching.dat')
 
150
        run_card = bannermod.RunCard(path)
 
151
        self.assertTrue(isinstance(run_card, bannermod.RunCard))
 
152
        self.assertTrue(isinstance(run_card, bannermod.RunCardLO))
 
153
        self.assertFalse(isinstance(run_card, bannermod.RunCardNLO))        
 
154
        
 
155
        path = pjoin(_file_path,'..', 'input_files', 'run_card_nlo.dat')
 
156
        run_card = bannermod.RunCard(path)
 
157
        self.assertTrue(isinstance(run_card, bannermod.RunCard))
 
158
        self.assertTrue(isinstance(run_card, bannermod.RunCardNLO))
 
159
        self.assertFalse(isinstance(run_card, bannermod.RunCardLO))         
 
160
        
 
161
        #check the copy
 
162
        run_card2 = bannermod.RunCard(run_card)
 
163
        self.assertTrue(isinstance(run_card, bannermod.RunCard))
 
164
        self.assertTrue(isinstance(run_card, bannermod.RunCardNLO))
 
165
        self.assertFalse(isinstance(run_card, bannermod.RunCardLO))         
 
166
        #check all list/dict are define
 
167
        self.assertTrue(hasattr(run_card2, 'user_set'))
 
168
        self.assertTrue(hasattr(run_card2, 'hidden_param'))
 
169
        self.assertTrue(hasattr(run_card2, 'not_in_include')) 
 
170
        self.assertTrue(hasattr(run_card2, 'fortran_name'))
 
171
        self.assertFalse(hasattr(run_card2, 'default'))
 
172
        self.assertTrue(hasattr(run_card2, 'cuts_parameter'))         
 
173
  
 
174
    def test_default(self):
 
175
      
 
176
        run_card = bannermod.RunCard()
 
177
        fsock = tempfile.NamedTemporaryFile(mode = 'w')
 
178
        run_card.write(fsock)
 
179
      
 
180
        run_card2 = bannermod.RunCard(fsock.name)
 
181
      
 
182
        for key in run_card:
 
183
            self.assertEqual(run_card[key], run_card2[key])
 
184
      
 
185
        run_card = bannermod.RunCardNLO()
 
186
        fsock = tempfile.NamedTemporaryFile(mode = 'w')
 
187
        run_card.write(fsock)
 
188
      
 
189
        run_card2 = bannermod.RunCard(fsock.name)
 
190
      
 
191
        for key in run_card:
 
192
            self.assertEqual(run_card[key], run_card2[key])      
 
193
 
 
194
MadLoopParam = bannermod.MadLoopParam
 
195
class TESTMadLoopParam(unittest.TestCase):
 
196
    """ A class to test the MadLoopParam functionality """
 
197
    
 
198
    
 
199
    def test_initMadLoopParam(self):
 
200
        """check that we can initialize a file"""
 
201
        
 
202
        #1. create the object without argument and the default file
 
203
        param1 = MadLoopParam()
 
204
        param2 = MadLoopParam(pjoin(MG5DIR,"Template", "loop_material","StandAlone",
 
205
                                      "Cards","MadLoopParams.dat"))
 
206
        
 
207
        #2. check that they are all equivalent
 
208
        self.assertEqual(param2.user_set, set())
 
209
        self.assertEqual(param1.user_set, set())
 
210
        for key, value1 in param1.items():
 
211
            self.assertEqual(value1, param2[key])
 
212
        
 
213
        #3. check that all the Default value in the file MadLoopParams.dat
 
214
        #   are coherent with the default in python
 
215
        
 
216
        fsock = open(pjoin(MG5DIR,"Template", "loop_material","StandAlone",
 
217
                                      "Cards","MadLoopParams.dat"))
 
218
        previous_line = ["", ""]
 
219
        for line in fsock:
 
220
            if previous_line[0].startswith('#'):
 
221
                name = previous_line[0][1:].strip()
 
222
                self.assertIn('default', line.lower())
 
223
                value = line.split('::')[1].strip()
 
224
                param2[name] = value # do this such that the formatting is done
 
225
                self.assertEqual(param1[name], param2[name])
 
226
                self.assertTrue(previous_line[1].startswith('!'))
 
227
            previous_line = [previous_line[1], line]
 
228
            
 
229
    def test_modifparameter(self):
 
230
        """ test that we can modify the parameter and that the formating is applied 
 
231
        correctly """
 
232
 
 
233
        #1. create the object without argument
 
234
        param1 = MadLoopParam()
 
235
 
 
236
        to_test = {"MLReductionLib": {'correct': ['1|2', ' 1|2 '],
 
237
                                      'wrong':[1/2, 0.3, True],
 
238
                                      'target': ['1|2', '1|2']},
 
239
                   "IREGIMODE": {'correct' : [1.0, 2, 3, -1, '1.0', '2', '-3', '-3.0'],
 
240
                                 'wrong' : ['1.5', '-1.5', 1.5, -3.4, True, 'starwars'],
 
241
                                 'target': [1,2,3,-1,1,2,-3,-3]
 
242
                                  },
 
243
                   "IREGIRECY": {'correct' : [True, False, 0, 1, '0', '1',
 
244
                                                '.true.', '.false.','T', 
 
245
                                                  'F', 'true', 'false', 'True \n'],
 
246
                                 'wrong' : ['a', [], 5, 66, {}, None, -1],
 
247
                                 "target": [True, False, False, True, False, True, 
 
248
                                            True, False,True, False,True,False, True]},
 
249
                   "CTStabThres": {'correct': [1.0, 1e-3, 1+0j, 1,"1d-3", "1e-3"],
 
250
                                   'wrong': [True, 'hello'],
 
251
                                   'target': [1.0,1e-3, 1.0, 1.0, 1e-3, 1e-3]}
 
252
                   }
 
253
 
 
254
        import madgraph.various.misc as misc
 
255
        for name, data in to_test.items():
 
256
            for i,value in enumerate(data['correct']):
 
257
                param1[name] = value
 
258
                self.assertEqual(param1[name],  data['target'][i])
 
259
                self.assertTrue(name.lower() not in param1.user_set)
 
260
                self.assertEqual(type(data['target'][i]), type(param1[name]))
 
261
            for value in data['wrong']:
 
262
                self.assertRaises(Exception, param1.__setitem__, (name, value))
 
263
                
 
264
    def test_writeMLparam(self):
 
265
        """check that the writting is correct"""
 
266
        
 
267
        param1 = MadLoopParam(pjoin(MG5DIR,"Template", "loop_material","StandAlone",
 
268
                                      "Cards","MadLoopParams.dat"))
 
269
        
 
270
        textio = StringIO.StringIO()
 
271
        param1.write(textio)
 
272
        text=textio.getvalue()
 
273
        
 
274
        #read the data.
 
275
        param2=MadLoopParam(text)
 
276
        
 
277
        #check that they are correct
 
278
        for key, value in param1.items():
 
279
            self.assertEqual(value, param2[key])
 
280
            self.assertTrue(key.lower() in param2.user_set)
 
281
            
 
282
    def test_sum_object(self):
 
283
        
 
284
        param1 = MadLoopParam(pjoin(MG5DIR,"Template", "loop_material","StandAlone",
 
285
                                      "Cards","MadLoopParams.dat"))
 
286
 
 
287
 
 
288
        new = {'test': 1, 'value': 'data----------------------------------'}
 
289
 
 
290
        ########################################################################
 
291
        # 1. simple sum all key different
 
292
        ########################################################################        
 
293
        param2 = param1 + new
 
294
 
 
295
        self.assertTrue(isinstance(param2, MadLoopParam))
 
296
        self.assertTrue(isinstance(param2, dict))
 
297
        self.assertNotEqual(id(param1), id(param2))
 
298
        
 
299
        #check that they are correct
 
300
        for key, value in param1.items():
 
301
            self.assertEqual(value, param2[key])
 
302
            self.assertFalse(key.lower() in param2.user_set)        
 
303
        for key, value in new.items():
 
304
            self.assertEqual(value, param2[key])
 
305
            self.assertFalse(key.lower() in param2.user_set)  
 
306
        self.assertTrue('test' not in param1)
 
307
                   
 
308
        
 
309
        
 
310
        ########################################################################
 
311
        # 2. add same key in both term
 
312
        ########################################################################
 
313
        new = {'test': 1, 'value': 'data', 'CTLoopLibrary':1}
 
314
        param2 = param1 + new
 
315
        #check that they are correct
 
316
        for key, value in param1.items():
 
317
            if key != 'CTLoopLibrary':
 
318
                self.assertEqual(value, param2[key])
 
319
                self.assertFalse(key.lower() in param2.user_set)   
 
320
                     
 
321
        for key, value in new.items():
 
322
            self.assertEqual(value, param2[key])
 
323
            self.assertFalse(key.lower() in param2.user_set)   
 
324
            
 
325
            
 
326
        ########################################################################
 
327
        # 3. reverse order
 
328
        ########################################################################
 
329
        param2 = new + param1   
 
330
        
 
331
        #check sanity
 
332
        self.assertFalse(isinstance(param2, MadLoopParam))
 
333
        self.assertTrue(isinstance(param2, dict))
 
334
        self.assertNotEqual(id(new), id(param2))
 
335
        self.assertNotEqual(id(param1), id(param2))
 
336
        
 
337
        #check that value are correct
 
338
        for key, value in param1.items():
 
339
                self.assertEqual(value, param2[key])        
 
340
        for key, value in new.items():
 
341
            if key != 'CTLoopLibrary':
 
342
                self.assertEqual(value, param2[key])
 
343
            
 
344
                
 
345
        
 
346
        
 
347
        
 
348
        
 
349
        
 
350
        
 
351
        
 
352
        
 
353
        
 
354
        
 
355
            
 
356
                
 
357
 
 
358
 
 
359
 
 
360
 
 
361
 
 
362
 
 
363
 
 
364
 
 
365
 
 
366
 
 
367
 
 
368
 
 
369
 
 
370
 
 
371
 
 
372
 
 
373
 
 
374
        
 
375
 
 
376
        
 
377
            
74
378
        
75
379
        
76
380