Best Python code snippet using tox_python
evaluation.py
Source:evaluation.py
1import sys2import math3import json4import fractions5# this function checks for the type of parsed string6def typeofvalue(text):7 try:8 int(text)9 return int10 except ValueError:11 pass12 try:13 float(text)14 return float15 except ValueError:16 pass17 try:18 fractions.Fraction(text)19 return fractions.Fraction20 except ValueError:21 pass22 return str23def fracttofloat(value):24 try:25 float(value)26 return float(value)27 except ValueError:28 return float(fractions.Fraction(value))29if len(sys.argv) > 2:30 if not len(sys.argv) == 3:31 print('usage (for decomposition solve output): '+sys.argv[0]+'-d <soplex_test_run>.out')32 quit()33elif not len(sys.argv) == 2:34 print('usage: '+sys.argv[0]+' <soplex_test_run>.out')35 quit()36# if the -d flag is provided, then we must produce the decomposition solve output37decomp = False38if sys.argv[1] == '-d':39 decomp = True40# specify columns for the output (can be modified)41columns = ['name','rows','cols','pviol','dviol','iters','polish','refs','solvetime','value','status']42ncolumns = len(columns)43if decomp:44 decompcolumns = ['ppiv','dpiv','avgpdegen','avgddegen','redprob','compprob','rediter','compiter','algiter']45 columns.extend(decompcolumns)46if decomp:47 outname = sys.argv[2]48else:49 outname = sys.argv[1]50dataname = outname.replace('.out','.json')51outfile = open(outname,'r')52outlines = outfile.readlines()53outfile.close()54testset = outname.split('/')[-1].split('.')[1]55testname = 'testset/'+testset+'.test'56soluname = 'testset/'+testset+'.solu'57# maximum length of instance names58namelength = 1859# tolerance for solution value check60tolerance = 1e-661instances = {}62stats = False63printedIdentifier = False64section = 'soplex'65for idx, outline in enumerate(outlines):66 # print identifier67 if outline.startswith('@01'):68 # convert line to used instance name69 linesplit = outline.split('/')70 linesplit = linesplit[-1].rstrip(' \n').rstrip('.gz').rstrip('.GZ').rstrip('.z').rstrip('.Z')71 linesplit = linesplit.split('.')72 instancename = linesplit[0]73 for i in range(1, len(linesplit)-1):74 instancename = instancename + '.' + linesplit[i]75 length = len(instancename)76 if length > namelength:77 shortname = instancename[0:int(namelength/2)-1] + '~' + instancename[length-int(namelength/2):]78 else:79 shortname = instancename80 # initialize new data set81 instances[instancename] = {}82 instances[instancename]['status'] = 'abort'83 instances[instancename]['name'] = shortname84 # wait for statistics block85 stats = False86 # invalidate instancename87 elif outline.startswith('=ready='):88 section = 'soplex'89 instancename = ''90 elif outline.startswith('=perplex='):91 section = 'perplex'92 stats = False93 instances[instancename]['perplex'] = 'unknown'94 elif outline.startswith('=qsoptex='):95 section = 'qsoptex'96 stats = False97 instances[instancename]['qso:stat'] = 'unknown'98 instances[instancename]['qso:lpval'] = '--'99 instances[instancename]['qso:time'] = instances[instancename]['timelimit']100 instances[instancename]['qso:prec'] = 64101 elif section == 'perplex':102 if outline.find('No such file or directory') >= 0:103 instances[instancename]['perplex'] = 'readerror'104 elif outline.startswith('Basis read'):105 instances[instancename]['perplex'] = 'timeout'106 elif outline.startswith('Solution is optimal'):107 if instances[instancename]['perplex'] == 'dinfeas':108 instances[instancename]['perplex'] = 'optimal'109 elif instances[instancename]['perplex'] == 'pdinfeas' or instances[instancename]['perplex'] == 'timeout':110 instances[instancename]['perplex'] = 'pinfeas'111 elif outline.startswith('Solution is feasible'):112 if instances[instancename]['perplex'] == 'pinfeas':113 instances[instancename]['perplex'] = 'optimal'114 elif instances[instancename]['perplex'] == 'pdinfeas' or instances[instancename]['perplex'] == 'timeout':115 instances[instancename]['perplex'] = 'dinfeas'116 elif outline.startswith('Solution is not ') and instances[instancename]['perplex'] == 'timeout':117 instances[instancename]['perplex'] = 'pdinfeas'118 elif section == 'qsoptex':119 if outline.startswith('Time for SOLVER:'):120 instances[instancename]['qso:time'] = float(outline.split()[-2])121 elif outline.find('Problem Solved Exactly') >= 0:122 instances[instancename]['qso:stat'] = 'optimal'123 elif outline.find('Problem Is Infeasible') >= 0:124 instances[instancename]['qso:stat'] = 'infeasible'125 elif outline.startswith('@24') and instances[instancename]['qso:time'] >= instances[instancename]['timelimit']:126 instances[instancename]['qso:stat'] = 'timeout'127 elif outline.startswith('LP Value'):128 instances[instancename]['qso:lpval'] = outline.split()[2].rstrip(',')129 elif outline.find('Trying mpf with') >= 0:130 instances[instancename]['qso:prec'] = max( int(outline.split()[3]), instances[instancename]['qso:prec'] )131 elif outline.startswith('SoPlex version'):132 instances[instancename]['githash'] = outline.split()[-1].rstrip(']')[0:9]133 if not printedIdentifier:134 printedIdentifier = True135 print('\n')136 print(outline)137 elif outline.startswith('Primal solution infeasible') or outline.startswith('Dual solution infeasible'):138 instances[instancename]['status'] = 'fail'139 elif outline.startswith('real:timelimit'):140 instances[instancename]['timelimit'] = float(outline.split()[-1])141 elif outline.startswith('Statistics'):142 stats = True143 if stats:144 if outline.startswith('SoPlex status') and not instances[instancename]['status'] == 'fail':145 if outline.find('time limit') >= 0:146 instances[instancename]['status'] = 'timeout'147 else:148 instances[instancename]['status'] = outline.split()[-1].strip('[]')149 elif outline.startswith('Solution'):150 instances[instancename]['value'] = fracttofloat(outlines[idx+1].split()[-1])151 elif outline.startswith('Original problem'):152 instances[instancename]['cols'] = int(outlines[idx+1].split()[2])153 instances[instancename]['boxedcols'] = int(outlines[idx+2].split()[2])154 instances[instancename]['lbcols'] = int(outlines[idx+3].split()[3])155 instances[instancename]['ubcols'] = int(outlines[idx+4].split()[3])156 instances[instancename]['freecols'] = int(outlines[idx+5].split()[2])157 instances[instancename]['rows'] = int(outlines[idx+6].split()[2])158 instances[instancename]['rangedrows'] = int(outlines[idx+7].split()[2])159 instances[instancename]['lhsrows'] = int(outlines[idx+8].split()[2])160 instances[instancename]['rhsrows'] = int(outlines[idx+9].split()[2])161 instances[instancename]['freerows'] = int(outlines[idx+10].split()[2])162 instances[instancename]['nonzeros'] = int(outlines[idx+11].split()[2])163 instances[instancename]['colnonzeros'] = float(outlines[idx+12].split()[3])164 instances[instancename]['rownonzeros'] = float(outlines[idx+13].split()[3])165 instances[instancename]['sparsity'] = float(outlines[idx+14].split()[2])166 instances[instancename]['minabsval'] = float(outlines[idx+15].split()[4])167 instances[instancename]['maxabsval'] = float(outlines[idx+16].split()[4])168 elif outline.startswith('Violation'):169 primviol = outlines[idx+2].split()[3]170 dualviol = outlines[idx+3].split()[3]171 if typeofvalue(primviol) in [int,float,fractions.Fraction] and typeofvalue(dualviol) in [int,float,fractions.Fraction]:172 instances[instancename]['pviol'] = float("{:.2e}".format(fracttofloat(primviol)))173 instances[instancename]['dviol'] = float("{:.2e}".format(fracttofloat(dualviol)))174 else:175 instances[instancename]['pviol'] = '-'176 instances[instancename]['dviol'] = '-'177 elif outline.startswith('Total time'):178 instances[instancename]['time'] = float(outline.split()[3])179 instances[instancename]['readtime'] = float(outlines[idx+1].split()[2])180 instances[instancename]['solvetime'] = float(outlines[idx+2].split()[2])181 instances[instancename]['preproctime'] = float(outlines[idx+3].split()[2])182 instances[instancename]['simplextime'] = float(outlines[idx+4].split()[2])183 instances[instancename]['synctime'] = float(outlines[idx+5].split()[2])184 instances[instancename]['transformtime'] = float(outlines[idx+6].split()[2])185 instances[instancename]['rationaltime'] = float(outlines[idx+7].split()[2])186 instances[instancename]['othertime'] = float(outlines[idx+8].split()[2])187 elif outline.startswith('Refinements'):188 instances[instancename]['refs'] = int(outline.split()[2])189 instances[instancename]['stallrefs'] = int(outlines[idx+1].split()[2])190 instances[instancename]['pivrefs'] = int(outlines[idx+2].split()[2])191 instances[instancename]['feasrefs'] = int(outlines[idx+3].split()[2])192 instances[instancename]['unbdrefs'] = int(outlines[idx+4].split()[2])193 elif outline.startswith('Iterations'):194 instances[instancename]['iters'] = int(outline.split()[2])195 instances[instancename]['scratchiters'] = int(outlines[idx+1].split()[3])196 instances[instancename]['basisiters'] = int(outlines[idx+2].split()[3])197 instances[instancename]['primaliters'] = int(outlines[idx+3].split()[2])198 instances[instancename]['dualiters'] = int(outlines[idx+4].split()[2])199 instances[instancename]['flips'] = int(outlines[idx+5].split()[3])200 instances[instancename]['polish'] = int(outlines[idx+6].split()[-1])201 instances[instancename]['speed'] = round(float(instances[instancename]['iters'])/max(instances[instancename]['solvetime'],tolerance),2)202 elif outline.startswith('LU factorizations'):203 instances[instancename]['lufacts'] = int(outline.split()[3])204 instances[instancename]['factortime'] = float(outlines[idx+2].split()[3])205 elif outline.startswith('LU solves'):206 instances[instancename]['lusolves'] = int(outline.split()[3])207 instances[instancename]['lusolvetime'] = float(outlines[idx+2].split()[3])208 elif decomp:209 if outline.startswith('Degeneracy'):210 instances[instancename]['ppiv'] = int(outlines[idx + 1].split()[3])211 instances[instancename]['dpiv'] = int(outlines[idx + 2].split()[3])212 instances[instancename]['primcand'] = int(outlines[idx + 3].split()[3])213 instances[instancename]['dualcand'] = int(outlines[idx + 4].split()[3])214 instances[instancename]['avgpdegen'] = float(outlines[idx + 5].split()[3])215 instances[instancename]['avgddegen'] = float(outlines[idx + 6].split()[3])216 elif outline.startswith('Algorithm Iterations'):217 instances[instancename]['algiter'] = int(outline.split()[2])218 elif outline.startswith('Decomp. Iterations'):219 instances[instancename]['rediter'] = int(outlines[idx + 3].split()[3])220 instances[instancename]['compiter'] = int(outlines[idx + 4].split()[3])221 elif outline.startswith('Red. Problem Status'):222 instances[instancename]['redprob'] = int(outline.split()[4])223 elif outline.startswith('Comp. Problem Status'):224 instances[instancename]['compprob'] = int(outline.split()[3])225# try parsing solution file226check_solu = False227try:228 with open(soluname):229 check_solu = True230except IOError:231 check_solu = False232if check_solu:233 solufile = open(soluname,'r')234 for soluline in solufile:235 solu = soluline.split()236 tag = solu[0]237 name = solu[1]238 if len(solu) == 3:239 value = solu[2]240 if typeofvalue(value) in [int,float]:241 value = float(value)242 else:243 if tag == '=inf=':244 value = 'infeasible'245 else:246 value = 'unknown'247 if name in instances:248 instances[name]['soluval'] = value249 if not instances[name]['status'] in ['timeout', 'fail', 'abort']:250 # check solution status251 if value in ['infeasible', 'unbounded']:252 if not instances[name]['status'] == value:253 instances[name]['status'] = 'inconsistent'254 elif value == 'unknown':255 instances[name]['status'] = 'not verified'256 elif (abs(instances[name]['value'] - value))/max(abs(instances[name]['value']),abs(value),tolerance) > tolerance:257 instances[name]['status'] = 'inconsistent'258 solufile.close()259# save dictionary to file later use in compare script260with open(dataname, 'w') as f:261 json.dump(instances, f)262# count solution status263fails = sum(1 for name in instances if instances[name]['status'] == 'fail')264timeouts = sum(1 for name in instances if instances[name]['status'] == 'timeout')265infeasible = sum(1 for name in instances if instances[name]['status'] == 'infeasible')266unbounded = sum(1 for name in instances if instances[name]['status'] == 'unbounded')267optimal = sum(1 for name in instances if instances[name]['status'] == 'optimal')268aborts = sum(1 for name in instances if instances[name]['status'] == 'abort')269inconsistents = sum(1 for name in instances if instances[name]['status'] == 'inconsistent')270length = []271output = ''272# calculate maximum width of each column273for i,c in enumerate(columns):274 length.append(len(c))275 for name in instances:276 length[i] = max(length[i],len(str(instances[name].get(c,''))))277 if i == ncolumns:278 output = output + ' |'279 output = output + ' ' + c.rjust(length[i] + 1)280# print column header281print(output)282print('-'*len(output))283# print data for all instances with the computed length284for name in sorted(instances):285 output = ''286 for i,c in enumerate(columns):287 if i == ncolumns:288 output = output + ' |'289 output = output + ' ' + str(instances[name].get(c, '--')).rjust(length[i] + 1)290 print(output)291print('\nResults (testset '+testname.split('/')[-1].split('.')[-2]+', settings '+outname.split('/')[-1].split('.')[-2]+'):')292print('{} total: {} optimal, {} infeasible, {} unbounded, {} timeouts, {} inconsistents, {} fails, {} aborts'.format(len(instances),optimal,infeasible,unbounded,timeouts,inconsistents,fails,aborts))293# try to check for missing files294check_test = False295try:296 with open(testname):297 check_test = True298except IOError:299 print('No testset file found to check run for completeness.')300if not check_solu:301 print('No solution file found to check objective values.')302if check_test:303 testfile = open(testname,'r')304 printedMissing = False305 for testline in testfile:306 linesplit = testline.split('/')307 linesplit = linesplit[len(linesplit) - 1].rstrip(' \n').rstrip('.gz').rstrip('.GZ').rstrip('.z').rstrip('.Z')308 linesplit = linesplit.split('.')309 instancename = linesplit[0]310 for i in range(1, len(linesplit)-1):311 instancename = instancename + '.' + linesplit[i]312 if not instancename in instances:313 if not printedMissing:314 print('\n')315 print('missing instance: '+instancename)316 printedMissing = True...
GenGMMHMMproto.py
Source:GenGMMHMMproto.py
1import os2import sys3inDIM = int(sys.argv[1])4inFEA = sys.argv[2]5inNSTATE = int(sys.argv[3])6outPROTO = sys.argv[4]7def GetIdentStr(dim, val):8 retval = ""9 for idx in range(0, dim):10 retval += " %e" % val11 return retval12outlines = []13# headline14outlines.append("~o <VECSIZE> %d <%s>" % (inDIM, inFEA))15# HMM macro16outlines.append('~h "proto"')17# <BEGINHMM>18outlines.append('<BEGINHMM>')19# <NUMSTATES>20outlines.append("<NUMSTATES> %d" % inNSTATE)21# for each state22index = 223while index < inNSTATE:24 # <STATE>25 outlines.append("<STATE> %d" % index)26 # <MEAN>27 outlines.append("<MEAN> %d" % inDIM)28 outlines.append(GetIdentStr(inDIM, 0.0))29 # <VARIANCE>30 outlines.append("<VARIANCE> %d" % inDIM)31 outlines.append(GetIdentStr(inDIM, 1.0))32 index += 133# <TRANSP>34outlines.append("<TRANSP> %d" % inNSTATE)35for ridx in range(0, inNSTATE):36 transline = ""37 for cidx in range(0, inNSTATE):38 if ridx == 0:39 if cidx == 1:40 transline += " %e" % 1.041 else:42 transline += " %e" % 0.043 elif ridx == inNSTATE - 1:44 transline += " %e" % 0.045 else:46 if cidx == ridx:47 transline += " %e" % 0.648 elif cidx == ridx + 1:49 transline += " %e" % 0.450 else:51 transline += " %e" % 0.052 outlines.append(transline)53# <ENDHMM>54outlines.append('<ENDHMM>')55file = open(outPROTO, 'w')56for eachline in outlines:57 file.write(eachline + os.linesep)...
Learn to execute automation testing from scratch with LambdaTest Learning Hub. Right from setting up the prerequisites to run your first automation test, to following best practices and diving deeper into advanced test scenarios. LambdaTest Learning Hubs compile a list of step-by-step guides to help you be proficient with different test automation frameworks i.e. Selenium, Cypress, TestNG etc.
You could also refer to video tutorials over LambdaTest YouTube channel to get step by step demonstration from industry experts.
Get 100 minutes of automation test minutes FREE!!