📄 average.py
字号:
#!/usr/bin/pythonimport os,pprint,reimport Gnuplotfiles = [[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+)")gp = Gnuplot.Gnuplot(debug=1)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 st = "stats.py" while not os.path.exists(st): st = "../"+st cmd = "cat "+files+"| "+st+" > "+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],"x[1]" if len(x[1][0][1])==0: continue if len(x[1][0][1][0][1])==0: continue #print "len",x[1] ne = x[0] print "x[1]",[z[0] for z in x[1]] poss = [z[0] for z in x[1]] lines = [] var = poss[0] #print "x1",x[1] index = [z[0] for z in x[1]].index(var) #print "var",var,[z[0] for z in x[1]].index(var) lines = [z[0] for z in x[1][index][1]] #print "lines",lines for x in lines: print "line",x for var in poss: print path+var+x endfix = [] endfix = os.path.commonprefix([e[::-1] for e in lines]) endfix = endfix[::-1] ret = [(n[:-len(endfix)],n) for n in lines] endfix = "-".join(endfix) #gp = [] ret.sort(lambda x,y:cmp(x[::-1],y[::-1])) print "ret",ret print "endfix",endfix print "path",(path+[ne])[-1] pth = "-".join(reduce(lambda x,y:x+y,path+[ne])) print "pth",pth values = [] points = [] #gp('set nomultiplot') gp('set term postscript enhanced monochrome 20') gp('set data style linespoints') gp('set key right top') gp('set size ratio 0.9') #gp('set multiplot') for (title,x) in ret: #print "x,title",x,title shortname = "-".join(title).replace("Node_","") print "shortname",shortname if shortname == "EuclRefine": shortname = "Euclidean" elif shortname == "HTRefine": shortname = "Hop-Terrain" elif shortname == "Savvides": shortname = "Multilateration" currpts = [] if shortname.find("Statistic")!=-1: currbad = [] curravg = [] poss.sort() nodecount = 2250/50 print "poss",poss for var in poss: variance = float(var[0][1:]) get = "-".join(var+x) fname = "output/res-"+pth+"-"+get print "var",var print "x",x print "fname",fname f = open(fname) check = "".join(f.readlines()) f.close() data = pattern.match(check) if data == None: print "check",check raise Exception currpts.append((variance*100,float(data.group(1)),int(data.group(2))/nodecount)) if shortname.find("Statistic")!=-1: bad = statistic.search(check) if bad == None: print shortname,check raise Exception goodval = int(data.group(2))/nodecount badval = int(bad.group(2))/nodecount currbad.append((variance*100,float(bad.group(1)),badval)) curravg.append((variance*100,((float(data.group(1))*goodval)+(float(bad.group(1))*badval))/(goodval+badval),goodval+badval)) print "good data",currpts if shortname.find("Statistic")==-1: values.append(Gnuplot.Data([z[0] for z in currpts],[z[1] for z in currpts],title=shortname)) points.append(Gnuplot.Data([z[0] for z in currpts],[z[2] for z in currpts],title=shortname)) else: values.append(Gnuplot.Data([z[0] for z in currpts],[z[1] for z in currpts],title=shortname+" (good)")) points.append(Gnuplot.Data([z[0] for z in currpts],[z[2] for z in currpts],title=shortname+" (good)")) #values.append(Gnuplot.Data([z[0] for z in currbad],[z[1] for z in currbad],title=shortname+" (bad)")) #points.append(Gnuplot.Data([z[0] for z in currbad],[z[2] for z in currbad],title=shortname+" (bad)")) values.append(Gnuplot.Data([z[0] for z in curravg],[z[1] for z in curravg],title=shortname+" (good+bad)")) points.append(Gnuplot.Data([z[0] for z in curravg],[z[2] for z in curravg],title=shortname+" (good+bad)")) gp('set xlabel "Range error variance (percentage of radio range)"') gp('set yrange [0:100]') #gp('set key right bottom') gp('set output "'+pth+'.ps"') gp('set ylabel "Average error (percentage of radio range)"') gp.plot(*values) gp('set yrange [0:100]') gp('set key right bottom') gp('set output "'+pth+'-pts.ps"') gp('set ylabel "Positioned nodes (percentage of non-anchor nodes)"') gp.plot(*points) #gp('set nomultiplot') #HistoPlot(gp,"graph-"+pth+".ps","") #" ".join((path+[ne])[-1])) #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 + -