Skip to content

feat(quick_update): IPython.display.update_display for live Jupyter cells - #1290

Merged
Jammy2211 merged 1 commit into
mainfrom
feature/quick-update-display-id
May 21, 2026
Merged

Jammy2211 merged 1 commit into
mainfrom
feature/quick-update-display-id

Conversation

@Jammy2211

Copy link
Copy Markdown
Collaborator

Summary

Adds in-cell live updating of the quick-update visualization when a search runs inside a Jupyter / Colab kernel. The threading and JAX-fast visualization already exist (BackgroundQuickUpdate from commit 1fee93174, plus the Phase A′/B work on PyAutoGalaxy + PyAutoLens). This PR wires IPython.display.update_display so the cell that ran search.fit(...) displays and refreshes in place during the fit instead of just writing PNGs to disk.

Phase C of z_features/fast_visualization.md. Phase A is superseded; Phase B shipped (#1288, PyAutoGalaxy#435, pipeline#15).

API Changes

One additive optional kwarg on BackgroundQuickUpdate: display_id: str = "pyauto_fit_progress". Behavioural change: when running inside a Jupyter / Colab kernel, the worker additionally pushes the freshly-written subplot_fit.png to the active cell via IPython.display.update_display. Outside a kernel — unchanged (PNG-on-disk only). No public symbol removed, renamed, or signature-broken.

See full details below.

Test Plan

  • New unit test: _is_ipython_kernel() returns False during normal pytest execution (script-mode fallback verified).
  • New unit test: _push_to_ipython() is a no-op when no PNG exists at paths.image_path (matches PYAUTO_FAST_PLOTS=1 runs).
  • New unit test: with a mocked IPython.display module + a synthetic PNG, the first call uses display(... display_id="pyauto_fit_progress") and subsequent calls use update_display(...) with the same id, so the cell output refreshes rather than appending.
  • pytest test_autofit/non_linear/test_quick_update.py — 12 passed locally (3 new + 9 existing, all green).
  • pytest test_autofit/non_linear/ — 212 passed locally (excluding the search/ subdir which has unrelated dependencies). No regressions.
  • Manual smoke: execute a tiny jupyter nbconvert --execute notebook fitting af.ex.Gaussian with n_live=25, n_like_max=200, iterations_per_quick_update=50, background_quick_update=True and confirm the cell shows one self-updating image, not stacked frames. (To be done by reviewer or post-release.)
Full API Changes (for automation & release notes)

Removed

  • None.

Added

  • BackgroundQuickUpdate(display_id: str = "pyauto_fit_progress") — new optional kwarg. Stable identifier passed to IPython.display.update_display so every quick-update frame refreshes the same cell output rather than appending. Override only when running two concurrent searches in the same kernel and wanting each in its own cell.

Changed Signature

  • BackgroundQuickUpdate.__init__ — gains display_id: str = "pyauto_fit_progress" keyword argument. Backwards compatible.

Changed Behaviour

  • BackgroundQuickUpdate._process_pending — after analysis.perform_quick_update(paths, instance) returns, additionally calls self._push_to_ipython(paths) when running inside a Jupyter / Colab IPython kernel (detected via IPython.get_ipython().config containing IPKernelApp). The push reads the freshly-written subplot_fit.png (fallbacks: fit.png, subplot_tracer.png) under paths.image_path and calls display(Image(filename=...), display_id=...) on the first iteration / update_display(..., display_id=...) thereafter. Script-mode execution and all non-Jupyter contexts are unchanged.

Added env var

  • PYAUTO_DISABLE_IPYTHON_DISPLAY=1 — opt-out for users running notebooks via papermill / automated nbconvert pipelines that don't want display side effects. When set, the display step is skipped even inside a kernel; PNGs still land on disk.

Migration

  • None required. Existing scripts and notebooks pick up the new behaviour automatically (notebooks gain live cell updates; scripts unchanged). Two-concurrent-search-in-one-kernel users can pass display_id="my-fit-2" to one of them.

Companion follow-up

After this lands in a release, the cookbook documentation gets added in autofit_workspace: a new __Live Quick-Update Visualization__ section in scripts/cookbooks/analysis.py covering iterations_per_quick_update, background_quick_update=True, this PR's in-cell live update, the script-mode fallback, and the PYAUTO_DISABLE_IPYTHON_DISPLAY opt-out. That ships via /ship_workspace once the library version is pip-installable.

Closes #1289 (library half).

🤖 Generated with Claude Code

…ells

`BackgroundQuickUpdate` now pushes the freshly-written `subplot_fit.png`
into the active Jupyter / Colab cell via `IPython.display.update_display`
with a stable `display_id`, so the cell that ran `search.fit(...)` shows
a single self-updating image during the fit rather than just writing
PNGs to disk.

How it works:

- After `analysis.perform_quick_update(paths, instance)` returns, the
  daemon worker checks `_is_ipython_kernel()` (try-imports IPython,
  inspects `get_ipython().config` for `IPKernelApp`).
- If running in a kernel, locates `subplot_fit.png` (with `fit.png` /
  `subplot_tracer.png` fallbacks) under `paths.image_path` and calls
  `display(Image(filename=...), display_id="pyauto_fit_progress")` on
  the first iteration, `update_display(... same id)` on subsequent
  iterations.
- Reads the PNG from disk (not a matplotlib `Figure`) — the worker is
  a daemon thread, and matplotlib Figure handling is not thread-safe.

Outside a kernel (plain `python my_fit.py`), the display layer is
silently skipped — script behaviour is unchanged. Users inside a
kernel can opt out via `PYAUTO_DISABLE_IPYTHON_DISPLAY=1` (papermill,
nbconvert pipelines, etc.). Any IPython-side exception is logged and
swallowed so a display failure never takes the search down.

New optional kwarg `BackgroundQuickUpdate(display_id="pyauto_fit_progress")`
for users running two concurrent searches in the same kernel.

Tests added:
- `_is_ipython_kernel()` returns False in pytest (script-mode fallback).
- `_push_to_ipython()` no-ops when no PNG exists yet.
- With a mocked IPython.display module, first call uses `display(...)`
  with the configured display_id; subsequent calls use `update_display`
  with the same id.

Phase C of z_features/fast_visualization.md. Closes #1289
(library half; autofit_workspace cookbook docs follow after this lands
in a release).

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
@Jammy2211 Jammy2211 added the pending-release PR queued for the next release build label May 21, 2026
@Jammy2211
Jammy2211 merged commit 362d2a1 into main May 21, 2026
7 checks passed
@Jammy2211

Copy link
Copy Markdown
Collaborator Author

Workspace PR: PyAutoLabs/autofit_workspace#62

@Jammy2211
Jammy2211 deleted the feature/quick-update-display-id branch May 21, 2026 20:36
@Jammy2211 Jammy2211 removed the pending-release PR queued for the next release build label Sep 4, 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.

feat(quick_update): IPython.display.update_display for live Jupyter cells

1 participant