44import os
55import magicgui
66import numpy as np
7+ import zarr
78from magicgui .widgets import Container , Label , LineEdit , SpinBox , ComboBox
89from magicgui .application import use_app
910from PyQt5 .QtWidgets import QFileDialog , QMessageBox
1011
11- from ..util import load_image_data
12+ from ..util import load_image_data , get_model_names
1213from .annotator_2d import annotator_2d
1314from .annotator_3d import annotator_3d
1415from .image_series_annotator import image_folder_annotator
@@ -37,6 +38,19 @@ def file_is_hirarchical(path_s):
3738 return os .path .splitext (path_s )[1 ] in [".hdf5" , ".h5" , "n5" , ".zarr" ]
3839
3940
41+ def _set_embeddings_file_attributes (embeddings_path : str , cb_model , re_tile_x , re_tile_y ):
42+ f = zarr .open (embeddings_path , "a" )
43+ if "tile_shape" in f .attrs :
44+ if f .attrs ["tile_shape" ] is None :
45+ re_tile_x .value = 0
46+ re_tile_y .value = 0
47+ else :
48+ re_tile_x .value = f .attrs ["tile_shape" ][0 ]
49+ re_tile_y .value = f .attrs ["tile_shape" ][1 ]
50+ if "model_type" in f .attrs :
51+ cb_model .value = f .attrs ["model_type" ]
52+
53+
4054@magicgui .magicgui (call_button = "2d annotator" , labels = False )
4155def _on_2d ():
4256 global config_dict
@@ -46,6 +60,11 @@ def _on_2d():
4660
4761 le_file_key_img = LineEdit (value = "*" , label = "File key input" )
4862 le_file_key_segm = LineEdit (value = "*" , label = "File key segmentation" )
63+ re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
64+ re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
65+ re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
66+ re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
67+ cb_model = ComboBox (value = "vit_h_lm" , choices = get_model_names (), label = "Model Type" )
4968
5069 @magicgui .magicgui (call_button = "Select image" , labels = False )
5170 def on_select_image ():
@@ -73,6 +92,7 @@ def on_select_embed():
7392 show_error ("Precompute embeddings file does not exist or has wrong file extension." )
7493 return
7594 args ["embedding_path" ] = path
95+ _set_embeddings_file_attributes (path , cb_model , re_tile_x , re_tile_y )
7696 except Exception as e :
7797 show_error (str (e ))
7898
@@ -93,12 +113,6 @@ def on_select_segm():
93113 pb_embed_sel = Container (widgets = [on_select_embed ], layout = "horizontal" , labels = False )
94114 pb_seg_segm = Container (widgets = [on_select_segm ], layout = "horizontal" , labels = False )
95115
96- re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
97- re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
98- re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
99- re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
100- cb_model = ComboBox (value = "vit_h" , choices = ["vit_h" , "vit_l" , "vit_b" ], label = "Model Type" )
101-
102116 @magicgui .magicgui (call_button = "2d annotator" , labels = False )
103117 def on_start ():
104118 try :
@@ -125,6 +139,7 @@ def on_start():
125139 sub_widget = Container (widgets = [Container (widgets = [on_start ], layout = "horizontal" , labels = False ),
126140 pb_img_sel , lbl_opt , pb_embed_sel , pb_seg_segm , le_file_key_img , le_file_key_segm ,
127141 cb_model , re_tile_x , re_tile_y , re_halo_x , re_halo_y ])
142+ sub_widget .root_native_widget .setWindowTitle ("Segment Anything for Microscopy" )
128143 main_widget .close ()
129144 sub_widget .show ()
130145
@@ -137,6 +152,12 @@ def _on_3d():
137152 le_file_key_img = LineEdit (value = "*" , label = "File key input" )
138153 le_file_key_segm = LineEdit (value = "*" , label = "File key segmentation" )
139154
155+ re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
156+ re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
157+ re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
158+ re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
159+ cb_model = ComboBox (value = "vit_h_lm" , choices = get_model_names (), label = "Model Type" )
160+
140161 @magicgui .magicgui (call_button = "Select images" , labels = False )
141162 def on_select_image ():
142163 try :
@@ -175,6 +196,7 @@ def on_select_embed():
175196 show_error ("Precompute embeddings file does not exist or has wrong file extension." )
176197 return
177198 args ["embedding_path" ] = path
199+ _set_embeddings_file_attributes (path , cb_model , re_tile_x , re_tile_y )
178200 except Exception as e :
179201 show_error (str (e ))
180202
@@ -208,12 +230,6 @@ def on_select_segm_dir():
208230 pb_embed_sel = Container (widgets = [on_select_embed ], layout = "horizontal" , labels = False )
209231 pb_seg_segm = Container (widgets = [on_select_segm , on_select_segm_dir ], layout = "horizontal" , labels = False )
210232
211- re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
212- re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
213- re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
214- re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
215- cb_model = ComboBox (value = "vit_h" , choices = ["vit_h" , "vit_l" , "vit_b" ], label = "Model Type" )
216-
217233 @magicgui .magicgui (call_button = "3d annotator" , labels = False )
218234 def on_start ():
219235 try :
@@ -240,6 +256,7 @@ def on_start():
240256 sub_widget = Container (widgets = [Container (widgets = [on_start ], layout = "horizontal" , labels = False ),
241257 pb_img_sel , lbl_opt , pb_embed_sel , pb_seg_segm , le_file_key_img , le_file_key_segm ,
242258 cb_model , re_tile_x , re_tile_y , re_halo_x , re_halo_y ])
259+ sub_widget .root_native_widget .setWindowTitle ("Segment Anything for Microscopy" )
243260 main_widget .close ()
244261 sub_widget .show ()
245262
@@ -250,6 +267,12 @@ def _on_series():
250267 config_dict ["args" ] = {}
251268 args = config_dict ["args" ]
252269
270+ re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
271+ re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
272+ re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
273+ re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
274+ cb_model = ComboBox (value = "vit_h_lm" , choices = get_model_names (), label = "Model Type" )
275+
253276 @magicgui .magicgui (call_button = "Select input directory" , labels = False )
254277 def on_select_input_dir ():
255278 try :
@@ -288,19 +311,14 @@ def on_select_embed():
288311 show_error ("Precompute embeddings file does not exist or has wrong file extension." )
289312 return
290313 args ["embedding_path" ] = path
314+ _set_embeddings_file_attributes (path , cb_model , re_tile_x , re_tile_y )
291315 except Exception as e :
292316 show_error (str (e ))
293317
294318 pb_input_sel = Container (widgets = [on_select_input_dir ], layout = "horizontal" , labels = False )
295319 pb_output_sel = Container (widgets = [on_select_output_dir ], layout = "horizontal" , labels = False )
296320 pb_embed_sel = Container (widgets = [on_select_embed ], layout = "horizontal" , labels = False )
297321
298- re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
299- re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
300- re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
301- re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
302- cb_model = ComboBox (value = "vit_h" , choices = ["vit_h" , "vit_l" , "vit_b" ], label = "Model Type" )
303-
304322 @magicgui .magicgui (call_button = "Image series annotator" , labels = False )
305323 def on_start ():
306324 try :
@@ -329,6 +347,7 @@ def on_start():
329347 sub_widget = Container (widgets = [Container (widgets = [on_start ], layout = "horizontal" , labels = False ),
330348 pb_input_sel , pb_output_sel , lbl_opt , pb_embed_sel , cb_model , re_tile_x ,
331349 re_tile_y , re_halo_x , re_halo_y ])
350+ sub_widget .root_native_widget .setWindowTitle ("Segment Anything for Microscopy" )
332351 main_widget .close ()
333352 sub_widget .show ()
334353
@@ -341,6 +360,12 @@ def _on_tracking():
341360 le_file_key_img = LineEdit (value = "*" , label = "File key input" )
342361 le_file_key_segm = LineEdit (value = "*" , label = "File key segmentation" )
343362
363+ re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
364+ re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
365+ re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
366+ re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
367+ cb_model = ComboBox (value = "vit_h_lm" , choices = get_model_names (), label = "Model Type" )
368+
344369 @magicgui .magicgui (call_button = "Select images" , labels = False )
345370 def on_select_image ():
346371 try :
@@ -379,6 +404,7 @@ def on_select_embed():
379404 show_error ("Precompute embeddings file does not exist or has wrong file extension." )
380405 return
381406 args ["embedding_path" ] = path
407+ _set_embeddings_file_attributes (path , cb_model , re_tile_x , re_tile_y )
382408 except Exception as e :
383409 show_error (str (e ))
384410
@@ -412,12 +438,6 @@ def on_select_result_dir():
412438 pb_embed_sel = Container (widgets = [on_select_embed ], layout = "horizontal" , labels = False )
413439 pb_seg_sel = Container (widgets = [on_select_results , on_select_result_dir ], layout = "horizontal" , labels = False )
414440
415- re_halo_x = SpinBox (value = 0 , max = 10000 , label = "Halo x" )
416- re_halo_y = SpinBox (value = 0 , max = 10000 , label = "Halo y" )
417- re_tile_x = SpinBox (value = 0 , max = 10000 , label = "Tile x" )
418- re_tile_y = SpinBox (value = 0 , max = 10000 , label = "Tile y" )
419- cb_model = ComboBox (value = "vit_h" , choices = ["vit_h" , "vit_l" , "vit_b" ], label = "Model Type" )
420-
421441 @magicgui .magicgui (call_button = "Tracking annotator" , labels = False )
422442 def on_start ():
423443 try :
@@ -444,6 +464,7 @@ def on_start():
444464 sub_widget = Container (widgets = [Container (widgets = [on_start ], layout = "horizontal" , labels = False ),
445465 pb_img_sel , lbl_opt , pb_embed_sel , pb_seg_sel , le_file_key_img , le_file_key_segm ,
446466 cb_model , re_tile_x , re_tile_y , re_halo_x , re_halo_y ])
467+ sub_widget .root_native_widget .setWindowTitle ("Segment Anything for Microscopy" )
447468 main_widget .close ()
448469 sub_widget .show ()
449470
@@ -460,6 +481,7 @@ def annotator():
460481 sub_container2 = Container (widgets = [_on_3d , _on_tracking ], labels = False )
461482 sub_container3 = Container (widgets = [sub_container1 , sub_container2 ], layout = "horizontal" , labels = False )
462483 main_widget = Container (widgets = [Label (value = "Segment Anything for Microscopy" ), sub_container3 ], labels = False )
484+ main_widget .root_native_widget .setWindowTitle ("Segment Anything for Microscopy" )
463485 main_widget .show (run = True )
464486
465487 if config_dict ["workflow" ] == "2d" :
0 commit comments