def plotDocUsageForProposal(docUsageByUID, savefilename=None, **kwargs): ''' Make trace plot of doc usage for each component. ''' pylab.figure() L = 0 maxVal = 0 for k, uid in enumerate(docUsageByUID): ys = np.asarray(docUsageByUID[uid]) xs = np.arange(0, ys.size) if k < 6: # only a few labels fit well on a legend pylab.plot(xs, ys, label=uid) else: pylab.plot(xs, ys) L = np.maximum(L, ys.size) maxVal = np.maximum(maxVal, ys.max()) # Use big chunk of left-hand side of plot for legend display xlims = np.asarray([-0.75*L, L-0.5]) pylab.xlim(xlims) pylab.xticks(np.arange(1, L)) pylab.ylim([0, 1.1*maxVal]) pylab.xlabel('num proposal steps') pylab.ylabel('num docs using each comp') pylab.legend(loc='upper left', fontsize=12) pylab.subplots_adjust(left=0.2) if savefilename is not None: pylab.savefig(savefilename, pad_inches=0) pylab.close('all')
def plotELBOtermsForProposal( curLdict, propLdictList, xs=None, ymin=-0.5, ymax=0.5, savefilename=None, **kwargs): ''' Create trace plot of ELBO gain/loss relative to current model. ''' pylab.figure() L = len(propLdictList) if xs is None: xs = np.arange(0, L) legendKeys = [] for key in curLdict: if key.count('_') == 0: legendKeys.append(key) for key in legendKeys: if key.count('total'): linewidth= 4 alpha = 1 style = '-' else: linewidth = 3 alpha = 0.5 style = '--' ys = np.asarray([propLdictList[i][key] for i in range(L)]) ys -= curLdict[key] pylab.plot(xs, ys, style, color=_getLineColorFromELBOKey(key), linewidth=linewidth, alpha=alpha, label=key) L = L + 1 xlims = np.asarray([-0.75*L, L-0.5]) pylab.xlim(xlims) pylab.xticks(xs) pylab.plot(xlims, np.zeros_like(xlims), 'k:') pylab.xlabel('num proposal steps') pylab.ylabel('L gain (prop - current)') pylab.legend(loc='lower left', fontsize=12) pylab.subplots_adjust(left=0.2) if savefilename is not None: pylab.savefig(savefilename, pad_inches=0) pylab.close('all')
def plotCompsFromSS(hmodel, SS, outfilepath=None, **kwargs): ''' Plot components defined by provided summary statistics. Provided hmodel is just used for its hyperparameters, not current comps. Post condition -------------- Create matplotlib figure showing (subset of) SS.K components. ''' if 'xlabels' not in kwargs: xlabels = uidsAndCounts2strlist(SS) kwargs['xlabels'] = xlabels tmpModel = hmodel.copy() tmpModel.obsModel.update_global_params(SS) plotCompsFromHModel(tmpModel, **kwargs) if outfilepath is not None: pylab.savefig(outfilepath) pylab.close('all') print('Wrote: %s' % (outfilepath))
def plotManyPanelsByPVar(jpathPattern='/tmp/', pvar=None, pvals=None, W=5, H=4, savefilename=None, doShowNow=False, **kwargs): ''' Create line plots for jobs matching pattern and provided kwargs ''' if pvar is None: jpathList = [jpathPattern] pvar = None pvals = [None] else: prefixfilepath = os.path.sep.join(jpathPattern.split(os.path.sep)[:-1]) PPListMap = makePPListMapFromJPattern(jpathPattern) if pvals is None: pvals = PPListMap[pvar] else: pvals = [p for p in pvals if p in PPListMap[pvar]] jpathList = makeListOfJPatternsWithSpecificVals( PPListMap, prefixfilepath=prefixfilepath, key=pvar, vals=pvals, **kwargs) nrows = 1 ncols = len(pvals) pylab.subplots(nrows=nrows, ncols=ncols, figsize=(ncols * W, nrows * H)) axH = None for panelID, panel_jobPattern in enumerate(jpathList): axH = pylab.subplot(nrows, ncols, panelID + 1, sharey=axH, sharex=axH) # Only show legend on first plot if panelID > 0 and 'loc' in kwargs: kwargs['loc'] = None kwargs['doShowNow'] = False plotMultipleLinesByLVar(panel_jobPattern, **kwargs) if pvar is not None: pylab.title('%s=%s' % (pvar, pvals[panelID])) pylab.subplots_adjust(bottom=0.15, wspace=0.5) if savefilename is not None: try: pylab.show(block=False) except TypeError: pass # when using IPython notebook pylab.savefig(savefilename, bbox_inches='tight', pad_inches=0) elif doShowNow: try: pylab.show(block=True) except TypeError: pass # when using IPython notebook Info = dict( nrows=nrows, ncols=ncols, ) return Info
def plotMultipleLinesByLVar(jpathPattern, lvar=None, lvals=None, ColorMap=DefaultColorList, loc=None, bbox_to_anchor=None, savefilename=None, tickfontsize=None, doShowNow=False, **kwargs): ''' Create line plots for provided jobs. ''' prefixfilepath = os.path.sep.join(jpathPattern.split(os.path.sep)[:-1]) PPListMap = makePPListMapFromJPattern(jpathPattern) if lvals is None: lvals = PPListMap[lvar] elif not isinstance(lvals, list): lvals = [lvals] # Make sure all lval values are street legal (aka exist on disk) lvals = [ll for ll in lvals if ll == '.best' or ll in PPListMap[lvar]] # Do ranking in advance for each relevant job '''if lvals[0] == '.best': xvar = kwargs['xvar'] if 'xvals' in kwargs: xvals = kwargs['xvals'] else: xvals = PPListMap[xvar] for xval in xvals: keyValDict = dict() keyValDict[xvar] = xval jpatternForXVal = makeJPatternWithSpecificVals( PPListMap, prefixfilepath=prefixfilepath, **keyValDict) TaskRanker.markBestAmongJobPatternOnDisk(jpatternForXVal) ''' # Create list of jobs with corresponding pattern jpathList = makeListOfJPatternsWithSpecificVals( PPListMap, prefixfilepath=prefixfilepath, key=lvar, vals=lvals, **kwargs) for lineID, line_jobPattern in enumerate(jpathList): line_label = '%s=%s' % (lvar, lvals[lineID]) if isinstance(ColorMap, dict): for label in [line_label, line_jobPattern]: try: line_color = ColorMap[label] except KeyError: line_color = DefaultColorList[lineID] else: # Access next elt in ColorMap list line_color = ColorMap[lineID] plotSingleLineAcrossJobsByXVar(line_jobPattern, label=line_label, color=line_color, lineID=lineID, lvar=lvar, **kwargs) if loc is not None and len(jpathList) > 1: pylab.legend(loc=loc, bbox_to_anchor=bbox_to_anchor) if tickfontsize is not None: pylab.tick_params(axis='both', which='major', labelsize=tickfontsize) if savefilename is not None: try: pylab.show(block=False) except TypeError: pass # when using IPython notebook pylab.savefig(savefilename, bbox_inches='tight', pad_inches=0) elif doShowNow: try: pylab.show(block=True) except TypeError: pass # when using IPython notebook
def plotJobs(jpaths, legNames, styles=None, density=2, xvar='laps', yvar='evidence', loc='upper right', xmin=None, xmax=None, taskids=None, savefilename=None, tickfontsize=None, bbox_to_anchor=None, **kwargs): ''' Create line plots for provided jobs. ''' nLines = len(jpaths) if nLines == 0: raise ValueError('Empty job list. Nothing to plot.') nLeg = len(legNames) for lineID in xrange(nLines): if styles is None: curStyle = dict(colorID=lineID) else: curStyle = styles[lineID] task_kwargs = dict(**kwargs) task_kwargs.update(curStyle) plot_all_tasks_for_job(jpaths[lineID], legNames[lineID], xvar=xvar, yvar=yvar, taskids=taskids, density=density, **task_kwargs) # Y-axis limit determination # If we have "enough" data about the run beyond two full passes of dataset, # we zoom in on the region of data beyond lap 2 if xvar == 'laps' and yvar == 'evidence': xmax = 0 ymin = np.inf ymin2 = np.inf ymax = -np.inf allRunsHaveXBeyond1 = True for line in pylab.gca().get_lines(): xd = line.get_xdata() yd = line.get_ydata() if xd.size < 3: allRunsHaveXBeyond1 = False continue posLap1 = np.searchsorted(xd, 1.0) posLap2 = np.searchsorted(xd, 2.0) if posLap1 < xd.size: ymin = np.minimum(ymin, yd[posLap1]) ymax = np.maximum(ymax, yd[posLap1:].max()) if posLap2 < xd.size: ymin2 = np.minimum(ymin2, yd[posLap2]) xmax = np.maximum(xmax, xd.max()) if xd.max() <= 1: allRunsHaveXBeyond1 = False if allRunsHaveXBeyond1 and xmax > 1.5: # If all relevant curves extend beyond x=1, only show that part xmin = 1.0 - 1e-5 else: xmin = 0 if allRunsHaveXBeyond1 and ymin2 < ymax: range1 = ymax - ymin range2 = ymax - ymin2 if 10 * range2 < range1: # Y values jump from lap1 to lap2 is enormous, # so let's just show y values from lap2 onward... ymin = ymin2 if (not np.allclose(ymax, ymin)) and allRunsHaveXBeyond1: pylab.ylim([ymin, ymax + 0.1 * (ymax - ymin)]) pylab.xlim([xmin, xmax + .05 * (xmax - xmin)]) if loc is not None and len(jpaths) > 1: pylab.legend(loc=loc, bbox_to_anchor=bbox_to_anchor) if tickfontsize is not None: pylab.tick_params(axis='both', which='major', labelsize=tickfontsize) if savefilename is not None: try: pylab.show(block=False) except TypeError: pass # when using IPython notebook pylab.savefig(savefilename, bbox_inches='tight', pad_inches=0) else: try: pylab.show(block=True) except TypeError: pass # when using IPython notebook
def plotJobs(jpaths, legNames, styles=None, fileSuffix='PredLik.mat', xvar='laps', yvar='avgLikScore', loc='upper right', minLap=0, showFinalPt=0, prefix='predlik', taskids=None, savefilename=None, tickfontsize=None, xjitter=None, bbox_to_anchor=None, **kwargs): ''' Create line plots for provided jobs ''' nLines = len(jpaths) nLeg = len(legNames) assert nLines <= nLeg jitterByJob = np.linspace(-.5, .5, len(jpaths)) for lineID in xrange(nLines): if styles is None: curStyle = dict(colorID=lineID) else: curStyle = styles[lineID] if xjitter is not None: xjitter = jitterByJob[lineID] plot_all_tasks_for_job(jpaths[lineID], legNames[lineID], minLap=minLap, xvar=xvar, yvar=yvar, fileSuffix=fileSuffix, showFinalPt=showFinalPt, prefix=prefix, taskids=taskids, xjitter=xjitter, **curStyle) if loc is not None and len(jpaths) > 1: pylab.legend(loc=loc, bbox_to_anchor=bbox_to_anchor) if tickfontsize is not None: pylab.tick_params(axis='both', which='major', labelsize=tickfontsize) if savefilename is not None: try: pylab.show(block=False) except TypeError: pass # when using IPython notebook pylab.savefig(savefilename, bbox_inches='tight', pad_inches=0) else: try: pylab.show(block=True) except TypeError: pass # when using IPython notebook