analysiswidgets.py 14.5 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
GEPARD - Gepard-Enabled PARticle Detection
Copyright (C) 2018  Lars Bittrich and Josef Brandt, Leibniz-Institut für 
Polymerforschung Dresden e. V. <bittrich-lars@ipfdd.de>    

This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program, see COPYING.  
If not, see <https://www.gnu.org/licenses/>.
"""
from PyQt5 import QtWidgets, QtCore

class PolymerTypeCheckboxes(QtWidgets.QScrollArea):
    PolymerCheckBoxToggled = QtCore.pyqtSignal()
    
    def __init__(self):
        super(PolymerTypeCheckboxes, self).__init__()
        self.setWidgetResizable(True)
        widget = QtWidgets.QWidget()
        self.setWidget(widget)
        
        self.layout = QtWidgets.QVBoxLayout(widget)

        self.checkBoxToggledSignalFunc = lambda: self.PolymerCheckBoxToggled.emit()
        
        self.createCheckBoxGroup()
        self.addShowAllCheckbox()
        
        self.polymerCheckBoxes = []

    def createCheckBoxGroup(self):
        '''
        Create groupbox for holding the individual checkeboxes for each polymer type
        :return:
        '''
        self.checkBoxGroup = QtWidgets.QGroupBox('Display Polymer Types:')
        self.checkBoxGroupLayout = QtWidgets.QVBoxLayout()
        self.checkBoxGroup.setLayout(self.checkBoxGroupLayout)
        self.layout.addWidget(self.checkBoxGroup)

    def addShowAllCheckbox(self):
        '''
        Adds a checkbox for toggling whether to display all polymer types in the connected plots,
        or only the ones that are selected.
        :return:
        '''
        self.showAllCheckBox = QtWidgets.QCheckBox('Show All')
        self.showAllCheckBox.setChecked(True)
        self.showAllCheckBox.setDisabled(True)
        self.showAllCheckBox.stateChanged.connect(self.checkBoxToggledSignalFunc)
        self.checkBoxGroupLayout.addWidget(self.showAllCheckBox)
    
    def updatePolymerCheckBoxes(self, polymerTypes):
        '''
        Takes a list of polymerTypes and recreates the polymer Checkboxes accordingly.
        :return:
        '''
        lastSelectedCheckBoxNames = self.getSelectedPolymers()
        self.resetCheckBoxGroup()
        
        self.polymerCheckBoxes = []
        for polymer in polymerTypes:
            newCheckBox = QtWidgets.QCheckBox(self)
            newCheckBox.setText(polymer)
            newCheckBox.stateChanged.connect(self.checkBoxToggledSignalFunc)
            if polymer in lastSelectedCheckBoxNames:
                newCheckBox.setChecked(True)
            
            self.polymerCheckBoxes.append(newCheckBox)
            self.checkBoxGroupLayout.addWidget(newCheckBox)
            
        self.checkBoxGroupLayout.addStretch()
        self.checkBoxGroup.setLayout(self.checkBoxGroupLayout)
    
    def resetCheckBoxGroup(self):
        self.checkBoxGroup.setParent(None)
        self.createCheckBoxGroup()
        self.addShowAllCheckbox()
        self.showAllCheckBox.setDisabled(False)

    def getSelectedPolymers(self):
        '''
        Return list of currently selected polymers
        :return:
        '''
        return [checkbox.text() for checkbox in self.polymerCheckBoxes if checkbox.isChecked()]


class PolymerNavigationToolbar(QtWidgets.QGroupBox):
    WidgetsUpdated = QtCore.pyqtSignal()
    JumpToIndicatedSpec = QtCore.pyqtSignal()
    
    def __init__(self, particleContainer):
        super(PolymerNavigationToolbar, self).__init__()
        self.setTitle('Navigate through polymers')
        self.layout = QtWidgets.QHBoxLayout()
        self.setLayout(self.layout)
        self.setDisabled(True)  
        
        self.particleContainer = particleContainer
        self.currentSpectrumIndex = None
        self.currentParticleIndex = None
114 115
        self.lastSpecIndex = None
        self.lastParticleIndex = None
116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171
        
        self.createWidgets()
        
    def createWidgets(self):
        self.specNumberSelector = QtWidgets.QSpinBox()
        self.specNumberSelector.setMinimumWidth(100)
        self.specNumberSelector.setMinimum(1)
        numSpectra = self.particleContainer.getNumberOfMeasurements()
        self.specNumberSelector.setMaximum(numSpectra)
        
        self.jumpToSpecBtn = QtWidgets.QPushButton('Jump To Spectrum of Number:')
        self.jumpToSpecBtn.released.connect(lambda: self.JumpToIndicatedSpec.emit())
        
        self.typeSelectorCombo = QtWidgets.QComboBox()
        self.typeSelectorCombo.setMinimumWidth(150)
        self.particleSelector = QtWidgets.QSpinBox()
        self.particleNumberLabel = QtWidgets.QLabel('of xx particles; ')
        
        self.spectrumSelector = QtWidgets.QSpinBox()
        self.spectrumNumberLabel = QtWidgets.QLabel('of xx spectra')
        for spinbox in [self.particleSelector, self.spectrumSelector]:
            spinbox.setMinimum(1)
            spinbox.setSingleStep(1)
            spinbox.setValue(1)
      
        self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
        self.particleSelector.valueChanged.connect(self.setParticleSelector)
        self.spectrumSelector.valueChanged.connect(self.setSpecSelector)
        
        self.layout.addWidget(self.jumpToSpecBtn)
        self.layout.addWidget(self.specNumberSelector)
        self.layout.addWidget(QtWidgets.QLabel('Select Polymer Type:'))
        self.layout.addWidget(self.typeSelectorCombo)
        self.layout.addStretch()
        self.layout.addWidget(QtWidgets.QLabel('Select Particle'))
        self.layout.addWidget(self.particleSelector)
        self.layout.addWidget(self.particleNumberLabel)
        self.layout.addStretch()
        self.layout.addWidget(QtWidgets.QLabel('Select Spectrum'))
        self.layout.addWidget(self.spectrumSelector)
        self.layout.addWidget(self.spectrumNumberLabel)
        
    def keyPressEvent(self, event):
        """
        If the enter key is pressed, all views are updated to the indicated spectrum number
        """
        if event.key() in [QtCore.Qt.Key_Return, QtCore.Qt.Key_Enter]:
            self.JumpToIndicatedSpec.emit()
    
    def updateWidgets(self):
        """
        Whenever the particle assignments in the particleContainer have changed, the widges have to be updated.
        :return:
        """
        uniquePolymers = self.particleContainer.getUniquePolymers()
        if len(uniquePolymers) > 0:
172 173 174
            self.lastSpecIndex = self.currentSpectrumIndex
            self.lastParticleIndex = self.currentParticleIndex
            
175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211
            self.setDisabled(False)    
            self.typeSelectorCombo.currentIndexChanged.disconnect()
            self.typeSelectorCombo.clear()
            self.typeSelectorCombo.addItems(uniquePolymers)
            self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
            
            numSpectra = self.particleContainer.getNumberOfMeasurements()
            self.specNumberSelector.setMaximum(numSpectra)
            
            self.setTypeSelector()
        else:
            self.setDisabled(True)    
    
    @QtCore.pyqtSlot(int)
    def setWidgetsToNewParticleIndex(self, particleIndex):
        """
        When a particle is selected in the sampleview, this method makes updating all widgets accordingly.
        :return:
        """
        try:
            self.particleSelector.valueChanged.disconnect()
            self.spectrumSelector.valueChanged.disconnect()
            self.typeSelectorCombo.currentIndexChanged.disconnect()
        except TypeError:
            pass #signals were not connected, nothing to disconnect...
        
        assignment = self.particleContainer.getParticleAssignmentByIndex(particleIndex)
        self.typeSelectorCombo.setCurrentText(assignment)
        
        self.currentParticleIndex = particleIndex
        partIndices = self.particleContainer.getIndicesOfParticleType(assignment)
        self.particleSelector.setMaximum(len(partIndices))
        self.particleNumberLabel.setText(f'of {len(partIndices)} particles; ')
        
        self.particleSelector.setValue(partIndices.index(particleIndex)+1)
        
        specIndices = self.particleContainer.getSpectraIndicesOfParticle(self.currentParticleIndex)
212 213 214 215 216 217 218 219 220 221 222
        if len(specIndices) > 0:
            self.spectrumSelector.setDisabled(False)
            self.spectrumSelector.setValue(1)
            self.spectrumSelector.setMaximum(len(specIndices))      
            self.currentSpectrumIndex = specIndices[self.spectrumSelector.value()-1]
            self.specNumberSelector.setValue(self.currentSpectrumIndex+1)
        else:
            self.spectrumSelector.setDisabled(True)
            self.spectrumSelector.setValue(0)
            self.spectrumSelector.setMaximum(0)      

223
        self.spectrumNumberLabel.setText(f'of {len(specIndices)} spectra')
224
            
225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251
        self.typeSelectorCombo.currentIndexChanged.connect(self.setTypeSelector)
        self.spectrumSelector.valueChanged.connect(self.setSpecSelector)
        self.particleSelector.valueChanged.connect(self.setParticleSelector)
        self.WidgetsUpdated.emit()
    
    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)
252 253 254 255 256 257 258 259
        if len(specIndices) == 0:
            self.spectrumSelector.setDisabled(True)
        else:
            self.spectrumSelector.setDisabled(False)
            
            self.currentSpectrumIndex = specIndices[self.spectrumSelector.value()-1]
            self.specNumberSelector.setValue(self.currentSpectrumIndex+1)
            
260 261 262 263
        if self.lastSpecIndex is not None:
            self.currentParticleIndex = self.particleContainer.getParticleIndexContainingSpecIndex(self.lastSpecIndex)
            self.lastSpecIndex = None
            self.setWidgetsToNewParticleIndex(self.currentParticleIndex)
264 265 266
        
        self.JumpToIndicatedSpec.emit()
        self.WidgetsUpdated.emit()
267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352


class FindColoredParticleWindow(QtWidgets.QWidget):
    ParticleOfIndexSelected = QtCore.pyqtSignal(int)
    def __init__(self, analysisparent):
        super(FindColoredParticleWindow, self).__init__()
        self.analysisparent = analysisparent
        self.particleContainer = analysisparent.particleContainer
        
        self.coloredParticles = None
        self.numColoredParticles = None
        self.currentParticleIndex = None
        
        self.label = QtWidgets.QLabel()
        self.prevBtn = QtWidgets.QPushButton("Go to partilcle 0", self)
        self.prevBtn.released.connect(self.goToPreviousParticle)
        
        self.nextBtn = QtWidgets.QPushButton("Go to particle 1", self)
        self.nextBtn.released.connect(self.goToNextParticle)
        self.cancelBtn = QtWidgets.QPushButton("Cancel", self)
        self.cancelBtn.released.connect(self.close)
        
        self.createLayout()
        self.getColoredParticles()
        self.goToFirstParticle()
        
    def createLayout(self):
        layout = QtWidgets.QVBoxLayout()
        self.setLayout(layout)
        layout.addWidget(self.label)
        layout.addWidget(self.prevBtn)
        layout.addWidget(self.nextBtn)
        layout.addWidget(self.cancelBtn)
        self.show()
    
    def getColoredParticles(self):
        """
        Retrieves a list of Particles from the PrticleContainer with all particles that are not unknown and not white
        :return:
        """
        self.coloredParticles = self.particleContainer.getIndicesOfColoredNotUnknownParticles()
        self.numColoredParticles = len(self.coloredParticles)
    
    def goToFirstParticle(self):
        self.currentParticleIndex = 0
        self.enableDisableButtons()
        self.updateToCurrentIndex()
        
    def goToNextParticle(self):
        if self.currentParticleIndex < self.numColoredParticles-1:
            self.currentParticleIndex += 1
            self.updateToCurrentIndex()
        self.enableDisableButtons()
            
    def goToPreviousParticle(self):
        if self.currentParticleIndex > 0:
            self.currentParticleIndex -= 1
            self.updateToCurrentIndex()
        self.enableDisableButtons()

    def enableDisableButtons(self):
        """
        Checks the previous and next particle buttons if they have to be enabled or disabled
        :return:
        """
        self.prevBtn.setEnabled(True)
        self.nextBtn.setEnabled(True)
        if self.currentParticleIndex == 0:
            self.prevBtn.setDisabled(True)
            self.prevBtn.setText('No previous particle.')
        if self.currentParticleIndex == self.numColoredParticles-1:
            self.nextBtn.setDisabled(True)
            self.nextBtn.setText('No next particle.')
    
    def updateToCurrentIndex(self):
        """
        Updates analysis- and sampleview to display the currently selected particle.
        :return:
        """
        self.label.setText(f'Displaying Particle {self.currentParticleIndex+1} 0f {self.numColoredParticles}')
        self.prevBtn.setText(f'Go to revious Particle')
        self.nextBtn.setText(f'Go to next Particle')
        
        indexOfParticleToDisplay = self.coloredParticles[self.currentParticleIndex]
        self.ParticleOfIndexSelected.emit(indexOfParticleToDisplay)
        self.analysisparent.jumpToIndicatedSpectrum()