Skip to content

Defer scipy imports so import spatialmath is faster - #244

Open
RoboX2020 wants to merge 3 commits into
rai-opensource:masterfrom
RoboX2020:prajval/perf/defer-scipy-import
Open

RoboX2020 wants to merge 3 commits into
rai-opensource:masterfrom
RoboX2020:prajval/perf/defer-scipy-import

Conversation

@RoboX2020

Copy link
Copy Markdown

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.

This branch has not been deployed

No deployments
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