Пример #1
0
 def calculate(self):
     stack = self.getStackDataObject()
     mcaIndex = stack.info.get('McaIndex')
     shape = stack.data.shape
     stack = None
     if mcaIndex not in [-1, len(shape) - 1]:
         raise IndexError("NNMA does not support stacks of images yet")
         return
     if self.configurationWidget is None:
         self.configurationWidget = NNMAParametersDialog(None, regions=True)
         self._status = qt.QLabel(self.configurationWidget)
         self._status.setAlignment(qt.Qt.AlignHCenter)
         font = qt.QFont(self._status.font())
         font.setBold(True)
         self._status.setFont(font)
         self._status.setText("Ready")
         self.configurationWidget.layout().addWidget(self._status)
     activeCurve = self.getActiveCurve()
     if activeCurve is None:
         #I could get some defaults from the stack itslef
         raise ValueError("Please select an active curve")
         return
     x, spectrum, legend, info = activeCurve
     spectrumLength = int(max(spectrum.shape))
     oldValue = self.configurationWidget.nPC.value()
     self.configurationWidget.nPC.setMaximum(spectrumLength)
     self.configurationWidget.nPC.setValue(min(oldValue, spectrumLength))
     binningOptions = [1]
     for number in [2, 3, 4, 5, 7, 9, 10, 11, 13, 15, 17, 19]:
         if (spectrumLength % number) == 0:
             binningOptions.append(number)
     # TODO: Should inform the configuration widget about the possibility
     #       to encounter non-finite data?
     ddict = {'options': binningOptions,
              'binning': 1,
              'method': 0}
     self.configurationWidget.setParameters(ddict)
     y = spectrum
     self.configurationWidget.setSpectrum(x, y, legend=legend, info=info)
     self.configurationWidget.show()
     self.configurationWidget.raise_()
     ret = self.configurationWidget.exec_()
     if ret:
         self._executeFunctionAndParameters()
Пример #2
0
 def calculate(self):
     stack = self.getStackDataObject()
     mcaIndex = stack.info.get('McaIndex')
     shape = stack.data.shape
     stack = None
     if mcaIndex not in [-1, len(shape) - 1]:
         raise IndexError("NNMA does not support stacks of images yet")
         return
     if self.configurationWidget is None:
         self.configurationWidget = NNMAParametersDialog(None)
         self._status = qt.QLabel(self.configurationWidget)
         self._status.setAlignment(qt.Qt.AlignHCenter)
         font = qt.QFont(self._status.font())
         font.setBold(True)
         self._status.setFont(font)
         self._status.setText("Ready")
         self.configurationWidget.layout().addWidget(self._status)
     activeCurve = self.getActiveCurve()
     if activeCurve is None:
         #I could get some defaults from the stack itslef
         raise ValueError("Please select an active curve")
         return
     x, spectrum, legend, info = activeCurve
     spectrumLength = int(max(spectrum.shape))
     oldValue = self.configurationWidget.nPC.value()
     self.configurationWidget.nPC.setMaximum(spectrumLength)
     self.configurationWidget.nPC.setValue(min(oldValue, spectrumLength))
     binningOptions = [1]
     for number in [2, 3, 4, 5, 7, 9, 10, 11, 13, 15, 17, 19]:
         if (spectrumLength % number) == 0:
             binningOptions.append(number)
     ddict = {'options': binningOptions,
              'binning': 1,
              'method': 0}
     self.configurationWidget.setParameters(ddict)
     y = spectrum
     self.configurationWidget.setSpectrum(x, y)
     self.configurationWidget.show()
     self.configurationWidget.raise_()
     ret = self.configurationWidget.exec_()
     if ret:
         self._executeFunctionAndParameters()
Пример #3
0
class NNMAStackPlugin(StackPluginBase.StackPluginBase):
    def __init__(self, stackWindow, **kw):
        if _logger.getEffectiveLevel() == logging.DEBUG:
            StackPluginBase.pluginBaseLogger.setLevel(logging.DEBUG)
        StackPluginBase.StackPluginBase.__init__(self, stackWindow, **kw)
        self.methodDict = {'Calculate': [self.calculate,
                                         "Perform NNMA",
                                         None],
                           'Show': [self._showWidget,
                                    "Show last results",
                                    PyMca_Icons.brushselect]}
        self.__methodKeys = ['Calculate', 'Show']
        self.configurationWidget = None
        self.widget = None
        self.thread = None

    def stackUpdated(self):
        _logger.debug("NNMAStackPlugin.stackUpdated() called")
        self.configurationWidget = None
        self.widget = None

    def selectionMaskUpdated(self):
        if self.widget is None:
            return
        if self.widget.isHidden():
            return
        mask = self.getStackSelectionMask()
        self.widget.setSelectionMask(mask)

    def mySlot(self, ddict):
        _logger.debug("mySlot %s %s", ddict['event'], ddict.keys())
        if ddict['event'] == "selectionMaskChanged":
            self.setStackSelectionMask(ddict['current'])
        elif ddict['event'] == "addImageClicked":
            self.addImage(ddict['image'], ddict['title'])
        elif ddict['event'] == "addAllClicked":
            for i in range(len(ddict["images"])):
                self.addImage(ddict['images'][i], ddict['titles'][i])            
        elif ddict['event'] == "removeImageClicked":
            self.removeImage(ddict['title'])
        elif ddict['event'] == "replaceImageClicked":
            self.replaceImage(ddict['image'], ddict['title'])
        elif ddict['event'] == "resetSelection":
            self.setStackSelectionMask(None)

    #Methods implemented by the plugin
    def getMethods(self):
        if self.widget is None:
            return [self.__methodKeys[0]]
        else:
            return self.__methodKeys

    def getMethodToolTip(self, name):
        return self.methodDict[name][1]

    def getMethodPixmap(self, name):
        return self.methodDict[name][2]

    def applyMethod(self, name):
        return self.methodDict[name][0]()

    #The specific part
    def calculate(self):
        stack = self.getStackDataObject()
        mcaIndex = stack.info.get('McaIndex')
        shape = stack.data.shape
        stack = None
        if mcaIndex not in [-1, len(shape) - 1]:
            raise IndexError("NNMA does not support stacks of images yet")
            return
        if self.configurationWidget is None:
            self.configurationWidget = NNMAParametersDialog(None, regions=True)
            self._status = qt.QLabel(self.configurationWidget)
            self._status.setAlignment(qt.Qt.AlignHCenter)
            font = qt.QFont(self._status.font())
            font.setBold(True)
            self._status.setFont(font)
            self._status.setText("Ready")
            self.configurationWidget.layout().addWidget(self._status)
        activeCurve = self.getActiveCurve()
        if activeCurve is None:
            #I could get some defaults from the stack itslef
            raise ValueError("Please select an active curve")
            return
        x, spectrum, legend, info = activeCurve
        spectrumLength = int(max(spectrum.shape))
        oldValue = self.configurationWidget.nPC.value()
        self.configurationWidget.nPC.setMaximum(spectrumLength)
        self.configurationWidget.nPC.setValue(min(oldValue, spectrumLength))
        binningOptions = [1]
        for number in [2, 3, 4, 5, 7, 9, 10, 11, 13, 15, 17, 19]:
            if (spectrumLength % number) == 0:
                binningOptions.append(number)
        # TODO: Should inform the configuration widget about the possibility
        #       to encounter non-finite data?
        ddict = {'options': binningOptions,
                 'binning': 1,
                 'method': 0}
        self.configurationWidget.setParameters(ddict)
        y = spectrum
        self.configurationWidget.setSpectrum(x, y, legend=legend, info=info)
        self.configurationWidget.show()
        self.configurationWidget.raise_()
        ret = self.configurationWidget.exec_()
        if ret:
            self._executeFunctionAndParameters()

    def _executeFunctionAndParameters(self):
        _logger.debug("NNMAStackPlugin _executeFunctionAndParameters")
        self.widget = None
        self.thread = CalculationThread.CalculationThread(\
                            calculation_method=self.actualCalculation)
        self.thread.finished.connect(self.threadFinished)
        self.configurationWidget.show()
        message = "Please wait. NNMA Calculation going on."
        _logger.debug("NNMAStackPlugin starting thread")
        self.thread.start()
        _logger.debug("NNMAStackPlugin waitingMessageDialog")
        CalculationThread.waitingMessageDialog(self.thread,
                                message=message,
                                parent=self.configurationWidget)
        _logger.debug("NNMAStackPlugin waitingMessageDialog passed")

    def actualCalculation(self):
        _logger.debug("NNMAStackPlugin actualCalculation")
        nnmaParameters = self.configurationWidget.getParameters()
        self._status.setText("Calculation going on")
        self.configurationWidget.setEnabled(False)
        #self.configurationWidget.close()
        #At some point I should make sure I get directly the
        #function and the parameters from the configuration widget
        function = nnmaParameters['function']
        ddict = {}
        ddict.update(nnmaParameters['kw'])
        ddict['ncomponents'] = nnmaParameters['npc']
        ddict['binning'] = nnmaParameters['binning']
        ddict['spectral_mask'] = nnmaParameters['spectral_mask']
        #ddict['kmeans'] = False
        if not self.isStackFinite():
            # one has to check for NaNs in the used region(s)
            # for the time being only in the global image
            # spatial_mask = numpy.isfinite(image_data)
            spatial_mask = numpy.isfinite(self.getStackOriginalImage())
            ddictParameters['mask'] = spatial_mask
        del nnmaParameters
        stack = self.getStackDataObject()
        if isinstance(stack, numpy.ndarray):
            if stack.data.dtype not in [numpy.float, numpy.float32]:
                _logger.warning("WARNING: Non floating point data")
                text = "Calculation going on."
                text += " WARNING: Non floating point data."
                self._status.setText(text)

        oldShape = stack.data.shape
        result = function(stack, **ddict)
        if stack.data.shape != oldShape:
            stack.data.shape = oldShape
        return result

    def threadFinished(self):
        _logger.debug("NNMAStackPlugin threadFinished")
        result = self.thread.result
        self.thread = None
        if type(result) == type((1,)):
            #if we receive a tuple there was an error
            if len(result):
                if type(result[0]) == type("Exception"):
                    if result[0] == "Exception":
                        self._status.setText("Ready after calculation error")
                        self.configurationWidget.setEnabled(True)
                        raise Exception(result[1], result[2])
                        return
        self._status.setText("Ready")
        curve = self.configurationWidget.getSpectrum(binned=True)
        if curve not in [None, []]:
            xValues = curve[0]
        else:
            xValues = None
        self.configurationWidget.setEnabled(True)
        self.configurationWidget.close()

        images, eigenValues, eigenVectors = result
        imageNames = None
        vectorNames = None
        nimages = images.shape[0]
        imageNames = []
        vectorNames = []
        vectorTitles = []
        for i in range(nimages):
            imageNames.append("NNMA Image %02d" % i)
            vectorNames.append("NNMA Component %02d" % i)
            vectorTitles.append("%g %% explained intensity" %\
                                               eigenValues[i])
        _logger.debug("NNMAStackPlugin threadFinished. Create widget")
        self.widget = StackPluginResultsWindow.StackPluginResultsWindow(\
                                        usetab=True)
        _logger.debug("NNMAStackPlugin threadFinished. Widget created")
        self.widget.buildAndConnectImageButtonBox(replace=True,
                                                  multiple=True)
        qt = StackPluginResultsWindow.qt
        self.widget.sigMaskImageWidgetSignal.connect(self.mySlot)
        if xValues is not None:
            xValues = [xValues] * nimages
        self.widget.setStackPluginResults(images,
                                          spectra=eigenVectors,
                                          image_names=imageNames,
                                          xvalues=xValues,
                                          spectra_names=vectorNames,
                                          spectra_titles=vectorTitles)
        self._showWidget()

    def _showWidget(self):
        if self.widget is None:
            return
        #Show
        self.widget.show()
        self.widget.raise_()

        #update
        self.selectionMaskUpdated()
Пример #4
0
class NNMAStackPlugin(StackPluginBase.StackPluginBase):
    def __init__(self, stackWindow, **kw):
        if _logger.getEffectiveLevel() == logging.DEBUG:
            StackPluginBase.pluginBaseLogger.setLevel(logging.DEBUG)
        StackPluginBase.StackPluginBase.__init__(self, stackWindow, **kw)
        self.methodDict = {'Calculate': [self.calculate,
                                         "Perform NNMA",
                                         None],
                           'Show': [self._showWidget,
                                    "Show last results",
                                    PyMca_Icons.brushselect]}
        self.__methodKeys = ['Calculate', 'Show']
        self.configurationWidget = None
        self.widget = None
        self.thread = None

    def stackUpdated(self):
        _logger.debug("NNMAStackPlugin.stackUpdated() called")
        self.configurationWidget = None
        self.widget = None

    def selectionMaskUpdated(self):
        if self.widget is None:
            return
        if self.widget.isHidden():
            return
        mask = self.getStackSelectionMask()
        self.widget.setSelectionMask(mask)

    def mySlot(self, ddict):
        _logger.debug("mySlot %s %s", ddict['event'], ddict.keys())
        if ddict['event'] == "selectionMaskChanged":
            self.setStackSelectionMask(ddict['current'])
        elif ddict['event'] == "addImageClicked":
            self.addImage(ddict['image'], ddict['title'])
        elif ddict['event'] == "addAllClicked":
            for i in range(len(ddict["images"])):
                self.addImage(ddict['images'][i], ddict['titles'][i])            
        elif ddict['event'] == "removeImageClicked":
            self.removeImage(ddict['title'])
        elif ddict['event'] == "replaceImageClicked":
            self.replaceImage(ddict['image'], ddict['title'])
        elif ddict['event'] == "resetSelection":
            self.setStackSelectionMask(None)

    #Methods implemented by the plugin
    def getMethods(self):
        if self.widget is None:
            return [self.__methodKeys[0]]
        else:
            return self.__methodKeys

    def getMethodToolTip(self, name):
        return self.methodDict[name][1]

    def getMethodPixmap(self, name):
        return self.methodDict[name][2]

    def applyMethod(self, name):
        return self.methodDict[name][0]()

    #The specific part
    def calculate(self):
        stack = self.getStackDataObject()
        mcaIndex = stack.info.get('McaIndex')
        shape = stack.data.shape
        stack = None
        if mcaIndex not in [-1, len(shape) - 1]:
            raise IndexError("NNMA does not support stacks of images yet")
            return
        if self.configurationWidget is None:
            self.configurationWidget = NNMAParametersDialog(None)
            self._status = qt.QLabel(self.configurationWidget)
            self._status.setAlignment(qt.Qt.AlignHCenter)
            font = qt.QFont(self._status.font())
            font.setBold(True)
            self._status.setFont(font)
            self._status.setText("Ready")
            self.configurationWidget.layout().addWidget(self._status)
        activeCurve = self.getActiveCurve()
        if activeCurve is None:
            #I could get some defaults from the stack itslef
            raise ValueError("Please select an active curve")
            return
        x, spectrum, legend, info = activeCurve
        spectrumLength = int(max(spectrum.shape))
        oldValue = self.configurationWidget.nPC.value()
        self.configurationWidget.nPC.setMaximum(spectrumLength)
        self.configurationWidget.nPC.setValue(min(oldValue, spectrumLength))
        binningOptions = [1]
        for number in [2, 3, 4, 5, 7, 9, 10, 11, 13, 15, 17, 19]:
            if (spectrumLength % number) == 0:
                binningOptions.append(number)
        ddict = {'options': binningOptions,
                 'binning': 1,
                 'method': 0}
        self.configurationWidget.setParameters(ddict)
        y = spectrum
        self.configurationWidget.setSpectrum(x, y)
        self.configurationWidget.show()
        self.configurationWidget.raise_()
        ret = self.configurationWidget.exec_()
        if ret:
            self._executeFunctionAndParameters()

    def _executeFunctionAndParameters(self):
        _logger.debug("NNMAStackPlugin _executeFunctionAndParameters")
        self.widget = None
        self.thread = CalculationThread.CalculationThread(\
                            calculation_method=self.actualCalculation)
        self.thread.finished.connect(self.threadFinished)
        self.configurationWidget.show()
        message = "Please wait. NNMA Calculation going on."
        _logger.debug("NNMAStackPlugin starting thread")
        self.thread.start()
        _logger.debug("NNMAStackPlugin waitingMessageDialog")
        CalculationThread.waitingMessageDialog(self.thread,
                                message=message,
                                parent=self.configurationWidget)
        _logger.debug("NNMAStackPlugin waitingMessageDialog passed")

    def actualCalculation(self):
        _logger.debug("NNMAStackPlugin actualCalculation")
        nnmaParameters = self.configurationWidget.getParameters()
        self._status.setText("Calculation going on")
        self.configurationWidget.setEnabled(False)
        #self.configurationWidget.close()
        #At some point I should make sure I get directly the
        #function and the parameters from the configuration widget
        function = nnmaParameters['function']
        ddict = {}
        ddict.update(nnmaParameters['kw'])
        ddict['ncomponents'] = nnmaParameters['npc']
        ddict['binning'] = nnmaParameters['binning']
        #ddict['kmeans'] = False
        del nnmaParameters
        stack = self.getStackDataObject()
        if isinstance(stack, numpy.ndarray):
            if stack.data.dtype not in [numpy.float, numpy.float32]:
                _logger.warning("WARNING: Non floating point data")
                text = "Calculation going on."
                text += " WARNING: Non floating point data."
                self._status.setText(text)

        oldShape = stack.data.shape
        result = function(stack, **ddict)
        if stack.data.shape != oldShape:
            stack.data.shape = oldShape
        return result

    def threadFinished(self):
        _logger.debug("NNMAStackPlugin threadFinished")
        result = self.thread.result
        self.thread = None
        if type(result) == type((1,)):
            #if we receive a tuple there was an error
            if len(result):
                if type(result[0]) == type("Exception"):
                    if result[0] == "Exception":
                        self._status.setText("Ready after calculation error")
                        self.configurationWidget.setEnabled(True)
                        raise Exception(result[1], result[2])
                        return
        self._status.setText("Ready")
        curve = self.configurationWidget.getSpectrum(binned=True)
        if curve not in [None, []]:
            xValues = curve[0]
        else:
            xValues = None
        self.configurationWidget.setEnabled(True)
        self.configurationWidget.close()

        images, eigenValues, eigenVectors = result
        imageNames = None
        vectorNames = None
        nimages = images.shape[0]
        imageNames = []
        vectorNames = []
        vectorTitles = []
        for i in range(nimages):
            imageNames.append("NNMA Image %02d" % i)
            vectorNames.append("NNMA Component %02d" % i)
            vectorTitles.append("%g %% explained intensity" %\
                                               eigenValues[i])
        _logger.debug("NNMAStackPlugin threadFinished. Create widget")
        self.widget = StackPluginResultsWindow.StackPluginResultsWindow(\
                                        usetab=True)
        _logger.debug("NNMAStackPlugin threadFinished. Widget created")
        self.widget.buildAndConnectImageButtonBox(replace=True,
                                                  multiple=True)
        qt = StackPluginResultsWindow.qt
        self.widget.sigMaskImageWidgetSignal.connect(self.mySlot)
        if xValues is not None:
            xValues = [xValues] * nimages
        self.widget.setStackPluginResults(images,
                                          spectra=eigenVectors,
                                          image_names=imageNames,
                                          xvalues=xValues,
                                          spectra_names=vectorNames,
                                          spectra_titles=vectorTitles)
        self._showWidget()

    def _showWidget(self):
        if self.widget is None:
            return
        #Show
        self.widget.show()
        self.widget.raise_()

        #update
        self.selectionMaskUpdated()