예제 #1
0
def launchML2DPlots(protML, selectedPlots):
    ''' Launch some plot for an ML2D protocol run '''
    #import matplotlib
    import numpy as np
    from protlib_gui_figure import XmippPlotter

    protML._plot_count = 0
    lastIter = lastIteration(protML)
    if lastIter == 0:
        return
    refs = protML.getFilename('iter_refs', iter=lastIter)
#    if not exists(refs):
#        return 
#    blocks = getBlocksInMetaDataFile(refs)
#    lastBlock = blocks[-1]
    
    def doPlot(plotName):
        return plotName in selectedPlots
    
    # Remove 'mirror' from list if DoMirror is false
    if doPlot('DoShowMirror') and not protML.DoMirror:
        selectedPlots.remove('DoShowMirror')
        
    n = len(selectedPlots)
    if n == 0:
        showWarning("ML2D plots", "Nothing to plot", protML.master)
        return
    elif n == 1:
        gridsize = [1, 1]
    elif n == 2:
        gridsize = [2, 1]
    else:
        gridsize = [2, 2]
        
    xplotter = XmippPlotter(*gridsize)
        
    # Create data to plot
    iters = range(1, lastIter+1)
    ll = []
    pmax = []
    for iter in iters:
        logs = protML.getFilename('iter_logs', iter=iter)
        md = MetaData(logs)
        id = md.firstObject()
        ll.append(md.getValue(MDL_LL, id))
        pmax.append(md.getValue(MDL_PMAX, id))
            
    if doPlot('DoShowLL'):
        a = xplotter.createSubPlot('Log-likelihood (should increase)', 'iterations', 'LL', yformat=True)
        a.plot(iters, ll)

    #Create plot of mirror for last iteration
    if doPlot('DoShowMirror'):
        from numpy import arange
        from matplotlib.ticker import FormatStrFormatter
        md = MetaData(refs)
        mirrors = [md.getValue(MDL_MIRRORFRAC, id) for id in md]
        nrefs = len(mirrors)
        ind = arange(1, nrefs + 1)
        width = 0.85
        a = xplotter.createSubPlot('Mirror fractions on last iteration', 'references', 'mirror fraction')
        a.set_xticks(ind + 0.45)
        a.xaxis.set_major_formatter(FormatStrFormatter('%1.0f'))
        a.bar(ind, mirrors, width, color='b')
        a.set_ylim([0, 1.])
        a.set_xlim([0.8, nrefs + 1])
        
    if doPlot('DoShowPmax'):
        a = xplotter.createSubPlot('Probabilities distribution', 'iterations', 'Pmax/Psum') 
        a.plot(iters, pmax, color='green')
    
    if doPlot('DoShowSignalChange'):
        md = MetaData()
        for iter in iters:
            fn = protML.getFilename('iter_refs', iter=iter)
            md2 = MetaData(fn)
            md2.fillConstant(MDL_ITER, str(iter))
            md.unionAll(md2)
        # 'iter(.*[1-9].*)@2D/ML2D/run_004/ml2d_iter_refs.xmd')
        #a = plt.subplot(gs[1, 1])
        #print "md:", md
        md2 = MetaData()    
        md2.aggregate(md, AGGR_MAX, MDL_ITER, MDL_SIGNALCHANGE, MDL_MAX)
        signal_change = [md2.getValue(MDL_MAX, id) for id in md2]
        xplotter.createSubPlot('Maximum signal change', 'iterations', 'signal change')
        xplotter.plot(iters, signal_change, color='green')
    
    return xplotter