Skip to content

Defer scipy imports so import spatialmath is faster - #244

Merged
taughz merged 3 commits into
rai-opensource:masterfrom
RoboX2020:prajval/perf/defer-scipy-import
Nov 5, 2026
Merged

taughz merged 3 commits into
rai-opensource:masterfrom
RoboX2020:prajval/perf/defer-scipy-import

Conversation

@RoboX2020

Copy link
Copy Markdown
Contributor

Part of #234 (the SciPy half; SymPy is not changed here).

spatialmath/base/quaternions.py and spatialmath/spline.py imported scipy.interpolate / scipy.spatial.transform at module level, so import spatialmath pulled in scipy even though only qrand (with a theta range), SplineSE3 and BSplineSE3 use it. These imports now happen inside those functions. The interpolate.interp1d return annotation stays as a string under TYPE_CHECKING.

Measured with python -X importtime -c "import spatialmath" on one machine, single run each: 1.42 s before, 0.40 s after. scipy is no longer in sys.modules after import spatialmath. sympy still is, so the remaining part of #234 (issymbol / argcheck) is untouched.

Added tests/test_import.py, which imports spatialmath in a subprocess and checks that no scipy module is loaded. It fails on master and passes here.

Run: full tests/ suite with MPLBACKEND=Agg, 348 passed, 3 skipped; black 23.10.0 clean on the changed files; qrand(theta_range=...) and importing SplineSE3/BSplineSE3 smoke-checked. Not run: the spline classes were not exercised by hand beyond what the existing tests cover, and only Python 3 on Linux was tried.

@petercorke

Copy link
Copy Markdown
Collaborator

Thank you for your interest in SMTB and for this contribution. It's great to see a first PR from you!

I've reviewed it and run the suite locally: scipy is no longer in sys.modules after import spatialmath, qrand(theta_range=…) and the spline classes still work, and the subprocess test is a good guard against it creeping back. It follows the same approach as #198 for matplotlib and matches the call sites listed in #234.

One note for context: #234 stays open since the SymPy half is still to do. This PR is already part of the integration branch in #246. Thanks again, and I'd be happy to see a PR for the SymPy part too if you're interested.

@RoboX2020

Copy link
Copy Markdown
Contributor Author

Thank you for reviewing and testing this, sir. I've opened #264 for the SymPy half of #234, separate from the SciPy work in this PR. It covers numeric-first imports followed by SymPy use, with subprocess tests for both import orders and for numeric use without SymPy installed.

#264

@taughz
taughz force-pushed the prajval/perf/defer-scipy-import branch from e5802a5 to cf3c701 Compare November 5, 2026 13:36
@codecov-commenter

Copy link
Copy Markdown

⚠️ Please install the 'codecov app svg image' to ensure uploads and comments are reliably processed by Codecov.

Codecov Report

✅ All modified and coverable lines are covered by tests.

📢 Thoughts on this report? Let us know!

@taughz taughz mentioned this pull request Nov 5, 2026
17 tasks done
@taughz
taughz merged commit 380c968 into rai-opensource:master Nov 5, 2026
16 checks passed
taughz pushed a commit that referenced this pull request Nov 5, 2026
Part of #234, covering the SymPy half only. The SciPy work remains
separate in #244.

`import spatialmath` no longer loads SymPy just to check scalar types.
`issymbol()` checks the already-loaded module, and scalar/vector checks
use it dynamically so importing SymPy after SpatialMath works too.

The other eager SymPy imports in vector norms, object-matrix
determinants and 2D pose extraction are deferred. Explicit symbolic
helpers load the optional dependency when called; explicit access to
`Symbol` and `symtype` remains available.

Added nine isolated-process test cases covering numeric imports and
operations, both import orders, symbolic arithmetic and transforms,
explicit helpers and legacy aliases, and numeric use with SymPy blocked.

Validation: Python 3.10/Linux, `MPLBACKEND=Agg python -m pytest tests/
-p no:xvfb -q`: 389 passed, 3 skipped, 18 subtests passed. No timing
claim is made here. Other Python versions and platforms were not tested.

**Note on star-imports:** `from spatialmath.base.symbolic import *` no
longer brings `sympy`, `Symbol` and `symtype` into the importing
namespace, since star-imports don't go through the module-level
`__getattr__`. Explicit `from spatialmath.base.symbolic import Symbol,
symtype` still works, and no users of the old behavior turned up in RTB,
MVTB, bdsim or Swift.
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.

4 participants