import numpy as np import bnpy; # bnpy package for learning and plotting BNP models import DDToyHMM # Module describing the toy dataset in question import glob; # for simple check that saved runs exist import os; from bnpy.viz.PlotUtil import pylab %pylab inline bnpy.viz.PlotUtil.ConfigPylabDefaults(pylab); doSaveEPS = 1; # Switch this to 1 to save these plots to .eps, for LaTeX publications. pathPattern = "%s/%s/%s" % (os.environ['BNPYOUTDIR'], 'DDToyHMM', 'nipsexperiment*') dirList = glob.glob(pathPattern) if len(dirList) == 0: raise ValueError("STOP! You have not run the expected experiments yet!") print "Found %d directories for jobs under the name 'nipsexperiment' for toy dataset DDToyHMM" % (len(dirList)) import Helper_DDToyHMM as Helper reload(Helper); Jdict = Helper.setUp(lineStyleByKey='Sticky') for key in Jdict.keys(): print key Jnonsticky = Helper.getSubsetByName(Jdict, 'Sticky=0') Jsticky = Helper.getSubsetByName(Jdict, 'Sticky=50') J50 = Helper.getSubsetByName(Jdict, 'K=50') DDToyHMM.transPi DDToyHMM.illustrate(Colors=Helper.getStateSeqColorMap()); pylab.xticks([-30, -15, 0, 15, 30]); pylab.yticks([-30, -15, 0, 15, 30]); if doSaveEPS: pylab.savefig('DDToyHMM_Illustrated.eps', bbox_inches='tight') Helper.makeLegendInOwnFigure(Jdict, names=['sampler', 'stoch', 'memo', 'delmerge:delete,merge', 'birth:birth,delete,merge', 'Sticky=0:Non-stick, kappa=0', 'Sticky=50:Sticky, kappa=50']) if doSaveEPS: pylab.savefig('LegendForAllAlgorithms.eps', bbox_inches='tight'); !sed -i -e 's/BoundingBox: 174 276 437 515/BoundingBox: 230 375 430 505/' LegendForAllAlgorithms.eps Helper.PlotUtil.plotKeff(J50, loc=None, xscale='log'); pylab.ylim([0, 60]); pylab.gca().set_yticks([8, 25, 50]); pylab.gca().set_yticklabels(['*8', '25', '50']); if doSaveEPS: pylab.savefig('DDToyHMM_KeffVsLaps_Kinit=50.eps', bbox_inches='tight'); Helper.PlotUtil.plotHammingDist(J50, loc=None, xscale='log'); if doSaveEPS: pylab.savefig('DDToyHMM_HammingVsLaps_Kinit=50.eps', bbox_inches='tight'); # Setup Helper.PlotUtil.LineStyleMap = Helper.getLineStyleMap_ByKValue() pylab.gca().set_yticks([8, 25, 50]); Helper.PlotUtil.plotKeff(Jnonsticky, loc=None, xscale='log'); pylab.ylabel('num states K'); if doSaveEPS: pylab.savefig('DDToyHMM_KeffVsLaps_nonsticky.eps', bbox_inches='tight'); Helper.PlotUtil.plotHammingDist(Jnonsticky, loc=None, xscale='log'); if doSaveEPS: pylab.savefig('DDToyHMM_HammingVsLaps_nonsticky.eps', bbox_inches='tight'); Helper.PlotUtil.plotELBO(Jnonsticky, loc=None, xscale='log'); if doSaveEPS: pylab.savefig('DDToyHMM_ELBOVsLaps_nonsticky.eps', bbox_inches='tight'); Helper.PlotUtil.plotKeff(Jsticky, loc=None, xscale='log'); pylab.ylabel('num states K'); if doSaveEPS: pylab.savefig('DDToyHMM_KeffVsLaps_hmmKappa=50.eps', bbox_inches='tight'); Helper.PlotUtil.plotHammingDist(Jsticky, loc=None, xscale='log'); if doSaveEPS: pylab.savefig('DDToyHMM_HammingVsLaps_hmmKappa=50.eps', bbox_inches='tight'); Helper.PlotUtil.plotELBO(Jsticky, loc=None, xscale='log'); if doSaveEPS: pylab.savefig('DDToyHMM_ELBOVsLaps_hmmKappa=50.eps', bbox_inches='tight'); seqNames = ['', '', '', '', '', '', ''] sequences = [1,3,5,7] xticks = [0, 200, 400, 600, 800] def MakeTitleForSegmentation(algName, lapQuery, Kinit=50.0): ''' Reads info from stored job on disk to make title with stats on segmentation. Returns ------- titleStr : string of form "sampler after 100 min (30 laps). Kinit=50, Kfinal=8." ''' lapQuery = str(lapQuery) Kinit = str(Kinit) path = Helper.PlotUtil.MakePath(Jnonsticky[algName + ' K=' + str(int(float(Kinit)))] + "/1/") lineNum = !grep -n $lapQuery $path/laps-saved-params.txt lineNum = lineNum[0].split(":")[0] # convert 25:1000 to 25 lineNum_p = lineNum + "p" lap = !sed -n $lineNum_p $path/laps-saved-params.txt time_sec = !sed -n $lineNum_p $path/times-saved-params.txt Kfinal = !sed -n $lineNum_p $path/Keff-saved-params.txt lap = lap[0] time_sec = time_sec[0] Kfinal = Kfinal[0] time_min = float(time_sec) / 60 # convert to minutes return "%s: K=%s after %s laps in %.0f min." % (algName, Kfinal, lapQuery, time_min) MakeTitleForSegmentation("sampler", 1000) lapToShow = 2000 algName = 'sampler' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow)); if doSaveEPS: pylab.savefig('DDToyHMM_EstZ_%s_lap=%d.eps' % (algName, lapToShow)); lapToShow = 5000 algName = 'sampler' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow)); if doSaveEPS: pylab.savefig('DDToyHMM_EstZ_%s_lap=%d.eps' % (algName, lapToShow)); lapToShow = 2000 algName = 'stoch' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow)); if doSaveEPS: pylab.savefig('DDToyHMM_EstZ_%s_lap=%d.eps' % (algName, lapToShow)); lapToShow = 100 algName = 'delmerge' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow).replace('delmerge', 'delete,merge')); if doSaveEPS: pylab.savefig('DDToyHMM_EstZ_%s_lap=%d.eps' % (algName, lapToShow)); lapToShow = 0 algName = 'sampler' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow)); lapToShow = 0 algName = 'memo' ax = Helper.PlotUtil.plotStateSeq(Jnonsticky[algName + ' K=50'], taskids='1', sequences=sequences, seqNames=seqNames, xticks=xticks, showELBOInTitle=0, lap=lapToShow) ax[0].set_title(MakeTitleForSegmentation(algName, lapToShow));