Skip to content

Commit

Permalink
Test conversion behavior
Browse files Browse the repository at this point in the history
  • Loading branch information
gselzer committed Sep 29, 2022
1 parent c089433 commit 9b2b699
Show file tree
Hide file tree
Showing 5 changed files with 101 additions and 14 deletions.
4 changes: 4 additions & 0 deletions src/napari_imagej/java.py
Original file line number Diff line number Diff line change
Expand Up @@ -322,6 +322,10 @@ def ColorTable(self):
def ColorTable8(self):
return "net.imglib2.display.ColorTable8"

@blocking_import
def ColorTables(self):
return "net.imagej.display.ColorTables"

@blocking_import
def ComplexType(self):
return "net.imglib2.type.numeric.ComplexType"
Expand Down
5 changes: 3 additions & 2 deletions src/napari_imagej/types/converters/images.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,8 @@ def _dataset_view_to_image(image: Any) -> Image:
name=view.getData().getName(),
)
if view.getColorTables().size() > 0:
kwargs["colormap"] = _color_table_to_colormap(view.getColorTables().get(0))
if not jc.ColorTables.isGrayColorTable(view.getColorTables().get(0)):
kwargs["colormap"] = _color_table_to_colormap(view.getColorTables().get(0))
return Image(**kwargs)


Expand All @@ -47,7 +48,7 @@ def _dataset_to_image(image: Any) -> Image:
name=imp.getName(),
)
if imp.getColorTableCount():
kwargs["colormap"] = imp.getColorTable(0)
kwargs["colormap"] = _color_table_to_colormap(imp.getColorTable(0))
return Image(**kwargs)


Expand Down
19 changes: 11 additions & 8 deletions src/napari_imagej/widgets/menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
from enum import Enum
from pathlib import Path
from threading import Thread
from typing import Any, Dict, Optional
from typing import Any, Dict, Optional, Union

from jpype import JImplements, JOverride
from magicgui.widgets import request_values
Expand Down Expand Up @@ -202,21 +202,24 @@ def get_chosen_layer(self) -> None:
if choices is not None:
# grab the chosen name
name = choices["data"]
# grab the chosen image
view = (
ij().get("org.scijava.display.DisplayService").getDisplay(name).get(0)
)
self._add_image_from_display_view(view)
display = ij().display().getDisplay(name)
# if the image is displayed, convert the DatasetView
if display:
self._add_image(display.get(0))
# Otherwise, just convert the object
else:
image = images[names.index(name)]
self._add_image(image)

def get_active_layer(self) -> None:
# Choose the active DatasetView
view = ij().get("net.imagej.display.ImageDisplayService").getActiveDatasetView()
if view is None:
log_debug("There is no active window to export to napari")
return
self._add_image_from_display_view(view)
self._add_image(view)

def _add_image_from_display_view(self, view: "jc.DatasetView"):
def _add_image(self, view: Union["jc.Dataset", "jc.DatasetView"]):
# Get the stuff needed for a new layer
py_image = ij().py.from_java(view)
# Create and add the layer
Expand Down
84 changes: 82 additions & 2 deletions tests/types/test_converters.py
Original file line number Diff line number Diff line change
Expand Up @@ -760,7 +760,87 @@ def test_OutOfBoundsFactory_conversion(ij):
# -- Images -- #


def test_image_layer_to_img(ij):
image = Image(data=np.ones((10, 10)))
@pytest.fixture
def test_dataset(ij) -> "jc.Dataset":
name = "test.foo"
dataset: jc.Dataset = ij.dataset().create(ij.py.to_java(np.ones((10, 10))))
dataset.setName(name)
return dataset


@pytest.fixture
def test_dataset_view(ij, test_dataset) -> "jc.DatasetView":
view: jc.DatasetView = ij.get(
"net.imagej.display.ImageDisplayService"
).createDataView(test_dataset)
view.rebuild()
view.resetColorTables(True)
yield view
# dispose of the view so it does not affect later tests
view.dispose()


def _assert_equal_color_maps(j_map: "jc.ColorTable", p_map):
p_color = p_map.map([x / 255 for x in range(256)])
# Assert color table "equality"
for i in range(j_map.getLength()):
for j in range(j_map.getComponentCount()):
assert j_map.get(j, i) == int(round(p_color[i, j] * 255))


def test_image_layer_to_dataset(ij):
"""Test conversion of an Image layer with a default colormap"""
name = "test_foo"
image = Image(data=np.ones((10, 10)), name=name)
j_img = ij.py.to_java(image)
assert isinstance(j_img, jc.Dataset)
assert name == j_img.getName()
assert 0 == j_img.getColorTableCount()


def test_colormap_image_layer_to_dataset(ij):
"""Test conversion of an Image layer with a chosen colormap"""
name = "test_foo"
image = Image(data=np.ones((10, 10)), name=name, colormap="red")
j_img = ij.py.to_java(image)
assert isinstance(j_img, jc.Dataset)
assert name == j_img.getName()
assert 1 == j_img.getColorTableCount()
_assert_equal_color_maps(j_img.getColorTable(0), image.colormap)


def test_dataset_to_image_layer(ij, test_dataset):
"""Test conversion of a Dataset with no colormap"""
p_img = ij.py.from_java(test_dataset)
assert isinstance(p_img, Image)
assert test_dataset.getName() == p_img.name
assert "gray" == p_img.colormap.name


def test_colormap_dataset_to_image_layer(ij, test_dataset):
"""Test conversion of a Dataset with a colormap"""
test_dataset.initializeColorTables(1)
test_dataset.setColorTable(jc.ColorTables.CYAN, 0)
p_img = ij.py.from_java(test_dataset)
assert isinstance(p_img, Image)
assert test_dataset.getName() == p_img.name
assert "gray" != p_img.colormap.name
_assert_equal_color_maps(test_dataset.getColorTable(0), p_img.colormap)


def test_dataset_view_to_image_layer(ij, test_dataset_view):
"""Test conversion of a Dataset with no colormap"""
p_img = ij.py.from_java(test_dataset_view)
assert isinstance(p_img, Image)
assert test_dataset_view.getData().getName() == p_img.name
assert "gray" == p_img.colormap.name


def test_colormap_dataset_view_to_image_layer(ij, test_dataset_view):
"""Test conversion of a Dataset with a colormap"""
test_dataset_view.setColorTable(jc.ColorTables.CYAN, 0)
p_img = ij.py.from_java(test_dataset_view)
assert isinstance(p_img, Image)
assert test_dataset_view.getData().getName() == p_img.name
assert "gray" != p_img.colormap.name
_assert_equal_color_maps(test_dataset_view.getColorTables().get(0), p_img.colormap)
3 changes: 1 addition & 2 deletions tests/widgets/test_menu.py
Original file line number Diff line number Diff line change
Expand Up @@ -271,8 +271,7 @@ def test_data_choosers(asserter, qtbot, ij, gui_widget_chooser):
button_to.clicked.emit()

# Assert that the data is now in ImageJ2
asserter(lambda: isinstance(ij.display().getActiveDisplay(), jc.ImageDisplay))
assert "test_to" == ij.display().getActiveDisplay().getName()
asserter(lambda: isinstance(ij.display().getDisplay("test_to"), jc.ImageDisplay))

# Use the chooser to transfer that data back
button_from.clicked.emit()
Expand Down

0 comments on commit 9b2b699

Please sign in to comment.