Repository navigation
perf: import pgraph without matplotlib, spatialmath or scipy - #70
Merged
Merged
Conversation
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>
Up to standards ✅🟢 Issues
|
| Metric | Results |
|---|---|
| Complexity | 14 |
| Duplication | 0 |
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.
Codecov Report✅ All modified and coverable lines are covered by tests. 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. 🚀 New features to boost your workflow:
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
import pgraphtook 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, becauseimport spatialmathbrings in sympy, scipy, PIL and, today, matplotlib; andmatplotlib.pyplot,mpl_toolkits.mplot3dand 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,_Arrow3Dand_Loop3Dmove to a new private modulepgraph/_plot.py.Graph._edge_artistimports from it, andplot,highlight_vertex,highlight_edgeandhighlight_pathimportpyplot(andplottheaxes_logicof spatialmath), when called.from pgraph._plot import _Arrow3D, _Loop3D).import pgraphtests/test_lazy_imports.pystarts a fresh interpreter to check what an import, a build, three planners, the spatial queries,subgraph,copy, the matrices,exportand 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