imagej / pyimagej

Use ImageJ from Python
https://pyimagej.readthedocs.io/
Other
473 stars 82 forks source link

imp.show() not working for Bio-format loaded .tif stacks #271

Closed AdamDHines closed 1 year ago

AdamDHines commented 1 year ago

I am writing a Python script that uses a GUI to open and run ImageJ with FIJI plugins to automate some data analysis with some difficulties interacting with the Bio-formats plugin. I am using MacOS Ventura 13.4 with Apple silicon, so hoping to develop all of this headless - particularly for end-user deployment purposes.

Following from issue #97, I was able to get some .tif stacks loaded and set as the current active image successfully and run a macro on it.

import imagej  
ij = image.init()
ij.ui().showUI()
img = ij.io().open('/data/some_tif.tif')
imp = ij.py.to_imageplus(img)
imp.show()
ij.py.run_macro(some_macro,args)
print('Macro run successfully')
[INFO] some_tif.tif = net.imagej.display.DefaultDatasetView@120350eb
[java.lang.Enum.toString] [INFO] some_tif.tif = net.imagej.display.DefaultDatasetView@120350eb
Macro run successfully

I have noticed a difference between .tif stacks loaded using ij.io().open() when ImageJ is initialized using ij = imagej.init('sc.fiji:fiji:2.5.0') . When the Bio-formats plugin is used to load a .tif stack, you lose the ability to call imp.show() to activate the current image to run macros.

Running this;

import imagej  
ij = image.init('sc.fiji:fiji:2.5.0')
ij.ui().showUI()
img = ij.io().open('/data/some_tif.tif')
imp = ij.py.to_imageplus(img)
imp.show()
ij.py.run_macro(some_macro,args)
print('Macro run successfully')

Results in the following error at imp.show()

Traceback (most recent call last):
  File "ImagePlus.java", line 479, in ij.ImagePlus.show
Exception: Java Exception

Running the dumpstate() function as defined in #97 for ij = imagej.init() after imp.show() case and ij = imagej.init('sc.fiji:fiji:2.5.0') without imp.show() returns;

# ij = imagej.init()

Active ImagePlus = img["some.tif.tif" (-2), 16-bit, 821x353x1x1x1600] ([821, 353, 1, 1, 1600]
Registered image IDs = [-2]

# ij = imagej.init('sc.fiji:fiji:2.5.0')

There are no images open[java.lang.Enum.toString] There are no images open

This is not ideal, since I would like to access plugins in the Fiji distribution. A workaround mentioned here where we point to a local install of Fiji ij = imagej.init('/Applications/Fiji.app') also does not work. It appears to be some fundamental difference in the way files are loaded between Bio-formats and ImageJ2. It would also be very ideal to use Bio-formats so that the user doesn't have to use a specific file type to run the software.

Trying the legacy importer img = ij.IJ.openImage('/data/some_tif.tif') also does not work with imp.show() when ImageJ is initialized with ij = imagej.init('sc.fiji:fiji:2.5.0').

Initializing ImageJ with ij = imagej.init('sc.fiji:fiji') also yields the same result, so it doesn't appear to be the Fiji version being loaded that's causing the problem.

Thanks in advance, would appreciate any help or advice on this.

Package versions

python 3.9.16
pyimagej 1.4.1
imglyb 2.1.0
jgo 1.0.4
jpype 1.4.1
labelling 0.1.12
matplotlib 3.7.1
numpy 1.24.3
scyjava 1.9.0
xarray 2023.5.0
ctrueden commented 1 year ago

@AdamDHines Can you please try sc.fiji:fiji:2.13.1 instead of sc.fiji:fiji:2.5.0? There is a bug in older versions of ImageJ2, which has since been fixed, relating to detection of open ImagePlus objects while running headless.

AdamDHines commented 1 year ago

Thanks for the reply, @ctrueden. This worked for me and I can now successfully run plugins. Should this be be updated in the documentation for headless operation since the examples that explain how to initialize Imagej2 all use sc.fiji:fiji:2.5.0? I'm happy to contribute.

ctrueden commented 1 year ago

@AdamDHines Glad to hear it worked! Yes, we want to update the docs to reference version 2.13.1 over 2.5.0, since it fixes some issues. Your offer of help is much appreciated. I would take you up on it, but it looks like @elevans already started working on it. We will also remove references to the obsolete "HACK: Work around ImagePlus#show() failure", since that trick is no longer necessary with newer ImageJ2 versions. Do you see other areas where the documentation could be improved?

One area where we could use some help is with improving PyImageJ's support for Java GUI elements on macOS. There are some open issues about it, and unfinished effort to enable ImageJ GUI support for use cases like napari-imagej, but no one on the LOCI team currently has time to dig into it further. I don't suppose you happen to know much about the macOS main thread event loop, and how it interacts with Python and/or Java programs, do you? :laughing:

AdamDHines commented 1 year ago

Great! I don't have any experience with MacOS main thread loops unfortunately, I'm new to Apple myself and everything I did before was on Windows.

An improvement on the documentation I could see is a working script example for writing and running a headless with macros that use plugins. The only one that exists to my knowledge needs Xvfb to work - blob detection. I don't know if this would be best suited under the 'Use cases' contents or if MacOS development should have its own tutorial notebook.

Again, happy to contribute a basic working script example after I've had a little more time to play around with it.

elevans commented 1 year ago

Hi @AdamDHines I'm planning to create a PR for the 2.5.0 -> 2.13.1 version update in our docs (as well as some other fixes and cleanup) by the end of this week. I totally agree with you in that we need more examples with running headless with macros. If you have the time to whip something up for the PR that would be great. But no worries if you don't, I'd still like to incorporate whatever example you have. We can host your example data as well so that we don't bloat the repo.

The relevant branch is: update-docs-layout-and-content

AdamDHines commented 1 year ago

Sounds good, @elevans. I can have a look at this on the weekend, putting together a minimal code working example wouldn't take me very long. I'll fork the repo and reply here when I've finished something.

AdamDHines commented 1 year ago

I just created PR from my fork that includes a couple of working headless macros scripts into the Headless.md document. Happy for you to host the example .tif stack.