From d2139ab104b21da91f24f818bbc9d1cebf9b3747 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 11:30:57 +0100 Subject: [PATCH 1/9] ADD: test/fiji/../jython/ various exploration scripts for testing behaviour of ImageJ/Fiji --- .../jython/create_Dataset_and_show_via_UIService.py | 12 ++++++++++++ .../fiji/macros/jython/create_ImagePlus_and_show.py | 6 ++++++ .../create_img_and_show_via_ImageJFunctions.py | 6 ++++++ .../jython/create_img_and_show_via_UIService.py | 6 ++++++ .../fiji/macros/jython/list_known_image_windows.py | 6 ++++++ .../fiji/macros/jython/test_ImagePlus_presence.py | 3 +++ src/test/fiji/macros/jython/test_dataset_presence.py | 3 +++ 7 files changed, 42 insertions(+) create mode 100644 src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py create mode 100644 src/test/fiji/macros/jython/create_ImagePlus_and_show.py create mode 100644 src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py create mode 100644 src/test/fiji/macros/jython/create_img_and_show_via_UIService.py create mode 100644 src/test/fiji/macros/jython/list_known_image_windows.py create mode 100644 src/test/fiji/macros/jython/test_ImagePlus_presence.py create mode 100644 src/test/fiji/macros/jython/test_dataset_presence.py diff --git a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py new file mode 100644 index 00000000..26da5a43 --- /dev/null +++ b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py @@ -0,0 +1,12 @@ +#@ Context ctx +#@ UIService ui + +from net.imagej import ImgPlus +from net.imglib2.img.planar import PlanarImgs +from net.imagej import DefaultDataset + +img = PlanarImgs.bytes(303, 302) +imp = ImgPlus(img, "ImgPlus turned into Dataset") +ds = DefaultDataset(ctx, imp) + +ui.show(ds) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/create_ImagePlus_and_show.py b/src/test/fiji/macros/jython/create_ImagePlus_and_show.py new file mode 100644 index 00000000..7c08db74 --- /dev/null +++ b/src/test/fiji/macros/jython/create_ImagePlus_and_show.py @@ -0,0 +1,6 @@ +from ij.process import ByteProcessor +from ij import ImagePlus + +p = ByteProcessor(303, 302) +imp = ImagePlus("pure ImagePlus", p) +imp.show() \ No newline at end of file diff --git a/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py b/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py new file mode 100644 index 00000000..b9f74584 --- /dev/null +++ b/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py @@ -0,0 +1,6 @@ +from net.imglib2.img.planar import PlanarImgs +from net.imglib2.img.display.imagej import ImageJFunctions + +img = PlanarImgs.bytes(303, 302) +ImageJFunctions.show(img, "img via ImageJFunctions") +# NB: ImageJFunctions internally convert to ImagePlus \ No newline at end of file diff --git a/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py new file mode 100644 index 00000000..4327e198 --- /dev/null +++ b/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py @@ -0,0 +1,6 @@ +#@ UIService ui + +from net.imglib2.img.planar import PlanarImgs + +img = PlanarImgs.bytes(303, 302) +ui.show("img via UIService", img) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/list_known_image_windows.py b/src/test/fiji/macros/jython/list_known_image_windows.py new file mode 100644 index 00000000..325b0468 --- /dev/null +++ b/src/test/fiji/macros/jython/list_known_image_windows.py @@ -0,0 +1,6 @@ +from ij import WindowManager + +print("----") +for s in WindowManager.getImageTitles(): + print(s) +print("----") \ No newline at end of file diff --git a/src/test/fiji/macros/jython/test_ImagePlus_presence.py b/src/test/fiji/macros/jython/test_ImagePlus_presence.py new file mode 100644 index 00000000..644ab509 --- /dev/null +++ b/src/test/fiji/macros/jython/test_ImagePlus_presence.py @@ -0,0 +1,3 @@ +#@ ImagePlus imp + +print("image "+imp.getTitle()+" of width x height: "+str(imp.getWidth())+'x'+str(imp.getHeight())) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/test_dataset_presence.py b/src/test/fiji/macros/jython/test_dataset_presence.py new file mode 100644 index 00000000..649431e0 --- /dev/null +++ b/src/test/fiji/macros/jython/test_dataset_presence.py @@ -0,0 +1,3 @@ +#@ Dataset dimg + +print("image "+dimg.getImgPlus().getName()+" of width x height: "+str(dimg.getWidth())+'x'+str(dimg.getHeight())) \ No newline at end of file From 4056b1ee008fc5fb21b04debb1048efd2c21dd47 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 19:32:46 +0100 Subject: [PATCH 2/9] ADD: test* (and reporting) scripts report also... ...python id (alike mem address) of the images --- src/test/fiji/macros/jython/test_ImagePlus_presence.py | 2 +- src/test/fiji/macros/jython/test_dataset_presence.py | 3 ++- 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/test/fiji/macros/jython/test_ImagePlus_presence.py b/src/test/fiji/macros/jython/test_ImagePlus_presence.py index 644ab509..6f8b79b9 100644 --- a/src/test/fiji/macros/jython/test_ImagePlus_presence.py +++ b/src/test/fiji/macros/jython/test_ImagePlus_presence.py @@ -1,3 +1,3 @@ #@ ImagePlus imp -print("image "+imp.getTitle()+" of width x height: "+str(imp.getWidth())+'x'+str(imp.getHeight())) \ No newline at end of file +print("image '"+imp.getTitle()+"' (id: "+str(id(imp))+") of width x height: "+str(imp.getWidth())+'x'+str(imp.getHeight())) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/test_dataset_presence.py b/src/test/fiji/macros/jython/test_dataset_presence.py index 649431e0..1f7bee4b 100644 --- a/src/test/fiji/macros/jython/test_dataset_presence.py +++ b/src/test/fiji/macros/jython/test_dataset_presence.py @@ -1,3 +1,4 @@ #@ Dataset dimg -print("image "+dimg.getImgPlus().getName()+" of width x height: "+str(dimg.getWidth())+'x'+str(dimg.getHeight())) \ No newline at end of file +print("image '"+dimg.getImgPlus().getName()+"' (id: "+str(id(dimg))+\ + ") of width x height: "+str(dimg.getWidth())+'x'+str(dimg.getHeight())) \ No newline at end of file From 5b4e7f41b906a1f0f700d804fd78b3c632d15d13 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 19:37:22 +0100 Subject: [PATCH 3/9] ADD: test/fiji/../jython/list_known_datasets via the DatasetService --- src/test/fiji/macros/jython/list_known_datasets.py | 7 +++++++ 1 file changed, 7 insertions(+) create mode 100644 src/test/fiji/macros/jython/list_known_datasets.py diff --git a/src/test/fiji/macros/jython/list_known_datasets.py b/src/test/fiji/macros/jython/list_known_datasets.py new file mode 100644 index 00000000..f2677407 --- /dev/null +++ b/src/test/fiji/macros/jython/list_known_datasets.py @@ -0,0 +1,7 @@ +#@ DatasetService ds + +print("----") +for dimg in ds.getDatasets(): + print("image '"+dimg.getImgPlus().getName()+"' (id: "+str(id(dimg))+\ + ") of width x height: "+str(dimg.getWidth())+'x'+str(dimg.getHeight())) +print("----") \ No newline at end of file From 7ab0988e5267e8e85d4581bc335ee97a5ee122f8 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 19:48:06 +0100 Subject: [PATCH 4/9] CHG: test/fiji/../jython/create* report id (address) of the created images --- src/test/fiji/macros/jython/create_ImagePlus_and_show.py | 4 +++- .../macros/jython/create_img_and_show_via_ImageJFunctions.py | 4 +++- .../fiji/macros/jython/create_img_and_show_via_UIService.py | 4 +++- 3 files changed, 9 insertions(+), 3 deletions(-) diff --git a/src/test/fiji/macros/jython/create_ImagePlus_and_show.py b/src/test/fiji/macros/jython/create_ImagePlus_and_show.py index 7c08db74..c8b4189b 100644 --- a/src/test/fiji/macros/jython/create_ImagePlus_and_show.py +++ b/src/test/fiji/macros/jython/create_ImagePlus_and_show.py @@ -3,4 +3,6 @@ p = ByteProcessor(303, 302) imp = ImagePlus("pure ImagePlus", p) -imp.show() \ No newline at end of file +imp.show() + +print("Created image of id: "+str(id(imp))) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py b/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py index b9f74584..19e9bfc9 100644 --- a/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py +++ b/src/test/fiji/macros/jython/create_img_and_show_via_ImageJFunctions.py @@ -3,4 +3,6 @@ img = PlanarImgs.bytes(303, 302) ImageJFunctions.show(img, "img via ImageJFunctions") -# NB: ImageJFunctions internally convert to ImagePlus \ No newline at end of file +# NB: ImageJFunctions internally create new ImagePlus around VirtualStack that's wrapping around this 'img' + +print("Created image of id: "+str(id(img))) \ No newline at end of file diff --git a/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py index 4327e198..2a16d142 100644 --- a/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py +++ b/src/test/fiji/macros/jython/create_img_and_show_via_UIService.py @@ -3,4 +3,6 @@ from net.imglib2.img.planar import PlanarImgs img = PlanarImgs.bytes(303, 302) -ui.show("img via UIService", img) \ No newline at end of file +ui.show("img via UIService", img) + +print("Created image of id: "+str(id(img))) \ No newline at end of file From a1711d22a070dea3c31f4c9a7c6bd4800fd71b49 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 19:48:43 +0100 Subject: [PATCH 5/9] COS: create_Dataset more verbose with fine-grained control --- .../create_Dataset_and_show_via_UIService.py | 19 ++++++++++++++----- 1 file changed, 14 insertions(+), 5 deletions(-) diff --git a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py index 26da5a43..74c846ab 100644 --- a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py +++ b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py @@ -1,12 +1,21 @@ -#@ Context ctx +#@ DatasetService dsService +#@ ObjectService objService #@ UIService ui +#@ boolean display_image (label="Display the created image:") +#@ string image_label (label="Title of the created image:", value="ImgPlus turned into Dataset #1") from net.imagej import ImgPlus from net.imglib2.img.planar import PlanarImgs -from net.imagej import DefaultDataset img = PlanarImgs.bytes(303, 302) -imp = ImgPlus(img, "ImgPlus turned into Dataset") -ds = DefaultDataset(ctx, imp) +imp = ImgPlus(img, image_label) +ds = dsService.create(imp) -ui.show(ds) \ No newline at end of file +print("Created img of id: "+str(id(img))) +print("Created ImgPlus of id: "+str(id(imp))) +print("Created dataset of id: "+str(id(ds))) + +if display_image: + ui.show(ds) +else: + objService.addObject(ds) \ No newline at end of file From c38f9cc33aba25fdb0f3cf86de9e07627fe696d1 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 23:06:01 +0100 Subject: [PATCH 6/9] FIX: create_Dataset demonstrates good use of a Dataset... ...when it is displayed in BDV, and yet emulating as if it was displayed with the standard IJ1 windows. The trick is in using the Dataset#incrementReferences() and decrementReferences(). Indeed, when references count drops to zero, the Dataset is released and no longer available anywhere in Fiji. --- .../create_Dataset_and_show_via_UIService.py | 24 +++++++++++++++++-- 1 file changed, 22 insertions(+), 2 deletions(-) diff --git a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py index 74c846ab..a61766f2 100644 --- a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py +++ b/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py @@ -1,12 +1,13 @@ #@ DatasetService dsService -#@ ObjectService objService #@ UIService ui #@ boolean display_image (label="Display the created image:") #@ string image_label (label="Title of the created image:", value="ImgPlus turned into Dataset #1") from net.imagej import ImgPlus from net.imglib2.img.planar import PlanarImgs +from bdv.util import BdvFunctions +# ------- standard creation of `img` and wrapping it "till it becomes" a `Dataset` ------- img = PlanarImgs.bytes(303, 302) imp = ImgPlus(img, image_label) ds = dsService.create(imp) @@ -15,7 +16,26 @@ print("Created ImgPlus of id: "+str(id(imp))) print("Created dataset of id: "+str(id(ds))) + +# ------- this is a Jython version of attaching a listener to "onWindowClose" event in BDV ------- +from java.awt.event import WindowAdapter + +def register_datasetDecrement_onBdvCloseWindow(bdv_handle, dataset): + viewer_panel = bdv_handle.getViewerPanel() + win = viewer_panel.getRootPane().getParent() + + class OnClose(WindowAdapter): + def windowClosed(self, event): + print("BDV window closed, decrementing id: "+str(id(dataset))) + dataset.decrementReferences() + + win.addWindowListener(OnClose()) + + +# ------- finally show the `Dataset` either way(s) ------- if display_image: ui.show(ds) else: - objService.addObject(ds) \ No newline at end of file + ds.incrementReferences() + bdv_stack_source = BdvFunctions.show(ds.getImgPlus(), image_label) + register_datasetDecrement_onBdvCloseWindow(bdv_stack_source.getBdvHandle(), ds) From bacae60c3d4723627ded2b0f8f8e389a7147e181 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Thu, 26 Feb 2026 23:11:03 +0100 Subject: [PATCH 7/9] COS: create_Dataset can display as IJ1 or BDV or both or not-display --- ...d_show_via_UIService.py => create_Dataset_and_show.py} | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) rename src/test/fiji/macros/jython/{create_Dataset_and_show_via_UIService.py => create_Dataset_and_show.py} (89%) diff --git a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py b/src/test/fiji/macros/jython/create_Dataset_and_show.py similarity index 89% rename from src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py rename to src/test/fiji/macros/jython/create_Dataset_and_show.py index a61766f2..2f1d5380 100644 --- a/src/test/fiji/macros/jython/create_Dataset_and_show_via_UIService.py +++ b/src/test/fiji/macros/jython/create_Dataset_and_show.py @@ -1,7 +1,8 @@ #@ DatasetService dsService #@ UIService ui -#@ boolean display_image (label="Display the created image:") #@ string image_label (label="Title of the created image:", value="ImgPlus turned into Dataset #1") +#@ boolean show_in_IJ (label="Display in ImageJ window:") +#@ boolean show_in_BDV (label="Display in BigDataViewer:") from net.imagej import ImgPlus from net.imglib2.img.planar import PlanarImgs @@ -33,9 +34,10 @@ def windowClosed(self, event): # ------- finally show the `Dataset` either way(s) ------- -if display_image: +if show_in_IJ: ui.show(ds) -else: + +if show_in_BDV: ds.incrementReferences() bdv_stack_source = BdvFunctions.show(ds.getImgPlus(), image_label) register_datasetDecrement_onBdvCloseWindow(bdv_stack_source.getBdvHandle(), ds) From a3069b976dd250dda65594ff0c034f08082d0f46 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Vladim=C3=ADr=20Ulman?= Date: Fri, 27 Feb 2026 00:12:13 +0100 Subject: [PATCH 8/9] ADD: WIP of README.md --- src/test/fiji/macros/jython/README.md | 66 +++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) create mode 100644 src/test/fiji/macros/jython/README.md diff --git a/src/test/fiji/macros/jython/README.md b/src/test/fiji/macros/jython/README.md new file mode 100644 index 00000000..bbabbd94 --- /dev/null +++ b/src/test/fiji/macros/jython/README.md @@ -0,0 +1,66 @@ +remarks: + + +# Use `Dataset` over `ImagePlus` in scripts + +This refers to the situation when a Jython script has (among others) this input: + +```python +#@ ImagePlus imp + +def somework_over_imageplus(imp): + pass +``` + +Such script will also work with pure `Datasets`, in which case +Fiji (most likely the ImageJ2-flavour of it) will internally prepare +a new `ImagePlus` that is wrapped around the `Dataset`, and *increases its +internal usage counter*. In fact, `ImageJFunctions.wrap()` does similar thing: +It also constructs a new `ImagePlus` that's around a virtual stack that's +around the given Imglib2's `img`. Coming back to the script example, the +auto-conversion happens only once, that said, executing the script again +will re-use the previously constructed `ImagePlus`. + +If ever a "system" auto-conversion of a `Dataset` to `ImagePlus` has taken +place, the created `ImagePlus` *blocks* releasing/freeing of the `Dataset`, +and one has to call `ImagePlus.close()` or similar function to effectively +loose this `ImagePlus` that in turn will unlock (*decrease the internal +usage counter*) the `Dataset`. **However**, if `ImagePlus` is created from +a `Dataset` (e.g., see below), this new `ImagePlus` is not blocking anything. + +Take home message for script programmers is to design their Zarr-expecting +scripts to consume + +```python +#@ Dataset dimg +``` + +and possibly build `ImagePlus` (if they really need it) in their script. +They could explicitly `ImagePlus.close()` before the end of their script, +but it doesn't seem to be any important. + +## Example + +```python +#@ Dataset dimg + +from net.imglib2.img.display.imagej import ImageJFunctions + +def get_imageplus(dataset): + img = dataset.getImgPlus() + return ImageJFunctions.wrap(img, img.getName()) + + +def somework_over_imageplus(imp): + # 2D Python-native array mapped over pixels from the current xy plane + pixels = imp.getProcessor().getPixels() + pass + + +imp = get_imageplus(dimg) +somework_over_imageplus(imp) + +# optional clean up: +imp.close() +``` + From 05894c8273bf8db686ebb338e9b69dd0bec22bdc Mon Sep 17 00:00:00 2001 From: hahmann Date: Thu, 11 Jun 2026 14:01:23 +0200 Subject: [PATCH 9/9] List pyramidal datasets alongside known datasets in Jython test script. --- src/test/fiji/macros/jython/list_known_datasets.py | 7 ------- .../jython/list_known_datasets_and_pyramidals.py | 10 ++++++++++ 2 files changed, 10 insertions(+), 7 deletions(-) delete mode 100644 src/test/fiji/macros/jython/list_known_datasets.py create mode 100644 src/test/fiji/macros/jython/list_known_datasets_and_pyramidals.py diff --git a/src/test/fiji/macros/jython/list_known_datasets.py b/src/test/fiji/macros/jython/list_known_datasets.py deleted file mode 100644 index f2677407..00000000 --- a/src/test/fiji/macros/jython/list_known_datasets.py +++ /dev/null @@ -1,7 +0,0 @@ -#@ DatasetService ds - -print("----") -for dimg in ds.getDatasets(): - print("image '"+dimg.getImgPlus().getName()+"' (id: "+str(id(dimg))+\ - ") of width x height: "+str(dimg.getWidth())+'x'+str(dimg.getHeight())) -print("----") \ No newline at end of file diff --git a/src/test/fiji/macros/jython/list_known_datasets_and_pyramidals.py b/src/test/fiji/macros/jython/list_known_datasets_and_pyramidals.py new file mode 100644 index 00000000..f4aaa9db --- /dev/null +++ b/src/test/fiji/macros/jython/list_known_datasets_and_pyramidals.py @@ -0,0 +1,10 @@ +# @ DatasetService ds +# @ PyramidalService ps + +print("----") +for dimg in ds.getDatasets(): + print("image '" + dimg.getImgPlus().getName() + "' (id: " + str(id(dimg)) + \ + ") of width x height: " + str(dimg.getWidth()) + 'x' + str(dimg.getHeight())) +for pyramidal in ps.getPyramidals(): + print("pyramidal '" + pyramidal.getPyramidName() + "' (id: " + str(id(pyramidal)) + ")") +print("----")