Skip to content
Snippets Groups Projects
GUI_main.py 108 KiB
Newer Older
  • Learn to ignore specific revisions
  • lpbsscientist's avatar
    lpbsscientist committed
    #        that the button is still in use, because it gets a blue color.
            self.button_changecellvalue.setChecked(False)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
        def DialogBoxECV(self, s):
    
            """This functions creates from the ExchangeCellValues.py file a 
    
    lpbsscientist's avatar
    lpbsscientist committed
            window which takes two integer entries and then swaps the cells having
            the given integer values.
            """
    #        creates a dialog window from the ExchangeCellValues.py file
            dlg = ecv.CustomDialog(self)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        if the user presses 'ok', it executes the code
            if dlg.exec_():
    
    #            it tests if both value to be swapped are not empty.
                if dlg.entry1.text()!= '' and dlg.entry2.text() != '':
    
    lpbsscientist's avatar
    lpbsscientist committed
    #                reads out the values and converts it into integers.
                    value1 = int(dlg.entry1.text())
                    value2 = int(dlg.entry2.text())
    
    lpbsscientist's avatar
    lpbsscientist committed
    #                calls the function which does the swap
                    self.m.ExchangeCellValue(value1,value2)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #                if the button to display the values of the cell is checked,
    #                the values are again displayed on the graph after the swap
    #                of cells.
                    if self.button_showval.isChecked():
                        self.m.ShowCellNumbersCurr()
                        self.m.ShowCellNumbersNext()
                        self.m.ShowCellNumbersPrev()
    
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
                return
    
    lpbsscientist's avatar
    lpbsscientist committed
        def SelectChannel(self, index):
            """This function is called when the button to select different channels
            is used. From the displayed list in the button, the chosen index
            corresponnds to the same index in the list of channels from the reader.
            So, it sets the default channel with the new index (called index below)
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        This function attributes the FOV chosen by the user corresponding to
    #        the index in the list of options.
            self.reader.default_channel = index
    
    #        update the pictures using the same function as the one used to 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        change the fields of view.
            self.ChangeFOV()
    
    lpbsscientist's avatar
    lpbsscientist committed
    
    
    
        def SelectFov(self, index):
    
            """This function is called when the button containing the list of 
    
    lpbsscientist's avatar
    lpbsscientist committed
            fields od view is used.
            The index correspondds to the field of view selected in the list.
    
    lpbsscientist's avatar
    lpbsscientist committed
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        This function attributes the FOV chosen by the user corresponding to
    #        the index in the list of options. First the mask is automatically
    #        saved.
            self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    #        the new index is set.
    
            self.FOVindex = index    
            
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        it updates the fov in the plot with the new index.
            self.ChangeFOV()
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        the flag of the fluorescence extraction is set to False (such that
    #        if the user extracts fluorescence data in the new field of  view,
    #        there is a dialog box asking to select the corresponding xls file
    #        for this field of view. IF there is no data sheet for this fov, the
    #        user can enter a new name to make a new file.)
            self.FlagFluoExtraction = False
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ChangeFOV(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        it changes the fov or channel according to the choice of the user
    #        and it updates the plot shown and it initializes the new fov/channel
    #        at t=0 by default.
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        set the time index to 0
            self.Tindex = 0
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        load the image and mask for the current plot
            self.m.currpicture = self.reader.LoadOneImage(self.Tindex,self.FOVindex)
            self.m.plotmask = self.reader.LoadMask(self.Tindex,self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        sets the image and the mask to 0 for the previous plot
            self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
            self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        load the image and the mask for the next plot.
            self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1, self.FOVindex)
            self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
            
            
    #        once the images and masks are loaded into the variables, they are 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        displaye in the gui.
            self.m.UpdateBckgrndPicture()
    
            
            
    #       enables the next frame button in case it was disabled when the 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        fov/channel was changed
            self.button_nextframe.setEnabled(True)
    
    #        disables the previous frame button in case it was active before 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        changing fov/channel.
            self.button_previousframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        updates the title of the plots to display the right time indices
    #        aboves the plots.
            self.UpdateTitleSubplots()
    
    #       if the button to show cell values is active, it shows the values again.
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    lpbsscientist's avatar
    lpbsscientist committed
    #       if the button to hide the mask was checked before changing fov/channel,
    #       it hides the mask again.
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        the button to set the time index is also set to 0/default again.
            self.button_timeindex.setText('')
    
    #        enables the neural network buttons if there is already an 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        existing prediction for the current image.
            self.EnableCNNButtons()
    
        def ChangeTimeFrame(self):
    
            """This funcion is called whenever the user gives an new time index, 
    
    lpbsscientist's avatar
    lpbsscientist committed
            to jump to the new given index, onces "enter" button is pressed.
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        it reads out the text in the button and converts it to an int.
            newtimeindex = int(self.button_timeindex.text())
            if newtimeindex >= 0 and newtimeindex <= self.reader.sizet-1:
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.Tindex = newtimeindex
    
    lpbsscientist's avatar
    lpbsscientist committed
                if self.Tindex == 0:
                    self.button_nextframe.setEnabled(True)
                    self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1,self.FOVindex)
                    self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                    self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.UpdateBckgrndPicture()
                    self.button_previousframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
                elif self.Tindex == self.reader.sizet-1:
                    self.button_previousframe.setEnabled(True)
                    self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-1, self.FOVindex)
                    self.m.prevplotmask = self.reader.LoadMask(self.Tindex-1, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.nextpicture =  np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                    self.m.nextplotmask =  np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.UpdateBckgrndPicture()
                    self.button_nextframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
                else:
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.button_nextframe.setEnabled(True)
                    self.button_previousframe.setEnabled(True)
                    self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-1, self.FOVindex)
                    self.m.prevplotmask = self.reader.LoadMask(self.Tindex-1, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.currpicture = self.reader.LoadOneImage(self.Tindex, self.FOVindex)
    
                    self.m.plotmask = self.reader.LoadMask(self.Tindex, self.FOVindex)              
                      
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+1,self.FOVindex)
                    self.m.nextplotmask = self.reader.LoadMask(self.Tindex+1, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.UpdateBckgrndPicture()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.UpdateTitleSubplots()
                self.button_timeindex.clearFocus()
    
    lpbsscientist's avatar
    lpbsscientist committed
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
    
    lpbsscientist's avatar
    lpbsscientist committed
                if self.button_hidemask.isChecked():
                    self.m.HideMask()
                self.EnableCNNButtons()
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
                self.button_timeindex.clearFocus()
                return
    
    lpbsscientist's avatar
    lpbsscientist committed
    #    def keyPressEvent(self, event):
    ##        print('keypressevengda211111')
    ##        print(event.key())
    #        if not self.button_nextframe.isChecked() and event.key() == Qt.Key_Right:
    #            self.button_nextframe.setChecked(True)
    #            self.ForwardTime()
    #            self.button_nextframe.setChecked(False)
    #        else:
    #            print('inside keypressevent')
    #            event.ignore()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def CellCorrespActivation(self):
                self.Disable(self.button_cellcorespondance)
                self.statusBar.showMessage('Doing the cell correspondance')
    
                if self.Tindex != 0:
                    self.m.plotmask,_ = self.reader.CellCorrespondance(self.Tindex, self.FOVindex)
                    self.m.updatedata()
                else:
                    self.m.plotmask = self.reader.LoadSeg(self.Tindex, self.FOVindex)
                    self.m.updatedata()
    
                self.Enable(self.button_cellcorespondance)
                self.button_cellcorespondance.setChecked(False)
                self.statusBar.clearMessage()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def SegmentBoxCheck(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            if self.button_segment.isChecked():
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.Disable(self.button_segment)
                self.EnableCorrectionsButtons()
                self.m.SegmentedMask = self.reader.LoadSeg(self.Tindex, self.FOVindex)
                self.m.tempplotmask = self.m.plotmask.copy()
                self.m.plotmask = self.m.SegmentedMask.copy()
                self.m.currmask.set_data((self.m.SegmentedMask%10 + 1)*(self.m.SegmentedMask != 0))
                self.m.ax.draw_artist(self.m.currplot)
                self.m.ax.draw_artist(self.m.currmask)
                self.m.update()
                self.m.flush_events()
    #            update the graph
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.button_SetSegmentation.setEnabled(True)
                self.button_savesegmask.setEnabled(True)
            else:
                self.m.SegmentedMask = self.m.plotmask.copy()
                self.m.plotmask = self.m.tempplotmask.copy()
                self.m.updatedata()
                self.button_SetSegmentation.setEnabled(False)
                self.button_savesegmask.setEnabled(False)
                self.Enable(self.button_segment)
    
    lpbsscientist's avatar
    lpbsscientist committed
        def SegmentThresholdedPredMask(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            segparamvalue = int(self.button_SetSegmentation.text())
            self.m.plotmask = self.reader.Segment(segparamvalue, self.Tindex,self.FOVindex)
            self.m.currmask.set_data((self.m.plotmask%10 + 1)*(self.m.plotmask != 0))
            self.m.ax.draw_artist(self.m.currplot)
            self.m.ax.draw_artist(self.m.currmask)
            self.m.update()
            self.m.flush_events()
    #        self.m.SegmentedMask = self.reader.Segment(segparamvalue, self.Tindex, self.FOVindex)
    #        update the plots to display the segmentation view
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ButtonSaveSegMask(self):
            """saves the segmented mask
            """
            self.reader.SaveSegMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ThresholdBoxCheck(self):
    
            """if the buttons is checked it shows the thresholded version of the 
    
    lpbsscientist's avatar
    lpbsscientist committed
            prediction, if it is not available it justs displays a null array.
            The buttons for the setting a threshold a value and to save it are then
            activated once this button is enabled.
            """
            if self.button_threshold.isChecked():
                self.Disable(self.button_threshold)
                self.m.ThresholdMask = self.reader.LoadThreshold(self.Tindex, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.currmask.set_data(self.m.ThresholdMask)
                self.m.ax.draw_artist(self.m.currplot)
                self.m.ax.draw_artist(self.m.currmask)
                self.m.update()
                self.m.flush_events()
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            update the gra
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.button_SetThreshold.setEnabled(True)
                self.button_savethresholdmask.setEnabled(True)
            else:
                self.m.updatedata()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.button_SetThreshold.setEnabled(False)
                self.button_savethresholdmask.setEnabled(False)
                self.Enable(self.button_threshold)
    
        def ThresholdPrediction(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            thresholdvalue = float(self.button_SetThreshold.text())
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.m.ThresholdMask = self.reader.ThresholdPred(thresholdvalue, self.Tindex,self.FOVindex)
            self.m.currmask.set_data(self.m.ThresholdMask)
            self.m.ax.draw_artist(self.m.currplot)
            self.m.ax.draw_artist(self.m.currmask)
            self.m.update()
            self.m.flush_events()
    #        self.m.ThresholdMask = self.reader.ThresholdPred(thresholdvalue, self.Tindex, self.FOVindex)
    #        update the plots to display the thresholded view
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ButtonSaveThresholdMask(self):
            """saves the thresholed mask
            """
    #        pass
            self.reader.SaveThresholdMask(self.Tindex, self.FOVindex, self.m.ThresholdMask)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ChangePreviousFrame(self):
    
            
             """This function is called when the previous frame buttons is pressed 
    
    lpbsscientist's avatar
    lpbsscientist committed
             and it tests if the buttons is enabled and if so it calls the
    
             BackwardTime() function. It should avoid the let the user do multiple 
    
    lpbsscientist's avatar
    lpbsscientist committed
             clicks and that the function is then called afterwards several times,
             once the frames and masks of the current time index have been loaded.
             """
    
    lpbsscientist's avatar
    lpbsscientist committed
             if self.button_previousframe.isEnabled():
                self.button_previousframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.button_nextframe.disconnect()
    #            self.button_nextframe.setShortcut('')
    
    #            self.button_nextframe.setEnabled(False)
    #            print(self.button_nextframe.isEnabled())
    #            self.button_nextframe.setChecked(False)
    #            self.Testbool = False
                self.BackwardTime()
    #            self.button_nextframe.setShortcut(Qt.Key_Right)
    #            self.button_nextframe.pressed.connect(self.Test)
    #            self.button_nextframe.setChecked(False)
                if self.Tindex >0:
                    self.button_previousframe.setEnabled(True)
    #            self.button_nextframe.pressed.connect(self.Test)
    
    #            self.button_nextframe.setChecked(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.Testbool = True
             else:
    #            print('jamais la dedans?')
                 return
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ChangeNextFrame(self):
    
            
            """This function is called when the next frame buttons is pressed 
    
    lpbsscientist's avatar
    lpbsscientist committed
            and it tests if the buttons is enabled and if so it calls the
    
            ForwardTime() function. It should avoid the let the user do multiple 
    
    lpbsscientist's avatar
    lpbsscientist committed
            clicks and that the function is then called afterwards several times,
            once the frames and masks of the current time index have been loaded.
            """
    #        self.button_nextframe.setShortcutEnabled(False)
            if self.button_nextframe.isEnabled():
                self.button_nextframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.button_nextframe.disconnect()
    #            self.button_nextframe.setShortcut('')
    
    #            self.button_nextframe.setEnabled(False)
    #            print(self.button_nextframe.isEnabled())
    #            self.button_nextframe.setChecked(False)
    #            self.Testbool = False
                self.ForwardTime()
    #            self.button_nextframe.setShortcut(Qt.Key_Right)
    #            self.button_nextframe.pressed.connect(self.Test)
    #            self.button_nextframe.setChecked(False)
    
                self.button_nextframe.setEnabled(True)
    #            self.button_nextframe.pressed.connect(self.Test)
    
    #            self.button_nextframe.setChecked(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.Testbool = True
            else:
                return
    #            print('jamais la dedans?')
    #            if QKeyEvent.key() == Qt.Key_Right:
    #                QKeyEvent.ignore()
    
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ForwardTime(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            """This function switches the frame in forward time index. And it tests
            several conditions if t == lastTimeIndex-1, because then the next frame
            button has to be disabled. It also tests if the show value of cells
    
            button and hidemask are active in order to hide/show the mask or to 
    
    lpbsscientist's avatar
    lpbsscientist committed
            show the cell values.
            """
    #        print(self.Tindex)
    #        the t frame is defined as the currently shown frame on the display.
    #        If the button "Next time frame" is pressed, this function is called
            self.statusBar.showMessage('Loading the next frame...')
    #        self.button_nextframe.setEnabled(False)
    #        self.button_nextframe.disconnect()
            self.Disable(self.button_nextframe)
    
    
            if self.Tindex + 1 < self.reader.sizet - 1 :
    
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
                
                self.m.prevpicture = self.m.currpicture.copy()
                self.m.prevplotmask = self.m.plotmask.copy()
                
                self.m.currpicture = self.m.nextpicture.copy()
                self.m.plotmask = self.m.nextplotmask.copy()
                
                
                self.m.nextpicture = self.reader.LoadOneImage(self.Tindex+2, self.FOVindex)
                self.m.nextplotmask = self.reader.LoadMask(self.Tindex+2, self.FOVindex)
                self.m.UpdateBckgrndPicture()
    
                if self.Tindex + 1 == 1:
                    self.button_previousframe.setEnabled(True)
                    
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
    
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
            
                self.m.prevpicture = self.m.currpicture.copy()
                self.m.prevplotmask = self.m.plotmask.copy()
                self.m.currpicture = self.m.nextpicture.copy()
                self.m.plotmask = self.m.nextplotmask.copy()
                
                
                
                self.m.nextpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                self.m.nextplotmask = np.zeros([self.reader.sizey,self.reader.sizex], dtype = np.uint16)
                self.m.UpdateBckgrndPicture()
    
                self.button_nextframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
    
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    lpbsscientist's avatar
    lpbsscientist committed
    
            self.Tindex = self.Tindex+1
            self.UpdateTitleSubplots()
    
    lpbsscientist's avatar
    lpbsscientist committed
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
            self.Enable(self.button_nextframe)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        self.button_nextframe.setChecked(False)
    
            self.statusBar.clearMessage()
    #        if self.Tindex < self.reader.sizet - 1 :
    #            self.button_nextframe.setEnabled(True)
            self.button_timeindex.setText(str(self.Tindex))
    
    
    lpbsscientist's avatar
    lpbsscientist committed
        def BackwardTime(self):
    
            
            """This function switches the frame in backward time index. And it 
    
    lpbsscientist's avatar
    lpbsscientist committed
            several conditions if t == 1, because then the button previous frame has to
    
            be disabled. It also tests if the show value of cells button and 
    
    lpbsscientist's avatar
    lpbsscientist committed
            hidemask are active in order to hide/show the mask or to show the cell
            values.
            """
    #        print(self.Tindex)
    #        the t frame is defined as the currently shown frame on the display.
    #        If the button "Previous time frame" is pressed, this function is called
            self.statusBar.showMessage('Loading the previous frame...')
    #        self.button_previousframe.setEnabled(False)
    #        self.button_previousframe.disconnect()
            self.Disable(self.button_previousframe)
            if self.Tindex == 1:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
                self.m.nextpicture = self.m.currpicture.copy()
                self.m.nextplotmask = self.m.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.currpicture = self.m.prevpicture.copy()
                self.m.plotmask = self.m.prevplotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.prevpicture = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
                self.m.prevplotmask = np.zeros([self.reader.sizey, self.reader.sizex], dtype = np.uint16)
    
    
                self.m.UpdateBckgrndPicture()
    
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.button_previousframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.nextpicture = self.m.currpicture.copy()
                self.m.nextplotmask = self.m.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.currpicture = self.m.prevpicture.copy()
                self.m.plotmask = self.m.prevplotmask.copy()
    
                self.m.prevpicture = self.reader.LoadOneImage(self.Tindex-2, self.FOVindex)
                self.m.prevplotmask = self.reader.LoadMask(self.Tindex-2, self.FOVindex)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.UpdateBckgrndPicture()
                if self.Tindex-1 == self.reader.sizet-2:
                    self.button_nextframe.setEnabled(True)
    
    lpbsscientist's avatar
    lpbsscientist committed
            if self.button_showval.isChecked():
                self.m.ShowCellNumbersCurr()
                self.m.ShowCellNumbersNext()
                self.m.ShowCellNumbersPrev()
    
    lpbsscientist's avatar
    lpbsscientist committed
    
    #        self.button_previousframe.clicked.connect(self.BackwardTime)
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.Tindex = self.Tindex-1
            self.UpdateTitleSubplots()
    
            if self.button_hidemask.isChecked():
                self.m.HideMask()
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.Enable(self.button_previousframe)
    
    lpbsscientist's avatar
    lpbsscientist committed
    
            if self.Tindex > 0:
    
                self.button_previousframe.setEnabled(True)          
                
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        self.button_previousframe.setChecked(False)
            self.statusBar.clearMessage()
            self.button_timeindex.setText(str(self.Tindex))
    
        def MouseDraw(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            """
            This function is called whenever the brush or the eraser button is
            pressed. On the first press event it calls the self.m.OneClick, which
            tests whether it is a right click or a left click. If it a right click
            it assigns the value of the pixel which has been right clicked
            to self.cellval, meaning that the next drawn pixels will be set to this
            value.
            If it is left clicked, then it draws a 3x3 square with the current
            value of self.cellval.
    
            If after left clicking you drag the mouse, then you start drawing 
    
    lpbsscientist's avatar
    lpbsscientist committed
            using the mouse and it stops once you release the left click.
    
    lpbsscientist's avatar
    lpbsscientist committed
            Same for the eraser button, it sets directly the value of self.cellval
            to 0.
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
            if self.button_drawmouse.isChecked():
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.statusBar.showMessage('Drawing using the brush, right click to set a value...')
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.Disable(self.button_drawmouse)
                self.m.tempmask = self.m.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.id2 = self.m.mpl_connect('button_press_event', self.m.OneClick)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.id = self.m.mpl_connect('motion_notify_event', self.m.PaintBrush)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.id3 = self.m.mpl_connect('button_release_event', self.m.ReleaseClick)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
                pixmap = QtGui.QPixmap('./icons/brush2.png')
                cursor = QtGui.QCursor(pixmap, 1,1)
                QApplication.setOverrideCursor(cursor)
    
    lpbsscientist's avatar
    lpbsscientist committed
            elif self.button_eraser.isChecked():
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.statusBar.showMessage('Erasing by setting the values to 0...')
                self.Disable(self.button_eraser)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.tempmask = self.m.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.cellval = 0
                self.id2 = self.m.mpl_connect('button_press_event', self.m.OneClick)
                self.id = self.m.mpl_connect('motion_notify_event', self.m.PaintBrush)
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.id3 = self.m.mpl_connect('button_release_event', self.m.ReleaseClick)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
                pixmap = QtGui.QPixmap('./icons/eraser.png')
                cursor = QtGui.QCursor(pixmap, 1,1)
                QApplication.setOverrideCursor(cursor)
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
                self.m.mpl_disconnect(self.id3)
                self.m.mpl_disconnect(self.id2)
                self.m.mpl_disconnect(self.id)
                QApplication.restoreOverrideCursor()
                self.Enable(self.button_drawmouse)
                self.Enable(self.button_eraser)
    
    lpbsscientist's avatar
    lpbsscientist committed
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
                self.statusBar.clearMessage()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def UpdateTitleSubplots(self):
    
            """This function updates the title of the plots according to the 
    
    lpbsscientist's avatar
    lpbsscientist committed
            current time index. So it called whenever a frame or a fov is changed.
            """
            if self.Tindex == 0:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('No frame {}'.format(''))
                self.m.titlenext.set_text('Next Time index {}'.format(self.Tindex+1))
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('No frame {}'.format(''))
    #            self.m.ax3.set_title('Next Time index {}'.format(self.Tindex+1))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
            elif self.Tindex == self.reader.sizet-1:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('Previous time index {}'.format(self.Tindex-1))
                self.m.titlenext.set_text('No frame {}'.format(''))
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('Previous time index {}'.format(self.Tindex-1))
    #            self.m.ax3.set_title('No frame {}'.format(''))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
            else:
                self.m.titlecurr.set_text('Time index {}'.format(self.Tindex))
                self.m.titleprev.set_text('Previous time index {}'.format(self.Tindex-1))
                self.m.titlenext.set_text('Next Time index {}'.format(self.Tindex+1))
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            self.m.ax.set_title('Time index {}'.format(self.Tindex))
    #            self.m.ax2.set_title('Previous time index {}'.format(self.Tindex-1))
    #            self.m.ax3.set_title('Next Time index {}'.format(self.Tindex+1))
    #            self.m.update()
    #            self.m.flush_events()
                self.m.draw()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ClickNewCell(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            this method is called when the button New Cell is clicked. If the button
    
            state corresponds to True (if is activated) then it connects the mouse 
    
    lpbsscientist's avatar
    lpbsscientist committed
            clicks on the pyqt window to the canvas (so to the matplolib figure).
            The connection has an "id" which is given by the integer self.id
            After the connections is made, it calls the Disable function with argument 0
            which turns off the other button(s).
    
            
            If the button is clicked but it is deactivated then it disconnects the 
    
    lpbsscientist's avatar
    lpbsscientist committed
            connection between the canvas and the window (the user can not interact
    
            with the plot anymore). 
    
    lpbsscientist's avatar
    lpbsscientist committed
            Storemouseclicks is a list corresponding to the coordinates of all mouse
            clicks between the activation and the deactivation of the button.
            So if it is empty, it does not draw anything because no clicks
    
            were registered. 
    
    lpbsscientist's avatar
    lpbsscientist committed
            But if it has some coordinates, it will draw a polygon where the vertices
            are the coordinates of all the mouseclicks.
            Once the figure has been updated with a new polygon, the other button(s)
    
            are again enabled. 
            
    
    lpbsscientist's avatar
    lpbsscientist committed
            """
            if self.button_newcell.isChecked():
                self.statusBar.showMessage('Draw a new cell...')
                self.m.tempmask = self.m.plotmask.copy()
                self.id = self.m.mpl_connect('button_press_event', self.m.MouseClick)
                self.Disable(self.button_newcell)
    
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.mpl_disconnect(self.id)
                if  self.m.storemouseclicks and self.TestSelectedPoints():
                    self.m.DrawRegion(True)
                else:
                    self.m.updatedata()
                self.Enable(self.button_newcell)
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
                self.statusBar.clearMessage()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def TestSelectedPoints(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            """This function is just used to catch an exception, when the new cell
            or the add region function is called. If all the dots drawn by the user
    
            are located on one line (horizontal or vertical) the DrawRegion 
            function calls a method to create a polygon and 
    
    lpbsscientist's avatar
    lpbsscientist committed
            it can not make a polygon out of straight line so it gives an error.
            In order to prevent this error, this function avoids to attempt to draw
            by returning False if the square are all on one line.
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
            allx = list(np.array(self.m.storemouseclicks)[:,0])
            ally = list(np.array(self.m.storemouseclicks)[:,1])
    
    lpbsscientist's avatar
    lpbsscientist committed
            resultx = all(elem == allx[0] for elem in allx)
            resulty = all(elem == ally[0] for elem in ally)
    
    lpbsscientist's avatar
    lpbsscientist committed
            if resultx or resulty:
                return False
            else:
                return True
    
    lpbsscientist's avatar
    lpbsscientist committed
        def clickmethod(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            this method is called when the button Add region is clicked. If the button
    
            state corresponds to True (if is activated) then it connects the mouse 
    
    lpbsscientist's avatar
    lpbsscientist committed
            clicks on the pyqt window to the canvas (so to the matplolib figure).
            The connection has an "id" which is given by the integer self.id
            After the connections is made, it calls the Disable function with argument 1
            which turns off the other button(s).
    
            
            If the button is clicked and it is deactivated then it disconnects the 
    
    lpbsscientist's avatar
    lpbsscientist committed
            connection between the canvas and the window (the user can not interact
    
            with the plot anymore). 
    
    lpbsscientist's avatar
    lpbsscientist committed
            Storemouseclicks is a list corresponding to the coordinates of all mouse
            clicks between the activation and the deactivation of the button.
            So if it is empty, it does not draw anything because no clicks
    
            were registered. 
    
    lpbsscientist's avatar
    lpbsscientist committed
            But if it has some coordinates, it will draw a polygon where the vertices
            are the coordinates of all the mouseclicks.
            Once the figure has been updated with a new polygon, the other button(s)
    
            are again enabled. 
            
    
    lpbsscientist's avatar
    lpbsscientist committed
            """
            if self.button_add_region.isChecked():
                self.statusBar.showMessage('Adding a region to an existing cell...')
                self.m.tempmask = self.m.plotmask.copy()
    
                self.id = self.m.mpl_connect('button_press_event', self.m.MouseClick)           
                self.Disable(self.button_add_region) 
                
    
    lpbsscientist's avatar
    lpbsscientist committed
            else:
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.m.mpl_disconnect(self.id)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #            test if the list is not empty and if the dots are not all in the same line
                if  self.m.storemouseclicks and self.TestSelectedPoints():
    
    lpbsscientist's avatar
    lpbsscientist committed
                    self.m.DrawRegion(False)
    
                else:
                    self.m.updatedata()
                self.Enable(self.button_add_region)
    
    lpbsscientist's avatar
    lpbsscientist committed
                if self.button_showval.isChecked():
                    self.m.ShowCellNumbersCurr()
                    self.m.ShowCellNumbersNext()
                    self.m.ShowCellNumbersPrev()
    
    lpbsscientist's avatar
    lpbsscientist committed
                self.statusBar.clearMessage()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def Enable(self, button):
    
    lpbsscientist's avatar
    lpbsscientist committed
             """
             this functions turns on buttons all the buttons, depending on the time
    
             index. (next and previous buttons should not be turned on if t = 0 
    
    lpbsscientist's avatar
    lpbsscientist committed
             or t = lasttimeindex)
             """
             if self.button_segment.isChecked():
                 self.EnableCorrectionsButtons()
                 self.button_home.setEnabled(True)
                 self.button_zoom.setEnabled(True)
                 self.button_pan.setEnabled(True)
                 self.button_back.setEnabled(True)
                 self.button_forward.setEnabled(True)
             else:
                 for k in range(0, len(self.buttonlist)):
                     if button != self.buttonlist[k]:
                         self.buttonlist[k].setEnabled(True)
    
    lpbsscientist's avatar
    lpbsscientist committed
             if self.Tindex == 0:
                 self.button_previousframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
             if self.Tindex == self.reader.sizet-1:
                 self.button_nextframe.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
             self.EnableCNNButtons()
    
    lpbsscientist's avatar
    lpbsscientist committed
        def Disable(self, button):
    
    lpbsscientist's avatar
    lpbsscientist committed
             """
    
             this functions turns off all the buttons except the one given in 
    
    lpbsscientist's avatar
    lpbsscientist committed
             argument.
             """
             flag = False
             if button == self.button_add_region or button == self.button_newcell or button == self.button_exval or button == self.button_changecellvalue or button == self.button_drawmouse or button == self.button_eraser:
                 if self.button_segment.isChecked():
                     flag = True
    
    
    lpbsscientist's avatar
    lpbsscientist committed
             for k in range(0,len(self.buttonlist)):
                 if button != self.buttonlist[k]:
                     self.buttonlist[k].setEnabled(False)
             if flag:
                 self.button_segment.setEnabled(True)
    
    lpbsscientist's avatar
    lpbsscientist committed
             if button == self.button_segment or button == self.button_threshold:
                 self.button_home.setEnabled(True)
                 self.button_zoom.setEnabled(True)
                 self.button_pan.setEnabled(True)
                 self.button_back.setEnabled(True)
                 self.button_forward.setEnabled(True)
    
        def EnableCNNButtons(self):
    
    lpbsscientist's avatar
    lpbsscientist committed
            if self.reader.TestPredExisting(self.Tindex, self.FOVindex):
    #            self.button_cnn.setEnabled(False)
                self.button_threshold.setEnabled(True)
                self.button_segment.setEnabled(True)
                self.button_cellcorespondance.setEnabled(True)
                self.button_extractfluorescence.setEnabled(True)
            else:
    #            self.button_cnn.setEnabled(True)
                self.button_threshold.setEnabled(False)
                self.button_segment.setEnabled(False)
                self.button_cellcorespondance.setEnabled(False)
                self.button_extractfluorescence.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
        def EnableCorrectionsButtons(self):
            self.button_newcell.setEnabled(True)
            self.button_add_region.setEnabled(True)
            self.button_drawmouse.setEnabled(True)
            self.button_eraser.setEnabled(True)
            self.button_exval.setEnabled(True)
            self.button_changecellvalue.setEnabled(True)
            self.button_showval.setEnabled(True)
    
    lpbsscientist's avatar
    lpbsscientist committed
        def DisableCorrectionsButtons(self):
            self.button_newcell.setEnabled(False)
            self.button_add_region.setEnabled(False)
            self.button_drawmouse.setEnabled(False)
            self.button_eraser.setEnabled(False)
            self.button_exval.setEnabled(False)
            self.button_changecellvalue.setEnabled(False)
            self.button_showval.setEnabled(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
        def ButtonSaveMask(self):
            """
            When this function is called, it saves the current mask
            (self.m.plotmask)
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.reader.SaveMask(self.Tindex, self.FOVindex, self.m.plotmask)
    
    lpbsscientist's avatar
    lpbsscientist committed
    
    class PlotCanvas(FigureCanvas):
    
        def __init__(self, parent=None):
            """this class defines the canvas. It initializes a figure, which is then
            used to plot our data using imshow.
    
    lpbsscientist's avatar
    lpbsscientist committed
            """
    
    lpbsscientist's avatar
    lpbsscientist committed
    
    #       define three subplots corresponding to the previous, current and next
    #       time index.
            fig, (self.ax2, self.ax, self.ax3) = plt.subplots(1,3, sharex = True, sharey = True)
    
            FigureCanvas.__init__(self, fig)
            self.setParent(parent)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #       this is some mambo jambo.
            FigureCanvas.setSizePolicy(self,
                    QSizePolicy.Expanding,
                    QSizePolicy.Expanding)
            FigureCanvas.updateGeometry(self)
    
            
    #       the self.currpicture attribute takes the original data and will then 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #       contain the updates drawn by the user.
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.currpicture = parent.currentframe
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.prevpicture = parent.previousframe
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.nextpicture = parent.nextframe
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.plotmask = parent.mask_curr
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.prevplotmask = parent.mask_previous
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.nextplotmask = parent.mask_next
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.tempmask = self.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.tempplotmask = self.plotmask.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.ThresholdMask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
            self.SegmentedMask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        this line is just here to not attribute a zero value to the plot
    #        because if so, then it does not update the plot and it stays blank.
    #        (it is unclear why..if someone finds a better solution)
            self.prevpicture = self.currpicture.copy()
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.currplot, self.currmask = self.plot(self.currpicture, self.plotmask, self.ax)
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.previousplot, self.previousmask = self.plot(self.prevpicture, self.prevplotmask, self.ax2)
            self.prevpicture = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype = np.uint16)
            self.prevplotmask = np.zeros([parent.reader.sizey, parent.reader.sizex], dtype =np.uint16)
    
    #        print('set visible')
    #        self.ax2.set_visible(False)
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.nextplot, self.nextmask = self.plot(self.nextpicture, self.nextplotmask, self.ax3)
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.previousplot.set_data(self.prevpicture)
            self.previousmask.set_data((self.prevplotmask%10+1)*(self.prevplotmask != 0))
    
            self.ax2.draw_artist(self.previousplot)
            self.ax2.draw_artist(self.previousmask)
            self.update()
            self.flush_events()
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.titlecurr = self.ax.set_title('Time index {}'.format(parent.Tindex))
            self.titleprev = self.ax2.set_title('No frame {}'.format(''))
            self.titlenext = self.ax3.set_title('Next Time index {}'.format(parent.Tindex+1))
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        these variables are just set to test the states of the buttons
    
    #        (button turned on or  off, etc..) of the buttons in the methods 
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        used in this class.
            self.button_showval_check = parent.button_showval
            self.button_newcell_check = parent.button_newcell
            self.button_add_region_check = parent.button_add_region
            self.button_drawmouse_check = parent.button_drawmouse
            self.button_eraser_check = parent.button_eraser
            self.button_hidemask_check = parent.button_hidemask
    
    lpbsscientist's avatar
    lpbsscientist committed
    #       It will plot for the first time and return the imshow function
    
    lpbsscientist's avatar
    lpbsscientist committed
            self.currmask.set_clim(0, 10)
            self.previousmask.set_clim(0,10)
            self.nextmask.set_clim(0,10)
    
    lpbsscientist's avatar
    lpbsscientist committed
    #       This attribute is a list which stores all the clicks of the mouse.
            self.storemouseclicks = []
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        This attribute is used to store the square where the mouse has been
    #        in order than to draw lines (Paintbrush function)
            self.storebrushclicks = [[False,False]]
    
    lpbsscientist's avatar
    lpbsscientist committed
    #        self.cellval is the variable which sets the value to the pixel