Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replace loci_tools.jar with bioformats_package.jar #403

Open
wants to merge 8 commits into
base: main
Choose a base branch
from
2 changes: 1 addition & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ dist/*
pims.egg-info/*
PIMS.egg-info/*
pims/version.py
pims/loci_tools.jar
pims/bioformats_package.jar
examples/.ipynb_checkpoints
pims/tests/data/bulk-water.mov.pims_buffer
pims/tests/data/bulk-water.mov.pims_meta
Expand Down
20 changes: 10 additions & 10 deletions doc/source/bioformats.rst
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ Or, for windows users,
download the binary from `Christoph Gohlke's website <http://www.lfd.uci.edu/~gohlke/pythonlibs/#jpype>`_.

On first use of pims.Bioformats(filename), the required java library
:file:`loci_tools.jar` will be automatically downloaded from
:file:`bioformats_package.jar` will be automatically downloaded from
`openmicroscopy.org <http://downloads.openmicroscopy.org/bio-formats/>`__.

Special functions
Expand All @@ -44,7 +44,7 @@ switches between them:
.. code-block:: python

# open a multi-experiment file and read the first experiment
reader = pims.BioformatsReader('path/to/file', series=0)
reader = pims.bioformats.BioformatsReader('path/to/file', series=0)
# switch to the third experiment
reader.series = 2

Expand All @@ -53,7 +53,7 @@ open a file with for instance 1 GB of java memory:

.. code-block:: python

reader = BioformatsReader('path/to/file', java_memory='1024m')
reader = pims.bioformats.BioformatsReader('path/to/file', java_memory='1024m')

Metadata
--------
Expand Down Expand Up @@ -95,17 +95,17 @@ To update the version of bioformats you are using in pims:

Now you should be able to use pims with the updated bioformats version.

Note: This pims command downloads a bioformats file named `loci_tools.jar`
Note: This pims command downloads a bioformats file named `bioformats_package.jar`
to your computer. There are a few possible locations where it might be stored.
The precedence order is (highest priority first):

1. pims package location
2. PROGRAMDATA/pims/loci_tools.jar
3. LOCALAPPDATA/pims/loci_tools.jar
4. APPDATA/pims/loci_tools.jar
5. /etc/loci_tools.jar
6. ~/.config/pims/loci_tools.jar
2. PROGRAMDATA/pims/bioformats_package.jar
3. LOCALAPPDATA/pims/bioformats_package.jar
4. APPDATA/pims/bioformats_package.jar
5. /etc/bioformats_package.jar
6. ~/.config/pims/bioformats_package.jar

If you encounter problems updating to the latest version of bioformats,
you may wish to manually remove `loci_tools.jar` from each of the six locations
you may wish to manually remove `bioformats_package.jar` from each of the six locations
and re-run the `pims.bioformats.download_jar` command again.
66 changes: 35 additions & 31 deletions pims/bioformats.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from turtle import pd
import numpy as np

from pims.base_frames import FramesSequence, FramesSequenceND
Expand All @@ -17,15 +18,15 @@ def available():

def _gen_jar_locations():
"""
Generator that yields optional locations of loci_tools.jar.
Generator that yields optional locations of bioformats_package.jar.
The precedence order is (highest priority first):

1. pims package location
2. PROGRAMDATA/pims/loci_tools.jar
3. LOCALAPPDATA/pims/loci_tools.jar
4. APPDATA/pims/loci_tools.jar
5. /etc/loci_tools.jar
6. ~/.config/pims/loci_tools.jar
2. PROGRAMDATA/pims/bioformats_package.jar
3. LOCALAPPDATA/pims/bioformats_package.jar
4. APPDATA/pims/bioformats_package.jar
5. /etc/bioformats_package.jar
6. ~/.config/pims/bioformats_package.jar
"""
yield os.path.dirname(__file__)
if 'PROGRAMDATA' in os.environ:
Expand All @@ -40,18 +41,18 @@ def _gen_jar_locations():

def _find_jar():
"""
Finds the location of loci_tools.jar, if necessary download it to a
Finds the location of bioformats_package.jar, if necessary download it to a
writeable location.
"""
for loc in _gen_jar_locations():
if os.path.isfile(os.path.join(loc, 'loci_tools.jar')):
return os.path.join(loc, 'loci_tools.jar')
if os.path.isfile(os.path.join(loc, 'bioformats_package.jar')):
return os.path.join(loc, 'bioformats_package.jar')

warn('loci_tools.jar not found, downloading')
warn('bioformats_package.jar not found, downloading')
return download_jar()


def download_jar(version='6'):
def download_jar(version='latest'):
""" Downloads the bioformats distribution of given version. """
from urllib.request import urlopen
import hashlib
Expand All @@ -68,23 +69,23 @@ def download_jar(version='6'):
else:
raise IOError('No writeable location found. In order to use the '
'Bioformats reader, please download '
'loci_tools.jar to the pims program folder or one of '
'bioformats_package.jar to the pims program folder or one of '
'the locations provided by _gen_jar_locations().')

url = ('http://downloads.openmicroscopy.org/bio-formats/' + version +
'/artifacts/loci_tools.jar')
url = ('https://downloads.openmicroscopy.org/bio-formats/' + version +
'/artifacts/bioformats_package.jar')

path = os.path.join(loc, 'loci_tools.jar')
loci_tools = urlopen(url).read()
path = os.path.join(loc, 'bioformats_package.jar')
bioformats_package = urlopen(url).read()
sha1_checksum = urlopen(url + '.sha1').read().split(b' ')[0].decode()

downloaded = hashlib.sha1(loci_tools).hexdigest()
downloaded = hashlib.sha1(bioformats_package).hexdigest()
if downloaded != sha1_checksum:
raise IOError("Downloaded loci_tools.jar has invalid checksum. "
raise IOError("Downloaded bioformats_package.jar has invalid checksum. "
"Please try again.")

with open(path, 'wb') as output:
output.write(loci_tools)
output.write(bioformats_package)

return path

Expand Down Expand Up @@ -330,27 +331,30 @@ def __init__(self, filename, meta=True, java_memory='512m',

# Start java VM and initialize logger (globally)
if not jpype.isJVMStarted():
from distutils.version import LooseVersion
from packaging.specifiers import SpecifierSet
from packaging.version import Version

loci_path = _find_jar()
bioformats_package_path = _find_jar()
# If we can turn off string auto-conversion, do so,
# since this is the recommended practice.
if LooseVersion(jpype.__version__) >= LooseVersion('0.7.0'):
version_spec = SpecifierSet(">=0.7.0")
if Version(jpype.__version__) in version_spec:
startJVM_kwargs = {'convertStrings': False}
else:
startJVM_kwargs = {} # convertStrings kwarg not supported for earlier jpype versions
jpype.startJVM(jpype.getDefaultJVMPath(), '-ea',
'-Djava.class.path=' + loci_path,
'-Djava.class.path=' + bioformats_package_path,
'-Xmx' + java_memory, **startJVM_kwargs)
log4j = jpype.JPackage('org.apache.log4j')
log4j.BasicConfigurator.configure()
log4j_logger = log4j.Logger.getRootLogger()
log4j_logger.setLevel(log4j.Level.ERROR)
logback = jpype.JPackage('ch.qos.logback.classic')
logger_context = logback.LoggerContext()
logback.BasicConfigurator().setContext(logger_context)
logback.BasicConfigurator().configure(logger_context)

if not jpype.isThreadAttachedToJVM():
jpype.attachThreadToJVM()
if not jpype.java.lang.Thread.isAttached():
jpype.java.lang.Thread.attach()

loci = jpype.JPackage('loci')
loci.common.DebugTools.enableLogging('ERROR')

# Initialize reader and metadata
self.filename = str(filename)
Expand Down Expand Up @@ -407,8 +411,8 @@ def __init__(self, filename, meta=True, java_memory='512m',
if isinstance(Jarr[:], np.ndarray):
read_mode = 'jpype'
else:
warn('Due to an issue with JPype 0.6.0, reading is slower. '
'Please consider upgrading JPype to 0.6.1 or later.')
# warn('Due to an issue with JPype 0.6.0, reading is slower. '
# 'Please consider upgrading JPype to 0.6.1 or later.')
try:
im = self._jbytearr_stringbuffer(Jarr)
im.reshape(self._sizeRGB, self._sizeX, self._sizeY)
Expand Down
3 changes: 1 addition & 2 deletions pims/tests/test_bioformats.py
Original file line number Diff line number Diff line change
Expand Up @@ -504,5 +504,4 @@ def test_metadata_tags(self):
assert 'PixelsPhysicalSizeX' in fields

if __name__ == '__main__':
unittest.runmodule(argv=[__file__, '-vvs'],
exit=False)
unittest.main(verbosity=2)