Skip to content

Commit

Permalink
Speed up import by replacing pkg_resources with importlib.resources
Browse files Browse the repository at this point in the history
Speed up `import ligo.skymap` by up to a second by replacing uses of
`pkg_resources` with the new Python standard library module
`importlib.resources` (or, for Python < 3.7, the backport
`importlib_resources`). The old `pkg_resources` module is known to be
slow because it does a lot of work on startup.

See, for example,
[pypa/setuptools#926](pypa/setuptools#926) and
[pypa/setuptools#510](pypa/setuptools#510).
  • Loading branch information
lpsinger committed Apr 29, 2020
1 parent 08f3c24 commit 37bd2fb
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 3 deletions.
8 changes: 7 additions & 1 deletion CHANGES.rst
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,13 @@ Changelog
0.2.1 (unreleased)
==================

- No changes yet.
- Speed up ``import ligo.skymap`` by up to a second by replacing uses of
``pkg_resources`` with the new Python standard library module
``importlib.resources`` (or, for Python < 3.7, the backport
``importlib_resources``). The old ``pkg_resources`` module is known to be
slow because it does a lot of work on startup. (See, for example,
https://github.com/pypa/setuptools/issues/926 and
https://github.com/pypa/setuptools/issues/510.)

0.2.0 (2020-04-21)
==================
Expand Down
9 changes: 7 additions & 2 deletions ligo/skymap/plot/cmap.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,17 +16,22 @@
#
"""Register some extra Matplotlib color maps"""

try:
from importlib import resources
except ImportError:
# FIXME: remove after dropping support for Python < 3.7
import importlib_resources as resources

from matplotlib import cm
from matplotlib import colors
import numpy as np
import pkg_resources

__all__ = ()


for name in ['cylon']:
# Read in color map RGB data.
with pkg_resources.resource_stream(__name__, name + '.csv') as f:
with resources.open_text(__package__, name + '.csv') as f:
data = np.loadtxt(f, delimiter=',')

# Create color map.
Expand Down
1 change: 1 addition & 0 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ install_requires =
astropy-healpix>=0.3 # https://github.com/astropy/astropy-healpix/pull/106
healpy
h5py
importlib-resources;python_version<'3.7'
lalsuite>=6.53
lscsoft-glue>=2.0.0
ligo-gracedb>=2.0.1
Expand Down

0 comments on commit 37bd2fb

Please sign in to comment.