Commit 92775bcc authored by JosefBrandt's avatar JosefBrandt
Browse files

Bugfix in contour selection
parent 83cdeb70
Loading
Loading
Loading
Loading
+92 −102
Original line number Diff line number Diff line
@@ -116,16 +116,21 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
        self.specNumberSelector = QtWidgets.QSpinBox()
        self.specNumberSelector.setMinimumWidth(100)
        self.specNumberSelector.setMinimum(1)
        self.specNumberSelector.setMaximum(1E6)
        numParticles = self.particleContainer.getNumberOfParticles()
        self.specNumberSelector.setMaximum(numParticles)
        
        self.jumpToSpecBtn = QtWidgets.QPushButton('Jump To Spectrum of Number:')
        self.jumpToSpecBtn.released.connect(self.jumpToIndicatedSpectrum)
        
        self.typeSelectorCombo = QtWidgets.QComboBox()
        self.typeSelectorCombo.setMinimumWidth(150)
        self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
        self.particleSelector = QtWidgets.QSpinBox()
        self.particleSelector.valueChanged.connect(self.setParticleSelector)
        self.particleNumberLabel = QtWidgets.QLabel('of xx particles; ')
        
        self.spectrumSelector = QtWidgets.QSpinBox()
        self.spectrumSelector.valueChanged.connect(self.setSpecSelector)
        self.spectrumNumberLabel = QtWidgets.QLabel('of xx spectra')
        for spinbox in [self.particleSelector, self.spectrumSelector]:
            spinbox.setMinimum(1)
@@ -218,8 +223,7 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
        self.applyHQIThresholdToResults()
        self.createHistogramData()
        self.initializeSpecPlot()
        self.displayNewPolymerType()
        self.updateSpecPlotToSelectedParticle()
        self.setTypeSelector()
        self.updateHistogramsAndContours()
    
    def createActions(self):
@@ -330,7 +334,25 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
        self.createHistogramData()
        self.updateHistogramsAndContours()
     
    def updateHistogramsAndContours(self):  
        self.createHistogramData()
        t0 = time.time()
        self.updateTypeHistogram()
        print('update type hist: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateSizeHistogram()
        print('update size hist: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateContourColors()
        print('update contours: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateLegend()
        print('update legend: {} ms'.format(round((time.time()-t0)*1000)))
        self.dataset.save()
        
    def createHistogramData(self):
        self.lastSelectedCheckBoxNames = self.getSelectedPolymers()
        
        ###Handle Checkboxes for all polymers...
        self.menuLayout.removeWidget(self.resultScrollarea)
        for i in [self.resultCheckBoxes, self.resultCheckBoxesLayout, self.resultScrollarea, self.layout_SArea]:
@@ -381,12 +403,14 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
            self.expSQLAct.setDisabled(False)
        
        self.navigationGroup.setEnabled(True)
#        self.typeSelectorCombo.currentIndexChanged.disconnect()
        self.typeSelectorCombo.currentIndexChanged.disconnect()
        self.typeSelectorCombo.clear()
        self.typeSelectorCombo.addItems(uniquePolymers)
#        self.typeSelectorCombo.currentIndexChanged.connect(self.displayNewPolymerType)
        self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
        
        numParticles = self.particleContainer.getNumberOfParticles()
        self.specNumberSelector.setMaximum(numParticles)
        
        self.polymerIndex = self.typeSelectorCombo.currentIndex()
#        if self.lastSpectrumInFocus is not None:
#            self.currentSpectrumIndex = self.lastSpectrumInFocus
#            self.displayNewPolymerType(resetCurrentIndex=False)
@@ -395,22 +419,6 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
#            print('displaying new type with resetting index')
#            self.displayNewPolymerType()
        
    def updateHistogramsAndContours(self):  
        self.createHistogramData()
        t0 = time.time()
        self.updateTypeHistogram()
        print('update type hist: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateSizeHistogram()
        print('update size hist: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateContourColors()
        print('update contours: {} ms'.format(round((time.time()-t0)*1000)))
        t0 = time.time()
        self.updateLegend()
        print('update legend: {} ms'.format(round((time.time()-t0)*1000)))
        self.dataset.save()
    
    def initializeSpecPlot(self):
        self.specPlot.loadSpectraAndInitializeSpecPlot()
        self.updateSpecPlot()
@@ -466,8 +474,6 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
            self.sizeHist_ax.set_xlim(3, 1100)        
            self.sizeHist_ax.figure.canvas.draw()
    
            self.lastSelectedCheckBoxNames = self.getSelectedPolymers()
    
    def updateSpecPlot(self):
        particleSize = self.particleContainer.getSizeOfParticleByIndex(self.currentParticleIndex)
        hqi = self.particleContainer.getHQIOfSpectrumIndex(self.currentSpectrumIndex)
@@ -505,102 +511,81 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
                    legendItems.append((polymer, color))
            self.viewparent.updateLegend(legendItems)

                        
    def getSelectedPolymers(self):
        return [checkbox.text() for checkbox in self.polymerCheckBoxes if checkbox.isChecked()]
     
    def getSpecIndexFromBoxesAndJumpThere(self):
        self.currentSpectrumIndex = self.getSpectrumIndexFromSpectrumSelector()
    def setTypeSelector(self):
        assignment = self.typeSelectorCombo.currentText()
        partIndices = self.particleContainer.getIndicesOfParticleType(assignment)
        self.particleSelector.setMaximum(len(partIndices))
        self.particleNumberLabel.setText(f'of {len(partIndices)} particles; ')
        self.particleSelector.setValue(1)
        self.setParticleSelector()
    
    def setParticleSelector(self):
        assignment = self.typeSelectorCombo.currentText()
        if assignment != '':
            partIndices = self.particleContainer.getIndicesOfParticleType(assignment)
            self.currentParticleIndex = partIndices[self.particleSelector.value()-1]
            
            specIndices = self.particleContainer.getSpectraIndicesOfParticle(self.currentParticleIndex)
            self.spectrumSelector.setValue(1)
            self.spectrumSelector.setMaximum(len(specIndices))        
            self.spectrumNumberLabel.setText(f'of {len(specIndices)} spectra')
            self.setSpecSelector()
        
    def setSpecSelector(self):
        specIndices = self.particleContainer.getSpectraIndicesOfParticle(self.currentParticleIndex)
        self.currentSpectrumIndex = specIndices[self.spectrumSelector.value()-1]
        self.specNumberSelector.setValue(self.currentSpectrumIndex+1)
        
        self.updateSpecPlot()
        self.jumpToIndicatedSpectrum()
    
    def jumpToIndicatedSpectrum(self, centerOn=True):
        print('jumping to spec index with centerOn', centerOn)
        ###disconnect Boxes
    def setWidgetsToNewParticleIndex(self, particleIndex):   #TODO: Probably refactor. Its basically a combination of the above methods... Just a bit different to avoid directly jumping to the particle in sampleview
        try:
            self.typeSelectorCombo.currentIndexChanged.disconnect()
            self.particleSelector.valueChanged.disconnect()
            self.spectrumSelector.valueChanged.disconnect()
            self.typeSelectorCombo.currentIndexChanged.disconnect()
        except TypeError:
            pass   #i.e., there was no connection
            pass #signals were not connected...
        
        
        specIndex = self.specNumberSelector.value()-1
        print('specIndex', specIndex)
        partIndex = self.particleContainer.getParticleIndexContainingSpecIndex(specIndex)
        print('partIndex', partIndex)
        assignment = self.particleContainer.getParticleAssignmentByIndex(partIndex)
        assignment = self.particleContainer.getParticleAssignmentByIndex(particleIndex)
        self.typeSelectorCombo.setCurrentText(assignment)
        
        self.currentParticleIndex = particleIndex
        partIndices = self.particleContainer.getIndicesOfParticleType(assignment)
        self.particleSelector.setValue(partIndices.index(partIndex)+1)
        
        specIndices = self.particleContainer.getSpectraIndicesOfParticle(partIndex)
        self.spectrumSelector.setValue(specIndices.index(specIndex)+1)
        
        if centerOn:
            self.viewparent.centerOnRamanIndex(self.currentSpectrumIndex, centerOn=True, highlightContour=True)
        self.viewparent.highLightContour(self.getParticleIndexFromParticleSelector())
            
        ###connect Boxes
        self.typeSelectorCombo.currentIndexChanged.connect(self.displayNewPolymerType)
        self.typeSelectorCombo.currentIndexChanged.connect(self.getSpecIndexFromBoxesAndJumpThere)
#        self.particleSelector.valueChanged.connect(self.updateSpecPlotToSelectedParticle)
        self.particleSelector.valueChanged.connect(self.getSpecIndexFromBoxesAndJumpThere)
#        self.spectrumSelector.valueChanged.connect(self.selectSpectrum)
        self.spectrumSelector.valueChanged.connect(self.getSpecIndexFromBoxesAndJumpThere)
        self.displayNewPolymerType()
        self.updateSpecPlotToSelectedParticle()
        self.updateHistogramsAndContours()
        self.particleSelector.setMaximum(len(partIndices))
        
        
    def selectParticleOfIndex(self, particleIndex, centerOn=False):
        specIndices = self.particleContainer.getSpectraIndicesOfParticle(particleIndex)
        self.specNumberSelector.setValue(specIndices[0]+1)
        self.jumpToIndicatedSpectrum(centerOn=centerOn)
        self.particleSelector.setValue(partIndices.index(particleIndex)+1)
        
    def displayNewPolymerType(self, resetCurrentIndex=True):
        polymerName = self.typeSelectorCombo.currentText()
        numParticles = self.particleContainer.getNumberOfParticlesOfAssignment(polymerName)
        self.particleSelector.setMaximum(numParticles)
        self.particleNumberLabel.setText(f'of {numParticles} Particles; ')
        
        self.particleSelector.setValue(1)
        specIndices = self.particleContainer.getSpectraIndicesOfParticle(self.currentParticleIndex)
        self.spectrumSelector.setValue(1)
        self.spectrumSelector.setMaximum(len(specIndices))        
        self.spectrumNumberLabel.setText(f'of {len(specIndices)} spectra')
        
        self.currentParticleIndex = self.getParticleIndexFromParticleSelector()
        numSpectra = self.particleContainer.getNumberOfSpectraOfParticle(self.currentParticleIndex)
        self.spectrumSelector.setMaximum(numSpectra)
        self.spectrumNumberLabel.setText(f'of {numSpectra} Spectra')
        self.currentSpectrumIndex = self.getSpectrumIndexFromSpectrumSelector()
        self.currentSpectrumIndex = specIndices[self.spectrumSelector.value()-1]
        self.specNumberSelector.setValue(self.currentSpectrumIndex+1)
        self.updateSpecPlot()
        
    def updateSpecPlotToSelectedParticle(self, resetSpectrumCount=True):
        polymerName = self.typeSelectorCombo.currentText()
        if polymerName != '':
            self.currentParticleIndex = self.getParticleIndexFromParticleSelector()
            numSpectra = self.particleContainer.getNumberOfSpectraOfParticle(self.currentParticleIndex)
            self.spectrumSelector.setMaximum(numSpectra)
            self.spectrumNumberLabel.setText(f'of {numSpectra} Spectra')
            if resetSpectrumCount:
                self.spectrumSelector.setValue(1)
        self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
        self.spectrumSelector.valueChanged.connect(self.setSpecSelector)
        self.particleSelector.valueChanged.connect(self.setParticleSelector)
        
            self.currentSpectrumIndex = self.getSpectrumIndexFromSpectrumSelector()
            self.updateSpecPlot()
    def jumpToIndicatedSpectrum(self):   #TODO: This should be transferred to sampleview...
        self.currentSpectrumIndex = self.specNumberSelector.value()-1
        self.currentParticleIndex = self.particleContainer.getParticleIndexContainingSpecIndex(self.currentSpectrumIndex)
        self.centerOnSpecIndex(self.currentSpectrumIndex)
        self.highLightContour(self.currentParticleIndex)
    
#    def selectSpectrum(self):
#        self.currentSpectrumIndex = self.getSpectrumIndexFromSpectrumSelector()
#        self.updateSpecPlot()
    def centerOnSpecIndex(self, index):
        self.viewparent.centerOnRamanIndex(index)
    
    def getSpectrumIndexFromSpectrumSelector(self):
        specIndicesOfParticle = self.particleContainer.getSpectraIndicesOfParticle(self.currentParticleIndex)
        try:
            return specIndicesOfParticle[self.spectrumSelector.value()-1]
        except IndexError:
            print(f'requested specIndex {self.spectrumSelector.value()-1} for particle Index {self.currentParticleIndex} of specIndices: {specIndicesOfParticle}')
    
    def getParticleIndexFromParticleSelector(self):
        particleIndicesOfType = self.particleContainer.getIndicesOfParticleType(self.typeSelectorCombo.currentText())
        return particleIndicesOfType[self.particleSelector.value()-1]
    def highLightContour(self, index):
        self.viewparent.highLightContour(index)
    
    def darkenBackground(self):
        self.viewparent.darkenPixmap = self.darkenAct.isChecked()
@@ -628,9 +613,13 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
        numIndices = len(indices)
        
        for index, partIndex in enumerate(indices):
            self.selectParticleOfIndex(partIndex, centerOn=True)
            self.setWidgetsToNewParticleIndex(partIndex)
            assignment = self.dataset.particleContainer.getParticleAssignmentByIndex(partIndex)
            specIndices = self.dataset.particleContainer.getSpectraIndicesOfParticle(partIndex)
            self.viewparent.highLightContour(partIndex)
            self.viewparent.centerOnRamanIndex(specIndices[0])
            reply = QtWidgets.QMessageBox.question(self, f'Particle {index+1} of {numIndices}',
                                "Do you want to remove that particle?",
                                f"Do you want to remove that particle? Type = {assignment}",
                                QtWidgets.QMessageBox.Yes | 
                                QtWidgets.QMessageBox.No | QtWidgets.QMessageBox.Cancel, QtWidgets.QMessageBox.No)
            
@@ -645,6 +634,7 @@ class ParticleAnalysis(QtWidgets.QMainWindow):
        
        self.particleContainer.resetParticleIndices()
        self.viewparent.resetContourIndices()
        self.updateHistogramsAndContours()
                
    
    def show_hide_labels(self):
+2 −2
Original line number Diff line number Diff line
@@ -337,9 +337,9 @@ class Particle(object):
    
    def getShortParticleSize(self):
        if not np.isnan(self.shortSize_ellipse):
            return round(self.shortSize_ellipse)
            return self.shortSize_ellipse
        elif not np.isnan(self.shortSize_box):
            return round(self.shortSize_box)
            return self.shortSize_box
        else:
            print(f'Error, particle size requested, but not yet set.\nParticle Index is {self.index}')
            raise ValueError
+7 −8
Original line number Diff line number Diff line
@@ -397,7 +397,8 @@ class SampleView(QtWidgets.QGraphicsView):
        I would prefer having it in the viewitems themselves, but somehow the sampleview has to check
        for already selected indices and also check when nothing is hit...
        '''
        def addContourToSelection(cnt):
        def acceptSelection(cnt):
            self.analysiswidget.setWidgetsToNewParticleIndex(cnt.particleIndex)
            cnt.isSelected = True
            cnt.update()
            self.selectedParticleIndices.append(cnt.particleIndex)
@@ -413,12 +414,10 @@ class SampleView(QtWidgets.QGraphicsView):
        for index, cnt in enumerate(self.contourItems):
            if cnt.polygon.containsPoint(p, QtCore.Qt.OddEvenFill):   #clicked on particle
                if not event.modifiers()==QtCore.Qt.ShiftModifier:
                    addContourToSelection(cnt)
                    self.analysiswidget.selectParticleOfIndex(cnt.particleIndex)
                    acceptSelection(cnt)
                else:
                    if cnt.particleIndex not in self.selectedParticleIndices:
                        addContourToSelection(cnt)
                        self.analysiswidget.selectParticleOfIndex(cnt.particleIndex)
                        acceptSelection(cnt)
                    elif cnt.particleIndex in self.selectedParticleIndices:
                        removeContourFromSelection(cnt)
            
@@ -617,15 +616,15 @@ class SampleView(QtWidgets.QGraphicsView):
            self.ramanscanitems[index].setHighLight(True)
    
    def highLightContour(self, index):
        print('highlighting contour', index)
        for contour in self.contourItems:
            contour.isSelected = False
        self.contourItems[index].isSelected = True
    
    
    def centerOnRamanIndex(self, index, centerOn=True, highlightContour=True):
    def centerOnRamanIndex(self, index, centerOn=True, highlightIndex=True):
        if centerOn:
            self.centerOn(self.ramanscanitems[index])
        if highlightContour:
        if highlightIndex:
            self.highLightRamanIndex(index)
        
    def clearItems(self):