import matplotlib.pyplot as plt from math import pi as Pi def prettyFloat(float): strFloat = `float`[:15] if(strFloat[:-1][-3:] == '000'): strFloat = strFloat[:-1] while(strFloat[-1] == '0'): strFloat = strFloat[:-1] if(strFloat[:-1][-3:] == '999'): strFloat = strFloat[:-1] while(strFloat[-1] == '9'): strFloat = strFloat[:-1] if(strFloat[-1] == '.'): strFloat = strFloat[:-1] return strFloat class plotMaker: def __init__(__self__, strDir = "", bDoSubfig=True): __self__.bDoSubfig = bDoSubfig __self__.nNumFigures = 0 __self__.bChangeAlpha = True __self__.strFname = "track_lambda_sim.txt" __self__.strDir = strDir __self__.colors = ['k', 'm', 'r','g','b'] __self__.bDoSubfig = bDoSubfig def setFile(__self__, strFname): __self__.strFname = strFname def setSubfigures(__self__, bDoSubfig): __self__.bDoSubfig = bDoSubfig def changeDir(__self__, strNewDir): __self__.strDir = strNewDir def plotFromFile(__self__, strFname="", strPlotTitle=0): if(strFname == ""): strFname = __self__.strFname strFnameSplit = strFname.split('\\')[-1].split('/')[-1] if(strPlotTitle == ""): strPlotTitle = strFname.split(strFnameSplit)[0] __self__.strPlotTitle = strPlotTitle iFile = open(__self__.strDir + strFname,'r') lines = iFile.readlines() iFile.close() (lPlus, lMinus) = map(float, lines[0].split('\t')) print `(lMinus, lPlus)` lineLists = map(lambda l: l.split('\t'), lines[1:]) titles = lineLists[0] floatData = map(lambda l: map(float, l), lineLists[1:]) fdt = map(lambda IDX: map(lambda l: l[IDX], floatData),range(0, len(titles))) if(len(titles) == 2): __self__.plotOneList(fdt[1], fdt[0], titles[1],lPlus) else: if(len(titles) == 3): __self__.plotTwoLists(fdt[1], fdt[2], fdt[0], titles[1], titles[2],lPlus) else: __self__.plotMultiList(fdt[1:], fdt[0], titles[1:], lPlus) return def clsEqvAng(__self__, angle, closeTo): lTest = map(lambda i: angle+2*Pi*i, range(-3,4)) diffMags = map(lambda ang: abs(ang-closeTo), lTest) minDiff = min(diffMags) minIndices = filter(lambda IDX: diffMags[IDX] == minDiff, range(0, len(diffMags))) return lTest[minIndices[0]] def fixAngDiscont(__self__, lIn): lOut = lIn[:1] lastAng = lOut[0] for ang in lIn[1:]: lastAng = __self__.clsEqvAng(ang, lastAng) lOut = lOut+[lastAng] return lOut def getAngPert(__self__, ang, closeTo): return 2*Pi*round((closeTo-ang)/(2*Pi)) def plotMultiList(__self__, lData, tSeries, strTitles, lPlus): colors = __self__.colors __self__.nNumFigures += 1 plt.figure(__self__.nNumFigures) if(__self__.bDoSubfig): ax1 = plt.subplot(311) if(__self__.strPlotTitle != 0): if(__self__.bDoSubfig): plt.title(__self__.strPlotTitle) else: plt.title(__self__.strPlotTitle +' : ' + strTitles[0]) print 'lPlus is ' + `lPlus` plt.plot(tSeries, lData[0], 'k') plt.plot(tSeries, map(lambda x: lPlus, tSeries), 'r--') plt.text(tSeries[-1], lPlus, '$\lambda_+ = '+prettyFloat(lPlus)+'$', color='r') plt.ylabel(strTitles[0]) if(not __self__.bDoSubfig): plt.xlabel('Number of communication rounds') if(__self__.strPlotTitle != 0): plt.title(__self__.strPlotTitle +' : ' + strTitles[1]) if(__self__.bDoSubfig): ax2 = plt.subplot(312) else: __self__.nNumFigures += 1 plt.figure(__self__.nNumFigures) scatter = plt.plot(tSeries, lData[1], 'ro') plt.setp(scatter, alpha=0.15) plt.setp(scatter, markersize=20.0) if(len(tSeries) > 1000): if(__self__.bChangeAlpha): newAlpha = 0.15*(1000.0/len(tSeries)) plt.setp(scatter, alpha=newAlpha) else: newRad = 20.0*(1000.0/len(tSeries)) plt.setp(scatter, markersize=newRad) plt.ylabel(strTitles[1]) if(not __self__.bDoSubfig): plt.xlabel('Number of communication rounds') if(__self__.strPlotTitle != 0): plt.title(__self__.strPlotTitle +' : ' + 'Agent angle relative to origin') if(__self__.bDoSubfig): ax3 = plt.subplot(313) else: __self__.nNumFigures += 1 plt.figure(__self__.nNumFigures) for i in range(2, len(lData)): lData[i] = __self__.fixAngDiscont(lData[i]) for i in range(3, len(lData)): angPert = __self__.getAngPert(lData[i][-1], lData[i-1][-1]) lData[i] = map(lambda x: x+angPert, lData[i]) for i in range(2, len(lData)): series = lData[i] currColor = colors[i%len(colors)] lines = plt.plot(tSeries, series, currColor) # lines2 = plt.plot(tSeries, map(lambda x: x+2*Pi, series), currColor+'--') plt.text(tSeries[1], series[1], strTitles[i], color=currColor) plt.ylabel("Angle") plt.xlabel('Number of communication rounds') print 'Type \"plt.show()\" to show plot' return def plotTwoLists(__self__, l1, l2, tSeries, strTitle1, strTitle2, lPlus): __self__.nNumFigures += 1 plt.figure(__self__.nNumFigures) plt.subplot(211) print 'lPlus is ' + `lPlus` plt.plot(tSeries, l1, 'k') plt.plot(tSeries, map(lambda x: lPlus, tSeries), 'r--') plt.text(tSeries[-1], lPlus, '$\lambda_+ = '+prettyFloat(lPlus)+'$', color='r') plt.ylabel(strTitle1) plt.xlabel('Number of communication rounds') plt.subplot(212) scatter = plt.plot(tSeries, l2, 'ro') plt.setp(scatter, alpha=0.15) plt.setp(scatter, markersize=20.0) if(len(tSeries) > 1000): if(__self__.bChangeAlpha): newAlpha = 0.15*(1000.0/len(tSeries)) plt.setp(scatter, alpha=newAlpha) else: newRad = 20.0*(1000.0/len(tSeries)) plt.setp(scatter, markersize=newRad) plt.ylabel(strTitle2) plt.xlabel('Number of communication rounds') print 'Type \"plt.show()\" to show plot' return def plotOneList(__self__,l1, tSeries, strTitle1, lPlus): __self__.nNumFigures += 1 plt.figure(__self__.nNumFigures) plt.plot(tSeries, l1, 'k') print 'lPlus is ' + `lPlus` plt.plot(tSeries, map(lambda x: lPlus, tSeries), 'r--') plt.text(tSeries[-1], lPlus, '$\lambda_+ = '+prettyFloat(lPlus)+'$', color='r') plt.ylabel(strTitle1) plt.xlabel('Number of communication rounds') plt.xlabel('Number of communication rounds') print 'Type \"plt.show()\" to show plot' return