Commit f6a572c
authored
perf: defer Matplotlib import until something actually plots (#198)
## Problem
`import spatialmath` unconditionally pulls in all of Matplotlib, even
for
users who never plot anything (RAI wishlist item: "Don't depend on
matplotlib unless you're actually plotting").
`spatialmath/base/__init__.py`
did `from spatialmath.base.animate import *` / `from
spatialmath.base.graphics import *` at package-import time, and those
two
modules do `import matplotlib.pyplot`. `geom2d.py`, `geom3d.py`,
`spline.py`,
and both `transforms2d.py`/`transforms3d.py` had their own copies of the
same pattern — some via a `try: import matplotlib.pyplot ... except
ImportError:` optional-dependency check that still imported eagerly,
just
without crashing if matplotlib turned out to be missing.
Measured on my machine: bare `import matplotlib` costs ~96ms,
`matplotlib.path` (needed structurally by `Polygon2`'s geometry, not
just
plotting) adds ~0ms on top of that, but `matplotlib.pyplot` specifically
(backend resolution + figure/state setup) adds another ~160ms. That's
the
cost this PR removes from `import spatialmath`. (Caveat: on this machine
matplotlib isn't the single biggest contributor to overall import time —
`scipy.interpolate` and `sympy`, when installed, are comparable — so
this
alone may not fully explain reports of ~9s import time on other
machines;
worth keeping an eye on.)
## Approach
Deliberately **not** touching matplotlib usage inside `graphics.py`/
`animate.py` themselves — dozens of `plt.Axes`-style annotations live in
there, and rewriting those would be a much bigger, riskier change than
this
needs to be. Instead, defer the **module import** of
`graphics`/`animate`
as a whole, plus a few other files with the same eager-import pattern:
- **`spatialmath/base/__init__.py`**: replace the two blanket `import *`
lines with a PEP 562 module `__getattr__` that imports
`animate.py`/`graphics.py` lazily on first access of one of their names
(`Animate`, `plot_box`, `tranimate`, etc.), not unconditionally at
package import time.
- **`spatialmath/base/transforms2d.py`, `transforms3d.py`**: both had a
module-level `try: import matplotlib.pyplot ... except ImportError:
_matplotlib_exists = False` used purely to decide whether to define
`trplot`/`trplot2`/`tranimate`/`tranimate2` at all. Replaced with a
cheap
`importlib.util.find_spec("matplotlib")` check (no real import), and
moved the small number of actual `plotvol2/3`, `axes_logic`,
`Animate`/`Animate2`, and `plt.show()` call sites into the specific
functions that use them.
- **`spatialmath/spline.py`, `geom3d.py`**: same pattern — moved `import
matplotlib.pyplot as plt` from module top into the one or two methods
that actually plot.
- **`spatialmath/geom2d.py`**: nuance — `Polygon2.__init__` uses
`matplotlib.path.Path` for real geometry (point containment etc.), and
`Polygon2.transformed()` uses `matplotlib.transforms.Affine2D` for the
same reason, so both stay at module level (cheap anyway, per the
measurement above). Only `matplotlib.pyplot` itself, and the
`plot_ellipse` import (only used by `Ellipse.plot`), moved.
All of the above needed `from __future__ import annotations` added where
not already present, so a signature like `ax: Optional[plt.Axes] = None`
doesn't force `plt` to be a real bound name at function *definition*
time
(module-import time, before the deferred import ever runs) — annotations
become lazy strings instead, which type checkers still read fine.
Matches
the existing convention already used in 8 other files in this codebase.
One real (minor) bug this surfaced: `tests/test_geom3d.py` used
`plt.figure()` without importing `matplotlib.pyplot` itself — it only
worked because `from spatialmath.geom3d import *` used to leak `plt` in
as
a wildcard-imported name. Fixed by importing it directly.
## Testing
- `import spatialmath` no longer puts `matplotlib.pyplot` in
`sys.modules`;
`matplotlib` (the ~96ms base package, structurally needed by
`Polygon2`'s
geometry) still does.
- Wall-clock `import spatialmath`, same machine, same warm caches:
~670ms
average on 3 runs before this change (`upstream/master`, via a throwaway
worktree), ~447ms average after.
- Full test suite green in both CI-like mode (`CI=true MPLBACKEND=Agg`:
338
passed, 4 skipped) and a local-run simulation (`MPLBACKEND=Agg`, `CI`
unset): 332 passed (excluding two files whose local-run behaviour
depends
on a separate, already-open PR, unrelated to this change).
- Manual smoke test with a forced Agg backend: `trplot`, `trplot2`,
`Polygon2.plot`/`.animate`/`.contains`, `Ellipse.plot`, `Line3.plot`,
`Plane3.plot`, `BSplineSE3.visualize` all still produce real output.
- `black --check` clean at the pinned 23.10.0.1 parent 6f9e892 commit f6a572c
7 files changed
Lines changed: 103 additions & 26 deletions
File tree
- spatialmath
- base
- tests
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
13 | | - | |
14 | 12 | | |
15 | 13 | | |
| 14 | + | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
| 18 | + | |
| 19 | + | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
| 23 | + | |
| 24 | + | |
| 25 | + | |
| 26 | + | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
| 42 | + | |
| 43 | + | |
| 44 | + | |
| 45 | + | |
| 46 | + | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
| 62 | + | |
| 63 | + | |
16 | 64 | | |
17 | 65 | | |
18 | 66 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
17 | 19 | | |
18 | 20 | | |
19 | 21 | | |
| 22 | + | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
23 | | - | |
24 | | - | |
25 | | - | |
26 | | - | |
27 | | - | |
28 | | - | |
| 27 | + | |
| 28 | + | |
| 29 | + | |
| 30 | + | |
29 | 31 | | |
30 | 32 | | |
31 | 33 | | |
32 | 34 | | |
33 | 35 | | |
34 | 36 | | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
35 | 40 | | |
36 | 41 | | |
37 | 42 | | |
| |||
1311 | 1316 | | |
1312 | 1317 | | |
1313 | 1318 | | |
1314 | | - | |
1315 | | - | |
1316 | | - | |
1317 | | - | |
1318 | 1319 | | |
1319 | 1320 | | |
1320 | 1321 | | |
| |||
1569 | 1570 | | |
1570 | 1571 | | |
1571 | 1572 | | |
| 1573 | + | |
| 1574 | + | |
1572 | 1575 | | |
1573 | 1576 | | |
1574 | 1577 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
15 | 17 | | |
16 | 18 | | |
17 | 19 | | |
| 20 | + | |
18 | 21 | | |
19 | 22 | | |
| 23 | + | |
20 | 24 | | |
21 | 25 | | |
22 | 26 | | |
| |||
46 | 50 | | |
47 | 51 | | |
48 | 52 | | |
49 | | - | |
50 | | - | |
51 | 53 | | |
52 | 54 | | |
53 | 55 | | |
54 | 56 | | |
| 57 | + | |
| 58 | + | |
| 59 | + | |
| 60 | + | |
| 61 | + | |
55 | 62 | | |
56 | 63 | | |
57 | 64 | | |
| |||
2994 | 3001 | | |
2995 | 3002 | | |
2996 | 3003 | | |
2997 | | - | |
2998 | | - | |
2999 | | - | |
3000 | | - | |
3001 | | - | |
3002 | | - | |
3003 | | - | |
| 3004 | + | |
| 3005 | + | |
| 3006 | + | |
| 3007 | + | |
3004 | 3008 | | |
3005 | 3009 | | |
3006 | 3010 | | |
| |||
3191 | 3195 | | |
3192 | 3196 | | |
3193 | 3197 | | |
| 3198 | + | |
| 3199 | + | |
3194 | 3200 | | |
3195 | 3201 | | |
3196 | 3202 | | |
| |||
3504 | 3510 | | |
3505 | 3511 | | |
3506 | 3512 | | |
| 3513 | + | |
| 3514 | + | |
3507 | 3515 | | |
3508 | 3516 | | |
3509 | 3517 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
9 | 9 | | |
10 | 10 | | |
11 | 11 | | |
12 | | - | |
| 12 | + | |
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
17 | 17 | | |
18 | 18 | | |
19 | 19 | | |
20 | | - | |
21 | 20 | | |
22 | 21 | | |
23 | 22 | | |
| |||
37 | 36 | | |
38 | 37 | | |
39 | 38 | | |
| 39 | + | |
| 40 | + | |
| 41 | + | |
40 | 42 | | |
41 | 43 | | |
42 | 44 | | |
| |||
450 | 452 | | |
451 | 453 | | |
452 | 454 | | |
| 455 | + | |
| 456 | + | |
453 | 457 | | |
454 | 458 | | |
455 | 459 | | |
| |||
1063 | 1067 | | |
1064 | 1068 | | |
1065 | 1069 | | |
| 1070 | + | |
| 1071 | + | |
1066 | 1072 | | |
1067 | 1073 | | |
1068 | 1074 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
6 | 6 | | |
7 | 7 | | |
8 | 8 | | |
9 | | - | |
| 9 | + | |
10 | 10 | | |
11 | 11 | | |
12 | 12 | | |
13 | 13 | | |
14 | 14 | | |
| 15 | + | |
| 16 | + | |
| 17 | + | |
15 | 18 | | |
16 | 19 | | |
17 | 20 | | |
| |||
1233 | 1236 | | |
1234 | 1237 | | |
1235 | 1238 | | |
| 1239 | + | |
| 1240 | + | |
1236 | 1241 | | |
1237 | 1242 | | |
1238 | 1243 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
| 1 | + | |
| 2 | + | |
1 | 3 | | |
2 | 4 | | |
3 | 5 | | |
| |||
6 | 8 | | |
7 | 9 | | |
8 | 10 | | |
9 | | - | |
| 11 | + | |
10 | 12 | | |
11 | | - | |
12 | 13 | | |
13 | 14 | | |
14 | 15 | | |
15 | 16 | | |
16 | 17 | | |
17 | 18 | | |
18 | 19 | | |
| 20 | + | |
| 21 | + | |
| 22 | + | |
19 | 23 | | |
20 | 24 | | |
21 | 25 | | |
| |||
39 | 43 | | |
40 | 44 | | |
41 | 45 | | |
| 46 | + | |
| 47 | + | |
42 | 48 | | |
43 | 49 | | |
44 | 50 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
14 | 14 | | |
15 | 15 | | |
16 | 16 | | |
| 17 | + | |
17 | 18 | | |
18 | 19 | | |
19 | 20 | | |
| |||
0 commit comments