Skip to content

Commit

Permalink
ENH: Support top-level wrapper for compare_images
Browse files Browse the repository at this point in the history
  • Loading branch information
bnmajor committed Dec 8, 2023
1 parent 5efe59f commit 0dec8bb
Show file tree
Hide file tree
Showing 6 changed files with 428 additions and 406 deletions.
796 changes: 399 additions & 397 deletions examples/integrations/itk/ThinPlateSpline.ipynb

Large diffs are not rendered by default.

11 changes: 7 additions & 4 deletions itkwidgets/_initialization_params.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@
from itkwidgets.viewer_config import MUI_HREF, PYDATA_SPHINX_HREF


INPUT_OPTIONS = ["image", "label_image", "point_set", "data"]

DATA_OPTIONS = ["image", "label_image", "point_set", "data", "fixed_image"]
INPUT_OPTIONS = [*DATA_OPTIONS, "compare"]

def init_params_dict(itk_viewer):
return {
Expand Down Expand Up @@ -75,7 +75,7 @@ def parse_input_data(init_data_kwargs):

def build_init_data(input_data):
result= None
for input_type in INPUT_OPTIONS:
for input_type in DATA_OPTIONS:
data = input_data.pop(input_type, None)
if data is None:
continue
Expand All @@ -84,6 +84,9 @@ def build_init_data(input_data):
if input_type == 'label_image':
result = _get_viewer_image(data, label=True)
render_type = RenderType.LABELIMAGE
elif input_type == 'fixed_image':
result = _get_viewer_image(data)
render_type = RenderType.FIXEDIMAGE
else:
result = _get_viewer_image(data, label=False)
elif render_type is RenderType.POINT_SET:
Expand All @@ -95,5 +98,5 @@ def build_init_data(input_data):


def defer_for_data_render(init_data):
deferred_keys = ['image', 'labelImage']
deferred_keys = ['image', 'labelImage', 'fixedImage']
return any([k in init_data.keys() for k in deferred_keys])
4 changes: 3 additions & 1 deletion itkwidgets/integrations/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -163,7 +163,9 @@ def _get_viewer_point_set(point_set):


def _detect_render_type(data, input_type) -> RenderType:
if input_type == 'image' or input_type == 'label_image':
if (input_type == 'image' or
input_type == 'label_image' or
input_type == 'fixed_image'):
return RenderType.IMAGE
elif input_type == 'point_set':
return RenderType.POINT_SET
Expand Down
1 change: 1 addition & 0 deletions itkwidgets/render_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ class RenderType(Enum):
LABELIMAGE = "labelImage"
GEOMETRY = "geometry"
POINT_SET = "pointSets"
FIXEDIMAGE = "fixedImage"
4 changes: 2 additions & 2 deletions itkwidgets/standalone_server.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
build_config,
build_init_data,
init_params_dict,
INPUT_OPTIONS,
DATA_OPTIONS,
)
from itkwidgets.viewer import view
from ngff_zarr import detect_cli_io_backend, cli_input_to_ngff_image, ConversionBackend
Expand Down Expand Up @@ -88,7 +88,7 @@ def input_dict(viewer_options):
def read_files(viewer_options):
user_input = vars(viewer_options)
reader = user_input.get("reader", None)
for param in INPUT_OPTIONS:
for param in DATA_OPTIONS:
input = user_input.get(param, None)
if input:
if reader:
Expand Down
18 changes: 16 additions & 2 deletions itkwidgets/viewer.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,8 @@ def __init__(
self.name = self.__str__()
input_data = parse_input_data(add_data_kwargs)
data = build_init_data(input_data)
if compare := input_data.get('compare'):
data['compare'] = compare
if ENVIRONMENT is not Env.HYPHA:
self.viewer_rpc = ViewerRPC(
ui_collapsed=ui_collapsed, rotate=rotate, ui=ui, init_data=data, parent=self.name, **add_data_kwargs
Expand Down Expand Up @@ -615,7 +617,19 @@ def compare_images(fixed_image: Union[str, Image], moving_image: Union[str, Imag
:return: viewer, display by placing at the end of a Jupyter or Colab cell. Query or set properties on the object to change the visualization.
:rtype: Viewer
"""
viewer = view()
viewer.compare_images(fixed_image=fixed_image, moving_image=moving_image, method=method, image_mix=image_mix, checkerboard=checkerboard, pattern=pattern, swap_image_order=swap_image_order)
options = {}
# if None let viewer use defaults or last value.
if method is not None:
options['method'] = method
if image_mix is not None:
options['imageMix'] = image_mix
if checkerboard is not None:
options['checkerboard'] = checkerboard
if pattern is not None:
options['pattern'] = pattern
if swap_image_order is not None:
options['swapImageOrder'] = swap_image_order

viewer = Viewer(data=None, image=moving_image, fixed_image=fixed_image, compare=options)
return viewer

0 comments on commit 0dec8bb

Please sign in to comment.