Skip to content

refactor(import): defer matplotlib.pyplot out of import autolens - #729

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/defer-import-pyplot
Sep 7, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/defer-import-pyplot

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Refs PyAutoLabs/PyAutoFit#1565 (census option O3 of the ci-timing-fast-tests epic; the PyAutoFit half, O2, is PyAutoFit feature/defer-import-scipy-special). Independent of that PR.

Summary

autolens/__init__.py:143 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. The imports now live inside the five functions that draw, matching what potential_correction/mesh.py:178 and iterative.py:661 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

before after
-X importtime matplotlib.pyplot under import autolens 325 ms cumulative absent
import autolens wall (best of 3, fresh processes) 1.54 s 1.17 s (this PR alone); 1.09 s with the PyAutoFit PR too

Still imported at top level and deliberately left alone: matplotlib core via autoarray.plot.* (a real plotting layer, other repo) and scipy.special via autogalaxy.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.
  • Three new tests in test_autolens/potential_correction/test_visualize_import.py: pyplot absent from sys.modules after import autolens in 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.
  • An AST pass confirms every plt/mpl/cm/ticker/MaxNLocator/make_axes_locatable load resolves to a local import. Four of the five drawing functions were exercised under Agg; show_fit_dpsi / show_fit_dpsi_src need a constructed fit and are covered by the import tests only — their deferred import is the same single plt line.

Guard

Import deferral only — no plotting logic, likelihood or JAX change.

pending-release: lands in the next PyAutoLens release; timings/unit/PyAutoLens.jsonl's import_s reads it back after merge.

🤖 Generated with Claude Code

https://claude.ai/code/session_01WYcmRnZf3cbC5Wth9bCkEg


Generated by Claude Code

`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
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label Sep 7, 2026 — with Claude
@Jammy2211
Jammy2211 merged commit 9468e3e into main Sep 7, 2026
4 checks passed
@Jammy2211
Jammy2211 deleted the feature/defer-import-pyplot branch September 7, 2026 02:22
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 26, 2026
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