From 3b5043255aea815f7c8f60aaabbf69a96f768a20 Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Mon, 7 Apr 2025 11:36:28 -0500 Subject: [PATCH 1/8] Test xarray naming post conversion --- tests/test_legacy.py | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/tests/test_legacy.py b/tests/test_legacy.py index 84077c33..7c3cfe3c 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -3,6 +3,7 @@ import numpy as np import pytest import scyjava as sj +import xarray # -- Fixtures -- @@ -13,6 +14,11 @@ def arr(): return empty_array +@pytest.fixture(scope="module") +def xarr(arr): + return xarray.DataArray(arr, name="test_xarray") + + @pytest.fixture(scope="module") def results_table(ij): if ij.legacy and ij.legacy.isActive(): @@ -141,6 +147,19 @@ def test_window_to_numpy_converts_active_image_to_xarray(ij, arr): assert (arr == new_arr.values).all +def test_linked_dataset_convertsion_to_xarray(ij, xarr): + ensure_legacy_enabled(ij) + ensure_gui_available(ij) + + # get a dataset, linked to an ImagePlus + dataset = ij.py.to_dataset(arr) + ij.ui().show(dataset) + + # convert the image data to xarray + xarr_out = ij.py.to_xarray(dataset) + assert xarr_out.name == "test_xarray" + + def test_functions_throw_warning_if_legacy_not_enabled(ij): ensure_legacy_disabled(ij) From 0376f90c3af1140113755aba083900c3a2536e8c Mon Sep 17 00:00:00 2001 From: Gabriel Selzer Date: Mon, 7 Apr 2025 11:42:53 -0500 Subject: [PATCH 2/8] Resolve typo --- tests/test_legacy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_legacy.py b/tests/test_legacy.py index 7c3cfe3c..2561076b 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -147,7 +147,7 @@ def test_window_to_numpy_converts_active_image_to_xarray(ij, arr): assert (arr == new_arr.values).all -def test_linked_dataset_convertsion_to_xarray(ij, xarr): +def test_linked_dataset_conversion_to_xarray(ij, xarr): ensure_legacy_enabled(ij) ensure_gui_available(ij) From ab2efdcbfb8ceb2fd02a4a29d6b7ac3b1ea094e1 Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Mon, 7 Apr 2025 15:38:00 -0500 Subject: [PATCH 3/8] Run CI test jobs with xvfb Legacy tests are always skipped because no framebuffer is available. --- .github/workflows/build.yml | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/.github/workflows/build.yml b/.github/workflows/build.yml index 5f85dad3..92b37d3c 100644 --- a/.github/workflows/build.yml +++ b/.github/workflows/build.yml @@ -45,9 +45,10 @@ jobs: python -m pip install -e '.[dev]' - name: Test PyImageJ - shell: bash - run: | - bin/test.sh + uses: GabrielBB/xvfb-action@v1 + with: + run: + bash bin/test.sh ensure-clean-code: runs-on: ubuntu-latest From d5fcb10f8c8c1786b54a1d16bd339ed9f42a5f57 Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 8 Apr 2025 09:22:51 -0500 Subject: [PATCH 4/8] Fix fiji stiching test --- tests/test_fiji.py | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/tests/test_fiji.py b/tests/test_fiji.py index 16c7d6dc..b329a759 100644 --- a/tests/test_fiji.py +++ b/tests/test_fiji.py @@ -5,22 +5,30 @@ def test_plugins_load_using_pairwise_stitching(ij): + # get pariwise stiching Fiji plugin, if available try: sj.jimport("plugin.Stitching_Pairwise") except TypeError: pytest.skip("No Pairwise Stitching plugin available. Skipping test.") + # skip if missing legacy or in headless mode if not ij.legacy: pytest.skip("No original ImageJ. Skipping test.") if ij.ui().isHeadless(): pytest.skip("No GUI. Skipping test.") + # create random image tiles tile1 = ij.IJ.createImage("Tile1", "8-bit random", 512, 512, 1) tile2 = ij.IJ.createImage("Tile2", "8-bit random", 512, 512, 1) + tile1.show() + tile2.show() + + # stich image tiles args = {"first_image": tile1.getTitle(), "second_image": tile2.getTitle()} ij.py.run_plugin("Pairwise stitching", args) result_name = ij.WindowManager.getCurrentImage().getTitle() + # clean up the windows ij.IJ.run("Close All", "") assert result_name == "Tile1<->Tile2" From b8ee5514253db5603dec2a2e79f32f18238fba5c Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 8 Apr 2025 09:23:42 -0500 Subject: [PATCH 5/8] Set mode to interactive for tests Note that I may need to interactive mode for testing conditional on the platform (i.e. macOS does not support interactive mode). --- conftest.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 45b9ddd7..0e75324a 100644 --- a/conftest.py +++ b/conftest.py @@ -21,7 +21,7 @@ def pytest_addoption(parser): "--headless", type=str2bool, action="store", - default=True, + default=False, help="Start in headless mode", ) parser.addoption( From dbf6aaa4326da67251e6dc6b6958befdaf9ad715 Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 8 Apr 2025 09:48:38 -0500 Subject: [PATCH 6/8] Fix ImagePlus sync test Showing a Dataset and modifying it is not enough. We need to next obtain an ImagePlus via the WindowManager and sync it. --- tests/test_legacy.py | 2 ++ 1 file changed, 2 insertions(+) diff --git a/tests/test_legacy.py b/tests/test_legacy.py index 2561076b..5ed9d44d 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -119,6 +119,8 @@ def test_get_imageplus_synchronizes_from_imagej_to_imagej2(ij, arr): ij.ui().show(ds) macro = """run("Add...", "value=5");""" ij.py.run_macro(macro) + imp = ij.WindowManager.getCurrentImage() + ij.py.sync_image(imp) assert arr[0, 0] == original + 5 From bd16ac2c9106ff78863da6e06dd61bce9ba2264e Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 8 Apr 2025 09:50:05 -0500 Subject: [PATCH 7/8] Fix dataset conversion linked xarray test --- tests/test_legacy.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/test_legacy.py b/tests/test_legacy.py index 5ed9d44d..3a29a454 100644 --- a/tests/test_legacy.py +++ b/tests/test_legacy.py @@ -154,7 +154,7 @@ def test_linked_dataset_conversion_to_xarray(ij, xarr): ensure_gui_available(ij) # get a dataset, linked to an ImagePlus - dataset = ij.py.to_dataset(arr) + dataset = ij.py.to_dataset(xarr) ij.ui().show(dataset) # convert the image data to xarray From 216d5ae34ade6ba9ea2eec60966b627e21967c91 Mon Sep 17 00:00:00 2001 From: Edward Evans Date: Tue, 8 Apr 2025 09:57:03 -0500 Subject: [PATCH 8/8] Force headless mode for tests on macOS --- conftest.py | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/conftest.py b/conftest.py index 0e75324a..8236ac2f 100644 --- a/conftest.py +++ b/conftest.py @@ -1,5 +1,5 @@ import argparse - +import sys import pytest import imagej @@ -46,6 +46,10 @@ def ij(request): imagej.when_imagej_starts(lambda ij: setattr(ij, "_testing", True)) mode = "headless" if headless else "interactive" + # set headless mode for macOS only + macos = sys.platform == "darwin" + if macos: + mode = "headless" ij = imagej.init(ij_dir, mode=mode, add_legacy=legacy) yield ij