From 37bd2fb7d7bc3e38e132d390826790a0b91c6fe4 Mon Sep 17 00:00:00 2001 From: Leo Singer Date: Wed, 29 Apr 2020 11:32:01 -0400 Subject: [PATCH] Speed up import by replacing pkg_resources with importlib.resources 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](https://github.com/pypa/setuptools/issues/926) and [pypa/setuptools#510](https://github.com/pypa/setuptools/issues/510). --- CHANGES.rst | 8 +++++++- ligo/skymap/plot/cmap.py | 9 +++++++-- setup.cfg | 1 + 3 files changed, 15 insertions(+), 3 deletions(-) diff --git a/CHANGES.rst b/CHANGES.rst index 93b62061..f663e120 100644 --- a/CHANGES.rst +++ b/CHANGES.rst @@ -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) ================== diff --git a/ligo/skymap/plot/cmap.py b/ligo/skymap/plot/cmap.py index 3dacf5c3..893f4595 100644 --- a/ligo/skymap/plot/cmap.py +++ b/ligo/skymap/plot/cmap.py @@ -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. diff --git a/setup.cfg b/setup.cfg index 69151c8e..42ece754 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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