Skip to content

perf: import pgraph without matplotlib, spatialmath or scipy - #70

Merged
petercorke merged 1 commit into
mainfrom
perf/lazy-matplotlib
Oct 13, 2026
Merged

petercorke merged 1 commit into
mainfrom
perf/lazy-matplotlib

Conversation

@petercorke

Copy link
Copy Markdown
Owner

import pgraph took 0.62 s. Profiling showed almost all of it was two imports that are only needed to plot:

  • from spatialmath.base.graphics import axes_logic (the one thing pgraph uses from spatialmath): 0.4 s, because import spatialmath brings in sympy, scipy, PIL and, today, matplotlib; and
  • matplotlib.pyplot, mpl_toolkits.mplot3d and friends, which two module-level classes (_Arrow3D, _Loop3D) needed just to be defined.

Making matplotlib lazy alone would save nothing, since spatialmath loads it too; both have to go.

Change

  • _loop_path, _Arrow3D and _Loop3D move to a new private module pgraph/_plot.py.
  • Graph._edge_artist imports from it, and plot, highlight_vertex, highlight_edge and highlight_path import pyplot (and plot the axes_logic of spatialmath), when called.
  • No behaviour change. The one test import moves (from pgraph._plot import _Arrow3D, _Loop3D).
import pgraph before after
time 0.62 s 0.04 s (numpy alone is 0.03 s)
matplotlib, spatialmath, scipy loaded yes no

tests/test_lazy_imports.py starts a fresh interpreter to check what an import, a build, three planners, the spatial queries, subgraph, copy, the matrices, export and a pickle round trip load (nothing heavy), and that plotting in 2D and 3D (arrows, loops, highlight_path) imports matplotlib at that point.

762 tests pass, mypy clean, docs build with no warnings.

This does not wait for the spatialmath lazy-import work: it is independent of it, and the saving is the same with or without it.

🤖 Generated with Claude Code

PGraph.py imported matplotlib, mpl_toolkits and spatialmath at module level,
and two of its classes subclassed FancyArrowPatch, so import pgraph took
0.62 s, nearly all of it spatialmath (which brings sympy, scipy, PIL and
matplotlib) and matplotlib.  They are used only to plot.

_loop_path, _Arrow3D and _Loop3D move to pgraph/_plot.py, which plot() and
the edge artist import; plot(), highlight_vertex, highlight_edge and
highlight_path import pyplot, and plot() spatialmath's axes_logic, when
called.  import pgraph is now 0.04 s, the cost of numpy.  A test runs a fresh
interpreter to check what an import, a build, a plan, a spatial query and an
export load, and that plotting still imports matplotlib late.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
@codacy-production

Copy link
Copy Markdown

Up to standards ✅

🟢 Issues 0 issues

Results:
0 new issues

View in Codacy

🟢 Metrics 14 complexity · 0 duplication

Metric Results
Complexity 14
Duplication 0

View in Codacy

NEW Get contextual insights on your PRs based on Codacy's metrics, along with PR and Jira context, without leaving GitHub. Enable AI reviewer
TIP This summary will be updated as you push new changes.

@petercorke
petercorke merged commit 5fea937 into main Oct 13, 2026
12 checks passed
@petercorke
petercorke deleted the perf/lazy-matplotlib branch October 13, 2026 02:06
@codecov

codecov Bot commented Oct 13, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 97.28%. Comparing base (c9d38e6) to head (7759a58).
⚠️ Report is 2 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main      #70      +/-   ##
==========================================
+ Coverage   97.26%   97.28%   +0.01%     
==========================================
  Files           3        4       +1     
  Lines        1570     1581      +11     
==========================================
+ Hits         1527     1538      +11     
  Misses         43       43              

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant