Skip to content

Commit a38d162

Browse files
committed
ENH: Move image conversion up to ensure error messages are shown
Signed-off-by: Brianna Major <[email protected]>
1 parent 840c107 commit a38d162

File tree

1 file changed

+34
-30
lines changed

1 file changed

+34
-30
lines changed

itkwidgets/viewer.py

Lines changed: 34 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -28,44 +28,19 @@ class ViewerRPC:
2828
"""Viewer remote procedure interface."""
2929

3030
def __init__(
31-
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", **add_data_kwargs
31+
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", data=None
3232
):
3333
"""Create a viewer."""
3434
self._init_viewer_kwargs = dict(ui_collapsed=ui_collapsed, rotate=rotate, ui=ui)
35-
self._init_viewer_kwargs.update(**add_data_kwargs)
36-
self.init_data = {}
35+
self.init_data = data
3736
self.img = display(HTML(f'<div />'), display_id=str(uuid.uuid4()))
3837
self.wid = None
3938
if ENVIRONMENT is not Env.JUPYTERLITE:
4039
self.viewer_event = threading.Event()
4140
self.data_event = threading.Event()
4241

43-
def _get_input_data(self):
44-
input_options = ["data", "image", "label_image", "point_sets"]
45-
inputs = []
46-
for option in input_options:
47-
data = self._init_viewer_kwargs.get(option, None)
48-
if data is not None:
49-
inputs.append((option, data))
50-
return inputs
51-
52-
def setup(self):
53-
self.init_data.clear()
54-
input_data = self._get_input_data()
55-
result= None
56-
for (input_type, data) in input_data:
57-
render_type = _detect_render_type(data, input_type)
58-
key = init_key_aliases()[input_type]
59-
if render_type is RenderType.IMAGE:
60-
if input_type == 'label_image':
61-
result = _get_viewer_image(data, label=True)
62-
else:
63-
result = _get_viewer_image(data, label=False)
64-
elif render_type is RenderType.POINT_SET:
65-
result = _get_viewer_point_sets(data)
66-
if result is None:
67-
result = data
68-
self.init_data[key] = result
42+
async def setup(self):
43+
pass
6944

7045
async def run(self, ctx):
7146
"""ImJoy plugin setup function."""
@@ -157,9 +132,11 @@ class Viewer:
157132
def __init__(
158133
self, ui_collapsed=True, rotate=False, ui="pydata-sphinx", **add_data_kwargs
159134
):
135+
input_data = self.input_data(add_data_kwargs)
136+
data = self.init_data(input_data)
160137
"""Create a viewer."""
161138
self.viewer_rpc = ViewerRPC(
162-
ui_collapsed=ui_collapsed, rotate=rotate, ui=ui, **add_data_kwargs
139+
ui_collapsed=ui_collapsed, rotate=rotate, ui=ui, data=data
163140
)
164141
if ENVIRONMENT is not Env.JUPYTERLITE:
165142
self.bg_jobs = bg.BackgroundJobManager()
@@ -176,6 +153,33 @@ def loop(self):
176153
def has_viewer(self):
177154
return hasattr(self.viewer_rpc, 'itk_viewer')
178155

156+
def input_data(self, init_data_kwargs):
157+
input_options = ["data", "image", "label_image", "point_sets"]
158+
inputs = []
159+
for option in input_options:
160+
data = init_data_kwargs.get(option, None)
161+
if data is not None:
162+
inputs.append((option, data))
163+
return inputs
164+
165+
def init_data(self, input_data):
166+
_init_data = {}
167+
result= None
168+
for (input_type, data) in input_data:
169+
render_type = _detect_render_type(data, input_type)
170+
key = init_key_aliases()[input_type]
171+
if render_type is RenderType.IMAGE:
172+
if input_type == 'label_image':
173+
result = _get_viewer_image(data, label=True)
174+
else:
175+
result = _get_viewer_image(data, label=False)
176+
elif render_type is RenderType.POINT_SET:
177+
result = _get_viewer_point_sets(data)
178+
if result is None:
179+
raise RuntimeError(f"Could not process the viewer {input_type}")
180+
_init_data[key] = result
181+
return _init_data
182+
179183
async def run_queued_requests(self):
180184
def _run_queued_requests(queue):
181185
method_name, args, kwargs = queue.get()

0 commit comments

Comments
 (0)