imagej / pyimagej

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

Add conversion support for index images to ImgLib2 and legacy ImageJ ROIs #283

Closed elevans closed 10 months ago

elevans commented 1 year ago

This PR adds support to convert index images (Python or Java) into either of the following:

  1. A net.imagej.roi.DefaultROITree populated with ImgLib2 ROIs.
  2. ImageJ RoiManager populated with legacy converted ROIs (e.g. net.imagej.legacy.convert.roi.polygon2d.Polygon2DWrapper).

Because there's no real way to tell the difference between an index image and some other type of image data (without making assumptions) I feel its best to not register these index image functions with scyjava/pyimagej converters.

Included in this PR:

To run the use case you'll need Cellpose and StarDist. This environment.yml file should do the trick:

name: cellstar
channels:
    - conda-forge
    - defaults
dependencies:
    - pyimagej
    - jupyterlab
    - openjdk=11
    - pip
    - pip:
        - tensorflow==2.12.*
        - stardist
        - cellpose

Don't forget to pull this branch and pip install -e . before running the notebook!

codecov[bot] commented 1 year ago

Codecov Report

Attention: 11 lines in your changes are missing coverage. Please review.

Comparison is base (c003095) 77.68% compared to head (f205852) 77.81%.

Files Patch % Lines
src/imagej/convert.py 64.00% 9 Missing :warning:
src/imagej/_java.py 86.66% 2 Missing :warning:
Additional details and impacted files ```diff @@ Coverage Diff @@ ## main #283 +/- ## ========================================== + Coverage 77.68% 77.81% +0.13% ========================================== Files 16 16 Lines 1949 2010 +61 ========================================== + Hits 1514 1564 +50 - Misses 435 446 +11 ```

:umbrella: View full report in Codecov by Sentry.
:loudspeaker: Have feedback on the report? Share it here.

hinerm commented 1 year ago

@elevans I created an environment with the indicated yml and pip install -e . but when I run the notebook the first cell fails with a dependency not found exception on StarDist.

When I paste the exact same line into a python interpreter launched in the exact same environment that started jupyter, it works no problem. :(

I noticed that I'm using python 3.8 locally and I think you used 3.11 when you made the ipynb. Is 3.11 necessary?

gselzer commented 1 year ago

@hinerm I ran using the following environment.yml:

name: cellstar
channels:
    - conda-forge
    - defaults
dependencies:
    - python=="3.8"
    - pyimagej
    - jupyterlab
    - openjdk=11
    - pip
    - pip:
        - tensorflow==2.12.*
        - stardist
        - cellpose

and things seem to work fine. How did you install python 3.8?

hinerm commented 1 year ago

How did you install python 3.8?

@gselzer it's just my system python, since it wasn't specified in the environment.yml specified.

I'm trying with your method of specification. Interestingly I earlier tried >=3.11 and environment creation failed with dependency conflicts.

Further interestingly, python=="3.8" syntax does not work on my computer. I have to remove the double quotes.

Edit: specifying python==3.8 did not work either. Still can't find StarDist

image

elevans commented 1 year ago

@hinerm I updated the jupyter notebook to use %pip install package magic per Curtis's recommendation. Would you mind giving the notebook a try again?

hinerm commented 1 year ago

@hinerm I updated the jupyter notebook to use %pip install package magic per Curtis's recommendation. Would you mind giving the notebook a try again?

Do I need to do anything different to install it? I assume if you use %pip install you need to trust the notebook?

I assume the environment file should now be:

name: cellstar
channels:
    - conda-forge
    - defaults
dependencies:
    - python=3.8
    - pyimagej
    - jupyterlab
    - openjdk=11
    - pip

Anyway, this doesn't work for me. It looks like the stardist has a dependency on csbdeep that is not compatible with tensorflow 2.12.

What version of stardist and csbdeep are you getting from pip? I think we want to pin a stardist version compatible with the chosen tensorflow on all OS's. Welcome to dependency hell.

image

elevans commented 10 months ago

@hinerm After many hours of painful testing across Linux, Windows and macOS I found the following shell commands to be the most successful for installing cellpose and stardist:

%%capture
!mamba install -y -c conda-forge tensorflow stardist
!pip install cellpose

I tested this on the following systems:

While testing on macOS, I got bit by the lack of interactive mode support. Bummer! So I included a check for macOS and switch to headless mode with some explanations.

elevans commented 10 months ago

I'm going to do something spicy :hot_pepper: and merge this branch. The actual new code in convert.py and the accompanying tests have already been reviewed since @hinerm had troubles with the Jupyter notebook. Letting mamba/conda resolve the tensorflow and stardist side of things while installing cellpose from PyPi and not conda-forge was the trick. Since then all I've done is update the cellpose jupyter notebook with shell commands, compatibility with macOS and extra notes.

ctrueden commented 10 months ago

Awesome, thanks @elevans!