@@ -273,6 +273,9 @@ def __init__(self, image=None, logger=None):
273273 self .ratio = 1.
274274 self .reset ()
275275
276+ # This needs to go after .reset() is called to get state fully set up:
277+ self .autobtn .checkStateChanged .connect (self .compute_saturation_if_checked )
278+
276279 self .load_3D = False
277280
278281 # if called with image, load it
@@ -522,8 +525,11 @@ def make_buttons(self):
522525 self .additional_seg_settings_qcollapsible .setContent (self .segmentation_settings )
523526 self .segBoxG .addWidget (self .additional_seg_settings_qcollapsible , widget_row , 0 , 1 , 9 )
524527
525- # connect edits to the diameter box to resizing the image :
528+ # connect edits to image processing steps :
526529 self .segmentation_settings .diameter_box .editingFinished .connect (self .update_scale )
530+ self .segmentation_settings .flow_threshold_box .returnPressed .connect (self .compute_cprob )
531+ self .segmentation_settings .cellprob_threshold_box .returnPressed .connect (self .compute_cprob )
532+ self .segmentation_settings .niter_box .returnPressed .connect (self .compute_cprob )
527533
528534 # Needed to do this for the drop down to not be open on startup
529535 self .additional_seg_settings_qcollapsible ._toggle_btn .setChecked (True )
@@ -951,12 +957,15 @@ def reset(self):
951957 self .opacity = 128 # how opaque masks should be
952958 self .outcolor = [200 , 200 , 255 , 200 ]
953959 self .NZ , self .Ly , self .Lx = 1 , 256 , 256
954- self .saturation = []
955- for r in range (3 ):
956- self .saturation .append ([[0 , 255 ] for n in range (self .NZ )])
957- self .sliders [r ].setValue ([0 , 255 ])
958- self .sliders [r ].setEnabled (False )
959- self .sliders [r ].show ()
960+ self .saturation = self .saturation if hasattr (self , 'saturation' ) else []
961+
962+ # only adjust the saturation if auto-adjust is on:
963+ if self .autobtn .isChecked ():
964+ for r in range (3 ):
965+ self .saturation .append ([[0 , 255 ] for n in range (self .NZ )])
966+ self .sliders [r ].setValue ([0 , 255 ])
967+ self .sliders [r ].setEnabled (False )
968+ self .sliders [r ].show ()
960969 self .currentZ = 0
961970 self .flows = [[], [], [], [], [[]]]
962971 # masks matrix
@@ -1655,6 +1664,10 @@ def get_normalize_params(self):
16551664 normalize_params = {** normalize_default , ** normalize_params }
16561665
16571666 return normalize_params
1667+
1668+ def compute_saturation_if_checked (self ):
1669+ if self .autobtn .isChecked ():
1670+ self .compute_saturation ()
16581671
16591672 def compute_saturation (self , return_img = False ):
16601673 norm = self .get_normalize_params ()
@@ -1704,42 +1717,43 @@ def compute_saturation(self, return_img=False):
17041717 else :
17051718 img_norm = self .stack if self .restore is None or self .restore == "filter" else self .stack_filtered
17061719
1707- self .saturation = []
1708- for c in range (img_norm .shape [- 1 ]):
1709- self .saturation .append ([])
1710- if np .ptp (img_norm [..., c ]) > 1e-3 :
1711- if norm3D :
1712- x01 = np .percentile (img_norm [..., c ], percentile [0 ])
1713- x99 = np .percentile (img_norm [..., c ], percentile [1 ])
1714- if invert :
1715- x01i = 255. - x99
1716- x99i = 255. - x01
1717- x01 , x99 = x01i , x99i
1718- for n in range (self .NZ ):
1719- self .saturation [- 1 ].append ([x01 , x99 ])
1720- else :
1721- for z in range (self .NZ ):
1722- if self .NZ > 1 :
1723- x01 = np .percentile (img_norm [z , :, :, c ], percentile [0 ])
1724- x99 = np .percentile (img_norm [z , :, :, c ], percentile [1 ])
1725- else :
1726- x01 = np .percentile (img_norm [..., c ], percentile [0 ])
1727- x99 = np .percentile (img_norm [..., c ], percentile [1 ])
1720+ if self .autobtn .isChecked ():
1721+ self .saturation = []
1722+ for c in range (img_norm .shape [- 1 ]):
1723+ self .saturation .append ([])
1724+ if np .ptp (img_norm [..., c ]) > 1e-3 :
1725+ if norm3D :
1726+ x01 = np .percentile (img_norm [..., c ], percentile [0 ])
1727+ x99 = np .percentile (img_norm [..., c ], percentile [1 ])
17281728 if invert :
17291729 x01i = 255. - x99
17301730 x99i = 255. - x01
17311731 x01 , x99 = x01i , x99i
1732- self .saturation [- 1 ].append ([x01 , x99 ])
1733- else :
1734- for n in range (self .NZ ):
1735- self .saturation [- 1 ].append ([0 , 255. ])
1736- print (self .saturation [2 ][self .currentZ ])
1732+ for n in range (self .NZ ):
1733+ self .saturation [- 1 ].append ([x01 , x99 ])
1734+ else :
1735+ for z in range (self .NZ ):
1736+ if self .NZ > 1 :
1737+ x01 = np .percentile (img_norm [z , :, :, c ], percentile [0 ])
1738+ x99 = np .percentile (img_norm [z , :, :, c ], percentile [1 ])
1739+ else :
1740+ x01 = np .percentile (img_norm [..., c ], percentile [0 ])
1741+ x99 = np .percentile (img_norm [..., c ], percentile [1 ])
1742+ if invert :
1743+ x01i = 255. - x99
1744+ x99i = 255. - x01
1745+ x01 , x99 = x01i , x99i
1746+ self .saturation [- 1 ].append ([x01 , x99 ])
1747+ else :
1748+ for n in range (self .NZ ):
1749+ self .saturation [- 1 ].append ([0 , 255. ])
1750+ print (self .saturation [2 ][self .currentZ ])
17371751
1738- if img_norm .shape [- 1 ] == 1 :
1739- self .saturation .append (self .saturation [0 ])
1740- self .saturation .append (self .saturation [0 ])
1752+ if img_norm .shape [- 1 ] == 1 :
1753+ self .saturation .append (self .saturation [0 ])
1754+ self .saturation .append (self .saturation [0 ])
17411755
1742- self .autobtn .setChecked (True )
1756+ # self.autobtn.setChecked(True)
17431757 self .update_plot ()
17441758
17451759
@@ -1838,14 +1852,31 @@ def compute_cprob(self):
18381852 if self .recompute_masks :
18391853 flow_threshold = self .segmentation_settings .flow_threshold
18401854 cellprob_threshold = self .segmentation_settings .cellprob_threshold
1855+ niter = self .segmentation_settings .niter
1856+ min_size = int (self .min_size .text ()) if not isinstance (
1857+ self .min_size , int ) else self .min_size
1858+
18411859 self .logger .info (
18421860 "computing masks with cell prob=%0.3f, flow error threshold=%0.3f" %
18431861 (cellprob_threshold , flow_threshold ))
1862+
1863+ try :
1864+ dP = self .flows [2 ].squeeze ()
1865+ cellprob = self .flows [3 ].squeeze ()
1866+ except IndexError :
1867+ self .logger .error ("Flows don't exist, try running model again." )
1868+ return
1869+
18441870 maski = dynamics .resize_and_compute_masks (
1845- self .flows [4 ][:- 1 ], self .flows [4 ][- 1 ], p = self .flows [3 ].copy (),
1846- cellprob_threshold = cellprob_threshold , flow_threshold = flow_threshold ,
1847- resize = self .cellpix .shape [- 2 :])[0 ]
1848-
1871+ dP = dP ,
1872+ cellprob = cellprob ,
1873+ niter = niter ,
1874+ do_3D = self .load_3D ,
1875+ min_size = min_size ,
1876+ # max_size_fraction=min_size_fraction, # Leave as default
1877+ cellprob_threshold = cellprob_threshold ,
1878+ flow_threshold = flow_threshold )
1879+
18491880 self .masksOn = True
18501881 if not self .OCheckBox .isChecked ():
18511882 self .MCheckBox .setChecked (True )
@@ -1911,6 +1942,9 @@ def compute_segmentation(self, custom=False, model_name=None, load_model=True):
19111942 flows_new .append (flows [0 ].copy ()) # RGB flow
19121943 flows_new .append ((np .clip (normalize99 (flows [2 ].copy ()), 0 , 1 ) *
19131944 255 ).astype ("uint8" )) # cellprob
1945+ flows_new .append (flows [1 ].copy ()) # XY flows
1946+ flows_new .append (flows [2 ].copy ()) # original cellprob
1947+
19141948 if self .load_3D :
19151949 if stitch_threshold == 0. :
19161950 flows_new .append ((flows [1 ][0 ] / 10 * 127 + 127 ).astype ("uint8" ))
@@ -1963,7 +1997,7 @@ def compute_segmentation(self, custom=False, model_name=None, load_model=True):
19631997 self .masksOn = True
19641998 self .MCheckBox .setChecked (True )
19651999 self .progress .setValue (100 )
1966- if self .restore != "filter" and self .restore is not None :
2000+ if self .restore != "filter" and self .restore is not None and self . autobtn . isChecked () :
19672001 self .compute_saturation ()
19682002 if not do_3D and not stitch_threshold > 0 :
19692003 self .recompute_masks = True
0 commit comments