Skip to content

Commit

Permalink
Reduce import time by lazily importing slow modules.
Browse files Browse the repository at this point in the history
  • Loading branch information
manthey committed Feb 9, 2022
1 parent ffe5257 commit 0c0c6e5
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 12 deletions.
2 changes: 1 addition & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
- Improve TileSource class repr ([#765](../../pull/765))
- Improve frame slider response with base quads ([#771](../../pull/771))
- Default to nearest-neighbor scaling in lossless image conversion ([#772](../../pull/772))
- Improve the import time ([#775](../../pull/775))
- Improve the import time ([#775](../../pull/775), [#777](../../pull/777))

### Bug Fixes
- The tile iterator could return excess tiles with overlap ([#773](../../pull/773))
Expand Down
16 changes: 13 additions & 3 deletions sources/bioformats/large_image_source_bioformats/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -28,8 +28,6 @@
import threading
import types

import bioformats
import javabridge
import numpy
from pkg_resources import DistributionNotFound, get_distribution

Expand All @@ -46,6 +44,9 @@
# package is not installed
pass

bioformats = None
# import javabridge
javabridge = None

_javabridgeStarted = None
_openImages = []
Expand Down Expand Up @@ -100,6 +101,14 @@ def _startJavabridge(logger):
global _javabridgeStarted

if _javabridgeStarted is None:
# Only import these when first asked. They are slow to import.
global bioformats
global javabridge
if bioformats is None:
import bioformats
if javabridge is None:
import javabridge

# We need something to wake up at exit and shut things down
monitor = threading.Thread(target=_monitor_thread)
monitor.daemon = True
Expand All @@ -119,7 +128,8 @@ def _startJavabridge(logger):
def _stopJavabridge(*args, **kwargs):
global _javabridgeStarted

javabridge.kill_vm()
if javabridge is not None:
javabridge.kill_vm()
_javabridgeStarted = None


Expand Down
26 changes: 18 additions & 8 deletions sources/nd2/large_image_source_nd2/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,7 @@
import types
import warnings

# Work around an issue in the PIMS package (can be removed once pims is
# released for Python 3.10). This must be before improt nd2reader.
if True:
import collections.abc
collections.Iterable = collections.abc.Iterable

import cachetools
import nd2reader
import numpy
from pkg_resources import DistributionNotFound, get_distribution

Expand All @@ -37,14 +30,30 @@
from large_image.exceptions import TileSourceError, TileSourceFileNotFoundError
from large_image.tilesource import FileTileSource

nd2reader = None

try:
__version__ = get_distribution(__name__).version
except DistributionNotFound:
# package is not installed
pass


warnings.filterwarnings('ignore', category=UserWarning, module='nd2reader')
def _lazyImport():
"""
Import the nd2reader module. This is done when needed rather than in the
module initialization because it is slow.
"""
global nd2reader

if nd2reader is None:
# Work around an issue in the PIMS package (can be removed once pims is
# released for Python 3.10). This must be before import nd2reader.
if True:
import collections.abc
collections.Iterable = collections.abc.Iterable
import nd2reader
warnings.filterwarnings('ignore', category=UserWarning, module='nd2reader')


class ND2FileTileSource(FileTileSource, metaclass=LruCacheMetaclass):
Expand Down Expand Up @@ -82,6 +91,7 @@ def __init__(self, path, **kwargs):

self._pixelInfo = {}

_lazyImport()
try:
self._nd2 = nd2reader.ND2Reader(self._largeImagePath)
except (UnicodeDecodeError,
Expand Down

0 comments on commit 0c0c6e5

Please sign in to comment.