AbigailMcGovern / iterseg

napari plugin for iteratively improving 3D instance segmentation of cells (u-net x watershed)
BSD 3-Clause "New" or "Revised" License
9 stars 1 forks source link

UnboundLocalError while running segmentation #24

Open raeesahhy opened 6 months ago

raeesahhy commented 6 months ago

Hi everyone,

I received the following error when I tried to run segment_data in Napari:

---------------------------------------------------------------------------
UnboundLocalError                         Traceback (most recent call last)
File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1196, in SignalInstance._run_emit_loop(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>, args=(False,))
   1195     with Signal._emitting(self):
-> 1196         self._run_emit_loop_inner()
        self = <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>
        self._run_emit_loop_inner = <bound method SignalInstance._run_emit_loop_immediate of <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>>
   1197 except RecursionError as e:

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1225, in SignalInstance._run_emit_loop_immediate(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>)
   1224 for caller in self._slots:
-> 1225     caller.cb(args)
        args = (False,)
        caller = <StrongFunction on magicgui.widgets._function_gui.FunctionGui.__init__.<locals>._disable_button_and_call>

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_weak_callback.py:357, in StrongFunction.cb(self=<StrongFunction on magicgui.widgets._function_gu...onGui.__init__.<locals>._disable_button_and_call>, args=())
    356     args = args[: self._max_args]
--> 357 self._f(*self._args, *args, **self._kwargs)
        args = ()
        self._f = <function FunctionGui.__init__.<locals>._disable_button_and_call at 0x318010400>
        self = <StrongFunction on magicgui.widgets._function_gui.FunctionGui.__init__.<locals>._disable_button_and_call>
        self._args = ()
        self._kwargs = {}

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/_function_gui.py:225, in FunctionGui.__init__.<locals>._disable_button_and_call()
    224 try:
--> 225     self.__call__()
        self = <FunctionGui segment_data(napari_viewer: napari.viewer.Viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer: napari.layers.image.image.Image = <Image layer 'image_time_0' at 0x300184050>, save_dir: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name: str = 'labels-prediction', segmenter: str = 'affinity-unet-watershed', network_or_config_file: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference: str = 'None', chunk_size: tuple = (10, 256, 256), margin: tuple = (1, 64, 64), debug: bool = True)>
    226 finally:

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/_function_gui.py:343, in FunctionGui.__call__(self=<FunctionGui segment_data(napari_viewer: napari....margin: tuple = (1, 64, 64), debug: bool = True)>, *args=(), **kwargs={})
    342 with _function_name_pointing_to_widget(self):
--> 343     value = self._function(*bound.args, **bound.kwargs)
        self = <FunctionGui segment_data(napari_viewer: napari.viewer.Viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer: napari.layers.image.image.Image = <Image layer 'image_time_0' at 0x300184050>, save_dir: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name: str = 'labels-prediction', segmenter: str = 'affinity-unet-watershed', network_or_config_file: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference: str = 'None', chunk_size: tuple = (10, 256, 256), margin: tuple = (1, 64, 64), debug: bool = True)>
        bound = <BoundArguments (napari_viewer=Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0' at 0x300184050>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', segmenter='affinity-unet-watershed', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)>
        self._function = <function segment_data at 0x31d38c040>
    345 self._call_count += 1

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/_dock_widgets.py:610, in segment_data(napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', segmenter='affinity-unet-watershed', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
    609 seg_func = segmenters[segmenter]
--> 610 seg_func(napari_viewer, input_volume_layer, save_dir, 
        seg_func = <function affinity_unet_watershed at 0x31d3062a0>
        napari_viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        save_dir = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact')
        name = 'labels-prediction'
        network_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
        chunk_size = (10, 256, 256)
        margin = (1, 64, 64)
        debug = True
    611          name, network_or_config_file, layer_reference, 
    612          chunk_size, margin, debug)

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:71, in affinity_unet_watershed(napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', unet_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
     35 '''
     36 Segment a 3D image or stack of 3D images using a affinity-unet-watershed
     37 algorithm. The image is passed to a U-Net, which outputs 5 feature maps:
   (...)
     69     This tells you how much overlap between chunks is used
     70 '''
---> 71 segmentation_wrapper(affinity_watershed_for_chunks, affinity_watershed_prep_config, 
        napari_viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        save_dir = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact')
        name = 'labels-prediction'
        unet_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
        chunk_size = (10, 256, 256)
        margin = (1, 64, 64)
        debug = True
     72                      napari_viewer, input_volume_layer, save_dir, name, unet_or_config_file,
     73                      layer_reference, chunk_size, margin, debug)

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:758, in segmentation_wrapper(processing_function=<function affinity_watershed_for_chunks>, config_prep_function=<function affinity_watershed_prep_config>, napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
    756 # get the config dict
    757 # -------------------
--> 758 config = config_prep_function(
        config_prep_function = <function affinity_watershed_prep_config at 0x31d34c860>
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        network_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
    759         input_volume_layer, network_or_config_file, layer_reference
    760         )
    761 if config is None:

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:104, in affinity_watershed_prep_config(input_volume_layer=<Image layer 'image_time_0'>, unet_or_config_file='/Users/raeesahhayatudin/Downloads/Training_Valid...sts_z_intact/242405_203956_unet_my-unet_meta.json', reference_layer='None')
    103     affinities_extent = 1
--> 104 if unet == 'labels layer':
    105     unet = reference_layer.metadata['unet']

UnboundLocalError: cannot access local variable 'unet' where it is not associated with a value

The above exception was the direct cause of the following exception:

EmitLoopError                             Traceback (most recent call last)
File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/bases/_value_widget.py:71, in ValueWidget._on_value_change(self=PushButton(value=False, annotation=None, name='call_button'), value=False)
     69 if value is self.null_value and not self._nullable:
     70     return
---> 71 self.changed.emit(value)
        value = False
        self.changed = <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>
        self = PushButton(value=False, annotation=None, name='call_button')

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1176, in SignalInstance.emit(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>, check_nargs=False, check_types=False, *args=(False,))
   1172     from ._group import EmissionInfo
   1174     SignalInstance._debug_hook(EmissionInfo(self, args))
-> 1176 self._run_emit_loop(args)
        self = <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>
        args = (False,)

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1213, in SignalInstance._run_emit_loop(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>, args=(False,))
   1205     loop_err = EmitLoopError(
   1206         exc=cb_err,
   1207         signal=self,
   (...)
   1210         emit_queue=self._emit_queue,
   1211     ).with_traceback(cb_err.__traceback__)
   1212     # this comment will show up in the traceback
-> 1213     raise loop_err from cb_err  # emit() call ABOVE || callback error BELOW
        loop_err = EmitLoopError("\n\nWhile emitting signal 'magicgui.widgets.PushButton.changed', a UnboundLocalError occurred in a callback:\n\n  Signal emitted at: /Users/raeesahhayatudin/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/bases/_value_widget.py:71, in _on_value_change\n    >  self.changed.emit(value)\n\n  Callback error at: /Users/raeesahhayatudin/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:104, in affinity_watershed_prep_config\n    >  if unet == 'labels layer':\n\n    Local variables:\n       input_volume_layer = <Image layer 'image_time_0' at 0x300184050>\n       unet_or_config_file = '/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyt...\n       reference_layer = 'None'\n       affinities_extent = 1\n       config = {'unet': '/Users/raeesahhayatudin/Downloads/Training_Validat...\n\nSee UnboundLocalError above for original traceback.")
   1214 finally:
   1215     self._recursion_depth -= 1

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1196, in SignalInstance._run_emit_loop(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>, args=(False,))
   1192     self._max_recursion_depth = max(
   1193         self._max_recursion_depth, self._recursion_depth
   1194     )
   1195     with Signal._emitting(self):
-> 1196         self._run_emit_loop_inner()
        self = <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>
        self._run_emit_loop_inner = <bound method SignalInstance._run_emit_loop_immediate of <SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>>
   1197 except RecursionError as e:
   1198     raise RecursionError(
   1199         f"RecursionError when "
   1200         f"emitting signal {self.name!r} with args {args}"
   1201     ) from e

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_signal.py:1225, in SignalInstance._run_emit_loop_immediate(self=<SignalInstance 'changed' on PushButton(value=False, annotation=None, name='call_button')>)
   1223 args = self._emit_queue.popleft()
   1224 for caller in self._slots:
-> 1225     caller.cb(args)
        args = (False,)
        caller = <StrongFunction on magicgui.widgets._function_gui.FunctionGui.__init__.<locals>._disable_button_and_call>

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/psygnal/_weak_callback.py:357, in StrongFunction.cb(self=<StrongFunction on magicgui.widgets._function_gu...onGui.__init__.<locals>._disable_button_and_call>, args=())
    355 if self._max_args is not None:
    356     args = args[: self._max_args]
--> 357 self._f(*self._args, *args, **self._kwargs)
        args = ()
        self._f = <function FunctionGui.__init__.<locals>._disable_button_and_call at 0x318010400>
        self = <StrongFunction on magicgui.widgets._function_gui.FunctionGui.__init__.<locals>._disable_button_and_call>
        self._args = ()
        self._kwargs = {}

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/_function_gui.py:225, in FunctionGui.__init__.<locals>._disable_button_and_call()
    223 self._call_button.enabled = False
    224 try:
--> 225     self.__call__()
        self = <FunctionGui segment_data(napari_viewer: napari.viewer.Viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer: napari.layers.image.image.Image = <Image layer 'image_time_0' at 0x300184050>, save_dir: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name: str = 'labels-prediction', segmenter: str = 'affinity-unet-watershed', network_or_config_file: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference: str = 'None', chunk_size: tuple = (10, 256, 256), margin: tuple = (1, 64, 64), debug: bool = True)>
    226 finally:
    227     self._call_button.enabled = True

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/_function_gui.py:343, in FunctionGui.__call__(self=<FunctionGui segment_data(napari_viewer: napari....margin: tuple = (1, 64, 64), debug: bool = True)>, *args=(), **kwargs={})
    341 self._tqdm_depth = 0  # reset the tqdm stack count
    342 with _function_name_pointing_to_widget(self):
--> 343     value = self._function(*bound.args, **bound.kwargs)
        self = <FunctionGui segment_data(napari_viewer: napari.viewer.Viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer: napari.layers.image.image.Image = <Image layer 'image_time_0' at 0x300184050>, save_dir: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name: str = 'labels-prediction', segmenter: str = 'affinity-unet-watershed', network_or_config_file: Optional[str] = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference: str = 'None', chunk_size: tuple = (10, 256, 256), margin: tuple = (1, 64, 64), debug: bool = True)>
        bound = <BoundArguments (napari_viewer=Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0' at 0x300184050>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', segmenter='affinity-unet-watershed', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)>
        self._function = <function segment_data at 0x31d38c040>
    345 self._call_count += 1
    346 if self._result_widget is not None:

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/_dock_widgets.py:610, in segment_data(napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', segmenter='affinity-unet-watershed', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
    556 '''
    557 Parameters
    558 ----------
   (...)
    607     is run in the same thread as the viewer so  errors will be caught propery.
    608 '''
    609 seg_func = segmenters[segmenter]
--> 610 seg_func(napari_viewer, input_volume_layer, save_dir, 
        seg_func = <function affinity_unet_watershed at 0x31d3062a0>
        napari_viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        save_dir = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact')
        name = 'labels-prediction'
        network_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
        chunk_size = (10, 256, 256)
        margin = (1, 64, 64)
        debug = True
    611          name, network_or_config_file, layer_reference, 
    612          chunk_size, margin, debug)

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:71, in affinity_unet_watershed(napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', unet_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
     24 def affinity_unet_watershed(
     25         napari_viewer, 
     26         input_volume_layer: napari.layers.Image,
   (...)
     33         debug: bool =False
     34     ):
     35     '''
     36     Segment a 3D image or stack of 3D images using a affinity-unet-watershed
     37     algorithm. The image is passed to a U-Net, which outputs 5 feature maps:
   (...)
     69         This tells you how much overlap between chunks is used
     70     '''
---> 71     segmentation_wrapper(affinity_watershed_for_chunks, affinity_watershed_prep_config, 
        napari_viewer = Viewer(camera=Camera(center=(0.0, 148.70936900611764, 371.0633091922413), zoom=0.8657173310508022, angles=(0.0, 0.0, 90.0), perspective=0.0, mouse_pan=True, mouse_zoom=True), cursor=Cursor(position=(7.0, 406.29921482764746, 557.6137876818664), scaled=True, size=1, style=<CursorStyle.STANDARD: 'standard'>), dims=Dims(ndim=3, ndisplay=2, last_used=0, range=((0.0, 33.0, 1.0), (0.0, 829.0, 1.0), (0.0, 771.0, 1.0)), current_step=(7, 414, 385), order=(0, 1, 2), axis_labels=('0', '1', '2')), grid=GridCanvas(stride=1, shape=(-1, -1), enabled=False), layers=[<Image layer 'image_time_0' at 0x300184050>, <Labels layer 'Labels' at 0x3028fb440>, <Shapes layer 'Shapes' at 0x31396cdd0>, <Image layer 'image_time_0_gt-from-ROI_img' at 0x3179017c0>, <Labels layer 'image_time_0_gt-from-ROI_labels' at 0x3138e1af0>, <Labels layer 'my-unet_labels' at 0x346f4be00>], help='use <2> for transform', status='Ready', tooltip=Tooltip(visible=False, text=''), theme='dark', title='napari', mouse_over_canvas=True, mouse_move_callbacks=[], mouse_drag_callbacks=[], mouse_double_click_callbacks=[], mouse_wheel_callbacks=[<function dims_scroll at 0x1666dce00>], _persisted_mouse_event={}, _mouse_drag_gen={}, _mouse_wheel_gen={}, keymap={})
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        save_dir = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact')
        name = 'labels-prediction'
        unet_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
        chunk_size = (10, 256, 256)
        margin = (1, 64, 64)
        debug = True
     72                          napari_viewer, input_volume_layer, save_dir, name, unet_or_config_file,
     73                          layer_reference, chunk_size, margin, debug)

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:758, in segmentation_wrapper(processing_function=<function affinity_watershed_for_chunks>, config_prep_function=<function affinity_watershed_prep_config>, napari_viewer=Viewer(camera=Camera(center=(0.0, 148.7093690061...ouse_drag_gen={}, _mouse_wheel_gen={}, keymap={}), input_volume_layer=<Image layer 'image_time_0'>, save_dir=PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact'), name='labels-prediction', network_or_config_file=PosixPath('/Users/raeesahhayatudin/Downloads/Tra...s_z_intact/242405_203956_unet_my-unet_meta.json'), layer_reference='None', chunk_size=(10, 256, 256), margin=(1, 64, 64), debug=True)
    754 viewer = napari_viewer
    756 # get the config dict
    757 # -------------------
--> 758 config = config_prep_function(
        config_prep_function = <function affinity_watershed_prep_config at 0x31d34c860>
        input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
        network_or_config_file = PosixPath('/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyter_TIFFTests_z_intact/242405_203956_unet_my-unet_meta.json')
        layer_reference = 'None'
    759         input_volume_layer, network_or_config_file, layer_reference
    760         )
    761 if config is None:
    762     config = {}

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:104, in affinity_watershed_prep_config(input_volume_layer=<Image layer 'image_time_0'>, unet_or_config_file='/Users/raeesahhayatudin/Downloads/Training_Valid...sts_z_intact/242405_203956_unet_my-unet_meta.json', reference_layer='None')
    102 if config.get('affinities_extent') is None:
    103     affinities_extent = 1
--> 104 if unet == 'labels layer':
    105     unet = reference_layer.metadata['unet']
    106 if unet == 'default':

EmitLoopError: 

While emitting signal 'magicgui.widgets.PushButton.changed', a UnboundLocalError occurred in a callback:

  Signal emitted at: /Users/raeesahhayatudin/micromamba/envs/annoseg/lib/python3.12/site-packages/magicgui/widgets/bases/_value_widget.py:71, in _on_value_change
    >  self.changed.emit(value)

  Callback error at: /Users/raeesahhayatudin/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:104, in affinity_watershed_prep_config
    >  if unet == 'labels layer':

    Local variables:
       input_volume_layer = <Image layer 'image_time_0' at 0x300184050>
       unet_or_config_file = '/Users/raeesahhayatudin/Downloads/Training_Validation_Jupyt...
       reference_layer = 'None'
       affinities_extent = 1
       config = {'unet': '/Users/raeesahhayatudin/Downloads/Training_Validat...

See UnboundLocalError above for original traceback.
jni commented 6 months ago

So the error is here:

File ~/micromamba/envs/annoseg/lib/python3.12/site-packages/iterseg/segmentation.py:104, in affinity_watershed_prep_config(input_volume_layer=<Image layer 'image_time_0'>, unet_or_config_file='/Users/raeesahhayatudin/Downloads/Training_Valid...sts_z_intact/242405_203956_unet_my-unet_meta.json', reference_layer='None')
    103     affinities_extent = 1
--> 104 if unet == 'labels layer':
    105     unet = reference_layer.metadata['unet']

which corresponds to this code:

https://github.com/AbigailMcGovern/iterseg/blob/ba22adeb6558c28d4fb9f0b31b963ed387e87933/src/iterseg/segmentation.py#L98-L105

I don't rightly know what it's supposed to do, because the unet variable cannot be 'labels layer', ever, in this code. 😂 I think this would fix this if there was a 'unet' key in the config: replace:

https://github.com/AbigailMcGovern/iterseg/blob/ba22adeb6558c28d4fb9f0b31b963ed387e87933/src/iterseg/segmentation.py#L100-L101

with

unet = config.get('unet', None)

But the whole function is pretty iffy so might be worth revisiting the entire flow. 😅