def test_parse_info_str_correct(self): "Test parse_info_str converts info strings to dictionaries" mystr = "param1 = value1\n param2=value 2\n \n" rightdict = {'param1':'value1', 'param2':'value 2'} self.assertEqual(rightdict, misc.get_pkg_info(mystr))
def __init__(self, banner_path=None): """ """ dict.__init__(self) #Look at the version if MADEVENT: self['mgversion'] = '#%s\n' % open(pjoin(MEDIR, 'MGMEVersion.txt')).read() else: info = misc.get_pkg_info() self['mgversion'] = info['version']+'\n' if banner_path: self.read_banner(banner_path)
def import_model(model_path, mgme_dir=MG4DIR, absolute=True): """create a model from a MG4 model directory.""" # Check for a valid directory model_path_old = model_path model_path = find_model_path(model_path, mgme_dir, absolute) files_list = [os.path.join(model_path, 'particles.dat'),\ os.path.join(model_path, 'interactions.dat')] for filepath in files_list: if not os.path.isfile(filepath): if not absolute: raise InvalidCmd, "%s directory is not a valid v4 model" % \ (model_path) else: return import_model(model_path_old, mgme_dir, False) # use pickle files if defined if files.is_uptodate(os.path.join(model_path, 'model.pkl'), files_list): model = save_load_object.load_from_file( \ os.path.join(model_path, 'model.pkl')) if model.has_key('version_tag') and model.get( 'version_tag') == os.path.realpath(model_path) + str( misc.get_pkg_info()): return model, model_path model = base_objects.Model() model.set('particles',files.read_from_file( \ os.path.join(model_path, 'particles.dat'), read_particles_v4)) model.set('interactions',files.read_from_file( \ os.path.join(model_path, 'interactions.dat'), read_interactions_v4, model['particles'])) model.set('name', os.path.split(model_path)[-1]) # save in a pickle files to fasten future usage if ReadWrite: try: save_load_object.save_to_file( os.path.join(model_path, 'model.pkl'), model) except Exception: logger.warning("fail to write %s. This is perfectly fine will just prevent speed boost in future load of this model" %\ os.path.join(model_path, 'model.pkl')) return model, model_path
def get_mg5_info_lines(self): """Return info lines for MG5, suitable to place at beginning of Python files""" info = misc.get_pkg_info() info_lines = "" if info and info.has_key('version') and info.has_key('date'): info_lines = "# MadGraph5_aMC@NLO v. %s, %s\n" % \ (info['version'], info['date']) info_lines = info_lines + \ " # By the MadGraph5_aMC@NLO Development Team\n" + \ " # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch" else: info_lines = " # by MadGraph5_aMC@NLO\n" + \ " # By the MadGraph5_aMC@NLO Development Team\n" + \ " # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch" return info_lines
def get_mg5_info_lines(self): """Return info lines for MG5, suitable to place at beginning of Python files""" info = misc.get_pkg_info() info_lines = "" if info and info.has_key('version') and info.has_key('date'): info_lines = "# MadGraph 5 v. %s, %s\n" % \ (info['version'], info['date']) info_lines = info_lines + \ " # By the MadGraph Development Team\n" + \ " # Please visit us at https://launchpad.net/madgraph5" else: info_lines = " # by MadGraph 5\n" + \ " # By the MadGraph Development Team\n" + \ " # Please visit us at https://launchpad.net/madgraph5" return info_lines
def __init__(self, banner_path=None): """ """ if isinstance(banner_path, Banner): return dict.__init__(self, banner_path) else: dict.__init__(self) #Look at the version if MADEVENT: self['mgversion'] = '#%s\n' % open(pjoin(MEDIR, 'MGMEVersion.txt')).read() else: info = misc.get_pkg_info() self['mgversion'] = info['version']+'\n' self.lhe_version = None if banner_path: self.read_banner(banner_path)
def import_model(model_path, mgme_dir = MG4DIR, absolute=True): """create a model from a MG4 model directory.""" # Check for a valid directory model_path_old = model_path model_path = find_model_path(model_path, mgme_dir, absolute) files_list = [os.path.join(model_path, 'particles.dat'),\ os.path.join(model_path, 'interactions.dat')] for filepath in files_list: if not os.path.isfile(filepath): if not absolute: raise InvalidCmd, "%s directory is not a valid v4 model" % \ (model_path) else: return import_model(model_path_old, mgme_dir, False) # use pickle files if defined if files.is_uptodate(os.path.join(model_path, 'model.pkl'), files_list): model = save_load_object.load_from_file( \ os.path.join(model_path, 'model.pkl')) if model.has_key('version_tag') and model.get('version_tag') == os.path.realpath(model_path) + str(misc.get_pkg_info()): return model, model_path model = base_objects.Model() model.set('particles',files.read_from_file( \ os.path.join(model_path, 'particles.dat'), read_particles_v4)) model.set('interactions',files.read_from_file( \ os.path.join(model_path, 'interactions.dat'), read_interactions_v4, model['particles'])) model.set('name', os.path.split(model_path)[-1]) # save in a pickle files to fasten future usage if ReadWrite: try: save_load_object.save_to_file(os.path.join(model_path, 'model.pkl'), model) except Exception: logger.warning("fail to write %s. This is perfectly fine will just prevent speed boost in future load of this model" %\ os.path.join(model_path, 'model.pkl')) return model, model_path
def get_MadLoop_Banner(cls, style='classic', color='blue', top_frame_char = '=', bottom_frame_char = '=', left_frame_char = '{',right_frame_char = '}', print_frame=True, side_margin = 7, up_margin = 1): """ Writes out MadLoop banner.""" colors = {'black':30,'red':31,'green':32,'yellow':33, 'blue':34,'magenta':35,'cyan':36,'lightred':91,'lightgreen':92, 'lightyellow':93,'lightblue':94,'lightmagenta':95,'lightcyan':96, 'white':97,'none':-1} if style.lower()=='random': color = random.choice(['blue','green','red']) reference = "Ref: arXiv:1103.0621v2, arXiv:1405.0301" version = "v%(version)s (%(date)s)"%misc.get_pkg_info() versionref = "%s, %s"%(version,reference) if style.lower() not in cls.get_style_keys()+['random']: raise MadGraph5Error('Incorrect style in MadLoopBanner. Must be'+\ ' one of the following: %s'%str(cls.get_style_keys()+['random'])) if isinstance(color,int): color_start ="char(27)//'[%im"%int color_end = "char(27)//'[0m" elif color.lower() in colors: if color.lower()=='none': color_start = "" color_end = "" else: color_start ="char(27)//'[%im"%colors[color.lower()] color_end = "char(27)//'[0m" else: raise MadGraph5Error('Incorrect color in MadLoopBanner. Must be and'+\ ' intenger or one of the following: %s'%str(list(colors.keys()))) def format_banner(banner): """ Format the raw banner text to give it a frame, colors and a margin.""" def fw(*args): """Fortran write line""" elems = [] for arg in args: if arg.startswith('char('): elems.append("%s'"%arg) continue # Hard-set the single and double quotes in the text to # make sure it is not processed by the FileWriter. arg = arg.replace("'","'//char(39)//'") arg = arg.replace('"',"'//char(34)//'") if len(arg)>0: elems.append("'%s'"%arg) return "write(*,*) %s"%("//".join(elems)) banner_lines = banner.split('\n') formatted_lines = [] # Determine the target width width = side_margin*2 + max(len(line) for line in banner_lines) if print_frame: width += 2 # Print the upper frame if print_frame: formatted_lines.append(fw(" %s "%(top_frame_char*(width-2)))) # Print the upper margin for i in range(up_margin): formatted_lines.append(fw("%(lside)s%(width)s%(rside)s"% {'lside':left_frame_char if print_frame else '', 'rside':right_frame_char if print_frame else '', 'width':' '*(width-2)})) # Now print the banner for line in banner_lines: line_elements = [] line_elements.append((left_frame_char if print_frame else '')+' '*side_margin) # Colorize the logo line_elements.append(color_start) # Make sure to write the reference in black found = False for tag in [versionref, reference, version]: if tag in line: line_elements.extend([line[:line.index(tag)], color_end,tag,color_start, line[line.index(tag)+len(tag):]+ ' '*(width-2*(side_margin+1)-len(line))]) found = True break if not found: line_elements.append(line+ ' '*(width-2*(side_margin+1)-len(line))) line_elements.append(color_end) line_elements.append(' '*side_margin+(right_frame_char if print_frame else '')) formatted_lines.append(fw(*line_elements)) # Print the lower margin (of height equal to up margin) for i in range(up_margin): formatted_lines.append(fw("%(lside)s%(width)s%(rside)s"% {'lside':left_frame_char if print_frame else '', 'rside':right_frame_char if print_frame else '', 'width':' '*(width-2)})) # Print the lower frame if print_frame: formatted_lines.append(fw(" %s "%(bottom_frame_char*(width-2)))) return '\n'.join(formatted_lines) # Now we define the raw banner text for each style: return format_banner( cls.get_raw_banner(style.lower()) %{'versionref':versionref, 'ref':reference, 'version':version})
def __init__(self, *arg, **opt): """Init history and line continuation""" # Tag allowing/forbiding question self.force = False # If possible, build an info line with current version number # and date, from the VERSION text file info = misc.get_pkg_info() info_line = "" if info and info.has_key('version') and info.has_key('date'): len_version = len(info['version']) len_date = len(info['date']) if len_version + len_date < 30: info_line = "#* VERSION %s %s %s *\n" % \ (info['version'], (30 - len_version - len_date) * ' ', info['date']) else: root_path = pjoin(os.path.dirname(__file__), os.path.pardir, os.path.pardir) version = open(pjoin(root_path, 'MGMEVersion.txt')).readline().strip() info_line = "#* VERSION %s %s *\n" % \ (version, (24 - len(version)) * ' ') # Create a header for the history file. # Remember to fill in time at writeout time! self.history_header = \ '#************************************************************\n' + \ '#* MadWeight 5 *\n' + \ '#* *\n' + \ "#* * * *\n" + \ "#* * * * * *\n" + \ "#* * * * * 5 * * * * *\n" + \ "#* * * * * *\n" + \ "#* * * *\n" + \ "#* *\n" + \ "#* *\n" + \ info_line + \ "#* *\n" + \ "#* The MadGraph Development Team - Please visit us at *\n" + \ "#* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \ '#* *\n' + \ '#************************************************************\n' + \ '#* *\n' + \ '#* Command File for MadWeight *\n' + \ '#* *\n' + \ '#* run as ./bin/madweight.py FILENAME *\n' + \ '#* *\n' + \ '#************************************************************\n' if info_line: info_line = info_line[1:] logger.info(\ "************************************************************\n" + \ "* *\n" + \ "* W E L C O M E to M A D G R A P H 5 *\n" + \ "* M A D W E I G H T *\n" + \ "* *\n" + \ "* * * *\n" + \ "* * * * * *\n" + \ "* * * * * 5 * * * * *\n" + \ "* * * * * *\n" + \ "* * * *\n" + \ "* *\n" + \ info_line + \ "* *\n" + \ "* The MadGraph Development Team - Please visit us at *\n" + \ "* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \ "* *\n" + \ "* Type 'help' for in-line help. *\n" + \ "* *\n" + \ "************************************************************") cmd.Cmd.__init__(self, *arg, **opt)
def import_model(model_path, mgme_dir = MG4DIR): """create a model from a MG4 model directory.""" # Check for a valid directory model_path = find_model_path(model_path, mgme_dir) files_list = [os.path.join(model_path, 'particles.dat'),\ os.path.join(model_path, 'interactions.dat')] for filepath in files_list: if not os.path.isfile(filepath): raise InvalidCmd, "%s directory is not a valid v4 model" % \ (model_path) # use pickle files if defined if files.is_uptodate(os.path.join(model_path, 'model.pkl'), files_list): model = save_load_object.load_from_file( \ os.path.join(model_path, 'model.pkl')) if model.has_key('version_tag') and model.get('version_tag') == os.path.realpath(model_path) + str(misc.get_pkg_info()): return model, model_path model = base_objects.Model() model.set('particles',files.read_from_file( \ os.path.join(model_path, 'particles.dat'), read_particles_v4)) model.set('interactions',files.read_from_file( \ os.path.join(model_path, 'interactions.dat'), read_interactions_v4, model['particles'])) model.set('name', os.path.split(model_path)[-1]) # save in a pickle files to fasten future usage save_load_object.save_to_file(os.path.join(model_path, 'model.pkl'), model) return model, model_path
def test_get_python_matrix_methods(self): """Test getting the matrix methods for Python for a matrix element.""" goal_method = (\ """class Matrix_0_uux_uux(object): def __init__(self): \"\"\"define the object\"\"\" self.clean() def clean(self): self.jamp = [] def smatrix(self,p, model): # # MadGraph5_aMC@NLO v. %(version)s, %(date)s # By the MadGraph5_aMC@NLO Development Team # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch # # MadGraph5_aMC@NLO StandAlone Version # # Returns amplitude squared summed/avg over colors # and helicities # for the point in phase space P(0:3,NEXTERNAL) # # Process: u u~ > u u~ # Process: c c~ > c c~ # # Clean additional output # self.clean() # # CONSTANTS # nexternal = 4 ndiags = 4 ncomb = 16 # # LOCAL VARIABLES # helicities = [ \\ [1,-1,-1,1], [1,-1,-1,-1], [1,-1,1,1], [1,-1,1,-1], [1,1,-1,1], [1,1,-1,-1], [1,1,1,1], [1,1,1,-1], [-1,-1,-1,1], [-1,-1,-1,-1], [-1,-1,1,1], [-1,-1,1,-1], [-1,1,-1,1], [-1,1,-1,-1], [-1,1,1,1], [-1,1,1,-1]] denominator = 36 # ---------- # BEGIN CODE # ---------- self.amp2 = [0.] * ndiags self.helEvals = [] ans = 0. for hel in helicities: t = self.matrix(p, hel, model) ans = ans + t self.helEvals.append([hel, t.real / denominator ]) ans = ans / denominator return ans.real def matrix(self, p, hel, model): # # MadGraph5_aMC@NLO v. %(version)s, %(date)s # By the MadGraph5_aMC@NLO Development Team # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch # # Returns amplitude squared summed/avg over colors # for the point with external lines W(0:6,NEXTERNAL) # # Process: u u~ > u u~ # Process: c c~ > c c~ # # # Process parameters # ngraphs = 4 nexternal = 4 nwavefuncs = 5 ncolor = 2 ZERO = 0. # # Color matrix # denom = [1,1]; cf = [[9,3], [3,9]]; # # Model parameters # WZ = model.get('parameter_dict')["WZ"] MZ = model.get('parameter_dict')["MZ"] GC_47 = model.get('coupling_dict')["GC_47"] GC_35 = model.get('coupling_dict')["GC_35"] GC_10 = model.get('coupling_dict')["GC_10"] # ---------- # Begin code # ---------- amp = [None] * ngraphs w = [None] * nwavefuncs w[0] = ixxxxx(p[0],ZERO,hel[0],+1) w[1] = oxxxxx(p[1],ZERO,hel[1],-1) w[2] = oxxxxx(p[2],ZERO,hel[2],+1) w[3] = ixxxxx(p[3],ZERO,hel[3],-1) w[4]= FFV1_3(w[0],w[1],GC_10,ZERO,ZERO) # Amplitude(s) for diagram number 1 amp[0]= FFV1_0(w[3],w[2],w[4],GC_10) w[4]= FFV2_5_3(w[0],w[1],GC_35,GC_47,MZ,WZ) # Amplitude(s) for diagram number 2 amp[1]= FFV2_5_0(w[3],w[2],w[4],GC_35,GC_47) w[4]= FFV1_3(w[0],w[2],GC_10,ZERO,ZERO) # Amplitude(s) for diagram number 3 amp[2]= FFV1_0(w[3],w[1],w[4],GC_10) w[4]= FFV2_5_3(w[0],w[2],GC_35,GC_47,MZ,WZ) # Amplitude(s) for diagram number 4 amp[3]= FFV2_5_0(w[3],w[1],w[4],GC_35,GC_47) jamp = [None] * ncolor jamp[0] = +1./6.*amp[0]-amp[1]+1./2.*amp[2] jamp[1] = -1./2.*amp[0]-1./6.*amp[2]+amp[3] self.amp2[0]+=abs(amp[0]*amp[0].conjugate()) self.amp2[1]+=abs(amp[1]*amp[1].conjugate()) self.amp2[2]+=abs(amp[2]*amp[2].conjugate()) self.amp2[3]+=abs(amp[3]*amp[3].conjugate()) matrix = 0. for i in range(ncolor): ztemp = 0 for j in range(ncolor): ztemp = ztemp + cf[i][j]*jamp[j] matrix = matrix + ztemp * jamp[i].conjugate()/denom[i] self.jamp.append(jamp) return matrix """ % misc.get_pkg_info()).split('\n') exporter = export_python.ProcessExporterPython(self.mymatrixelement, self.mypythonmodel) matrix_methods = exporter.get_python_matrix_methods()["0_uux_uux"].\ split('\n') self.assertEqual(matrix_methods, goal_method)
def runIOTests(arg=[''],update=True,force=0,synchronize=False): """ running the IOtests associated to expression. By default, this launch all the tests created in classes inheriting IOTests. """ # Update the tarball, while removing the .backups. def noBackUps(tarinfo): if tarinfo.name.endswith('.BackUp'): return None else: return tarinfo if synchronize: print "Please, prefer updating the reference file automatically "+\ "rather than by hand." tar = tarfile.open(_hc_comparison_tarball, "w:bz2") tar.add(_hc_comparison_files, \ arcname=path.basename(_hc_comparison_files), filter=noBackUps) tar.close() # I am too lazy to work out the difference with the existing tarball and # put it in the log. So this is why one should refrain from editing the # reference files by hand. text = " \nModifications performed by hand on %s at %s in"%(\ str(datetime.date.today()),misc.format_timer(0.0)[14:]) text += '\n MadGraph5_aMC@NLO v. %(version)s, %(date)s\n'%misc.get_pkg_info() log = open(_hc_comparison_modif_log,mode='a') log.write(text) log.close() print "INFO:: Ref. tarball %s updated"%str(_hc_comparison_tarball) return if len(arg)!=1 or not isinstance(arg[0],str): print "Exactly one argument, and in must be a string, not %s."%arg return arg=arg[0] # Extract the tarball for hardcoded comparison if necessary if not path.isdir(_hc_comparison_files): if path.isfile(_hc_comparison_tarball): tar = tarfile.open(_hc_comparison_tarball,mode='r:bz2') tar.extractall(path.dirname(_hc_comparison_files)) tar.close() else: os.makedirs(_hc_comparison_files) # Make a backup of the comparison file directory in order to revert it if # the user wants to ignore the changes detected (only when updating the refs) hc_comparison_files_BackUp = _hc_comparison_files+'_BackUp' if update and path.isdir(_hc_comparison_files): if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(hc_comparison_files_BackUp) shutil.copytree(_hc_comparison_files,hc_comparison_files_BackUp) IOTestManager.testFolders_filter = arg.split('/')[0].split('&') IOTestManager.testNames_filter = arg.split('/')[1].split('&') IOTestManager.filesChecked_filter = '/'.join(arg.split('/')[2:]).split('&') #print "INFO:: Using folders %s"%str(IOTestManager.testFolders_filter) #print "INFO:: Using test names %s"%str(IOTestManager.testNames_filter) #print "INFO:: Using file paths %s"%str(IOTestManager.filesChecked_filter) # Initiate all the IOTests from all the setUp() IOTestsInstances = [] start = time.time() for IOTestsClass in IOTestFinder(): # Instantiate the class IOTestsInstances.append(IOTestsClass()) # Run the setUp IOTestsInstances[-1].setUp() # Find the testIO defined and use them in load mode only, we will run # them later here. IOTestsFunctions = IOTestFinder() IOTestsFunctions.collect_function(IOTestsClass,prefix='testIO') for IOTestFunction in IOTestsFunctions: start = time.time() eval('IOTestsInstances[-1].'+IOTestFunction.split('.')[-1]+\ '(load_only=True)') setUp_time = time.time() - start if setUp_time > 0.5: print colored%(34,"Loading IOtest %s is slow (%s)"% (colored%(32,'.'.join(IOTestFunction.split('.')[-3:])), colored%(34,'%.2fs'%setUp_time))) if len(IOTestsInstances)==0: print "No IOTest found." return # runIOTests cannot be made a classmethod, so I use an instance, but it does # not matter which one as no instance attribute will be used. try: modifications = IOTestsInstances[-1].runIOTests( update = update, force = force, verbose=True, testKeys=IOTestManager.all_tests.keys()) except KeyboardInterrupt: if update: # Remove the BackUp of the reference files. if not path.isdir(hc_comparison_files_BackUp): print "\nWARNING:: Update interrupted and modifications already "+\ "performed could not be reverted." else: shutil.rmtree(_hc_comparison_files) mv(hc_comparison_files_BackUp,_hc_comparison_files) print colored%(34, "\nINFO:: Update interrupted, existing modifications reverted.") sys.exit(0) else: print "\nINFO:: IOTest runs interrupted." sys.exit(0) tot_time = time.time() - start if modifications == 'test_over': print colored%(32,"\n%d IOTests "%len(IOTestManager.all_tests.keys()))+\ "successfully tested in %s."%(colored%(34,'%.2fs'%tot_time)) sys.exit(0) elif not isinstance(modifications,dict): print "Error during the files update." sys.exit(0) if sum(len(v) for v in modifications.values())>0: # Display the modifications text = colored%(34, " \nModifications performed on %s at %s in"%(\ str(datetime.date.today()),misc.format_timer(0.0)[14:])) text += colored%(34, '\n MadGraph5_aMC@NLO v. %(version)s, %(date)s\n'%misc.get_pkg_info()) for key in modifications.keys(): if len(modifications[key])==0: continue text += colored%(32,"The following reference files have been %s :"%key) text += '\n'+'\n'.join([" %s"%mod for mod in modifications[key]]) text += '\n' print text try: answer = Cmd.timed_input(question= "Do you want to apply the modifications listed above? [y/n] >",default="y") except KeyboardInterrupt: answer = 'n' if answer == 'y': log = open(_hc_comparison_modif_log,mode='a') log.write(text) log.close() if IOTestManager._compress_ref_fodler: tar = tarfile.open(_hc_comparison_tarball, "w:bz2") tar.add(_hc_comparison_files, \ arcname=path.basename(_hc_comparison_files), filter=noBackUps) tar.close() print colored%(32,"INFO:: tarball %s updated"%str(_hc_comparison_tarball)) else: print colored%(32,"INFO:: Reference %s updated"%\ str(os.path.basename(_hc_comparison_files))) if len(modifications['created'])>0: print colored%(31,"Some ref. files have been created; add "+\ "them to the revision with\n "+ "bzr add tests/input_files/IOTestsComparison") # Make sure to remove the BackUp files filelist = glob.glob(os.path.join(_hc_comparison_files, '*','*','*.BackUp')) for f in filelist: os.remove(f) else: if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(_hc_comparison_files) shutil.copytree(hc_comparison_files_BackUp,_hc_comparison_files) print colored%(32,"INFO:: No modifications applied.") else: print colored%(31, "ERROR:: Could not revert the modifications. No backup found.") else: print colored%(32,"\nNo modifications performed. No update necessary.") # Remove the BackUp of the reference files. if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(hc_comparison_files_BackUp)
for filepath in files_list: if not os.path.isfile(filepath): if not absolute: raise InvalidCmd, "%s directory is not a valid v4 model" % \ (model_path) else: return import_model(model_path_old, mgme_dir, False) # use pickle files if defined if files.is_uptodate(os.path.join(model_path, 'model.pkl'), files_list): model = save_load_object.load_from_file( \ os.path.join(model_path, 'model.pkl')) if model.has_key('version_tag') and model.get( 'version_tag') == os.path.realpath(model_path) + str( misc.get_pkg_info()): return model, model_path model = base_objects.Model() model.set('particles',files.read_from_file( \ os.path.join(model_path, 'particles.dat'), read_particles_v4)) model.set('interactions',files.read_from_file( \ os.path.join(model_path, 'interactions.dat'), read_interactions_v4, model['particles'])) model.set('name', os.path.split(model_path)[-1]) # save in a pickle files to fasten future usage
def runIOTests(arg=[''], update=True, force=0, synchronize=False): """ running the IOtests associated to expression. By default, this launch all the tests created in classes inheriting IOTests. """ # Update the tarball, while removing the .backups. def noBackUps(tarinfo): if tarinfo.name.endswith('.BackUp'): return None else: return tarinfo if synchronize: print "Please, prefer updating the reference file automatically "+\ "rather than by hand." tar = tarfile.open(_hc_comparison_tarball, "w:bz2") tar.add(_hc_comparison_files, \ arcname=path.basename(_hc_comparison_files), filter=noBackUps) tar.close() # I am too lazy to work out the difference with the existing tarball and # put it in the log. So this is why one should refrain from editing the # reference files by hand. text = " \nModifications performed by hand on %s at %s in"%(\ str(datetime.date.today()),misc.format_timer(0.0)[14:]) text += '\n MadGraph5_aMC@NLO v. %(version)s, %(date)s\n' % misc.get_pkg_info( ) log = open(_hc_comparison_modif_log, mode='a') log.write(text) log.close() print "INFO:: Ref. tarball %s updated" % str(_hc_comparison_tarball) return if len(arg) != 1 or not isinstance(arg[0], str): print "Exactly one argument, and in must be a string, not %s." % arg return arg = arg[0] # Extract the tarball for hardcoded comparison if necessary if not path.isdir(_hc_comparison_files): if path.isfile(_hc_comparison_tarball): tar = tarfile.open(_hc_comparison_tarball, mode='r:bz2') tar.extractall(path.dirname(_hc_comparison_files)) tar.close() else: os.makedirs(_hc_comparison_files) # Make a backup of the comparison file directory in order to revert it if # the user wants to ignore the changes detected (only when updating the refs) hc_comparison_files_BackUp = _hc_comparison_files + '_BackUp' if update and path.isdir(_hc_comparison_files): if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(hc_comparison_files_BackUp) shutil.copytree(_hc_comparison_files, hc_comparison_files_BackUp) IOTestManager.testFolders_filter = arg.split('/')[0].split('&') IOTestManager.testNames_filter = arg.split('/')[1].split('&') IOTestManager.filesChecked_filter = '/'.join(arg.split('/')[2:]).split('&') #print "INFO:: Using folders %s"%str(IOTestManager.testFolders_filter) #print "INFO:: Using test names %s"%str(IOTestManager.testNames_filter) #print "INFO:: Using file paths %s"%str(IOTestManager.filesChecked_filter) # Initiate all the IOTests from all the setUp() IOTestsInstances = [] start = time.time() for IOTestsClass in IOTestFinder(): # Instantiate the class IOTestsInstances.append(IOTestsClass()) # Run the setUp IOTestsInstances[-1].setUp() # Find the testIO defined and use them in load mode only, we will run # them later here. IOTestsFunctions = IOTestFinder() IOTestsFunctions.collect_function(IOTestsClass, prefix='testIO') for IOTestFunction in IOTestsFunctions: start = time.time() # Add all the tests automatically (i.e. bypass filters) if the # specified test is the name of the IOtest. the [7:] is to # skip the testIO prefix name_filer_bu = None if IOTestFunction.split('.')[-1][7:] in \ IOTestManager.testNames_filter: name_filer_bu = IOTestManager.testNames_filter IOTestManager.testNames_filter = ['ALL'] existing_tests = IOTestManager.all_tests.keys() eval('IOTestsInstances[-1].'+IOTestFunction.split('.')[-1]+\ '(load_only=True)') if name_filer_bu: new_tests = [test[0] for test in IOTestManager.all_tests.keys() \ if test not in existing_tests] IOTestManager.testNames_filter = name_filer_bu + new_tests name_filer_bu = None setUp_time = time.time() - start if setUp_time > 0.5: print colored % ( 34, "Loading IOtest %s is slow (%s)" % (colored % (32, '.'.join(IOTestFunction.split('.')[-3:])), colored % (34, '%.2fs' % setUp_time))) if len(IOTestsInstances) == 0: print "No IOTest found." return # runIOTests cannot be made a classmethod, so I use an instance, but it does # not matter which one as no instance attribute will be used. try: modifications = IOTestsInstances[-1].runIOTests( update=update, force=force, verbose=True, testKeys=IOTestManager.all_tests.keys()) except KeyboardInterrupt: if update: # Remove the BackUp of the reference files. if not path.isdir(hc_comparison_files_BackUp): print "\nWARNING:: Update interrupted and modifications already "+\ "performed could not be reverted." else: shutil.rmtree(_hc_comparison_files) mv(hc_comparison_files_BackUp, _hc_comparison_files) print colored % ( 34, "\nINFO:: Update interrupted, existing modifications reverted." ) sys.exit(0) else: print "\nINFO:: IOTest runs interrupted." sys.exit(0) tot_time = time.time() - start if modifications == 'test_over': print colored%(32,"\n%d IOTests "%len(IOTestManager.all_tests.keys()))+\ "successfully tested in %s."%(colored%(34,'%.2fs'%tot_time)) sys.exit(0) elif not isinstance(modifications, dict): print "Error during the files update." sys.exit(0) if len(modifications['missing']) > 0: text = '\n' text += colored % ( 31, "The following files were not generated by the tests, fix this!") text += '\n' + '\n'.join( [" %s" % mod for mod in modifications['missing']]) print text modifications['missing'] = [] if sum(len(v) for v in modifications.values()) > 0: # Display the modifications text = colored%(34, " \nModifications performed on %s at %s in"%(\ str(datetime.date.today()),misc.format_timer(0.0)[14:])) text += colored % ( 34, '\n MadGraph5_aMC@NLO v. %(version)s, %(date)s\n' % misc.get_pkg_info()) for key in modifications.keys(): if len(modifications[key]) == 0: continue text += colored % ( 32, "The following reference files have been %s :" % key) text += '\n' + '\n'.join( [" %s" % mod for mod in modifications[key]]) text += '\n' print text try: answer = Cmd.timed_input( question= "Do you want to apply the modifications listed above? [y/n] >", default="y") except KeyboardInterrupt: answer = 'n' if answer == 'y': log = open(_hc_comparison_modif_log, mode='a') log.write(text) log.close() if IOTestManager._compress_ref_fodler: tar = tarfile.open(_hc_comparison_tarball, "w:bz2") tar.add(_hc_comparison_files, \ arcname=path.basename(_hc_comparison_files), filter=noBackUps) tar.close() print colored % (32, "INFO:: tarball %s updated" % str(_hc_comparison_tarball)) else: print colored%(32,"INFO:: Reference %s updated"%\ str(os.path.basename(_hc_comparison_files))) if len(modifications['created']) > 0: print colored%(31,"Some ref. files have been created; add "+\ "them to the revision with\n "+ "bzr add tests/input_files/IOTestsComparison") # Make sure to remove the BackUp files filelist = glob.glob( os.path.join(_hc_comparison_files, '*', '*', '*.BackUp')) for f in filelist: os.remove(f) else: if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(_hc_comparison_files) shutil.copytree(hc_comparison_files_BackUp, _hc_comparison_files) print colored % (32, "INFO:: No modifications applied.") else: print colored % ( 31, "ERROR:: Could not revert the modifications. No backup found." ) else: print colored % (32, "\nNo modifications performed. No update necessary.") # Remove the BackUp of the reference files. if path.isdir(hc_comparison_files_BackUp): shutil.rmtree(hc_comparison_files_BackUp)
def runIOTests(self, update = False, force = 0, verbose=False, \ testKeys='instanceList'): """ Run the IOTests for this instance (defined in self.instance_tests) and compare the files of the chosen tests against the hardcoded ones stored in tests/input_files/IOTestsComparison. If you see an error in the comparison and you are sure that the newest output is correct (i.e. you understand that this modification is meant to be so). Then feel free to automatically regenerate this file with the newest version by doing ./test_manager -i U folderName/testName/fileName If update is True (meant to be used by __main__ only) then it will create/update/remove the files instead of testing them. The argument tests can be a list of tuple-keys describing the tests to cover. Otherwise it is the instance_test list. The force argument must be 10 if you do not want to monitor the modifications on the updated files. If it is 0 you will monitor all modified file and if 1 you will monitor each modified file of a given name only once. """ # First make sure that the tarball need not be untarred # Extract the tarball for hardcoded in all cases to make sure the # IOTestComparison folder is synchronized with it. if IOTestManager._compress_ref_fodler: if path.isdir(_hc_comparison_files): try: shutil.rmtree(_hc_comparison_files) except IOError: pass if path.isfile(_hc_comparison_tarball): tar = tarfile.open(_hc_comparison_tarball, mode='r:bz2') tar.extractall(path.dirname(_hc_comparison_files)) tar.close() else: raise MadGraph5Error( "Could not find the comparison tarball %s." % _hc_comparison_tarball) else: if not path.isdir(_hc_comparison_files): raise MadGraph5Error( "Could not find the comparison tarball %s." % _hc_comparison_tarball) # In update = True mode, we keep track of the modification to # provide summary information modifications = { 'updated': [], 'created': [], 'removed': [], 'missing': [] } # List all the names of the files for which modifications have been # reviewed at least once.The approach taken here is different than # with the list refusedFolder and refusedTest. # The key of the dictionary are the filenames and the value are string # determining the user answer for that file. reviewed_file_names = {} # Chose what test to cover if testKeys == 'instanceList': testKeys = self.instance_tests if verbose: print("\n== "+colored%(32,"Operational mode")+\ " : file %s ==\n"%(colored%(34,('UPDATE' if update else 'TESTING')))) for (folder_name, test_name) in testKeys: try: (iotest, iotestManager) = self.all_tests[(folder_name, test_name)] except KeyError: raise MadGraph5Error('Test (%s,%s) could not be found.'\ %(folder_name, test_name)) if verbose: print("Processing %s in %s" % (colored % (32, test_name), colored % (34, folder_name))) files_path = iotest.run(iotestManager) try: pass # files_path = iotest.run(iotestManager) except Exception as e: iotest.clean_output() if not verbose: raise e else: print(colored%(31," Test %s "%test_name+\ "crashed with the following error:\n %s."%str(e))) continue # First create the list of files to check as the user might be using # regular expressions. filesToCheck = [] # Store here the files reckognized as veto rules (with filename # starting with '-') veto_rules = [] for fname in iotest.testedFiles: # Disregard the veto rules regexp_finder = re.compile( r'^(?P<veto>-)?(?P<root_folder>.*)(\/)?\[(?P<regexp>.*)\]$' ) found = regexp_finder.search(fname) if not found is None: # folder without the final / base_path = pjoin(files_path, found.group('root_folder')) regexp = re.compile(found.group('regexp')) # In filesToCheck, we must remove the files_path/ prepended for root, dirnames, filenames in os.walk(base_path): for file in filenames: if not regexp.search(str(os.path.relpath( pjoin(root,file),base_path))) is None and \ not path.islink(pjoin(root,file)): new_target = os.path.relpath( pjoin(root, file), files_path) if found.group('veto') == '-': veto_rules.append(new_target) else: filesToCheck.append(new_target) else: fn = fname[1:] if fname.startswith('-') else fname if (not path.exists(pjoin(files_path, fn))) or path.islink( pjoin(files_path, fn)): if force in [0, 1]: answer = Cmd.timed_input(question= """The IOTest %s does not create file '%s'. Fix it! [type 'enter'] >"""\ %(test_name,fn),default="y") modifications['missing'].append( "%s/%s/%s" % (folder_name, test_name, path.basename(fname))) if verbose: print(" > [ %s ] "%(colored%(31,"MISSING"))+\ "%s/%s/%s"%(folder_name,test_name,path.basename(fname))) else: if fname.startswith('-'): veto_rules.append(fn) else: filesToCheck.append(fn) # Apply the trimming of the veto rules filesToCheck = [f for f in filesToCheck if f not in veto_rules] if update: # Remove files which are no longer used for comparison activeFiles = [self.toFileName(f) for f in filesToCheck] for file in glob.glob(pjoin(_hc_comparison_files,folder_name,\ test_name,'*')): # Ignore the .BackUp files and directories. Also ignore # a file which was previously flagged missing because it # was explicitly specified in the list of files that the # test *must* provide. if path.basename(file).endswith('.BackUp') or \ path.isdir(file) or \ pjoin(folder_name,test_name,path.basename(file)) in \ modifications['missing']: continue if path.basename(file) not in activeFiles: if force==0 or (force==1 and \ path.basename(file) not in list(reviewed_file_names.keys())): answer = Cmd.timed_input(question= """Obsolete ref. file %s in %s/%s detected, delete it? [y/n] >"""\ %(path.basename(file),folder_name,test_name) ,default="y") reviewed_file_names[path.basename(file)] = answer elif (force==1 and \ path.basename(file) in list(reviewed_file_names.keys())): answer = reviewed_file_names[path.basename(file)] else: answer = 'Y' if answer not in ['Y', 'y', '']: if verbose: print(" > [ %s ] "%(colored%(31,"IGNORED"))+\ "file deletion %s/%s/%s"%(folder_name,test_name, path.basename(file))) continue os.remove(file) if verbose: print(" > [ %s ] "%(colored%(31,"REMOVED"))+\ "%s/%s/%s"%(folder_name,test_name,path.basename(file))) modifications['removed'].append('/'.join( str(file).split('/')[-3:])) # Make sure it is not filtered out by the user-filter if self.filesChecked_filter != ['ALL']: new_filesToCheck = [] for file in filesToCheck: # Try if it matches any filter for filter in self.filesChecked_filter: # A regular expression if filter.endswith(']'): split = filter[:-1].split('[') # folder without the final / folder = split[0][:-1] if folder != path.dirname(pjoin(file)): continue search = re.compile('['.join(split[1:])) if not search.match(path.basename(file)) is None: new_filesToCheck.append(file) break # Just the exact filename elif filter == file: new_filesToCheck.append(file) break filesToCheck = new_filesToCheck # Now we can scan them and process them one at a time # Keep track of the folders and testNames the user did not want to # create refused_Folders = [] refused_testNames = [] for fname in filesToCheck: file_path = path.abspath(pjoin(files_path, fname)) self.assertTrue(path.isfile(file_path), 'File %s not found.' % str(file_path)) comparison_path = pjoin(_hc_comparison_files,\ folder_name,test_name,self.toFileName(fname)) if not update: if not os.path.isfile(comparison_path): iotest.clean_output() if not verbose: raise MadGraph5Error("Missing ref. files for test %s\n"%test_name+\ "Create them with './test_manager.py -U %s'"%test_name) continue else: print(colored % (31, 'The ref. file %s' % str( '/'.join(comparison_path.split('/')[-3:])) + ' does not exist.')) print(colored % (34, 'Consider creating it with ' + './test_manager.py -U %s' % test_name)) exit(0) goal = open(comparison_path).read() % misc.get_pkg_info() if not verbose: self.assertFileContains(open(file_path), goal) else: try: self.assertFileContains(open(file_path), goal) except AssertionError: if verbose: print(" > %s differs from the reference." % fname) else: if not path.isdir(pjoin(_hc_comparison_files, folder_name)): if force == 0: if folder_name in refused_Folders: continue answer = Cmd.timed_input( question= """New folder %s detected, create it? [y/n] >""" % folder_name, default="y") if answer not in ['Y', 'y', '']: refused_Folders.append(folder_name) if verbose: print(" > [ %s ] folder %s"\ %(colored%(31,"IGNORED"),folder_name)) continue if verbose: print(" > [ %s ] folder %s"%\ (colored%(32,"CREATED"),folder_name)) os.makedirs(pjoin(_hc_comparison_files, folder_name)) if not path.isdir( pjoin(_hc_comparison_files, folder_name, test_name)): if force == 0: if (folder_name, test_name) in refused_testNames: continue answer = Cmd.timed_input( question= """New test %s/%s detected, create it? [y/n] >""" % (folder_name, test_name), default="y") if answer not in ['Y', 'y', '']: refused_testNames.append( (folder_name, test_name)) if verbose: print(" > [ %s ] test %s/%s"\ %(colored%(31,"IGNORED"),folder_name,test_name)) continue if verbose: print(" > [ %s ] test %s/%s"\ %(colored%(32,"CREATED"),folder_name,test_name)) os.makedirs( pjoin(_hc_comparison_files, folder_name, test_name)) # Transform the package information to make it a template file = open(file_path, 'r') target = file.read() # So that if % appear, we cast them to %% which are not formatted. target = target.replace('%', '%%') # So that the version and date is automatically updated target = target.replace('MadGraph5_aMC@NLO v. %(version)s, %(date)s'\ %misc.get_pkg_info(), 'MadGraph5_aMC@NLO v. %(version)s, %(date)s') target = target.replace('v%(version)s (%(date)s)'\ %misc.get_pkg_info(), 'v%(version)s (%(date)s)') file.close() if os.path.isfile(comparison_path): file = open(comparison_path, 'r') existing = file.read() file.close() if existing == target: continue else: # Copying the existing reference as a backup tmp_path = pjoin(_hc_comparison_files,folder_name,\ test_name,self.toFileName(fname)+'.BackUp') if os.path.isfile(tmp_path): os.remove(tmp_path) file = open(tmp_path, 'w') file.write(target) file.close() if force==0 or (force==1 and path.basename(\ comparison_path) not in list(reviewed_file_names.keys())): text = \ """File %s in test %s/%s differs by the following (reference file first): """%(fname,folder_name,test_name) text += misc.Popen(['diff',str(comparison_path), str(tmp_path)],stdout=subprocess.PIPE).\ communicate()[0].decode('utf-8') # Remove the last newline if text[-1] == '\n': text = text[:-1] if (len(text.split('\n')) < 15): print(text) else: pydoc.pager(text) print("Difference displayed in editor.") answer = '' while answer not in ['y', 'n']: answer = Cmd.timed_input( question= """Ref. file %s differs from the new one (see diff. before), update it? [y/n/h/r] >""" % fname, default="y") if answer not in ['y', 'n']: if answer == 'r': pydoc.pager(text) else: print("reference path: %s" % comparison_path) print("code returns: %s" % tmp_path) os.remove(tmp_path) reviewed_file_names[path.basename(\ comparison_path)] = answer elif (force==1 and path.basename(\ comparison_path) in list(reviewed_file_names.keys())): answer = reviewed_file_names[path.basename(\ comparison_path)] else: answer = 'Y' if answer not in ['Y', 'y', '']: if verbose: print(" > [ %s ] %s"%\ (colored%(31,"IGNORED"),fname)) continue # Copying the existing reference as a backup back_up_path = pjoin(_hc_comparison_files,folder_name,\ test_name,self.toFileName(fname)+'.BackUp') if os.path.isfile(back_up_path): os.remove(back_up_path) cp(comparison_path, back_up_path) if verbose: print(" > [ %s ] %s"\ %(colored%(32,"UPDATED"),fname)) modifications['updated'].append('/'.join( comparison_path.split('/')[-3:])) else: if force==0 or (force==1 and path.basename(\ comparison_path) not in list(reviewed_file_names.keys())): answer = Cmd.timed_input( question= """New file %s detected, create it? [y/n] >""" % fname, default="y") reviewed_file_names[path.basename(\ comparison_path)] = answer elif (force==1 and path.basename(\ comparison_path) in list(reviewed_file_names.keys())): answer = reviewed_file_names[\ path.basename(comparison_path)] else: answer = 'Y' if answer not in ['Y', 'y', '']: if verbose: print(" > [ %s ] %s"%\ (colored%(31,"IGNORED"),fname)) continue if verbose: print(" > [ %s ] %s"%\ (colored%(32,"CREATED"),fname)) modifications['created'].append('/'.join( comparison_path.split('/')[-3:])) file = open(comparison_path, 'w') file.write(target) file.close() # Clean the iotest output iotest.clean_output() # Monitor the modifications when in creation files mode by returning the # modifications dictionary. if update: return modifications else: return 'test_over'
def __init__(self, *arg, **opt): """Init history and line continuation""" # Tag allowing/forbiding question self.force = False # If possible, build an info line with current version number # and date, from the VERSION text file info = misc.get_pkg_info() info_line = "" if info and info.has_key('version') and info.has_key('date'): len_version = len(info['version']) len_date = len(info['date']) if len_version + len_date < 30: info_line = "#* VERSION %s %s %s *\n" % \ (info['version'], (30 - len_version - len_date) * ' ', info['date']) else: root_path = pjoin(os.path.dirname(__file__), os.path.pardir,os.path.pardir) version = open(pjoin(root_path,'MGMEVersion.txt')).readline().strip() info_line = "#* VERSION %s %s *\n" % \ (version, (24 - len(version)) * ' ') # Create a header for the history file. # Remember to fill in time at writeout time! self.history_header = \ '#************************************************************\n' + \ '#* MadWeight 5 *\n' + \ '#* *\n' + \ "#* * * *\n" + \ "#* * * * * *\n" + \ "#* * * * * 5 * * * * *\n" + \ "#* * * * * *\n" + \ "#* * * *\n" + \ "#* *\n" + \ "#* *\n" + \ info_line + \ "#* *\n" + \ "#* The MadGraph Development Team - Please visit us at *\n" + \ "#* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \ '#* *\n' + \ '#************************************************************\n' + \ '#* *\n' + \ '#* Command File for MadWeight *\n' + \ '#* *\n' + \ '#* run as ./bin/madweight.py FILENAME *\n' + \ '#* *\n' + \ '#************************************************************\n' if info_line: info_line = info_line[1:] logger.info(\ "************************************************************\n" + \ "* *\n" + \ "* W E L C O M E to M A D G R A P H 5 *\n" + \ "* M A D W E I G H T *\n" + \ "* *\n" + \ "* * * *\n" + \ "* * * * * *\n" + \ "* * * * * 5 * * * * *\n" + \ "* * * * * *\n" + \ "* * * *\n" + \ "* *\n" + \ info_line + \ "* *\n" + \ "* The MadGraph Development Team - Please visit us at *\n" + \ "* https://server06.fynu.ucl.ac.be/projects/madgraph *\n" + \ "* *\n" + \ "* Type 'help' for in-line help. *\n" + \ "* *\n" + \ "************************************************************") cmd.Cmd.__init__(self, *arg, **opt)
logging.warning("This is a very large difference. Indicating a wrong manipulation.") logging.warning("and can creates trouble for the auto-update.") answer = raw_input('Do you want to continue anyway? (y/n)') if answer != 'y': exit() else: logging.warning("CRITICAL: current version on the web is %s" % web_version) logging.warning("This FORBIDS any auto-update for this version.") rev_nb=None answer = raw_input('Do you want to continue anyway? (y/n)') if answer != 'y': exit() # 1. bzr branch the present directory to a new directory # MadGraph5_vVERSION filepath = "MG5_aMC_v" + misc.get_pkg_info()['version'].replace(".", "_") filename = "MG5_aMC_v" + misc.get_pkg_info()['version'] + ".tar.gz" if path.exists(filepath): logging.info("Removing existing directory " + filepath) shutil.rmtree(filepath) logging.info("Branching " + MG5DIR + " to directory " + filepath) status = subprocess.call(['bzr', 'branch', MG5DIR, filepath]) if status: logging.error("bzr branch failed. Script stopped") exit() # 1. Remove the .bzr directory and clean bin directory file, # take care of README files. shutil.rmtree(path.join(filepath, '.bzr'))
def test_get_python_matrix_methods(self): """Test getting the matrix methods for Python for a matrix element.""" goal_method = (\ """class Matrix_0_uux_uux(object): def __init__(self): \"\"\"define the object\"\"\" self.clean() def clean(self): self.jamp = [] def smatrix(self,p, model): # # MadGraph5_aMC@NLO v. %(version)s, %(date)s # By the MadGraph5_aMC@NLO Development Team # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch # # MadGraph5_aMC@NLO StandAlone Version # # Returns amplitude squared summed/avg over colors # and helicities # for the point in phase space P(0:3,NEXTERNAL) # # Process: u u~ > u u~ # Process: c c~ > c c~ # # Clean additional output # self.clean() # # CONSTANTS # nexternal = 4 ndiags = 4 ncomb = 16 # # LOCAL VARIABLES # helicities = [ \\ [-1,-1,-1,-1], [-1,-1,-1,1], [-1,-1,1,-1], [-1,-1,1,1], [-1,1,-1,-1], [-1,1,-1,1], [-1,1,1,-1], [-1,1,1,1], [1,-1,-1,-1], [1,-1,-1,1], [1,-1,1,-1], [1,-1,1,1], [1,1,-1,-1], [1,1,-1,1], [1,1,1,-1], [1,1,1,1]] denominator = 36 # ---------- # BEGIN CODE # ---------- self.amp2 = [0.] * ndiags self.helEvals = [] ans = 0. for hel in helicities: t = self.matrix(p, hel, model) ans = ans + t self.helEvals.append([hel, t.real / denominator ]) ans = ans / denominator return ans.real def matrix(self, p, hel, model): # # MadGraph5_aMC@NLO v. %(version)s, %(date)s # By the MadGraph5_aMC@NLO Development Team # Visit launchpad.net/madgraph5 and amcatnlo.web.cern.ch # # Returns amplitude squared summed/avg over colors # for the point with external lines W(0:6,NEXTERNAL) # # Process: u u~ > u u~ # Process: c c~ > c c~ # # # Process parameters # ngraphs = 4 nexternal = 4 nwavefuncs = 5 ncolor = 2 ZERO = 0. # # Color matrix # denom = [1,1]; cf = [[9,3], [3,9]]; # # Model parameters # WZ = model.get('parameter_dict')["WZ"] MZ = model.get('parameter_dict')["MZ"] GC_47 = model.get('coupling_dict')["GC_47"] GC_35 = model.get('coupling_dict')["GC_35"] GC_10 = model.get('coupling_dict')["GC_10"] # ---------- # Begin code # ---------- amp = [None] * ngraphs w = [None] * nwavefuncs w[0] = ixxxxx(p[0],ZERO,hel[0],+1) w[1] = oxxxxx(p[1],ZERO,hel[1],-1) w[2] = oxxxxx(p[2],ZERO,hel[2],+1) w[3] = ixxxxx(p[3],ZERO,hel[3],-1) w[4]= FFV1_3(w[0],w[1],GC_10,ZERO,ZERO) # Amplitude(s) for diagram number 1 amp[0]= FFV1_0(w[3],w[2],w[4],GC_10) w[4]= FFV2_5_3(w[0],w[1],GC_35,GC_47,MZ,WZ) # Amplitude(s) for diagram number 2 amp[1]= FFV2_5_0(w[3],w[2],w[4],GC_35,GC_47) w[4]= FFV1_3(w[0],w[2],GC_10,ZERO,ZERO) # Amplitude(s) for diagram number 3 amp[2]= FFV1_0(w[3],w[1],w[4],GC_10) w[4]= FFV2_5_3(w[0],w[2],GC_35,GC_47,MZ,WZ) # Amplitude(s) for diagram number 4 amp[3]= FFV2_5_0(w[3],w[1],w[4],GC_35,GC_47) jamp = [None] * ncolor jamp[0] = +1./6.*amp[0]-amp[1]+1./2.*amp[2] jamp[1] = -1./2.*amp[0]-1./6.*amp[2]+amp[3] self.amp2[0]+=abs(amp[0]*amp[0].conjugate()) self.amp2[1]+=abs(amp[1]*amp[1].conjugate()) self.amp2[2]+=abs(amp[2]*amp[2].conjugate()) self.amp2[3]+=abs(amp[3]*amp[3].conjugate()) matrix = 0. for i in range(ncolor): ztemp = 0 for j in range(ncolor): ztemp = ztemp + cf[i][j]*jamp[j] matrix = matrix + ztemp * jamp[i].conjugate()/denom[i] self.jamp.append(jamp) return matrix """ % misc.get_pkg_info()).split('\n') exporter = export_python.ProcessExporterPython(self.mymatrixelement, self.mypythonmodel) matrix_methods = exporter.get_python_matrix_methods()["0_uux_uux"].\ split('\n') self.assertEqual(matrix_methods, goal_method)
def runIOTests(self, update = False, force = 0, verbose=False, \ testKeys='instanceList'): """ Run the IOTests for this instance (defined in self.instance_tests) and compare the files of the chosen tests against the hardcoded ones stored in tests/input_files/IOTestsComparison. If you see an error in the comparison and you are sure that the newest output is correct (i.e. you understand that this modification is meant to be so). Then feel free to automatically regenerate this file with the newest version by doing ./test_manager -i U folderName/testName/fileName If update is True (meant to be used by __main__ only) then it will create/update/remove the files instead of testing them. The argument tests can be a list of tuple-keys describing the tests to cover. Otherwise it is the instance_test list. The force argument must be 10 if you do not want to monitor the modifications on the updated files. If it is 0 you will monitor all modified file and if 1 you will monitor each modified file of a given name only once. """ # First make sure that the tarball need not be untarred # Extract the tarball for hardcoded in all cases to make sure the # IOTestComparison folder is synchronized with it. if IOTestManager._compress_ref_fodler: if path.isdir(_hc_comparison_files): try: shutil.rmtree(_hc_comparison_files) except IOError: pass if path.isfile(_hc_comparison_tarball): tar = tarfile.open(_hc_comparison_tarball,mode='r:bz2') tar.extractall(path.dirname(_hc_comparison_files)) tar.close() else: raise MadGraph5Error, \ "Could not find the comparison tarball %s."%_hc_comparison_tarball else: if not path.isdir(_hc_comparison_files): raise MadGraph5Error, \ "Could not find the comparison tarball %s."%_hc_comparison_tarball # In update = True mode, we keep track of the modification to # provide summary information modifications={'updated':[],'created':[], 'removed':[], 'missing':[]} # List all the names of the files for which modifications have been # reviewed at least once.The approach taken here is different than # with the list refusedFolder and refusedTest. # The key of the dictionary are the filenames and the value are string # determining the user answer for that file. reviewed_file_names = {} # Chose what test to cover if testKeys == 'instanceList': testKeys = self.instance_tests if verbose: print "\n== "+colored%(32,"Operational mode")+\ " : file %s ==\n"%(colored%(34,('UPDATE' if update else 'TESTING'))) for (folder_name, test_name) in testKeys: try: (iotest, iotestManager) = self.all_tests[(folder_name, test_name)] except KeyError: raise MadGraph5Error, 'Test (%s,%s) could not be found.'\ %(folder_name, test_name) if verbose: print "Processing %s in %s"%( colored%(32,test_name),colored%(34,folder_name)) files_path = iotest.run(iotestManager) try: pass # files_path = iotest.run(iotestManager) except Exception as e: iotest.clean_output() if not verbose: raise e else: print colored%(31," Test %s "%test_name+\ "crashed with the following error:\n %s."%str(e)) continue # First create the list of files to check as the user might be using # regular expressions. filesToCheck=[] # Store here the files reckognized as veto rules (with filename # starting with '-') veto_rules = [] for fname in iotest.testedFiles: # Disregard the veto rules regexp_finder = re.compile( r'^(?P<veto>-)?(?P<root_folder>.*)(\/)?\[(?P<regexp>.*)\]$') found = regexp_finder.search(fname) if not found is None: # folder without the final / base_path = pjoin(files_path,found.group('root_folder')) regexp = re.compile(found.group('regexp')) # In filesToCheck, we must remove the files_path/ prepended for root, dirnames, filenames in os.walk(base_path): for file in filenames: if not regexp.search(str(os.path.relpath( pjoin(root,file),base_path))) is None and \ not path.islink(pjoin(root,file)): new_target = os.path.relpath( pjoin(root,file),files_path) if found.group('veto')=='-': veto_rules.append(new_target) else: filesToCheck.append(new_target) else: fn = fname[1:] if fname.startswith('-') else fname if (not path.exists(pjoin(files_path,fn))) or path.islink(pjoin(files_path,fn)): if force in [0,1]: answer = Cmd.timed_input(question= """The IOTest %s does not create file '%s'. Fix it! [type 'enter'] >"""\ %(test_name,fn),default="y") modifications['missing'].append( "%s/%s/%s"%(folder_name,test_name,path.basename(fname))) if verbose: print " > [ %s ] "%(colored%(31,"MISSING"))+\ "%s/%s/%s"%(folder_name,test_name,path.basename(fname)) else: if fname.startswith('-'): veto_rules.append(fn) else: filesToCheck.append(fn) # Apply the trimming of the veto rules filesToCheck = [f for f in filesToCheck if f not in veto_rules] if update: # Remove files which are no longer used for comparison activeFiles = [self.toFileName(f) for f in filesToCheck] for file in glob.glob(pjoin(_hc_comparison_files,folder_name,\ test_name,'*')): # Ignore the .BackUp files and directories. Also ignore # a file which was previously flagged missing because it # was explicitly specified in the list of files that the # test *must* provide. if path.basename(file).endswith('.BackUp') or \ path.isdir(file) or \ pjoin(folder_name,test_name,path.basename(file)) in \ modifications['missing']: continue if path.basename(file) not in activeFiles: if force==0 or (force==1 and \ path.basename(file) not in reviewed_file_names.keys()): answer = Cmd.timed_input(question= """Obsolete ref. file %s in %s/%s detected, delete it? [y/n] >"""\ %(path.basename(file),folder_name,test_name) ,default="y") reviewed_file_names[path.basename(file)] = answer elif (force==1 and \ path.basename(file) in reviewed_file_names.keys()): answer = reviewed_file_names[path.basename(file)] else: answer = 'Y' if answer not in ['Y','y','']: if verbose: print " > [ %s ] "%(colored%(31,"IGNORED"))+\ "file deletion %s/%s/%s"%(folder_name,test_name, path.basename(file)) continue os.remove(file) if verbose: print " > [ %s ] "%(colored%(31,"REMOVED"))+\ "%s/%s/%s"%(folder_name,test_name,path.basename(file)) modifications['removed'].append( '/'.join(str(file).split('/')[-3:])) # Make sure it is not filtered out by the user-filter if self.filesChecked_filter!=['ALL']: new_filesToCheck = [] for file in filesToCheck: # Try if it matches any filter for filter in self.filesChecked_filter: # A regular expression if filter.endswith(']'): split=filter[:-1].split('[') # folder without the final / folder=split[0][:-1] if folder!=path.dirname(pjoin(file)): continue search = re.compile('['.join(split[1:])) if not search.match(path.basename(file)) is None: new_filesToCheck.append(file) break # Just the exact filename elif filter==file: new_filesToCheck.append(file) break filesToCheck = new_filesToCheck # Now we can scan them and process them one at a time # Keep track of the folders and testNames the user did not want to # create refused_Folders = [] refused_testNames = [] for fname in filesToCheck: file_path = path.abspath(pjoin(files_path,fname)) self.assertTrue(path.isfile(file_path), 'File %s not found.'%str(file_path)) comparison_path = pjoin(_hc_comparison_files,\ folder_name,test_name,self.toFileName(fname)) if not update: if not os.path.isfile(comparison_path): iotest.clean_output() if not verbose: raise MadGraph5Error,\ "Missing ref. files for test %s\n"%test_name+\ "Create them with './test_manager.py -U %s'"%test_name continue else: print colored%(31,'The ref. file %s' %str('/'.join(comparison_path.split('/')[-3:]))+' does not exist.') print colored%(34,'Consider creating it with '+ './test_manager.py -U %s'%test_name) exit(0) goal = open(comparison_path).read()%misc.get_pkg_info() if not verbose: self.assertFileContains(open(file_path), goal) else: try: self.assertFileContains(open(file_path), goal) except AssertionError: if verbose: print " > %s differs from the reference."%fname else: if not path.isdir(pjoin(_hc_comparison_files,folder_name)): if force==0: if folder_name in refused_Folders: continue answer = Cmd.timed_input(question= """New folder %s detected, create it? [y/n] >"""%folder_name ,default="y") if answer not in ['Y','y','']: refused_Folders.append(folder_name) if verbose: print " > [ %s ] folder %s"\ %(colored%(31,"IGNORED"),folder_name) continue if verbose: print " > [ %s ] folder %s"%\ (colored%(32,"CREATED"),folder_name) os.makedirs(pjoin(_hc_comparison_files,folder_name)) if not path.isdir(pjoin(_hc_comparison_files,folder_name, test_name)): if force==0: if (folder_name,test_name) in refused_testNames: continue answer = Cmd.timed_input(question= """New test %s/%s detected, create it? [y/n] >"""%(folder_name,test_name) ,default="y") if answer not in ['Y','y','']: refused_testNames.append((folder_name,test_name)) if verbose: print " > [ %s ] test %s/%s"\ %(colored%(31,"IGNORED"),folder_name,test_name) continue if verbose: print " > [ %s ] test %s/%s"\ %(colored%(32,"CREATED"),folder_name,test_name) os.makedirs(pjoin(_hc_comparison_files,folder_name, test_name)) # Transform the package information to make it a template file = open(file_path,'r') target=file.read() # So that if % appear, we cast them to %% which are not formatted. target = target.replace('%','%%') # So that the version and date is automatically updated target = target.replace('MadGraph5_aMC@NLO v. %(version)s, %(date)s'\ %misc.get_pkg_info(), 'MadGraph5_aMC@NLO v. %(version)s, %(date)s') target = target.replace('v%(version)s (%(date)s)'\ %misc.get_pkg_info(), 'v%(version)s (%(date)s)') file.close() if os.path.isfile(comparison_path): file = open(comparison_path,'r') existing = file.read() file.close() if existing == target: continue else: # Copying the existing reference as a backup tmp_path = pjoin(_hc_comparison_files,folder_name,\ test_name,self.toFileName(fname)+'.BackUp') if os.path.isfile(tmp_path): os.remove(tmp_path) file = open(tmp_path,'w') file.write(target) file.close() if force==0 or (force==1 and path.basename(\ comparison_path) not in reviewed_file_names.keys()): text = \ """File %s in test %s/%s differs by the following (reference file first): """%(fname,folder_name,test_name) text += misc.Popen(['diff',str(comparison_path), str(tmp_path)],stdout=subprocess.PIPE).\ communicate()[0] # Remove the last newline if text[-1]=='\n': text=text[:-1] if (len(text.split('\n'))<15): print text else: pydoc.pager(text) print "Difference displayed in editor." answer = '' while answer not in ['y', 'n']: answer = Cmd.timed_input(question= """Ref. file %s differs from the new one (see diff. before), update it? [y/n/h/r] >"""%fname ,default="y") if answer not in ['y','n']: if answer == 'r': pydoc.pager(text) else: print "reference path: %s" % comparison_path print "code returns: %s" % tmp_path os.remove(tmp_path) reviewed_file_names[path.basename(\ comparison_path)] = answer elif (force==1 and path.basename(\ comparison_path) in reviewed_file_names.keys()): answer = reviewed_file_names[path.basename(\ comparison_path)] else: answer = 'Y' if answer not in ['Y','y','']: if verbose: print " > [ %s ] %s"%\ (colored%(31,"IGNORED"),fname) continue # Copying the existing reference as a backup back_up_path = pjoin(_hc_comparison_files,folder_name,\ test_name,self.toFileName(fname)+'.BackUp') if os.path.isfile(back_up_path): os.remove(back_up_path) cp(comparison_path,back_up_path) if verbose: print " > [ %s ] %s"\ %(colored%(32,"UPDATED"),fname) modifications['updated'].append( '/'.join(comparison_path.split('/')[-3:])) else: if force==0 or (force==1 and path.basename(\ comparison_path) not in reviewed_file_names.keys()): answer = Cmd.timed_input(question= """New file %s detected, create it? [y/n] >"""%fname ,default="y") reviewed_file_names[path.basename(\ comparison_path)] = answer elif (force==1 and path.basename(\ comparison_path) in reviewed_file_names.keys()): answer = reviewed_file_names[\ path.basename(comparison_path)] else: answer = 'Y' if answer not in ['Y','y','']: if verbose: print " > [ %s ] %s"%\ (colored%(31,"IGNORED"),fname) continue if verbose: print " > [ %s ] %s"%\ (colored%(32,"CREATED"),fname) modifications['created'].append( '/'.join(comparison_path.split('/')[-3:])) file = open(comparison_path,'w') file.write(target) file.close() # Clean the iotest output iotest.clean_output() # Monitor the modifications when in creation files mode by returning the # modifications dictionary. if update: return modifications else: return 'test_over'
def get_MadLoop_Banner(cls, style='classic', color='blue', top_frame_char = '=', bottom_frame_char = '=', left_frame_char = '{',right_frame_char = '}', print_frame=True, side_margin = 7, up_margin = 1): """ Writes out MadLoop banner.""" colors = {'black':30,'red':31,'green':32,'yellow':33, 'blue':34,'magenta':35,'cyan':36,'lightred':91,'lightgreen':92, 'lightyellow':93,'lightblue':94,'lightmagenta':95,'lightcyan':96, 'white':97,'none':-1} if style.lower()=='random': color = random.choice(['blue','green','red']) reference = "Ref: arXiv:1103.0621v2, arXiv:1405.0301" version = "v%(version)s (%(date)s)"%misc.get_pkg_info() versionref = "%s, %s"%(version,reference) if style.lower() not in cls.get_style_keys()+['random']: raise MadGraph5Error('Incorrect style in MadLoopBanner. Must be'+\ ' one of the following: %s'%str(cls.get_style_keys()+['random'])) if isinstance(color,int): color_start ="char(27)//'[%im"%int color_end = "char(27)//'[0m" elif color.lower() in colors: if color.lower()=='none': color_start = "" color_end = "" else: color_start ="char(27)//'[%im"%colors[color.lower()] color_end = "char(27)//'[0m" else: raise MadGraph5Error('Incorrect color in MadLoopBanner. Must be and'+\ ' intenger or one of the following: %s'%str(colors.keys())) def format_banner(banner): """ Format the raw banner text to give it a frame, colors and a margin.""" def fw(*args): """Fortran write line""" elems = [] for arg in args: if arg.startswith('char('): elems.append("%s'"%arg) continue # Hard-set the single and double quotes in the text to # make sure it is not processed by the FileWriter. arg = arg.replace("'","'//char(39)//'") arg = arg.replace('"',"'//char(34)//'") if len(arg)>0: elems.append("'%s'"%arg) return "write(*,*) %s"%("//".join(elems)) banner_lines = banner.split('\n') formatted_lines = [] # Determine the target width width = side_margin*2 + max(len(line) for line in banner_lines) if print_frame: width += 2 # Print the upper frame if print_frame: formatted_lines.append(fw(" %s "%(top_frame_char*(width-2)))) # Print the upper margin for i in range(up_margin): formatted_lines.append(fw("%(lside)s%(width)s%(rside)s"% {'lside':left_frame_char if print_frame else '', 'rside':right_frame_char if print_frame else '', 'width':' '*(width-2)})) # Now print the banner for line in banner_lines: line_elements = [] line_elements.append((left_frame_char if print_frame else '')+' '*side_margin) # Colorize the logo line_elements.append(color_start) # Make sure to write the reference in black found = False for tag in [versionref, reference, version]: if tag in line: line_elements.extend([line[:line.index(tag)], color_end,tag,color_start, line[line.index(tag)+len(tag):]+ ' '*(width-2*(side_margin+1)-len(line))]) found = True break if not found: line_elements.append(line+ ' '*(width-2*(side_margin+1)-len(line))) line_elements.append(color_end) line_elements.append(' '*side_margin+(right_frame_char if print_frame else '')) formatted_lines.append(fw(*line_elements)) # Print the lower margin (of height equal to up margin) for i in range(up_margin): formatted_lines.append(fw("%(lside)s%(width)s%(rside)s"% {'lside':left_frame_char if print_frame else '', 'rside':right_frame_char if print_frame else '', 'width':' '*(width-2)})) # Print the lower frame if print_frame: formatted_lines.append(fw(" %s "%(bottom_frame_char*(width-2)))) return '\n'.join(formatted_lines) # Now we define the raw banner text for each style: return format_banner( cls.get_raw_banner(style.lower()) %{'versionref':versionref, 'ref':reference, 'version':version})