Opening czi files with Jython

bio-formats
jython
imagej
python

#1

Dear all,

I would like to be able to open czi files in a python script. I’m trying with Jython in macro window from FIJI with pieces of code taken from http://wiki.cmci.info/documents/120206pyip_cooking/python_imagej_cookbook

Code:

from ij.io import TiffDecoder
from ij.io import OpenDialog
from loci.plugins import BF

op = OpenDialog("Choose Track Data...", "")
src = op.getDirectory()+ op.getFileName()
imps = BF.openImagePlus(src)
imp4 = imps[0] 

I get this error:

console: Failed to install '': java.nio.charset.UnsupportedCharsetException: cp0.

I searched for it but I only found issues related to Java package through python, and Java is beyond my knowledge.

What I want to achieve is open with Python czi files and get the metadata and the array of the file to do some processing on it. So I thought to use FIJI bioformat for that.

I tried to install python-bioformat but I didn’t get how to install it through conda or pip properly.

I also have been checking this code: https://www.lfd.uci.edu/~gohlke/code/czifile.py.html
But it raises an error related to the loading of memmap.

I’m using Windows 10, with anaconda environment python 2.7 and spyder.

Is there any way to do this in python directly?
If not, how can I use FIJI to make easier this task and store a variable in python with the array (and the metadata if possible)?

Whatever help will be welcome,

if I forgot to write down some crucial information please let me know.

Thanks


#2

Dear @malj390,

take a look at this illustrative example on how to access information with Jython using the Bio-Formats API:

Please keep in mind that Fiji uses Jython, not vanilla Python. If you want to stick to Python, you will have to take another look at python-bioformats. As far as I know, it is not available via conda, so you will have to use pip to install it…

Best,
Stefan


#3

Thank you @stelfrich,

I’m trying first with bioformats (both ways locally and non locally) but have this error.

(py27) C:\Users\User>pip install python-bioformats-1.3.2.tar.gz
Processing c:\users\User\python-bioformats-1.3.2.tar.gz
Requirement already satisfied: future in c:\users\User\anaconda2\envs\py27\lib\site-packages (from python-bioformats==1.3.2)
Collecting javabridge>=1.0 (from python-bioformats==1.3.2)
  Using cached javabridge-1.0.15.tar.gz
    Complete output from command python setup.py egg_info:
    No handlers could be found for logger "locate"
    Traceback (most recent call last):
      File "<string>", line 1, in <module>
      File "c:\users\User\appdata\local\temp\pip-build-e9_4wk\javabridge\setup.py", line 393, in <module>
        ext_modules=ext_modules(),
      File "c:\users\User\appdata\local\temp\pip-build-e9_4wk\javabridge\setup.py", line 95, in ext_modules
        raise Exception("JVM not found")
    Exception: JVM not found

    ----------------------------------------
Command "python setup.py egg_info" failed with error code 1 in c:\users\User\appdata\local\temp\pip-build-e9_4wk\javabridge\

(py27) C:\Users\User>pip install javabridge
Requirement already satisfied: javabridge in c:\users\User\anaconda2\envs\py27\lib\site-packages
Requirement already satisfied: numpy in c:\users\User\anaconda2\envs\py27\lib\site-packages (from javabridge)

Thanks


#4

Do you have a JDK installed on your machine? If so, do you have $JAVA_HOME set to that path?


#5

After set up the environtments variable in Windows:
I put these ones:

C:\Users\User\Anaconda2\envs\py27\Lib\site-packages\javabridge
C:\Users\User\Anaconda2\envs\py27\Lib\site-packages\javabridge-1.0.14_1_gdf84bf8-py2.7.egg-info
C:\Program Files\Java\jre1.8.0_162\bin\javaw.exe

And I tried to install again python-bioformats.

Collecting python-bioformats
Requirement already satisfied: future in c:\users\User\anaconda2\envs\py27\lib\site-packages (from python-bioformats)
Collecting javabridge>=1.0 (from python-bioformats)
  Using cached javabridge-1.0.15.tar.gz
Requirement already satisfied: numpy in c:\users\User\anaconda2\envs\py27\lib\site-packages (from javabridge>=1.0->python-bioformats)
Building wheels for collected packages: javabridge
  Running setup.py bdist_wheel for javabridge ... error
  Complete output from command C:\Users\User\Anaconda2\envs\py27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\User\\appdata\\local\\temp\\pip-build-jg8f6i\\javabridge\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" bdist_wheel -d c:\users\User\appdata\local\temp\tmpknp6vlpip-wheel- --python-tag cp27:
  running bdist_wheel
  running build
  running build_py
  creating build
  creating build\lib.win-amd64-2.7
  creating build\lib.win-amd64-2.7\javabridge
  copying javabridge\jutil.py -> build\lib.win-amd64-2.7\javabridge
  copying javabridge\locate.py -> build\lib.win-amd64-2.7\javabridge
  copying javabridge\noseplugin.py -> build\lib.win-amd64-2.7\javabridge
  copying javabridge\wrappers.py -> build\lib.win-amd64-2.7\javabridge
  copying javabridge\_version.py -> build\lib.win-amd64-2.7\javabridge
  copying javabridge\__init__.py -> build\lib.win-amd64-2.7\javabridge
  creating build\lib.win-amd64-2.7\javabridge\tests
  copying javabridge\tests\test_cpython.py -> build\lib.win-amd64-2.7\javabridge\tests
  copying javabridge\tests\test_javabridge.py -> build\lib.win-amd64-2.7\javabridge\tests
  copying javabridge\tests\test_jutil.py -> build\lib.win-amd64-2.7\javabridge\tests
  copying javabridge\tests\test_wrappers.py -> build\lib.win-amd64-2.7\javabridge\tests
  copying javabridge\tests\__init__.py -> build\lib.win-amd64-2.7\javabridge\tests
  creating build\lib.win-amd64-2.7\javabridge\jars
  copying javabridge\jars\cpython.jar -> build\lib.win-amd64-2.7\javabridge\jars
  copying javabridge\jars\rhino-1.7R4.jar -> build\lib.win-amd64-2.7\javabridge\jars
  copying javabridge\jars\runnablequeue.jar -> build\lib.win-amd64-2.7\javabridge\jars
  copying javabridge\jars\test.jar -> build\lib.win-amd64-2.7\javabridge\jars
  running build_ext
  C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\runnablequeue\RunnableQueue.java
  warning: [options] bootstrap class path not set in conjunction with -source 1.6
  1 warning
  C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\test\RealRect.java
  warning: [options] bootstrap class path not set in conjunction with -source 1.6
  1 warning
  C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPython.java c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPythonInvocationHandler.java
  warning: [options] bootstrap class path not set in conjunction with -source 1.6
  Note: c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPythonInvocationHandler.java uses unchecked or unsafe operations.
  Note: Recompile with -Xlint:unchecked for details.
  1 warning
  building 'javabridge._javabridge' extension
  error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

  ----------------------------------------
  Failed building wheel for javabridge
  Running setup.py clean for javabridge
Failed to build javabridge
Installing collected packages: javabridge, python-bioformats
  Found existing installation: javabridge 1.0.14-1-gdf84bf8
    Uninstalling javabridge-1.0.14-1-gdf84bf8:
      Successfully uninstalled javabridge-1.0.14-1-gdf84bf8
  Running setup.py install for javabridge ... error
    Complete output from command C:\Users\User\Anaconda2\envs\py27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\User\\appdata\\local\\temp\\pip-build-jg8f6i\\javabridge\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\User\appdata\local\temp\pip-lozeyp-record\install-record.txt --single-version-externally-managed --compile:
    running install
    running build
    running build_py
    creating build
    creating build\lib.win-amd64-2.7
    creating build\lib.win-amd64-2.7\javabridge
    copying javabridge\jutil.py -> build\lib.win-amd64-2.7\javabridge
    copying javabridge\locate.py -> build\lib.win-amd64-2.7\javabridge
    copying javabridge\noseplugin.py -> build\lib.win-amd64-2.7\javabridge
    copying javabridge\wrappers.py -> build\lib.win-amd64-2.7\javabridge
    copying javabridge\_version.py -> build\lib.win-amd64-2.7\javabridge
    copying javabridge\__init__.py -> build\lib.win-amd64-2.7\javabridge
    creating build\lib.win-amd64-2.7\javabridge\tests
    copying javabridge\tests\test_cpython.py -> build\lib.win-amd64-2.7\javabridge\tests
    copying javabridge\tests\test_javabridge.py -> build\lib.win-amd64-2.7\javabridge\tests
    copying javabridge\tests\test_jutil.py -> build\lib.win-amd64-2.7\javabridge\tests
    copying javabridge\tests\test_wrappers.py -> build\lib.win-amd64-2.7\javabridge\tests
    copying javabridge\tests\__init__.py -> build\lib.win-amd64-2.7\javabridge\tests
    creating build\lib.win-amd64-2.7\javabridge\jars
    copying javabridge\jars\cpython.jar -> build\lib.win-amd64-2.7\javabridge\jars
    copying javabridge\jars\rhino-1.7R4.jar -> build\lib.win-amd64-2.7\javabridge\jars
    copying javabridge\jars\runnablequeue.jar -> build\lib.win-amd64-2.7\javabridge\jars
    copying javabridge\jars\test.jar -> build\lib.win-amd64-2.7\javabridge\jars
    running build_ext
    C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\runnablequeue\RunnableQueue.java
    warning: [options] bootstrap class path not set in conjunction with -source 1.6
    1 warning
    C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\test\RealRect.java
    warning: [options] bootstrap class path not set in conjunction with -source 1.6
    1 warning
    C:\Program Files\Java\jdk1.8.0_162\bin\javac.exe -source 6 -target 6 c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPython.java c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPythonInvocationHandler.java
    warning: [options] bootstrap class path not set in conjunction with -source 1.6
    Note: c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\java\org\cellprofiler\javabridge\CPythonInvocationHandler.java uses unchecked or unsafe operations.
    Note: Recompile with -Xlint:unchecked for details.
    1 warning
    building 'javabridge._javabridge' extension
    error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

    ----------------------------------------
  Rolling back uninstall of javabridge
Command "C:\Users\User\Anaconda2\envs\py27\python.exe -u -c "import setuptools, tokenize;__file__='c:\\users\\User\\appdata\\local\\temp\\pip-build-jg8f6i\\javabridge\\setup.py';f=getattr(tokenize, 'open', open)(__file__);code=f.read().replace('\r\n', '\n');f.close();exec(compile(code, __file__, 'exec'))" install --record c:\users\User\appdata\local\temp\pip-lozeyp-record\install-record.txt --single-version-externally-managed --compile" failed with error code 1 in c:\users\User\appdata\local\temp\pip-build-jg8f6i\javabridge\

It didn’t work because there was a problem with Javabridge so I proceed to install it and this is the output.

(py27) C:\Users\User>pip install javabridge
Requirement already satisfied: javabridge in c:\users\User\anaconda2\envs\py27\lib\site-packages
Requirement already satisfied: numpy in c:\users\User\anaconda2\envs\py27\lib\site-packages (from javabridge)

Thank you in advance.

Maybe I didn’t understand well about $JAVA_HOME.
Where should I set up this JAVA_HOME, in Java configuration? or Windows?


#6

Sorry I should pay more attention to the raised errors.

 error: Microsoft Visual C++ 9.0 is required. Get it from http://aka.ms/vcpython27

The problem was solved installing Microsoft Visual C++ Compiler for Python 2.7.

Thank you


#7

Here is some documentation on the installation of python-javabridge: http://pythonhosted.org/javabridge/installation.html

You need a C compiler to build some components. You can get it from the link: http://aka.ms/vcpython27