From 7d3fb94059f95430b0d893ef184483f527c1fe1b Mon Sep 17 00:00:00 2001
From: Farzaneh Labbaf <f.labbaf97@gmail.com>
Date: Thu, 23 Mar 2023 18:24:00 +0100
Subject: [PATCH] Change retrack button to retrack multiple frame with one
 click

---
 GUI_main.py          | 51 +++++++++++++++++++++++++++++++++++++++++++-
 init/InitButtons.py  |  6 +++---
 misc/BatchRetrack.py | 31 +++++++++++++++++++++++++++
 3 files changed, 84 insertions(+), 4 deletions(-)
 create mode 100644 misc/BatchRetrack.py

diff --git a/GUI_main.py b/GUI_main.py
index fc86fcf..2990a97 100644
--- a/GUI_main.py
+++ b/GUI_main.py
@@ -103,6 +103,9 @@ import ChangeOneCellValue as cocv
 #a specific range of pictures.
 import LaunchBatchPrediction as lbp
 
+#this file contains a dialog window where a time range can be selected to retrack
+import BatchRetrack as br
+
 #this file initializes all the buttons present in the gui, sets the shortcuts
 #to these buttons and also connect the buttons to the function that are 
 #triggered when the buttons are pressed.
@@ -968,7 +971,53 @@ class App(QMainWindow):
             self.button_timeindex.clearFocus()
             return
         
-     
+    def BatchCellCorresp(self):
+        def reset():
+            self.ClearStatusBar()
+            self.Enable(self.button_cellcorrespondence)
+            self.button_cellcorrespondence.setChecked(False)
+        
+        self.Disable(self.button_cellcorrespondence)
+        self.WriteStatusBar('retracking... ')
+
+        if(self.Tindex==self.reader.sizet-1):
+            QMessageBox.critical(self, "This is the last frame. Nothing to retrack after this")
+            reset()
+            return 
+
+
+        # creates a dialog window from the BatchRetrack.py file
+        dlg = br.CustomDialog(self)
+        
+        # this if tests if the user pressed 'ok' in the dialog window
+        if dlg.exec_() == QDialog.Accepted:
+            # it tests if the user has entered some values
+            if not (dlg.entry1.text()!= ''):
+                QMessageBox.critical(self, "Error", "No Time Specified")
+                reset()
+                return 
+            
+            # reads out the entry given by the user and converts the index
+            # to integers
+            time_value1 = int(dlg.entry1.text())
+    
+            # it tests if the value is bigger than current frame
+            if (time_value1 <  self.Tindex or time_value1 > self.reader.sizet-1):
+                QMessageBox.critical(self, "Error", 'Invalid Time Constraints')
+                reset()
+                return
+
+            for t in range(self.Tindex+1, time_value1+1):
+                log.debug('start correspondance for frame'.format(t))                    
+                #calls the cell correspondance for current time, t, and t+1
+                temp_mask = self.reader.CellCorrespondence(t, self.FOVindex)               
+                self.reader.SaveMask(t, self.FOVindex, temp_mask)
+                log.debug('finish correspondance and savemask for frame'.format(t))                    
+            
+            self.ReloadThreeMasks()
+            log.info('reload three frames')
+        reset()
+
     def CellCorrespActivation(self):
         self.Disable(self.button_cellcorrespondence)
         self.WriteStatusBar('Doing the cell correspondence')
diff --git a/init/InitButtons.py b/init/InitButtons.py
index 60ab866..aed08a8 100644
--- a/init/InitButtons.py
+++ b/init/InitButtons.py
@@ -186,10 +186,10 @@ def Init(parent):
     # MAKE THE CELL Correspondence
     parent.button_cellcorrespondence.setEnabled(False)
     parent.button_cellcorrespondence.setCheckable(True)
-    parent.button_cellcorrespondence.clicked.connect(parent.CellCorrespActivation)
+    parent.button_cellcorrespondence.clicked.connect(parent.BatchCellCorresp)
     parent.button_cellcorrespondence.setMaximumWidth(150)
-    parent.button_cellcorrespondence.setStatusTip('Retrack cell ID numbers based on the previous frame.')
-    
+    parent.button_cellcorrespondence.setStatusTip('Retrack cell ID numbers for next frame(s) or selected next frames based on this frame.')
+        
     # EXTRACT FLUORESCENCE IN DIFFERENT CHANNELS    
     parent.button_extractfluorescence.setEnabled(False)
     parent.button_extractfluorescence.toggle()
diff --git a/misc/BatchRetrack.py b/misc/BatchRetrack.py
new file mode 100644
index 0000000..a52c810
--- /dev/null
+++ b/misc/BatchRetrack.py
@@ -0,0 +1,31 @@
+from PyQt5.QtWidgets import QApplication, QMainWindow, QMenu, QVBoxLayout, QSizePolicy, QMessageBox, QWidget, QPushButton, QShortcut, QComboBox, QDialog, QDialogButtonBox, QInputDialog, QLineEdit, QFormLayout, QLabel
+from PyQt5 import QtGui
+#from PyQt5.QtGui import QIcon, QKeySequence
+from PyQt5.QtCore import pyqtSignal, QObject, Qt
+#import PyQt package, allows for GUI interactions
+
+class CustomDialog(QDialog):
+
+    def __init__(self, *args, **kwargs):
+        super(CustomDialog, self).__init__(*args, **kwargs)
+        app, = args
+        self.setWindowTitle("Retrack")
+        self.setGeometry(100,100, 500,200)
+        
+        self.entry1 = QLineEdit()
+        self.entry1.setValidator(QtGui.QIntValidator())
+        self.entry1.setMaxLength(4)
+        self.entry1.setAlignment(Qt.AlignRight)
+        self.labeltime = QLabel("Enter farme number between {} to {}".format(app.Tindex+1, app.reader.sizet-1))
+        flo = QFormLayout()
+        flo.addWidget(self.labeltime)
+        flo.addRow('retracking frames from {} (next frame) to '.format(app.Tindex+1), self.entry1)       
+        
+        QBtn = QDialogButtonBox.Ok | QDialogButtonBox.Cancel
+        
+        self.buttonBox = QDialogButtonBox(QBtn)
+        self.buttonBox.accepted.connect(self.accept)
+        self.buttonBox.rejected.connect(self.reject)
+        flo.addWidget(self.buttonBox)
+        self.setLayout(flo)
+        
\ No newline at end of file
-- 
GitLab