⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 average-oldgraphs.py

📁 用于OMNeT++的模板
💻 PY
字号:
#!/usr/bin/pythonimport os,pprint,refrom histopost import HistoPlotfrom lyxtable import lyxtablefiles = [[x.split('-'),[]] for x in os.listdir('output') if x.find('log')==0 and x.find('err')!=-1]#log-parameters-a0.45-r14-v0.3-n10-p2false-fl1-Node_Statistic-PathPA-250-scenario100-0.3-0--1-0-0-0-0-todo = [([],x) for x in files]def subtree(tree,path):	pcopy = path[:]	ret = tree	while len(pcopy)>0:		loc = pcopy.pop(0)		#print loc		for (val,children) in ret:			if loc == val:				ret = children				break		else:			print "loc,path",loc,path			raise Exception	return retdef listtree(tree,path,maxdepth=-1):	neigh = subtree(tree,path)	if maxdepth == 0 or len(neigh)==0:		return [reduce(lambda x,y:x+y,path,[])]	ret = []	for (child,children) in neigh:		#print "path+child",path+[child]		ret.extend(listtree(tree,path+[child],maxdepth-1))	#print "ret",ret	return retpattern = re.compile("POSITIONED (\d+\.\d+) (\d+)")statistic = re.compile("BAD (\d+\.\d+) (\d+)")def results(tree,path, first_run):	outfile = "output/res-"+"-".join(reduce(lambda x,y:x+y,path,[]))	if not os.path.exists(outfile):		print "outfile",outfile		lt = listtree(tree,path)		#print "listtree",path,lt		ret = ["-".join(x) for x in lt]		files = "output/"+" output/".join(ret)		#print "ret",ret		#print "files",files		cmd = "cat "+files+"| ../../stats.py > "+outfile		os.system(cmd)		#os.system("rm "+outfile)	neigh = subtree(tree,path)	#print neigh		if len(neigh[0][1])>0:		print "path",path		for x in neigh:			results(tree,path+[x[0]],first_run)			if first_run:				continue			try:				ret = []				print "path2",path,x[0]				if len(x[1][0][1])==0:					continue				print "len",x[1][0][1]				ne = x[0]				print "x[1]",[z[0] for z in x[1]]				poss = [z[0] for z in x[1]]				endfix = []				#paths = listtree(tree,path+[x[0]],2)				endfix = os.path.commonprefix([x[::-1] for x in poss])				endfix = endfix[::-1]				#print "paths",paths				ret = [x[:-len(endfix)] for x in poss]				#print prefix,ret[0]				endfix = "-".join(endfix)				gp = []				ret.sort(lambda x,y:cmp(x[::-1],y[::-1]))				keep = [0 for x in ret[0]]				for x in range(len(ret[0])):					for z in ret:						if ret[0][x] != z[x]:							keep[x] = 1							break				#print keep								ret = [[x] for x in ret]				for z in ret:					z.append([z[0][x] for x in range(len(z[0])) if keep[x]==1])				#print ret[0][0],keep				#print "no keep",[ret[0][0][x] for x in range(len(ret[0][0])) if keep[x]==0]				print "path",(path+[ne])[-1]				pth = "-".join(reduce(lambda x,y:x+y,path+[ne]))				print "pth",pth				values = []				for (x,title) in ret:					print "x,title",x,title					x = "-".join(x)					if x == "":						f = open("output/res-"+pth)						title = "blank"					else:						f = open("output/res-"+pth+"-"+x+"-"+endfix)					check = "".join(f.readlines())					f.close()					data = pattern.match(check)					if data == None:						print check						raise Exception					shortname = "-".join(title).replace("Node_","")					bad = statistic.search(check)					if bad == None:						print shortname,check						raise Exception					gp.append((shortname+" (good)",float(data.group(1))))					gp.append((shortname+" (bad)",float(bad.group(1))))					pos_val = int(data.group(2))					bad_val = int(bad.group(2))					print "check",check					print "bad",bad.groups()					print "data",data.groups()					gp.append((shortname+" (averaged)",((float(data.group(1))*pos_val)+(float(bad.group(1))*bad_val))/(bad_val+pos_val)))					values.append((shortname,"good","%.2f%%" % float(data.group(1)),pos_val))					values.append((shortname,"bad","%.2f%%" % float(bad.group(1)),bad_val))					values.append((shortname,"averaged","%.2f%%" % gp[-1][1],pos_val+bad_val))								print "prefix",pth				print "gp",gp				vals = [x[1] for x in gp]				print vals				print sum(vals[:3])/3,sum(vals[4:])/3				#HistoPlot(gp,"graph-"+pth+".ps","") #" ".join((path+[ne])[-1]))				#values.sort(lambda x,y:cmp(x[1],y[1]))				#lyxtable("table-"+pth+".lyx", ["Algorithm","Node class","Average error","Number of Nodes"],values)				#f = file("table-"+pth,'w')				#for (name,percentage,value) in values:				#	f.write(" ".join([name,str(percentage),str(value)])+"\n")				#f.close()								#raise Exception			except:				print ""				print "PROBLEM!"				#print "paths",paths				#print "prefix",prefix				print "endfix",endfix				print "ret",ret				#print "ne",ne				print "path",path				raisetry:	while len(todo)>0:		(depth,set) = todo.pop()		neigh = subtree(files,depth)		#print "depth,set",(depth,set)		try:			neigh.index(set)		except ValueError:			continue		#print neigh				subset = [[x[0][1:],x[1]] for x in neigh if len(x[0])>0 and x[0][0] == set[0][0]]		if len(subset)<2:			continue		if len(subset)==1:			#print subset			neigh = subtree(files,depth[:-1])			print depth			print subset			print neigh[0][1]			raise Exception		for x in subset:			#print "x",x,set[0][0]							check = [[set[0][0]]+x[0],x[1]]			#print "check", check			#print "neigh[-1]",neigh[-1]			neigh.remove(check)		app = [[set[0][0]]]+[subset]		#pprint.pprint(app)		neigh.append(app)		#print "set[0][0]",set[0][0]		for x in subset:			app = (depth+[set[0][0]],x)			#print app			todo.append((depth+[[set[0][0]]],x))	todo = [([],x) for x in files]	while len(todo)>0:		(depth,(curr,children)) = todo.pop()		if len(children)==1:			neigh = subtree(files,depth)			neigh.remove([curr,children])			(c_name,c_children) = children[0]			neigh.append([curr+c_name,c_children])			todo.append((depth,[curr+c_name,c_children]))		else:			#print "depth",depth			#print children			for x in children:				todo.append((depth+[curr],x))	results(files,[],first_run=True)	results(files,[],first_run=False)finally:	#pprint.pprint(files)	pass#print files

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -