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

Reduce import time by lazily importing slow modules. #777

Merged
merged 1 commit into from
Feb 9, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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