Repository navigation
Prevent unsafe autofocus scans and plot progress in real time - #1241
Conversation
Reuse analysis: centralize the existing autofocus step calculation, reuse stage limit state and ValidatedSpinbox error styling, and mirror HistogramController coalescing and blitting rather than adding parallel APIs or rendering infrastructure.
Reuse the existing Autofocus lifecycle, ValidatedSpinbox state, stage-update paths, event pump, and histogram blitting pattern rather than adding parallel public APIs.
Reuse the existing Autofocus lifecycle for one exact scan planner and authoritative stage checks. Reuse ValidatedSpinbox error styling and existing stage position/limit update paths for immediate popup feedback.
Reuse the existing event pump for defensive progress snapshots and mirror the histogram controller's Tk coalescing, persistent artist, blit cache, and full-draw fallback.
Keep progress publication nonblocking, freeze trajectory settings across acquisition setup and execution, refresh edited limits immediately, preserve precise diagnostics, and unregister popup event handlers on close.
Isolate replaceable real-time progress in a dedicated single-slot queue so final plots, warnings, and completion metadata retain reliable event capacity. Move defocus-calibration completion processing out of the popup lifecycle so closing the window cannot suppress configuration updates.\n\nReuse analysis: retains the existing model event pump, Autofocus feature, controller listener map, and channel-defocus notification paths; adds no user-callable API or parallel acquisition lifecycle.
Restore package-scoped dummy controller configuration, stage-limit methods, defocus state, and popup handlers after each autofocus test. Keep the advanced-stage limit-refresh test from leaking its f_max override into later channel and stage tests.
There was a problem hiding this comment.
Pull request overview
This PR hardens autofocus safety and UX by enforcing exact stage-limit bounds for planned scan trajectories, providing immediate popup validation feedback, and streaming autofocus plot updates in real time without letting progress traffic interfere with reliable events or calibration completion handling.
Changes:
- Centralizes scan-position planning and reuses it for frame counts, bounds validation, and execution-time moves (with authoritative model-side rejection of out-of-bounds scans).
- Adds continuously refreshed bounds feedback in the autofocus settings popup and refresh hooks when stage position/limits change.
- Introduces a dedicated single-slot progress transport and Tk-thread-coalesced Matplotlib updates (blitting with full-draw fallback), plus popup-independent defocus-calibration completion processing.
Reviewed changes
Copilot reviewed 13 out of 13 changed files in this pull request and generated 1 comment.
Show a summary per file
| File | Description |
|---|---|
| test/model/features/test_autofocus.py | Adds unit coverage for scan planning, bounds diagnostics, frozen settings, failed-move handling, and progress snapshot behavior. |
| test/controller/test_controller.py | Verifies stage-limit/position updates refresh autofocus bounds and that progress transport can’t starve reliable events. |
| test/controller/sub_controllers/test_autofocus.py | Adds headless GUI/controller tests for inline bounds warnings, progress coalescing, blitting behavior, and popup lifetime. |
| test/controller/sub_controllers/test_advanced_stage_settings.py | Ensures editing stage min/max triggers autofocus popup bounds refresh when open. |
| src/navigate/view/popups/autofocus_setting_popup.py | Adds an inline (reserved-row) bounds-warning label/variable in the autofocus popup. |
| src/navigate/model/model.py | Threads a new single-slot autofocus progress queue through model construction. |
| src/navigate/model/features/autofocus.py | Implements shared scan planning, authoritative bounds checks, failed-move hard stops, frozen scan settings, and progress snapshot publishing. |
| src/navigate/controller/sub_controllers/stages_advanced.py | Refreshes autofocus bounds immediately after live stage-limit edits for the active microscope. |
| src/navigate/controller/sub_controllers/autofocus.py | Adds live bounds validation, progress plotting with Tk coalescing + blit caching, and routes calibration completion to a persistent helper. |
| src/navigate/controller/controller.py | Adds the one-slot progress queue, drains it ahead of reliable events, and refreshes popup bounds on stage state changes. |
| src/navigate/controller/autofocus_calibration.py | New persistent controller to process autofocus completion metadata independent of popup lifetime. |
| docs/superpowers/specs/2026-08-07-autofocus-bounds-realtime-plot-design.md | Documents the design constraints and behavior for bounds validation and real-time plotting. |
| docs/superpowers/plans/2026-08-07-autofocus-bounds-realtime-plot.md | Documents the implementation plan and test expectations for the feature set. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| frame_count = int(scan_range // step_size) + 1 | ||
| first_position = center - (frame_count // 2) * step_size | ||
| return tuple(first_position + index * step_size for index in range(frame_count)) |
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## develop #1241 +/- ##
===========================================
+ Coverage 65.29% 65.38% +0.09%
===========================================
Files 190 193 +3
Lines 26612 27719 +1107
===========================================
+ Hits 17377 18125 +748
- Misses 9235 9594 +359
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Harness. 🚀 New features to boost your workflow:
|
Summary
Root cause
Autofocus frame planning and execution did not share an authoritative bounds check, so an out-of-range trajectory could begin and then stop after the first unsuccessful move. The popup also had no continuously refreshed indication that the requested path was unsafe. Plotting waited for the final autofocus event, and early progress delivery initially shared capacity and popup lifetime with reliable completion handling.
User impact
The requested scan geometry is now treated as an exact safety contract. Users see an inline red warning before starting, receive a final blocking dialog if needed, and can immediately see when moving the focus stage or changing limits makes the scan valid. During a valid autofocus run, the graph updates as measurements arrive while acquisition remains nonblocking.
Implementation
Validation
git diff --check origin/develop...HEADpassedCloses #315