From a51ca33e26389c822eeea73a6c57dda889943905 Mon Sep 17 00:00:00 2001
From: lpbsscientist <lpbsscientist@gmail.com>
Date: Fri, 13 Aug 2021 23:23:08 +0200
Subject: [PATCH] SJR: latest

---
 GUI_main.py            |  7 ++++++-
 init/InitButtons.py    |  2 ++
 unet/neural_network.py | 12 ++++++++++--
 unet/segment.py        | 26 ++++++++++++++++++++++----
 4 files changed, 40 insertions(+), 7 deletions(-)

diff --git a/GUI_main.py b/GUI_main.py
index 1dbc1f8..a571ac1 100644
--- a/GUI_main.py
+++ b/GUI_main.py
@@ -1257,6 +1257,10 @@ class App(QMainWindow):
         if do_draw or do_erase:
             self.m.tempmask = self.m.plotmask.copy()
             
+            #save status of the showval check box, then turn off (slows brush/eraser down too much)
+            self.showval_was_checked = self.button_showval.isChecked()
+            self.button_showval.setChecked(False)
+            
             if do_draw:
                 self.WriteStatusBar(('Draw using the brush, right click to select '
                                      'the cell to draw.'))
@@ -1271,7 +1275,7 @@ class App(QMainWindow):
             self.id2 = self.m.mpl_connect('button_press_event', 
                                           lambda e: self.m.OneClick(e, radius))
             self.id = self.m.mpl_connect('motion_notify_event', 
-                                         lambda e: self.m.PaintBrush(e, radius))
+                                          lambda e: self.m.PaintBrush(e, radius))
             self.id3 = self.m.mpl_connect('button_release_event', self.m.ReleaseClick)
                 
                         
@@ -1282,6 +1286,7 @@ class App(QMainWindow):
             QApplication.restoreOverrideCursor()
             self.Enable(self.button_drawmouse)
             self.Enable(self.button_eraser)
+            self.button_showval.setChecked(self.showval_was_checked)
             self.SaveMask()
             self.ClearStatusBar()
             
diff --git a/init/InitButtons.py b/init/InitButtons.py
index 3dff210..60ab866 100644
--- a/init/InitButtons.py
+++ b/init/InitButtons.py
@@ -158,6 +158,8 @@ def Init(parent):
     parent.button_changecellvalue.setEnabled(True)
     parent.button_changecellvalue.clicked.connect(parent.ChangeOneValue)
     parent.button_changecellvalue.setMaximumWidth(150)
+    parent.button_changecellvalue.setShortcut("C")
+    parent.button_changecellvalue.setToolTip("Shortcut: C")
 #    parent.button_changecellvalue.setStatusTip('')
     parent.button_changecellvalue.setStatusTip('Change ID value of one cell. Use left click to select one cell and enter a new ID value.')
 
diff --git a/unet/neural_network.py b/unet/neural_network.py
index c3e66a6..543af68 100644
--- a/unet/neural_network.py
+++ b/unet/neural_network.py
@@ -65,9 +65,17 @@ def prediction(im, is_pc):
                  input_size = (None,None,1))
 
     if is_pc:
-        path = path_weights + 'unet_weights_batchsize_25_Nepochs_100_SJR0_10.hdf5'
+        path = '/home/sjrahi/Research/YeaZ/unet_weights/2021_05_27_budding_BF_all/files-p-G/unet_weights_budding_BF_multilab-p-G_SJR_0_1_batchsize_10_Nepochs_250.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_pombe_Silke_all_SJR_0_1_batchsize_5_Nepochs_500.hdf5'
+        # path_weights + 'unet_weights_batchsize_25_Nepochs_100_SJR0_10.hdf5'
     else:
-        path = path_weights + 'weights_budding_BF_02.hdf5'
+        path = '/home/sjrahi/Research/YeaZ/unet_weights/2021_05_09_pombe_all/unet_weights_pombe_all_SJR_0_2_batchsize_8_Nepochs_400.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_BF_batchsize_15_Nepochs_400_SJR_0_1.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_BF_batchsize_25_Nepochs_200_SJR_0_1.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_epoch_600_batchsize_50_fold_1_4.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_epoch_2000_batchsize_50_fold_1.hdf5'
+        # '/home/sjrahi/Research/YeaZ/unet_weights/2021_04_15_Silke_pombe/unet_weights_epoch_600_batchsize_50_fold_1_4.hdf5'
+        # path_weights + 'weights_budding_BF_02.hdf5'
     
     if not os.path.exists(path):
         raise ValueError('Path does not exist')
diff --git a/unet/segment.py b/unet/segment.py
index 2a7822e..c333fb9 100644
--- a/unet/segment.py
+++ b/unet/segment.py
@@ -94,10 +94,12 @@ def cell_merge(wsh, pred):
         
         objcounter = objcounter + 1
         orig1 = objs[obj1,:,:]
+        
+        last_obj2_added_to_obj1 = -1
 
         for obj2 in range(obj1+1,wsh.max()):
             dil2 = dil_objs[obj2,:,:]
-        
+            
             # only check border if bounding box overlaps, and second mask 
             # is not yet deleted
             if (do_box_overlap(obj_coords[obj1,:], obj_coords[obj2,:])
@@ -107,7 +109,7 @@ def cell_merge(wsh, pred):
                 border_pred = pred[border]
                 
                 # Border is too small to be considered
-                if len(border_pred) < 32:
+                if len(border_pred) < 16:	#SJR: Changed on 18.04.2021 from previously 32. Not sure why 32. I remember 8.
                     continue
                 
                 # Sum of top 25% of predicted border values
@@ -119,11 +121,27 @@ def cell_merge(wsh, pred):
                 # merge cells
                 if top_border_height / top_border_area > .99:
                     orig1 = np.logical_or(orig1, objs[obj2,:,:])
-                    dil_objs[obj1,:,:] = np.logical_or(dil1, dil2)
+                    dil1 = np.logical_or(dil1, dil2)
+                    dil_objs[obj1,:,:] = dil1
                     dil_objs[obj2,:,:] = np.zeros((wshshape[0], wshshape[1]))
                     obj_coords[obj1,:] = get_bounding_box(dil_objs[obj1,:,:])
+#                    obj_coords[obj2,:] = get_bounding_box(dil_objs[obj2,:,:])
+                    last_obj2_added_to_obj1 = obj2
+        
+        # the last object that obj1 was merged with should be equal to obj1 so that additional cells could be merged with it
+        if last_obj2_added_to_obj1 > -1:
+            obj2 = last_obj2_added_to_obj1
+            dil_objs[obj2,:,:] = dil1
+            objs[obj2,:,:] = orig1
+            obj_coords[obj2,:] = get_bounding_box(dil_objs[obj2,:,:])
+            
                     
-        wshclean = wshclean + orig1*objcounter
+        wshclean = (1-orig1)*wshclean + orig1*objcounter
+        
+    # resort wshclean
+    u = np.unique(wshclean)[1:]	#ignore background
+    for obj1 in range(len(u)):
+        wshclean[wshclean==u[obj1]] = obj1 + 1
             
     return wshclean
 
-- 
GitLab