Skip to content

Commit

Permalink
perf(plotting): improve module import time
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
kmnhan committed Nov 19, 2024
1 parent 9043580 commit 4ecd38d
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 14 deletions.
9 changes: 0 additions & 9 deletions src/erlab/plotting/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
"""

import importlib
import io
import os
import pkgutil
Expand All @@ -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
Expand Down
6 changes: 4 additions & 2 deletions src/erlab/plotting/annotations.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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():
Expand Down
3 changes: 2 additions & 1 deletion src/erlab/plotting/bz.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 = []
Expand Down
3 changes: 1 addition & 2 deletions src/erlab/plotting/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,7 @@
<https://matplotlib.org/stable/tutorials/colors/colormaps.html>`_ colormaps, `cmasher
<https://cmasher.readthedocs.io>`_, `cmocean <https://matplotlib.org/cmocean/>`_, and
`colorcet <https://colorcet.holoviz.org>`_ 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
----------------------
Expand Down

0 comments on commit 4ecd38d

Please sign in to comment.