Skip to content

Commit

Permalink
ENH: Wait for image data to render before progressing
Browse files Browse the repository at this point in the history
If image data is provided on initialization the viewer is not marked as ready
until the data has been rendered. If set_image or set_label_image are called
the viewer is again marked as "not ready" until the new data is rendered.
  • Loading branch information
bnmajor committed Dec 8, 2023
1 parent 03bd1ef commit a461453
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 5 deletions.
5 changes: 5 additions & 0 deletions itkwidgets/_initialization_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,3 +92,8 @@ def build_init_data(input_data):
raise RuntimeError(f"Could not process the viewer {input_type}")
input_data[render_type.value] = result
return input_data


def defer_for_data_render(init_data):
deferred_keys = ['image', 'labelImage']
return any([k in init_data.keys() for k in deferred_keys])
14 changes: 11 additions & 3 deletions itkwidgets/cell_watcher.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,17 +38,22 @@ def viewer_objects(self):
return list(self.data.keys())

def add_viewer(self, view):
self._data[view] = {'name': None, 'status': False}
self.data[view] = {'name': None, 'status': False}

def set_name(self, view, name):
if view not in self.data.keys():
self.add_viewer(view)
self._data[view]['name'] = name
self.data[view]['name'] = name

def update_viewer_status(self, view, status):
if view not in self.data.keys():
self.add_viewer(view)
self._data[view]['status'] = status
self.data[view]['status'] = status

def viewer_ready(self, view):
if viewer := self.data.get(view):
return viewer['status']
return False


class CellWatcher(object):
Expand Down Expand Up @@ -94,6 +99,9 @@ def update_viewer_status(self, view, status):
# Might be ready now, try again
self.create_task(self.execute_next_request)

def viewer_ready(self, view):
return self.viewers.viewer_ready(view)

def _task_cleanup(self, task):
global background_tasks
try:
Expand Down
16 changes: 14 additions & 2 deletions itkwidgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@
build_config,
parse_input_data,
build_init_data,
defer_for_data_render,
)
from .cell_watcher import CellWatcher
from .imjoy import register_itkwasm_imjoy_codecs
Expand Down Expand Up @@ -82,8 +83,12 @@ async def run(self, ctx):
if ENVIRONMENT is not Env.JUPYTERLITE:
# Create the initial screenshot
await self.create_screenshot()
# Once the viewer has been created any queued requests can be run
CellWatcher().update_viewer_status(self.parent, True)
itk_viewer.registerEventListener(
'renderedImageAssigned', self.update_viewer_status
)
if not defer_for_data_render(self.init_data):
# Once the viewer has been created any queued requests can be run
CellWatcher().update_viewer_status(self.parent, True)

# Wait and then update the screenshot in case rendered level changed
await asyncio.sleep(10)
Expand Down Expand Up @@ -118,6 +123,10 @@ def update_screenshot(self, base64_image):
''')
self.img.display(html)

def update_viewer_status(self, name):
if not CellWatcher().viewer_ready(self.parent):
CellWatcher().update_viewer_status(self.parent, True)


class Viewer:
"""Pythonic Viewer class."""
Expand Down Expand Up @@ -201,6 +210,7 @@ def set_image(self, image: Image, name: str = 'Image'):
svc.set_label_or_image('image')
else:
self.viewer_rpc.itk_viewer.setImage(image, name)
CellWatcher().update_viewer_status(self.name, False)
elif render_type is RenderType.POINT_SET:
image = _get_viewer_point_set(image)
self.viewer_rpc.itk_viewer.setPointSets(image)
Expand Down Expand Up @@ -318,6 +328,7 @@ def compare_images(self, fixed_image: Union[str, Image], moving_image: Union[str
if swap_image_order is not None:
options['swapImageOrder'] = swap_image_order
self.viewer_rpc.itk_viewer.compareImages(fixed_name, moving_name, options)
CellWatcher().update_viewer_status(self.name, False)

@fetch_value
def set_label_image(self, label_image: Image):
Expand All @@ -331,6 +342,7 @@ def set_label_image(self, label_image: Image):
svc.set_label_or_image('label_image')
else:
self.viewer_rpc.itk_viewer.setLabelImage(label_image)
CellWatcher().update_viewer_status(self.name, False)
elif render_type is RenderType.POINT_SET:
label_image = _get_viewer_point_set(label_image)
self.viewer_rpc.itk_viewer.setPointSets(label_image)
Expand Down

0 comments on commit a461453

Please sign in to comment.