Ejemplo n.º 1
0
class MaskPreviewDialog(ImagePreviewDialog):
    def _beforePreview(self):
        self.dim = 256
        self.samplingRate = 1
        self.unit = getattr(self, 'unit', UNIT_PIXEL)

        if self.unit != UNIT_PIXEL:
            self.samplingRate = self.firstItem.getSamplingRate()

        self.dim_par = self.firstItem.getDim()[0] * self.samplingRate

        self.ratio = self.dim / float(self.dim_par)

        self.previewLabel = 'Central slice'

    def _createPreview(self, frame):
        """ Should be implemented by subclasses to 
        create the items preview. 
        """
        from pyworkflow.gui.matplotlib_image import MaskPreview

        if self.maskRadius == -1:
            self.iniRadius = self.dim_par / 2
        else:
            self.iniRadius = self.maskRadius

        self.preview = MaskPreview(frame,
                                   self.dim,
                                   label=self.previewLabel,
                                   outerRadius=self.iniRadius * self.ratio)
        self.preview.grid(row=0, column=0)

    def _createControls(self, frame):
        self.addRadiusBox(frame)

    def addRadiusBox(self, parent):
        self.radiusSlider = LabelSlider(
            parent,
            'Outer radius (%s)' % self.unit,
            from_=0,
            to=int(self.dim_par / 2),
            value=self.iniRadius,
            step=self.samplingRate,
            callback=lambda a, b, c: self.updateRadius())
        self.radiusSlider.grid(row=0, column=0, padx=5, pady=5)

    def updateRadius(self):
        self.preview.updateMask(self.radiusSlider.get() * self.ratio)

    def getRadius(self):
        return int(self.radiusSlider.get())
Ejemplo n.º 2
0
class MaskPreviewDialog(ImagePreviewDialog):
    
    def _beforePreview(self):
        self.dim = 256
        self.samplingRate = 1
        self.unit = getattr(self, 'unit', UNIT_PIXEL)
        
        if self.unit != UNIT_PIXEL:
            self.samplingRate = self.firstItem.getSamplingRate()
            
        self.dim_par = self.firstItem.getDim()[0] * self.samplingRate
        
        self.ratio = self.dim / float(self.dim_par)
        
        self.previewLabel = 'Central slice'
    
    def _createPreview(self, frame):
        """ Should be implemented by subclasses to 
        create the items preview. 
        """
        from pyworkflow.gui.matplotlib_image import MaskPreview    
        
        if self.maskRadius == -1:
            self.iniRadius = self.dim_par/2 
        else:
            self.iniRadius = self.maskRadius
            
        self.preview = MaskPreview(frame, self.dim, label=self.previewLabel,
                                   outerRadius=self.iniRadius*self.ratio)
        self.preview.grid(row=0, column=0) 
    
    def _createControls(self, frame):
        self.addRadiusBox(frame) 
        
    def addRadiusBox(self, parent):
        self.radiusSlider = LabelSlider(parent, 'Outer radius (%s)' % self.unit,
                                        from_=0, to=int(self.dim_par/2),
                                        value=self.iniRadius,
                                        step=self.samplingRate,
                                        callback=lambda a, b, c:self.updateRadius())
        self.radiusSlider.grid(row=0, column=0, padx=5, pady=5) 
    
    def updateRadius(self):
        self.preview.updateMask(self.radiusSlider.get() * self.ratio)     
        
    def getRadius(self):
        return int(self.radiusSlider.get())
Ejemplo n.º 3
0
class PreWhiteningDialog(dialog.Dialog):
    def __init__(self, form, workingDir, **kwargs):
        """ 
        Params:
            parent: parent windows of the dialog.
            provider: the TreeProvider to populate items tree.
        """
        self.form = form
        self.workingDir = workingDir
        buttons = [('Select', dialog.RESULT_YES), ('Cancel', dialog.RESULT_CANCEL)]
        dialog.Dialog.__init__(self, form.root, "Pre-whitening", buttons=buttons, default='Select', **kwargs)

    def _runBeforePreWhitening(self):
        prot = self.form.protocol
        # Convert input volumes
        ih = ImageHandler()
        if prot.useSplitVolume:
            ih.convert(prot.volumeHalf1.get(), join(self.workingDir, 'volume1.map'))
            ih.convert(prot.volumeHalf2.get(), join(self.workingDir, 'volume2.map'))
        else:
            ih.convert(prot.inputVolume.get(), join(self.workingDir, 'volume1.map'))
    
        self.results = prot.runResmap(self.workingDir, wizardMode=True)
         
    def _runPreWhitening(self, newElbowAngstrom, newRampWeight):
        # Add resmap libraries to the path
        results = self.results
        
        self.figure.clear()
        myCreatePrewhiteningFigure(results, self.figure, newElbowAngstrom, newRampWeight)
        
    def body(self, bodyFrame):
        figFrame = FigureFrame(bodyFrame, figsize=(18, 9))
        figFrame.grid(row=0, column=0, columnspan=5)
        
        #self._runBeforePreWhitening(self.prot)
        dialog.FlashMessage(self.form.root, "Running Pre-Whitening tool...", 
                            func=self._runBeforePreWhitening)
        results = self.results
        
        self.figure = figFrame.getFigure() #plt.figure(figsize=(18, 9))
        self._runPreWhitening(results['newElbowAngstrom'], 
                              results['newRampWeight'])
        
        #bodyFrame.config()
        bodyFrame.columnconfigure(0, weight=1)
        bodyFrame.rowconfigure(0, weight=1)
        
        controlsFrame = tk.Frame(bodyFrame)
        controlsFrame.grid(row=1, column=0)
        
        self.elbowSlider = LabelSlider(controlsFrame, "Angstroms", from_=2.1*results['vxSize'], to=100, 
                             value=results['newElbowAngstrom'])
        self.elbowSlider.grid(row=1, column=0, padx=5, pady=5)
        
        self.rampSlider = LabelSlider(controlsFrame, "Ramp weight", from_=0.0, to=1., 
                             value=results['newRampWeight'])
        self.rampSlider.grid(row=1, column=1, padx=5, pady=5)
        
        self.updateBtn = HotButton(controlsFrame, "   Update   ", 
                                   command=self._onUpdate,
                                   tooltip="Update plots with new pre-whitening parameters.")
        self.updateBtn.grid(row=1, column=2, padx=10, pady=5)
        
    def getElbowValue(self):
        return self.elbowSlider.get()
        
    def getRampValue(self):
        return self.rampSlider.get()
    
    def _onUpdate(self, e=None):
        self._runPreWhitening(self.getElbowValue(), self.getRampValue())
Ejemplo n.º 4
0
class PreWhiteningDialog(dialog.Dialog):
    def __init__(self, form, workingDir, **kwargs):
        """ 
        Params:
            parent: parent windows of the dialog.
            provider: the TreeProvider to populate items tree.
        """
        self.form = form
        self.workingDir = workingDir
        buttons = [('Select', dialog.RESULT_YES),
                   ('Cancel', dialog.RESULT_CANCEL)]
        dialog.Dialog.__init__(self,
                               form.root,
                               "Pre-whitening",
                               buttons=buttons,
                               default='Select',
                               **kwargs)

    def _runBeforePreWhitening(self):
        prot = self.form.protocol
        # Convert input volumes
        ih = ImageHandler()
        if prot.useSplitVolume:
            ih.convert(prot.volumeHalf1.get(),
                       join(self.workingDir, 'volume1.map'))
            ih.convert(prot.volumeHalf2.get(),
                       join(self.workingDir, 'volume2.map'))
        else:
            ih.convert(prot.inputVolume.get(),
                       join(self.workingDir, 'volume1.map'))

        self.results = prot.runResmap(self.workingDir, wizardMode=True)

    def _runPreWhitening(self, newElbowAngstrom, newRampWeight):
        # Add resmap libraries to the path
        results = self.results

        self.figure.clear()
        myCreatePrewhiteningFigure(results, self.figure, newElbowAngstrom,
                                   newRampWeight)

    def body(self, bodyFrame):
        figFrame = FigureFrame(bodyFrame, figsize=(18, 9))
        figFrame.grid(row=0, column=0, columnspan=5)

        #self._runBeforePreWhitening(self.prot)
        dialog.FlashMessage(self.form.root,
                            "Running Pre-Whitening tool...",
                            func=self._runBeforePreWhitening)
        results = self.results

        self.figure = figFrame.getFigure()  #plt.figure(figsize=(18, 9))
        self._runPreWhitening(results['newElbowAngstrom'],
                              results['newRampWeight'])

        #bodyFrame.config()
        bodyFrame.columnconfigure(0, weight=1)
        bodyFrame.rowconfigure(0, weight=1)

        controlsFrame = tk.Frame(bodyFrame)
        controlsFrame.grid(row=1, column=0)

        self.elbowSlider = LabelSlider(controlsFrame,
                                       "Angstroms",
                                       from_=2.1 * results['vxSize'],
                                       to=100,
                                       value=results['newElbowAngstrom'])
        self.elbowSlider.grid(row=1, column=0, padx=5, pady=5)

        self.rampSlider = LabelSlider(controlsFrame,
                                      "Ramp weight",
                                      from_=0.0,
                                      to=1.,
                                      value=results['newRampWeight'])
        self.rampSlider.grid(row=1, column=1, padx=5, pady=5)

        self.updateBtn = HotButton(
            controlsFrame,
            "   Update   ",
            command=self._onUpdate,
            tooltip="Update plots with new pre-whitening parameters.")
        self.updateBtn.grid(row=1, column=2, padx=10, pady=5)

    def getElbowValue(self):
        return self.elbowSlider.get()

    def getRampValue(self):
        return self.rampSlider.get()

    def _onUpdate(self, e=None):
        self._runPreWhitening(self.getElbowValue(), self.getRampValue())
Ejemplo n.º 5
0
class CtfDialog(DownsampleDialog):
    def _createRightPreview(self, rightFrame):
        self.isInnerRad = True
        listeners = {
            "<Button-4>": self.makeBigger,
            "<Button-5>": self.makeSmaller,
            "<Up>": self.upKeyPress,
            "<Down>": self.downKeyPress
        }
        from pyworkflow.gui.matplotlib_image import PsdPreview
        return PsdPreview(rightFrame,
                          self.dim,
                          self.lf,
                          self.hf,
                          label=self.rightPreviewLabel,
                          listenersDict=listeners)

    def downKeyPress(self, event):
        self.isInnerRad = False
        self.highlightOuterSlider()

    def upKeyPress(self, event):
        self.isInnerRad = True
        self.highlightInnerSlider()

    def highlightOuterSlider(self):
        self.hfSlider.highlightLabel()
        self.lfSlider.removeHighlightFromLabel()

    def highlightInnerSlider(self):
        self.lfSlider.highlightLabel()
        self.hfSlider.removeHighlightFromLabel()

    def makeBigger(self, event):
        self.isMakingBigger = True
        if self.isInnerRad:
            self.lf = self.lf + self.step
        else:
            new_val = self.hf + self.step
            if new_val <= 0.5:  # Don't make the mask bigger unless the is equal or lower than the max
                self.hf = new_val
        self.manageMaskVals()

    def makeSmaller(self, event):
        self.isMakingBigger = False
        if self.isInnerRad:
            new_val = self.lf - self.step
            if new_val >= 0:
                self.lf = new_val
        else:
            self.hf = self.hf - self.step
        self.manageMaskVals()

    def manageMaskVals(self):
        if self.isMakingBigger:
            if self.isInnerRad and self.lf >= self.hf:  # Inner ring can't be bigger than outer ring
                # Subtract one step to go back to the nearest lower value
                self.lf = self.hf - self.step
        else:
            if not self.isInnerRad and self.hf <= self.lf:  # Outer ring can't be smaller than inner
                # ring
                # Add one step to go back to the nearest higher value
                self.hf = self.lf + self.step

        # Set the ring sliders in case it comes from the mouse wheel
        self.setFreq(self.lfSlider, self.lf)
        self.setFreq(self.hfSlider, self.hf)

        # Show values
        self.showValues(self.hfVar, self.hfSlider)
        self.showValues(self.lfVar, self.lfSlider)

        # Update mask
        self.rightPreview.updateFreq(self.lf, self.hf)

    def _showInAngstroms(self):
        return getattr(self, 'showInAngstroms', False)

    def _createControls(self, frame):
        frame.columnconfigure(0, weight=1)
        frame.columnconfigure(1, weight=1)
        frame.columnconfigure(2, weight=1)

        self.step = 0.01
        self.freqFrame = ttk.LabelFrame(frame, text="Frequencies")
        self.freqFrame.columnconfigure(0, weight=3)
        self.freqFrame.grid(row=0, column=2, sticky='news')
        self.lfSlider = LabelSlider(
            self.freqFrame,
            'Low freq',
            value=self.lf,
            from_=0.01,
            to=0.5,
            length=178,
            labelWidth=13,
            step=self.step,
            showvalue=0,
            callback=lambda a, b, c: self.updateSliderInnerRadius(),
        )
        self.lfSlider.grid(row=0, column=0, padx=5, pady=5, sticky='news')
        self.lfSlider.highlightLabel()
        self.hfSlider = LabelSlider(
            self.freqFrame,
            'High freq',
            value=self.lf,
            from_=0.01,
            to=0.5,
            length=178,
            labelWidth=13,
            step=self.step,
            showvalue=0,
            callback=lambda a, b, c: self.updateSliderOuterRadius(),
        )
        self.hfSlider.grid(row=1, column=0, padx=5, pady=5, sticky='news')

        # Pack and configure low freq slider
        self.lfVar = tk.StringVar()
        self.lfLabel = tk.Label(self.freqFrame,
                                textvariable=self.lfVar,
                                width=26)
        self.lfLabel.grid(row=0, column=1, sticky='nse', padx=5, pady=5)
        # Pack and configure high freq slider
        self.hfVar = tk.StringVar()
        self.hfLabel = tk.Label(self.freqFrame,
                                textvariable=self.hfVar,
                                width=26)
        self.hfLabel.grid(row=1, column=1, sticky='nse', padx=5, pady=5)
        # Update both mask and sliders with the initial values
        self.manageMaskVals()

    # def getDownsample(self):
    #     return 1.0  # Micrograph previously downsample, not taken into account here

    def updateFreqRing(self):
        self.rightPreview.updateFreq(self.getLowFreq(), self.getHighFreq())
        self.showValues(self.lfVar, self.lfSlider)
        self.showValues(self.hfVar, self.hfSlider)

    def updateSliderOuterRadius(self):
        new_val = self.getFreq(self.hfSlider)
        # Carry out the action only if the slider is clicked & dragged (prevent from minimal variations when the mouse
        # is on the slider but not clicking on it)
        if abs(new_val - self.hf) >= self.step:
            self.highlightOuterSlider()
            self.isInnerRad = False
            self.isMakingBigger = False
            # Check if the user is making the ring bigger
            if new_val > self.hf:
                self.isMakingBigger = True

            self.hf = new_val
            self.manageMaskVals()

    def updateSliderInnerRadius(self):
        new_val = self.getFreq(self.lfSlider)
        # Highlight only if the slider is clicked & dragged (prevent from minimal variations when the mouse is on the
        # slider but not clicking on it)
        if abs(new_val - self.lf) >= self.step:
            self.highlightInnerSlider()
            self.isInnerRad = True
            self.isMakingBigger = False
            # Check if the user is making the ring bigger
            if new_val > self.lf:
                self.isMakingBigger = True

            self.lf = new_val
            self.manageMaskVals()

    def showValues(self, var2set, labSlider):
        """
        Show the values selected for the inner and outer radius. If the units are angstroms (sampling_rate = 1,
        it will show only one value to avoid redundancies
        """
        sr = self.firstItem.getSamplingRate()
        freqVal = max(self.getFreq(labSlider),
                      0.0001)  # avoid division by zero
        if sr == 1:
            var2set.set('{:2.2f} {}'.format(labSlider.slider.get(),
                                            emcts.UNIT_PIXEL))
        else:
            var2set.set('{:2.2f} rad/{} | {:5.1f} {}'.format(
                labSlider.slider.get(), emcts.UNIT_ANGSTROM_SYMBOL,
                self.getDownsample() * sr / freqVal,
                emcts.UNIT_ANGSTROM_SYMBOL))

    def getLowFreq(self):
        return self.lfSlider.get()

    def getHighFreq(self):
        return self.hfSlider.get()

    @staticmethod
    def getFreq(freqSlider):
        return freqSlider.get()

    @staticmethod
    def setFreq(freqSlider, val):
        freqSlider.slider.set(val)