[116] | 1 | import matplotlib.pyplot as plt |
---|
| 2 | from mpl_toolkits.mplot3d import axes3d |
---|
| 3 | import numpy as np |
---|
| 4 | import os |
---|
| 5 | import sys |
---|
| 6 | |
---|
| 7 | def wykres(threadsy,capacitiesy,wyniki,iteracje,atrybut): |
---|
| 8 | kolory=['red','green','blue'] |
---|
| 9 | alfy=[0.1,0.1,0.8] |
---|
| 10 | fig = plt.figure() |
---|
| 11 | ax = fig.add_subplot(111, projection='3d') |
---|
| 12 | ax.view_init(30,-150) |
---|
| 13 | for i,iteracja in enumerate(iteracje): |
---|
| 14 | X, Y = np.meshgrid(threadsy, capacitiesy) #X,Y are arrays of same size, 2D, with separated indexes |
---|
| 15 | Z=np.empty(X.shape) |
---|
| 16 | Z.fill(np.nan) #instead of nan, one could set -10 to clearly see on the plot which values are missing |
---|
| 17 | for wynik in wyniki: |
---|
| 18 | threads,capacity,slownik=wynik |
---|
| 19 | if slownik['iter']==iteracja: |
---|
| 20 | print threads,capacity,slownik[atrybut] if atrybut in slownik else np.nan |
---|
| 21 | threadsindex=threadsy.index(threads) |
---|
| 22 | capacitiesindex=capacitiesy.index(capacity) |
---|
| 23 | Z[capacitiesindex,threadsindex]=slownik[atrybut] if atrybut in slownik else np.nan |
---|
| 24 | print Z |
---|
| 25 | ktore=len(kolory)-len(iteracje)+i |
---|
| 26 | ax.plot_surface(X, Y, Z, rstride=1, cstride=1, color=kolory[ktore], linewidth=1, alpha=alfy[ktore],edgecolor=(0,0,0,alfy[ktore])) |
---|
| 27 | ax.set_xlabel('threads') |
---|
| 28 | ax.set_ylabel('capacity') |
---|
| 29 | ax.set_zlabel(atrybut) |
---|
| 30 | ax.set_xticks(threadsy) |
---|
| 31 | ax.set_yticks(capacitiesy) |
---|
| 32 | ax.set_zlim((0,ax.get_zlim()[1])) #scale from (forced) zero to auto |
---|
| 33 | plt.savefig("wykres_%d_%s.png" % (iteracja,atrybut)) |
---|
| 34 | #plt.show() #if you wanted to rotate the chart interactively |
---|
| 35 | plt.close() |
---|
| 36 | |
---|
| 37 | |
---|
| 38 | |
---|
| 39 | |
---|
| 40 | |
---|
| 41 | ####################################################### main ####################################################### |
---|
| 42 | |
---|
| 43 | threadsy=set() #used to store values of 'threads' that are in use. This is helpful because later a single multi-dimensional numpy array is used to keep all results |
---|
| 44 | capacitiesy=set() #used to store values of 'capacity' that are in use |
---|
| 45 | wyniki=[] #list of all results |
---|
| 46 | |
---|
| 47 | os.chdir(sys.argv[1]) #files with plots will also be created in that directory |
---|
| 48 | for plik in os.listdir('.'): |
---|
| 49 | if plik.endswith(".out") and plik.count('_')==2: |
---|
| 50 | podzielone=plik.replace('.','_').split('_') |
---|
| 51 | threads=int(podzielone[1]) |
---|
| 52 | capacity=int(podzielone[2]) |
---|
| 53 | threadsy.add(threads) |
---|
| 54 | capacitiesy.add(capacity) |
---|
| 55 | plik = open("test_%d_%d.out" % (threads,capacity), "r") #should be the same name as the original file |
---|
| 56 | slowniki = [] |
---|
| 57 | for linia in plik: |
---|
| 58 | linia=linia.strip() |
---|
| 59 | if linia.startswith("[INFO] Script::Message - iter="): |
---|
| 60 | slownik={} |
---|
| 61 | for pole in linia.split(' '): |
---|
| 62 | if '=' in pole: |
---|
| 63 | pole=pole.strip(',') |
---|
| 64 | pole=pole.split('=') |
---|
| 65 | slownik[pole[0]]=float(pole[1]) |
---|
| 66 | print slownik |
---|
| 67 | slowniki.append(slownik) |
---|
| 68 | wyniki.append((threads,capacity,slownik)) |
---|
| 69 | plik.close() |
---|
| 70 | |
---|
| 71 | threadsy=sorted(list(threadsy)) |
---|
| 72 | capacitiesy=sorted(list(capacitiesy)) |
---|
| 73 | print threadsy |
---|
| 74 | print capacitiesy |
---|
| 75 | |
---|
| 76 | wykres(threadsy,capacitiesy,wyniki,[3.0,6.0,9.0],'simsteps') |
---|
| 77 | wykres(threadsy,capacitiesy,wyniki,[3.0,6.0,9.0],'evals') |
---|
| 78 | wykres(threadsy,capacitiesy,wyniki,[3.0,6.0,9.0],'migrations') |
---|
| 79 | wykres(threadsy,capacitiesy,wyniki,[4.0,9.0],'fit_avg') |
---|
| 80 | wykres(threadsy,capacitiesy,wyniki,[4.0,9.0],'fit_max') |
---|
| 81 | |
---|
| 82 | #draws simple 2D plots of progress in consecutive iter's: |
---|
| 83 | #x=[] |
---|
| 84 | #y1=[] |
---|
| 85 | #y2=[] |
---|
| 86 | #for s in slowniki: |
---|
| 87 | # x.append(s['iter']) |
---|
| 88 | # y1.append(s['avg_fit']) |
---|
| 89 | # y2.append(s['max_fit']) |
---|
| 90 | #plt.plot(x,y1) |
---|
| 91 | #plt.plot(x,y2) |
---|
| 92 | #plt.savefig("avg_%d_%d.png" % (threads,capacity)) |
---|
| 93 | #plt.close() |
---|
| 94 | |
---|