CellProfiler / python-bioformats

Read and write life sciences file formats
Other
131 stars 46 forks source link

Trying to save image results in JavaException #31

Open jakirkham opened 9 years ago

jakirkham commented 9 years ago

I tried to do the following, but ran into the exception below. I think this line ( https://github.com/CellProfiler/python-bioformats/blob/master/bioformats/formatwriter.py#L94 ) may be suspect. However, I could be wrong.

>>> import javabridge
>>> javabridge.start_vm()
>>> import numpy as np
>>> import bioformats
>>> b = (np.random.random((110,120)) * ( np.iinfo(np.int16).max - np.iinfo(np.int16).min ) + np.iinfo(np.int16).min).astype(np.int16)
>>> bioformats.formatwriter.write_image("test.tiff", b, bioformats.omexml.PT_INT16)
Exception in thread "Thread-1" org.mozilla.javascript.EvaluatorException: Function importClass must be called with a class; had "[JavaPackage loci.formats.services.OMEXMLService]" instead. (<java-python-bridge>#1)
    at org.mozilla.javascript.DefaultErrorReporter.runtimeError(DefaultErrorReporter.java:77)
    at org.mozilla.javascript.Context.reportRuntimeError(Context.java:913)
    at org.mozilla.javascript.Context.reportRuntimeError(Context.java:969)
    at org.mozilla.javascript.Context.reportRuntimeError1(Context.java:932)
    at org.mozilla.javascript.ImporterTopLevel.js_importClass(ImporterTopLevel.java:162)
    at org.mozilla.javascript.ImporterTopLevel.execIdCall(ImporterTopLevel.java:237)
    at org.mozilla.javascript.IdFunctionObject.call(IdFunctionObject.java:97)
    at org.mozilla.javascript.optimizer.OptRuntime.callName(OptRuntime.java:63)
    at org.mozilla.javascript.gen._java_python_bridge__1._c_script_0(<java-python-bridge>:1)
    at org.mozilla.javascript.gen._java_python_bridge__1.call(<java-python-bridge>)
    at org.mozilla.javascript.ContextFactory.doTopCall(ContextFactory.java:394)
    at org.mozilla.javascript.ScriptRuntime.doTopCall(ScriptRuntime.java:3091)
    at org.mozilla.javascript.gen._java_python_bridge__1.call(<java-python-bridge>)
    at org.mozilla.javascript.gen._java_python_bridge__1.exec(<java-python-bridge>)
    at org.mozilla.javascript.Context.evaluateString(Context.java:1079)
---------------------------------------------------------------------------
JavaException                             Traceback (most recent call last)
<ipython-input-10-a1c0e18e7b26> in <module>()
----> 1 bioformats.formatwriter.write_image("test.tiff", b, bioformats.omexml.PT_INT16)

/zopt/conda/envs/nanshenv/lib/python2.7/site-packages/bioformats/formatwriter.pyc in write_image(pathname, pixels, pixel_type, c, z, t, size_c, size_z, size_t, channel_names)
    108                           xml=xml,
    109                           index=index,
--> 110                           buffer=pixel_buffer))
    111 
    112 def convert_pixels_to_buffer(pixels, pixel_type):

/zopt/conda/envs/nanshenv/lib/python2.7/site-packages/javabridge-1.0.11-py2.7-macosx-10.5-x86_64.egg/javabridge/jutil.pyc in run_script(script, bindings_in, bindings_out, class_loader)
    352              "Ljava/lang/Object;)"
    353              "Ljava/lang/Object;",
--> 354              scope, script, "<java-python-bridge>", 0, None)
    355         result = unwrap_javascript(result)
    356         for k in list(bindings_out):

/zopt/conda/envs/nanshenv/lib/python2.7/site-packages/javabridge-1.0.11-py2.7-macosx-10.5-x86_64.egg/javabridge/jutil.pyc in call(o, method_name, sig, *args)
    853     ret_sig = sig[sig.find(')')+1:]
    854     nice_args = get_nice_args(args, args_sig)
--> 855     result = fn(*nice_args)
    856     x = env.exception_occurred()
    857     if x is not None:

/zopt/conda/envs/nanshenv/lib/python2.7/site-packages/javabridge-1.0.11-py2.7-macosx-10.5-x86_64.egg/javabridge/jutil.pyc in fn(*args)
    820             x = env.exception_occurred()
    821             if x is not None:
--> 822                 raise JavaException(x)
    823             return result
    824     else:

JavaException: Function importClass must be called with a class; had "[JavaPackage loci.formats.services.OMEXMLService]" instead. (<java-python-bridge>#1)
jakirkham commented 9 years ago

Oops, missed this class_path=bioformats.JARS when starting the VM.

jakirkham commented 9 years ago

After making that change, it tries to save, but seems to take a long time and never really completes. The file does appear, but is corrupted and I get some warnings from log4j. Here are the log messages I see. I don't think they should be the cause of any problems. It also seems this may be an upstream issue.

log4j:WARN No appenders could be found for logger (loci.common.services.ServiceFactory).
log4j:WARN Please initialize the log4j system properly.
log4j:WARN See http://logging.apache.org/log4j/1.2/faq.html#noconfig for more info.
LeeKamentsky commented 9 years ago

You're right about the log messages - not a big problem. I just tried:

import numpy as np
import javabridge
import bioformats
javabridge.start_vm(class_path=bioformats.JARS)
b = (np.random.random((110,120)) * ( np.iinfo(np.int16).max - np.iinfo(np.int16).min ) + np.iinfo(np.int16).min).astype(np.int16)
bioformats.formatwriter.write_image("c:/temp/test.tiff", b, bioformats.omexml.PT_INT16)

and got image

I can't imagine that you'd run into memory problems. I tested it on Windows 7, Python 2.7, Bioformats 1.0.5, javabridge 1.0.11 and numpy 1.8.2. What versions are you running?

jakirkham commented 9 years ago

Version information.

$ conda list 
# packages in environment at /zopt/conda/envs/bioformatsenv:
#
certifi                   14.05.14                 py27_0    defaults
cloog                     0.18.0                        0    defaults
gmp                       5.1.2                         6    defaults
ipython                   3.2.1.9+ccf11df          py27_0    file:///zopt/conda/conda-bld/osx-64/ipython-3.2.1.9+ccf11df-py27_0.tar.bz2
isl                       0.12.2                        1    defaults
javabridge                1.0.11                 nppy27_0    file:///zopt/conda/conda-bld/osx-64/javabridge-1.0.11-nppy27_0.tar.bz2
jinja2                    2.8                      py27_0    defaults
jsonschema                2.4.0                    py27_0    defaults
libgcc                    4.8.5                         0    jakirkham
libsodium                 0.4.5                         0    defaults
markupsafe                0.23                     py27_0    defaults
mistune                   0.7.1                    py27_0    defaults
mpc                       1.0.1                         0    defaults
mpfr                      3.1.2                         0    defaults
nose                      1.3.7                    py27_0    defaults
numpy                     1.9.3                  py27_101  [openblas]  jakirkham
openblas                  0.2.14                      675    jakirkham
openssl                   1.0.1k                        1    <unknown>
pip                       7.1.2                    py27_0    defaults
ptyprocess                0.4                      py27_0    defaults
pygments                  2.0.2                    py27_0    defaults
python                    2.7.10                        1    defaults
python-bioformats         1.0.5                    py27_0    file:///zopt/conda/conda-bld/osx-64/python-bioformats-1.0.5-py27_0.tar.bz2
python.app                1.2                      py27_4    defaults
pyzmq                     14.7.0                   py27_0    defaults
readline                  6.2                           2    <unknown>
setuptools                18.3.2                   py27_0    defaults
sqlite                    3.8.4.1                       1    <unknown>
ssl_match_hostname        3.4.0.2                  py27_0    defaults
terminado                 0.5                      py27_0    defaults
tk                        8.5.18                        0    <unknown>
tornado                   4.2.1                    py27_0    jakirkham
wheel                     0.26.0                   py27_1    defaults
zeromq                    4.0.5                         0    defaults
zlib                      1.2.8                         0    <unknown>

Also, on Mac OS 10.9. Latest version of JRE (1.8.0), as well.

LeeKamentsky commented 9 years ago

All looks good. If you're running from source, can you execute:

python setup.py develop
python nosetests.py

I have it running 131 tests without errors after checking out the 1.0.5 tag.

jakirkham commented 9 years ago

Testing on both python-bioformats and python-javabridge is passing for me when using HEAD or the latest release. Both were installed in develop mode. With python-javabridge, I simply ran nosetests.

jakirkham commented 9 years ago

I tried to reproduce this on Linux and was unable to. The platforms tested were Ubuntu Precise inside a docker container and CentOS 6.3 native install on a cluster. This seems to be a problem only on Mac.

I also tried to downgrade Java 1.8 to 1.7, in which I was successful. However, this still did not resolve the problem.

More info: It appears to open a Java Application called "bin", after which it hangs. I hope this additional information is helpful.

Mackbit commented 7 years ago

@jakirkham Have you solved this issue yet? I have exactly the same problem when running on Mac.

jakirkham commented 7 years ago

Nope, haven't had the opportunity to play with this lately.