Ejemplo n.º 1
0
    def _showFSC(self, paramName=None):
        threshold = self.resolutionThreshold.get()
        nrefs = len(self._refsList)
        gridsize = self._getGridSize(nrefs)
        emlib.activateMathExtensions()

        for ref3d in self._refsList:
            xplotter = XmippPlotter(*gridsize, windowTitle='Resolution FSC')
            legends = []
            show = False
            plot_title = 'Ref3D_%s' % ref3d
            a = xplotter.createSubPlot(plot_title,
                                       'frequency(1/A)',
                                       'FSC',
                                       yformat=False)
            legends = []
            for it in self._iterations:
                file_name = self.protocol._getFileName('resolutionXmdFile',
                                                       iter=it,
                                                       ref=ref3d)
                if exists(file_name):
                    show = True
                    legends.append('iter %d' % it)
                    self._plotFSC(a, file_name)
                    xplotter.showLegend(legends)
            if show:
                if threshold < self.maxFrc:
                    a.plot([self.minInv, self.maxInv], [threshold, threshold],
                           color='black',
                           linestyle='--')
                a.grid(True)
            else:
                raise Exception("Set a valid iteration to show its FSC")

            return [xplotter]
 def _createAngDist2D(self, it):
     # Common variables to use
     nrefs = len(self._refsList)
     gridsize = self._getGridSize(nrefs)
     xplotter = XmippPlotter(*gridsize, mainTitle='Iteration %d' % it, windowTitle="Angular Distribution")
     for ref3d in self._refsList:
         classesFn = self.protocol._getFileName('outClassesXmd', iter=it, ref=ref3d)
         if exists(classesFn):
             md = xmippLib.MetaData(classesFn)
             title = 'Ref3D_%d' % ref3d
             xplotter.plotMdAngularDistribution(title, md)
         else:
             print "File %s does not exist" % classesFn
             return None
     
     return xplotter
Ejemplo n.º 3
0
    def _showOneColorslice(self, param=None):
        imageFile = self.protocol._getFileName(OUTPUT_RESOLUTION_FILE)
        imgData, _, _, volDim = self.getImgData(imageFile)

        xplotter = XmippPlotter(x=1,
                                y=1,
                                mainTitle="Local Resolution Slices "
                                "along %s-axis." % self._getAxis())
        sliceNumber = self.sliceNumber.get()
        if sliceNumber < 0:
            sliceNumber = int(volDim[0] / 2)
        else:
            sliceNumber -= 1
        # sliceNumber has no sense to start in zero
        plot = self._createSlicePlot(imgData, sliceNumber, xplotter)
        xplotter.getColorBar(plot)

        return [plt.show(xplotter)]
Ejemplo n.º 4
0
    def _showVolumeColorSlices(self, mapFile):
        imageFile = self.protocol._getFileName(mapFile)
        imgData, _, _, _ = self.getImgData(imageFile)
        imgData, min_Res, max_Res, voldim__ = self.getImgData(imageFile)

        xplotter = XmippPlotter(x=2,
                                y=2,
                                mainTitle="Local Resolution Slices "
                                "along %s-axis." % self._getAxis())

        # The slices to be shown are close to the center. Volume size is divided in
        # 9 segments, the fouth central ones are selected i.e. 3,4,5,6
        for i in range(3, 7):
            sliceNumber = self.getSlice(i, imgData)
            plot = self._createSlicePlot(imgData, sliceNumber, xplotter)
        xplotter.getColorBar(plot)

        return [plt.show(xplotter)]
Ejemplo n.º 5
0
    def createSubPlot(self, title, xlabel, ylabel):

        if self.useLastPlot and self.last_subplot:
            ax = self.last_subplot
            ax.cla()
            ax.set_title(title)
        else:
            ax = XmippPlotter.createSubPlot(self, title, xlabel, ylabel)

        return ax
Ejemplo n.º 6
0
    def _showOneColorslice(self, param=None):
        """
        It shows a single colored slice of the 3DFSC
        """
        imageFile = self.protocol._getExtraPath(OUTPUT_3DFSC)
        img = ImageHandler().read(imageFile)
        imgData = img.getData()

        xplotter = XmippPlotter(x=1,
                                y=1,
                                mainTitle="3DFSC Slices "
                                "along %s-axis." % self._getAxis())
        sliceNumber = self.sliceNumber.get()
        if sliceNumber < 0:
            x, _, _, _ = ImageHandler().getDimensions(imageFile)
            sliceNumber = int(x / 2)
        else:
            sliceNumber -= 1
        # sliceNumber has no sense to start in zero
        a = xplotter.createSubPlot("Slice %s" % (sliceNumber + 1), '', '')
        matrix = self.getSliceImage(imgData, sliceNumber, self._getAxis())
        plot = xplotter.plotMatrix(a,
                                   matrix,
                                   0,
                                   1,
                                   cmap=self.getColorMap(),
                                   interpolation="nearest")
        xplotter.getColorBar(plot)

        return [plt.show(xplotter)]
Ejemplo n.º 7
0
    def _show3DFSCcolorSlices(self, param=None):
        """
        It opens 4 colores slices of the 3DFSC
        """
        errors = []
        if self.protocol.estimate3DFSC:
            img = ImageHandler().read(
                self.protocol._getExtraPath(OUTPUT_3DFSC))
            imgData = img.getData()

            xplotter = XmippPlotter(x=2,
                                    y=2,
                                    mainTitle="3DFSC Color Slices"
                                    "along %s-axis." % self._getAxis())
            # The slices to be shown are close to the center. Volume size is divided in
            # 9 segments, the fouth central ones are selected i.e. 3,4,5,6
            for i in range(3, 7):
                sliceNumber = self.getSlice(i, imgData)
                a = xplotter.createSubPlot("Slice %s" % (sliceNumber + 1), '',
                                           '')
                matrix = self.getSliceImage(imgData, sliceNumber,
                                            self._getAxis())
                plot = xplotter.plotMatrix(a,
                                           matrix,
                                           0,
                                           1,
                                           cmap=self.getColorMap(),
                                           interpolation="nearest")
            xplotter.getColorBar(plot)
            return [xplotter]
        else:
            errors.append("The 3dFSC estimation of the 3dFSC was not selected"
                          "in the protocol form.")
            return errors
Ejemplo n.º 8
0
    def _plotCurveAnisotropy(self, fnmd, title, xTitle, yTitle, mdLabelX,
                             mdLabelY1, mdLabelY2):
        """
        This function is called by _showAnisotropyCurve
        It shows the FSO curve in terms of the resolution
        The horizontal axis is linear in this plot.
        """
        md = xmippLib.MetaData(fnmd)
        xplotter = XmippPlotter(figure=None)
        xplotter.plot_title_fontsize = 11

        a = xplotter.createSubPlot(title, xTitle, yTitle, 1, 1)
        xplotter.plotMdFile(md, mdLabelX, mdLabelY1, 'g')

        xx, yy = self._prepareDataForPlot(md, mdLabelX, mdLabelY1)
        _, yyBingham = self._prepareDataForPlot(md, mdLabelX, mdLabelY2)

        from matplotlib.ticker import FuncFormatter
        a.axes.xaxis.set_major_formatter(FuncFormatter(self._formatFreq))
        a.axes.set_ylim([-0.1, 1.1])
        a.axes.plot(xx, yy, 'g')
        a.axes.set_xlabel('Resolution (A)')
        a.axes.set_ylabel('FSO (a.u)')
        hthresholds = [0.1, 0.5, 0.9]
        a.axes.hlines(hthresholds,
                      xx[0],
                      xx[-1],
                      colors='k',
                      linestyles='dashed')
        a.axes.grid(True)
        textstr = ''

        res_01, okToPlot_01 = self.interpolRes(0.1, xx, yy)
        res_05, okToPlot_05 = self.interpolRes(0.5, xx, yy)
        res_09, okToPlot_09 = self.interpolRes(0.9, xx, yy)

        a.axes.plot(xx, yyBingham, 'r--')

        if (okToPlot_01 and okToPlot_05 and okToPlot_09):
            textstr = str(0.9) + ' --> ' + str(
                "{:.2f}".format(res_09)) + 'A\n' + str(0.5) + ' --> ' + str(
                    "{:.2f}".format(res_05)) + 'A\n' + str(
                        0.1) + ' --> ' + str("{:.2f}".format(res_01)) + 'A'
            a.axes.axvspan(1.0 / res_09,
                           1.0 / res_01,
                           alpha=0.3,
                           color='green')

            props = dict(boxstyle='round', facecolor='white')
            a.axes.text(0.0,
                        0.0,
                        textstr,
                        fontsize=12,
                        ha="left",
                        va="bottom",
                        bbox=props)

        return plt.show()
Ejemplo n.º 9
0
    def _plotCurveFSC(self, fnmd, title, xTitle, yTitle, mdLabelX, mdLabelY):
        """
        This function is called by _showFSCCurve.
        It shows the FSC curve in terms of the resolution
        That this is not the normal representation of the FSC
        """
        md = xmippLib.MetaData(fnmd)
        xplotter = XmippPlotter(figure=None)
        xplotter.plot_title_fontsize = 11

        a = xplotter.createSubPlot(title, xTitle, yTitle, 1, 1)
        xplotter.plotMdFile(md, mdLabelX, mdLabelY, 'g')

        a.xaxis.set_major_formatter(FuncFormatter(self._formatFreq))
        xx, yy = self._prepareDataForPlot(md, mdLabelX, mdLabelY)
        a.hlines(0.143, xx[0], xx[-1], colors='k', linestyles='dashed')
        a.grid(True)

        return plt.show()
Ejemplo n.º 10
0
    def _plotHistogramAngularMovement(self, paramName=None):
        #from numpy import arange
        #from matplotlib.ticker import FormatStrFormatter

        plots = []
        colors = ['g', 'b', 'r', 'y', 'c', 'm', 'k']
        lenColors = len(colors)

        numberOfBins = self.numberOfBins.get()
        md = emlib.MetaData()
        for it in self._iterations:
            mdFn = self.protocol._mdDevitationsFn(it)
            if emlib.existsBlockInMetaDataFile(mdFn):
                md.read(mdFn)
                if not self.usePsi:
                    md.fillConstant(emlib.MDL_ANGLE_PSI, 0.)

                nrefs = len(self._refsList)
                gridsize = self._getGridSize(nrefs)
                xplotterShift = XmippPlotter(*gridsize,
                                             mainTitle='Iteration_%d\n' % it,
                                             windowTitle="ShiftDistribution")
                xplotter = XmippPlotter(*gridsize,
                                        mainTitle='Iteration_%d' % it,
                                        windowTitle="AngularDistribution")

                for ref3d in self._refsList:
                    mDoutRef3D = emlib.MetaData()
                    mDoutRef3D.importObjects(
                        md, emlib.MDValueEQ(emlib.MDL_REF3D, ref3d))
                    _frequency = "Frequency (%d)" % mDoutRef3D.size()

                    xplotterShift.createSubPlot(
                        "%s_ref3D_%d" %
                        (emlib.label2Str(emlib.MDL_SHIFT_DIFF), ref3d),
                        "pixels", _frequency)
                    xplotter.createSubPlot(
                        "%s_ref3D_%d" %
                        (emlib.label2Str(emlib.MDL_ANGLE_DIFF), ref3d),
                        "degrees", _frequency)
                    #mDoutRef3D.write("*****@*****.**",MD_APPEND)
                    xplotter.plotMd(
                        mDoutRef3D,
                        emlib.MDL_ANGLE_DIFF,
                        emlib.MDL_ANGLE_DIFF,
                        color=colors[ref3d % lenColors],
                        #nbins=50
                        nbins=int(numberOfBins)
                    )  #if nbins is present do an histogram
                    xplotterShift.plotMd(
                        mDoutRef3D,
                        emlib.MDL_SHIFT_DIFF,
                        emlib.MDL_SHIFT_DIFF,
                        color=colors[ref3d % lenColors],
                        nbins=int(numberOfBins
                                  ))  #if nbins is present do an histogram

                    if self.angleSort:
                        mDoutRef3D.sort(emlib.MDL_ANGLE_DIFF)
                        fn = emlib.FileName()
                        baseFileName = self.protocol._getExtraPath(
                            "angle_sort.xmd")
                        fn = self.protocol._getRefBlockFileName(
                            "angle_iter", it, "ref3D", ref3d, baseFileName)
                        mDoutRef3D.write(fn, emlib.MD_APPEND)
                        print("File with sorted angles saved in:", fn)

                    if self.shiftSort:
                        mDoutRef3D.sort(emlib.MDL_SHIFT_DIFF)
                        fn = emlib.FileName()
                        baseFileName = self.protocol._getExtraPath(
                            "angle_sort.xmd")
                        fn = self.protocol._getRefBlockFileName(
                            "shift_iter", it, "ref3D", ref3d, baseFileName)
                        mDoutRef3D.write(fn, emlib.MD_APPEND)
                        print("File with sorted shifts saved in:", fn)

                    plots.append(xplotterShift)
                    plots.append(xplotter)
            else:
                print("File %s does not exist" % mdFn)
        return plots
Ejemplo n.º 11
0
 def __init__(self, **kwargs):
     """ Create the plotter, 'data' should be passed in **kwargs.
     """
     self._data = kwargs.get('data')
     XmippPlotter.__init__(self, **kwargs)
     self.useLastPlot = False
Ejemplo n.º 12
0
def createPlots(protML, selectedPlots):
    ''' Launch some plot for an ML2D protocol run '''
    from xmipp3.viewers.plotter import XmippPlotter
    from pwem import emlib
    
    protML._plot_count = 0
    lastIter = protML._lastIteration()
    if lastIter == 0:
        return
    refs = protML._getIterMdClasses(it=lastIter, block='classes')
#    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)
        print("No plots")
        return 
    elif n == 1:
        gridsize = [1, 1]
    elif n == 2:
        gridsize = [2, 1]
    else:
        gridsize = [2, 2]
        
    xplotter = XmippPlotter(x=gridsize[0], y=gridsize[1])
        
    # Create data to plot
    iters = range(0, lastIter+1, 1)
    ll = []
    pmax = []
    for iter in iters:
        logs = protML._getIterMdImages(it=iter, block='info')
        md = emlib.MetaData(logs)
        id = md.firstObject()
        ll.append(md.getValue(emlib.MDL_LL, id))
        pmax.append(md.getValue(emlib.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 = emlib.MetaData(refs)
        mirrors = [md.getValue(emlib.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', 'classes', '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.3, nrefs + 1])
        
    if doPlot('doShowPmax'):
        a = xplotter.createSubPlot('Probabilities distribution', 'iterations', 'Pmax/Psum') 
        a.plot(iters, pmax, color='green')
    
    if doPlot('doShowSignalChange'):
        md = emlib.MetaData()
        for iter in iters:
            fn = protML._getIterMdClasses(it=iter, block='classes')
            md2 = emlib.MetaData(fn)
            md2.fillConstant(emlib.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: %s" % md)
        md2 = emlib.MetaData()
        md2.aggregate(md, emlib.AGGR_MAX, emlib.MDL_ITER, emlib.MDL_SIGNALCHANGE, emlib.MDL_MAX)
        signal_change = [md2.getValue(emlib.MDL_MAX, id) for id in md2]
        xplotter.createSubPlot('Maximum signal change', 'iterations', 'signal change')
        xplotter.plot(iters, signal_change, color='green')
    
    return [xplotter]