From 4ecd38d7dd9239cbe64290d88d95c2920b140f3b Mon Sep 17 00:00:00 2001 From: Kimoon Han Date: Tue, 19 Nov 2024 14:59:30 +0900 Subject: [PATCH] perf(plotting): improve module import time Accelerates initial import time by refactoring heavy imports to reside inside functions. Furthermore, automatically importing cmocean, cmasher, and colorcet packages are now disabled. The user must import them manually to access those colormaps. --- src/erlab/plotting/__init__.py | 9 --------- src/erlab/plotting/annotations.py | 6 ++++-- src/erlab/plotting/bz.py | 3 ++- src/erlab/plotting/colors.py | 3 +-- 4 files changed, 7 insertions(+), 14 deletions(-) diff --git a/src/erlab/plotting/__init__.py b/src/erlab/plotting/__init__.py index 0de6570c..a79ddaa6 100644 --- a/src/erlab/plotting/__init__.py +++ b/src/erlab/plotting/__init__.py @@ -19,7 +19,6 @@ """ -import importlib import io import os import pkgutil @@ -30,14 +29,6 @@ import matplotlib.style import numpy as np -# Import colormaps if available -if importlib.util.find_spec("cmasher"): - importlib.import_module("cmasher") -if importlib.util.find_spec("cmocean"): - importlib.import_module("cmocean") -if importlib.util.find_spec("colorcet"): - importlib.import_module("colorcet") - def load_igor_ct( file: str | os.PathLike | io.BytesIO, name: str, register_reversed: bool = True diff --git a/src/erlab/plotting/annotations.py b/src/erlab/plotting/annotations.py index 64635de1..e67988f0 100644 --- a/src/erlab/plotting/annotations.py +++ b/src/erlab/plotting/annotations.py @@ -28,8 +28,6 @@ from typing import TYPE_CHECKING, Any, Literal, cast import matplotlib -import matplotlib.backends.backend_pdf -import matplotlib.backends.backend_svg import matplotlib.figure import matplotlib.font_manager import matplotlib.mathtext @@ -286,8 +284,12 @@ def copy_mathtext( fig.text(0, depth / height, s, fontproperties=fontproperties) if svg: + import matplotlib.backends.backend_svg + matplotlib.backends.backend_svg.FigureCanvasSVG(fig) else: + import matplotlib.backends.backend_pdf + matplotlib.backends.backend_pdf.FigureCanvasPdf(fig) for k, v in mathtext_rc.items(): diff --git a/src/erlab/plotting/bz.py b/src/erlab/plotting/bz.py index 38463c19..91a00ea9 100644 --- a/src/erlab/plotting/bz.py +++ b/src/erlab/plotting/bz.py @@ -8,7 +8,6 @@ import matplotlib.pyplot as plt import numpy as np import numpy.typing as npt -import scipy.spatial from matplotlib.patches import RegularPolygon from erlab.plotting.colors import axes_textcolor @@ -70,6 +69,8 @@ def get_bz_edge( # Get index of origin zero_ind = np.where((points == 0).all(axis=1))[0][0] + import scipy.spatial + vor = scipy.spatial.Voronoi(points) lines = [] diff --git a/src/erlab/plotting/colors.py b/src/erlab/plotting/colors.py index c3a0182f..72d1723e 100644 --- a/src/erlab/plotting/colors.py +++ b/src/erlab/plotting/colors.py @@ -7,8 +7,7 @@ `_ colormaps, `cmasher `_, `cmocean `_, and `colorcet `_ packages can be installed to extend the -available colormaps. If these packages are installed, they will be automatically -imported upon importing `erlab.plotting`. +available colormaps. Colormap Normalization ----------------------