class ImagePreviewDialog(PreviewDialog): def _beforePreview(self): self.dim = 256 self.previewLabel = '' def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ from pyworkflow.gui.matplotlib_image import ImagePreview self.preview = ImagePreview(frame, self.dim, label=self.previewLabel) self.preview.grid(row=0, column=0) def _itemSelected(self, obj): index = obj.getIndex() filename = obj.getFileName() if index: filename = "%03d@%s" % (index, filename) # self.image = xmipp.Image() self.image = ImageHandler()._img try: self.image.readPreview(filename, self.dim) if filename.endswith('.psd'): self.image.convertPSD() self.Z = self.image.getData() except Exception, e: from pyworkflow.gui.matplotlib_image import getPngData self.Z = getPngData(findResource('no-image.png')) dialog.showError("Input particles", "Error reading image <%s>" % filename, self) self.preview.updateData(self.Z)
def getImgData(self, imgFile): import numpy as np img = ImageHandler().read(imgFile) imgData = img.getData() maxRes = np.amax(imgData) imgData2 = np.ma.masked_where(imgData < 0.1, imgData, copy=True) minRes = np.amin(imgData2) return imgData2, minRes, maxRes
class BsoftFilterDialog(DownsampleDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "Filtered particle" self.message = "Filtering particle..." self.previewLabel = "Particle" self.rightImage = ImageHandler()._img def _createControls(self, frame): pass #FIXME # self.freqFrame = ttk.LabelFrame(frame, text="Frequencies", padding="5 5 5 5") # self.freqFrame.grid(row=0, column=0) # if self.protocolParent.filterType <= FILTER_SPACE_REAL: # self.radiusSlider = self.addFreqSlider('Radius', self.protocolParent.filterRadius.get(), col=0) # else: # self.lfSlider = self.addFreqSlider('Low freq', self.protocolParent.lowFreq.get(), col=0) # self.hfSlider = self.addFreqSlider('High freq', self.protocolParent.highFreq.get(), col=1) # if self.protocolParent.filterType == FILTER_FERMI: # self.tempSlider = self.addFreqSlider('Temperature', self.protocolParent.temperature.get(), col=2) # radiusButton = tk.Button(self.freqFrame, text='Preview', command=self._doPreview) # radiusButton.grid(row=0, column=3, padx=5, pady=5) def _doPreview(self, e=None): if self.lastObj is None: dialog.showError("Empty selection", "Select an item first before preview", self) else: self._computeRightPreview() def updateFilteredImage(self): self.rightPreview.updateData(self.rightImage.getData()) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ from pyworkflow.em.packages.xmipp3 import locationToXmipp # Copy image to filter to Tmp project folder inputPath = os.path.join("Tmp", "bsoft_filter_input.spi") outputPath = os.path.join("Tmp", "bsoft_filter_output.spi") cleanPath(inputPath, outputPath) ih = ImageHandler() ih.convert(self.lastObj.getLocation(), inputPath) self.protocolParent.runFilter(inputPath, outputPath) # Get output image and update filtered image img = ih._img img.read(outputPath) self.rightImage = img self.updateFilteredImage()
class SpiderFilterDialog(DownsampleDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "Filtered particle" self.message = "Filtering particle..." self.previewLabel = "Particle" self.rightImage = ImageHandler()._img def _createControls(self, frame): self.freqFrame = ttk.LabelFrame(frame, text="Frequencies", padding="5 5 5 5") self.freqFrame.grid(row=0, column=0) if self.protocolParent.filterType <= FILTER_SPACE_REAL: self.radiusSlider = self.addFreqSlider('Radius', self.protocolParent.filterRadius.get(), col=0) else: self.lfSlider = self.addFreqSlider('Low freq', self.protocolParent.lowFreq.get(), col=0) self.hfSlider = self.addFreqSlider('High freq', self.protocolParent.highFreq.get(), col=1) if self.protocolParent.filterType == FILTER_FERMI: self.tempSlider = self.addFreqSlider('Temperature', self.protocolParent.temperature.get(), col=2) radiusButton = tk.Button(self.freqFrame, text='Preview', command=self._doPreview) radiusButton.grid(row=0, column=3, padx=5, pady=5) def _doPreview(self, e=None): if self.lastObj is None: dialog.showError("Empty selection", "Select an item first before preview", self) else: self._computeRightPreview() def getRadius(self): return self.radiusSlider.get() def addFreqSlider(self, label, value, col): slider = LabelSlider(self.freqFrame, label, from_=0, to=0.5, value=value, callback=None) slider.grid(row=0, column=col, padx=5, pady=5) return slider def getLowFreq(self): return self.lfSlider.get() def getHighFreq(self): return self.hfSlider.get() def getTemperature(self): return self.tempSlider.get() def updateFilteredImage(self): self.rightPreview.updateData(self.rightImage.getData()) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ from pyworkflow.em.packages.xmipp3 import locationToXmipp # Copy image to filter to Tmp project folder outputName = os.path.join("Tmp", "filtered_particle") outputPath = outputName + ".spi" cleanPath(outputPath) outputLoc = (1, outputPath) ih = ImageHandler() ih.convert(self.lastObj.getLocation(), outputLoc) outputLocSpiStr = locationToSpider(1, outputName) pars = {} pars["filterType"] = self.protocolParent.filterType.get() pars["filterMode"] = self.protocolParent.filterMode.get() pars["usePadding"] = self.protocolParent.usePadding.get() pars["op"] = "FQ" if self.protocolParent.filterType <= FILTER_SPACE_REAL: pars['filterRadius'] = self.getRadius() else: pars['lowFreq'] = self.getLowFreq() pars['highFreq'] = self.getHighFreq() if self.protocolParent.filterType == FILTER_FERMI: pars['temperature'] = self.getTemperature() filter_spider(outputLocSpiStr, outputLocSpiStr, **pars) # Get output image and update filtered image img = ImageHandler()._img locXmippStr = locationToXmipp(1, outputPath) img.read(locXmippStr) self.rightImage = img self.updateFilteredImage()
class BandPassFilterDialog(DownsampleDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "Filtered" self.message = "Computing filtered image..." self.previewLabel = "Image" self.rightImage = ImageHandler()._img def _createControls(self, frame): self.freqFrame = ttk.LabelFrame(frame, text="Frequencies (%s)" % self.unit, padding="5 5 5 5") self.freqFrame.grid(row=0, column=0) self.showLowFreq = getattr(self, 'showLowFreq', True) self.showHighFreq = getattr(self, 'showHighFreq', True) self.showDecay = getattr(self, 'showDecay', True) if (not self.showLowFreq) or (not self.showHighFreq): label_high = 'Freq' label_low = 'Freq' else: label_high = 'High freq' label_low = 'Low freq' self.samplingRate = 1.0 self.sliTo = 0.5 self.sliFrom = 0. if self.unit == UNIT_ANGSTROM: self.samplingRate = self.firstItem.getSamplingRate() self.itemDim, _, _ = self.firstItem.getDim() self.sliFrom = 2. * self.samplingRate self.sliTo = 2. * self.itemDim * self.samplingRate self.step = self.sliTo / 1000 if self.showLowFreq: self.lfSlider = self.addFreqSlider(label_low, self.lowFreq, col=0) if self.showHighFreq: self.hfSlider = self.addFreqSlider(label_high, self.highFreq, col=1) if self.showDecay: self.freqDecaySlider = self.addFreqSlider('Decay', self.freqDecay, col=2) def addFreqSlider(self, label, value, col): fromValue = self.sliFrom toValue = self.sliTo if self.unit == UNIT_ANGSTROM: fromValue = self.sliTo toValue = self.sliFrom slider = LabelSlider( self.freqFrame, label, from_=fromValue, to=toValue, step=self.step, value=value, callback=lambda a, b, c: self.updateFilteredImage()) slider.grid(row=0, column=col, padx=5, pady=5) return slider def updateFilteredImage(self): self._computeRightPreview() self.rightPreview.updateData(self.rightImage.getData()) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ from pyworkflow.em.packages.xmipp3.convert import getImageLocation xmipp.bandPassFilter(self.rightImage, getImageLocation(self.lastObj), self.getLowFreq(), self.getHighFreq(), self.getFreqDecay(), self.dim) def getLowFreq(self): if self.showLowFreq: if self.unit == UNIT_ANGSTROM: return self.samplingRate / self.lfSlider.get() else: return self.lfSlider.get() return 0. def getHighFreq(self): if self.showHighFreq: if self.unit == UNIT_ANGSTROM: return self.samplingRate / self.hfSlider.get() else: return self.hfSlider.get() return 1.0 def getFreqDecay(self): if self.showDecay: if self.unit == UNIT_ANGSTROM: return self.samplingRate / self.freqDecaySlider.get() else: return self.freqDecaySlider.get() return 0.
class DownsampleDialog(ImagePreviewDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "PSD" self.message = "Computing PSD..." self.previewLabel = "Micrograph" self.rightImage = ImageHandler()._img def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ leftFrame = tk.Frame(frame) leftFrame.grid(row=0, column=0) rightFrame = tk.Frame(frame) rightFrame.grid(row=0, column=1) ImagePreviewDialog._createPreview(self, leftFrame) self.rightPreview = self._createRightPreview(rightFrame) self.rightPreview.grid(row=0, column=0) def _createRightPreview(self, rightFrame): from pyworkflow.gui.matplotlib_image import ImagePreview return ImagePreview(rightFrame, self.dim, label=self.rightPreviewLabel) def _createControls(self, frame): self.downVar = tk.StringVar() self.downVar.set(getattr(self, 'downsample', 1)) downFrame = tk.Frame(frame) downFrame.grid(row=0, column=0, sticky='nw') downLabel = tk.Label(downFrame, text='Downsample') downLabel.grid(row=0, column=0, padx=5, pady=5) downEntry = tk.Entry(downFrame, width=10, textvariable=self.downVar) downEntry.grid(row=0, column=1, padx=5, pady=5) downButton = tk.Button(downFrame, text='Preview', command=self._doPreview) downButton.grid(row=0, column=2, padx=5, pady=5) def getDownsample(self): return float(self.downVar.get()) def _itemSelected(self, obj): self.lastObj = obj ImagePreviewDialog._itemSelected(self, obj) dialog.FlashMessage(self, self.message, func=self._computeRightPreview) self.rightPreview.updateData(self.rightImage.getData()) def _doPreview(self, e=None): if self.lastObj is None: dialog.showError("Empty selection", "Select an item first before preview", self) else: self._itemSelected(self.lastObj) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ xmipp.fastEstimateEnhancedPSD(self.rightImage, self.lastObj.getFileName(), self.getDownsample(), self.dim, 2)
class BandPassFilterDialog(DownsampleDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "Filtered" self.message = "Computing filtered image..." self.previewLabel = "Image" self.rightImage = ImageHandler()._img def _createControls(self, frame): self.freqFrame = ttk.LabelFrame(frame, text="Frequencies ("+self.unit+")", padding="5 5 5 5") self.freqFrame.grid(row=0, column=0) self.showLowFreq = getattr(self, 'showLowFreq', True) self.showHighFreq = getattr(self, 'showHighFreq', True) self.showDecay = getattr(self, 'showDecay', True) if (not self.showLowFreq) or (not self.showHighFreq): label_high = 'Freq' label_low = 'Freq' else: label_high = 'High freq' label_low = 'Low freq' self.samplingRate = 1.0 self.sliTo = 0.5 self.sliFrom = 0. if self.unit == UNIT_ANGSTROM: self.samplingRate = self.firstItem.getSamplingRate() self.itemDim,_,_ = self.firstItem.getDim() self.sliFrom = 2.*self.samplingRate self.sliTo = 2.*self.itemDim*self.samplingRate self.step = self.sliTo/1000 if self.showLowFreq: self.lfSlider = self.addFreqSlider(label_low, self.lowFreq, col=0) if self.showHighFreq: self.hfSlider = self.addFreqSlider(label_high, self.highFreq, col=1) if self.showDecay: self.freqDecaySlider = self.addFreqSlider('Decay', self.freqDecay, col=2) def addFreqSlider(self, label, value, col): fromValue = self.sliFrom toValue = self.sliTo if self.unit == UNIT_ANGSTROM: fromValue = self.sliTo toValue = self.sliFrom slider = LabelSlider(self.freqFrame, label, from_=fromValue, to=toValue, step=self.step , value=value, callback=lambda a, b, c:self.updateFilteredImage()) slider.grid(row=0, column=col, padx=5, pady=5) return slider def updateFilteredImage(self): self._computeRightPreview() self.rightPreview.updateData(self.rightImage.getData()) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ from pyworkflow.em.packages.xmipp3.convert import getImageLocation xmipp.bandPassFilter(self.rightImage, getImageLocation(self.lastObj), self.getLowFreq(), self.getHighFreq(), self.getFreqDecay(), self.dim) def getLowFreq(self): if self.showLowFreq: if self.unit == UNIT_ANGSTROM: return self.samplingRate/self.lfSlider.get() else: return self.lfSlider.get() return 0. def getHighFreq(self): if self.showHighFreq: if self.unit == UNIT_ANGSTROM: return self.samplingRate/self.hfSlider.get() else: return self.hfSlider.get() return 1.0 def getFreqDecay(self): if self.showDecay: if self.unit == UNIT_ANGSTROM: return self.samplingRate/self.freqDecaySlider.get() else: return self.freqDecaySlider.get() return 0.
class DownsampleDialog(ImagePreviewDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "PSD" self.message = "Computing PSD..." self.previewLabel = "Micrograph" self.rightImage = ImageHandler()._img def _createPreview(self, frame): """ Should be implemented by subclasses to create the items preview. """ leftFrame = tk.Frame(frame) leftFrame.grid(row=0, column=0) rightFrame = tk.Frame(frame) rightFrame.grid(row=0, column=1) ImagePreviewDialog._createPreview(self, leftFrame) self.rightPreview = self._createRightPreview(rightFrame) self.rightPreview.grid(row=0, column=0) def _createRightPreview(self, rightFrame): from pyworkflow.gui.matplotlib_image import ImagePreview return ImagePreview(rightFrame, self.dim, label=self.rightPreviewLabel) def _createControls(self, frame): self.downVar = tk.StringVar() self.downVar.set(getattr(self, 'downsample', 1)) downFrame = tk.Frame(frame) downFrame.grid(row=0, column=0, sticky='nw') downLabel = tk.Label(downFrame, text='Downsample') downLabel.grid(row=0, column=0, padx=5, pady=5) downEntry = tk.Entry(downFrame, width=10, textvariable=self.downVar) downEntry.grid(row=0, column=1, padx=5, pady=5) downButton = tk.Button(downFrame, text='Preview', command=self._doPreview) downButton.grid(row=0, column=2, padx=5, pady=5) def getDownsample(self): return float(self.downVar.get()) def _itemSelected(self, obj): self.lastObj = obj ImagePreviewDialog._itemSelected(self, obj) dialog.FlashMessage(self, self.message, func=self._computeRightPreview) #self._computeRightPreview(obj) self.rightPreview.updateData(self.rightImage.getData()) def _doPreview(self, e=None): if self.lastObj is None: dialog.showError("Empty selection", "Select an item first before preview", self) else: self._itemSelected(self.lastObj) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ xmipp.fastEstimateEnhancedPSD(self.rightImage, self.lastObj.getFileName(), self.getDownsample(), self.dim, 2)
class SpiderFilterDialog(DownsampleDialog): def _beforePreview(self): ImagePreviewDialog._beforePreview(self) self.lastObj = None self.rightPreviewLabel = "Filtered particle" self.message = "Filtering particle..." self.previewLabel = "Particle" self.rightImage = ImageHandler()._img def _createControls(self, frame): self.freqFrame = ttk.LabelFrame(frame, text="Frequencies", padding="5 5 5 5") self.freqFrame.grid(row=0, column=0) if self.protocolParent.filterType <= FILTER_FERMI: self.radiusSlider = self.addFreqSlider( 'Radius', self.protocolParent.filterRadius.get(), col=0) else: self.lfSlider = self.addFreqSlider( 'Low freq', self.protocolParent.lowFreq.get(), col=0) self.hfSlider = self.addFreqSlider( 'High freq', self.protocolParent.highFreq.get(), col=1) if self.protocolParent.filterType == FILTER_FERMI: self.tempSlider = self.addFreqSlider( 'Temperature', self.protocolParent.temperature.get(), col=2) radiusButton = tk.Button(self.freqFrame, text='Preview', command=self._doPreview) radiusButton.grid(row=0, column=3, padx=5, pady=5) def _doPreview(self, e=None): if self.lastObj is None: dialog.showError("Empty selection", "Select an item first before preview", self) else: self._computeRightPreview() def getRadius(self): return self.radiusSlider.get() def addFreqSlider(self, label, value, col): slider = LabelSlider(self.freqFrame, label, from_=0, to=0.5, value=value, callback=None) slider.grid(row=0, column=col, padx=5, pady=5) return slider def getLowFreq(self): return self.lfSlider.get() def getHighFreq(self): return self.hfSlider.get() def getTemperature(self): return self.tempSlider.get() def updateFilteredImage(self): self.rightPreview.updateData(self.rightImage.getData()) def _computeRightPreview(self): """ This function should compute the right preview using the self.lastObj that was selected """ from pyworkflow.em.packages.xmipp3 import locationToXmipp # Copy image to filter to Tmp project folder outputName = os.path.join("Tmp", "filtered_particle") outputPath = outputName + ".spi" cleanPath(outputPath) outputLoc = (1, outputPath) ih = ImageHandler() ih.convert(self.lastObj.getLocation(), outputLoc) outputLocSpiStr = locationToSpider(1, outputName) pars = {} pars["filterType"] = self.protocolParent.filterType.get() pars["filterMode"] = self.protocolParent.filterMode.get() pars["usePadding"] = self.protocolParent.usePadding.get() pars["op"] = "FQ" if self.protocolParent.filterType <= FILTER_FERMI: pars['filterRadius'] = self.getRadius() else: pars['lowFreq'] = self.getLowFreq() pars['highFreq'] = self.getHighFreq() if self.protocolParent.filterType == FILTER_FERMI: pars['temperature'] = self.getTemperature() filter_spider(outputLocSpiStr, outputLocSpiStr, **pars) # Get output image and update filtered image img = ImageHandler()._img locXmippStr = locationToXmipp(1, outputPath) img.read(locXmippStr) self.rightImage = img self.updateFilteredImage()