From c0ff9b3f67e7d63e0784edf0742403abf7e99525 Mon Sep 17 00:00:00 2001 From: Viktor van der Valk <33719474+ViktorvdValk@users.noreply.github.com> Date: Mon, 31 May 2021 16:22:06 +0200 Subject: [PATCH] World coordinates support (#3) * ENH: World coordinates support * ENH: Update test for world coordinates version * ENH: Version bump to 0.0.3 --- checkerboard/__init__.py | 2 +- checkerboard/_tests/test_dock_widget.py | 10 ++++++--- checkerboard/checkerboard.py | 28 +++++++++++++++---------- requirements.txt | 2 ++ 4 files changed, 27 insertions(+), 15 deletions(-) diff --git a/checkerboard/__init__.py b/checkerboard/__init__.py index da1f85d..0d85720 100644 --- a/checkerboard/__init__.py +++ b/checkerboard/__init__.py @@ -3,7 +3,7 @@ __author__ = "Viktor van der Valk" __email__ = "v.o.van_der_valk@lumc.nl" -__version__ = "0.0.2" +__version__ = "0.0.3" def get_module_version(): diff --git a/checkerboard/_tests/test_dock_widget.py b/checkerboard/_tests/test_dock_widget.py index 2cd95f3..3e11cf4 100644 --- a/checkerboard/_tests/test_dock_widget.py +++ b/checkerboard/_tests/test_dock_widget.py @@ -3,6 +3,9 @@ import itk import pytest import numpy as np +from itk_napari_conversion import image_layer_from_image +from itk_napari_conversion import image_from_image_layer +import napari # this is your plugin name declared in your napari.plugins entry point MY_PLUGIN_NAME = "napari-checkerboard" @@ -15,12 +18,13 @@ def image_generator(x1, x2, y1, y2, data_dir='none'): image = np.zeros([100, 100], np.float32) image[y1:y2, x1:x2] = 1 image = itk.image_view_from_array(image) + image = image_layer_from_image(image) return image def checkerboard_filter(*args, **kwargs): - filter = checkerboard.checkerboard() - return filter(*args, **kwargs) + cb_filter = checkerboard.checkerboard() + return cb_filter(*args, **kwargs) # @pytest.mark.parametrize("widget_name", MY_WIDGET_NAMES) @@ -41,4 +45,4 @@ def test_checkerboard(): image1 = image_generator(25, 75, 25, 75) image2 = image_generator(1, 51, 10, 60) result_image = checkerboard_filter(image1, image2, pattern=4) - assert type(result_image[0]) == np.ndarray + assert isinstance(result_image, napari.layers.Image) diff --git a/checkerboard/checkerboard.py b/checkerboard/checkerboard.py index c5881fe..f372a5b 100644 --- a/checkerboard/checkerboard.py +++ b/checkerboard/checkerboard.py @@ -3,6 +3,9 @@ import numpy as np import itk import checkerboard.utils as utils +from itk_napari_conversion import image_from_image_layer +from itk_napari_conversion import image_layer_from_image + def on_init(widget): """ @@ -15,9 +18,9 @@ def on_init(widget): call_button="create checkerboard", pattern={"max": 20, "step": 1, "tooltip": "Select the gridsize of the checkerboard"}) -def checkerboard(image1: "napari.types.ImageData", - image2: "napari.types.ImageData", - pattern: int = 3) -> 'napari.types.LayerDataTuple': +def checkerboard(image1: "napari.layers.Image", + image2: "napari.layers.Image", + pattern: int = 3) -> 'napari.layers.Image': """ Takes user input images and returns a checkerboard blend of the images. """ @@ -25,13 +28,11 @@ def checkerboard(image1: "napari.types.ImageData", print("No images selected for registration.") return error("No images selected for registration.") - image1 = np.asarray(image1).astype(np.float32) - image2 = np.asarray(image2).astype(np.float32) - - itk_image1 = itk.GetImageFromArray(image1) - itk_image2 = itk.GetImageFromArray(image2) - input1 = itk_image1 - input2 = itk_image2 + # Convert image layer to itk_image + itk_image1 = image_from_image_layer(image1) + itk_image2 = image_from_image_layer(image2) + itk_image1 = itk_image1.astype(itk.F) + itk_image2 = itk_image2.astype(itk.F) region_image1 = itk_image1.GetLargestPossibleRegion() region_image2 = itk_image2.GetLargestPossibleRegion() @@ -70,6 +71,9 @@ def checkerboard(image1: "napari.types.ImageData", resampler.SetReferenceImage(itk_image2) resampler.Update() input1 = resampler.GetOutput() + else: + input1 = itk_image1 + input2 = itk_image2 checkerboard_filter = itk.CheckerBoardImageFilter.New(input1, input2) @@ -79,7 +83,9 @@ def checkerboard(image1: "napari.types.ImageData", checkerboard_filter.Update() checkerboard = checkerboard_filter.GetOutput() - return np.asarray(checkerboard).astype(np.float32), {'name': 'checkerboard'} + layer = image_layer_from_image(checkerboard) + layer.name = "checkerboard " + image1.name + " " + image2.name + return layer @napari_hook_implementation diff --git a/requirements.txt b/requirements.txt index 29fc18f..608f02d 100644 --- a/requirements.txt +++ b/requirements.txt @@ -3,3 +3,5 @@ numpy>=1.19.0 napari>=0.4.6 magicgui>=0.2.6 itk-elastix>=0.11.1 +itk_napari_conversion>=0.3.1 +napari-itk-io>=0.1.0