Repository navigation
refactor(import): defer matplotlib.pyplot out of import autolens - #729
Merged
Merged
Conversation
`autolens/__init__.py` imports `potential_correction`, whose `__init__` imports `visualize`, which imported `matplotlib.pyplot` (and `cm`, `ticker`, `MaxNLocator`, `make_axes_locatable`, `matplotlib`) at module level: ~0.32 s on every `import autolens` for six drawing functions almost no process calls (census O3 of the ci-timing-fast-tests epic, PyAutoHeart timings/unit_import_census_2026-09.md). The imports now live inside the five functions that draw, matching what `potential_correction/mesh.py` and `iterative.py` already do. Nothing outside the module referenced `visualize.plt` et al. `scipy.interpolate` / `scipy.spatial` stay at module level (autoarray already imports scipy.spatial). Measured: `-X importtime` matplotlib.pyplot 325 ms → absent; `import autolens` 1.54 → 1.09 s with the sibling PyAutoFit change, 1.17 s with this one alone (best of 3, fresh processes). Suite 623 passed, 1 xfailed; three new tests pin the clean import, that no matplotlib name is bound on the module, and that the drawing functions are still exported. Four of the five drawing functions were exercised under Agg; the two fit-summary figures need a fit object and are covered by the import tests only. Refs PyAutoLabs/PyAutoFit#1565 Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg
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.
Refs PyAutoLabs/PyAutoFit#1565 (census option O3 of the
ci-timing-fast-testsepic; the PyAutoFit half, O2, is PyAutoFitfeature/defer-import-scipy-special). Independent of that PR.Summary
autolens/__init__.py:143importspotential_correction, whose__init__importsvisualize, which importedmatplotlib.pyplot(andcm,ticker,MaxNLocator,make_axes_locatable,matplotlib) at module level: ~0.32 s on everyimport autolensfor six drawing functions almost no process calls. The imports now live inside the five functions that draw, matching whatpotential_correction/mesh.py:178anditerative.py:661already do. Nothing outside the module referencedvisualize.pltet al.;scipy.interpolate/scipy.spatialstay at module level (autoarray already imports scipy.spatial).Measured
-X importtimematplotlib.pyplot underimport autolensimport autolenswall (best of 3, fresh processes)Still imported at top level and deliberately left alone:
matplotlibcore viaautoarray.plot.*(a real plotting layer, other repo) andscipy.specialviaautogalaxy.profiles.mass.abstract.mge(a real numerical dependency, other repo).API Changes
None. Same functions, same names, same figures.
Validation
python3 -m pytest -q -n auto: 623 passed, 1 xfailed.test_autolens/potential_correction/test_visualize_import.py: pyplot absent fromsys.modulesafterimport autolensin a fresh process; no matplotlib name bound on the module (so a name put back at the top would fail a test rather than silently re-import pyplot); the drawing functions still exported.plt/mpl/cm/ticker/MaxNLocator/make_axes_locatableload resolves to a local import. Four of the five drawing functions were exercised under Agg;show_fit_dpsi/show_fit_dpsi_srcneed a constructed fit and are covered by the import tests only — their deferred import is the same singlepltline.Guard
Import deferral only — no plotting logic, likelihood or JAX change.
pending-release: lands in the next PyAutoLens release;timings/unit/PyAutoLens.jsonl'simport_sreads it back after merge.🤖 Generated with Claude Code
https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg
Generated by Claude Code