Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
15 commits
Select commit Hold shift + click to select a range
f4a9196
feat(sequencer): a Z FIRST acquisition runs on the controller, one bu…
hongquanli Nov 1, 2026
000315f
Merge branch 'feat/hardware-sequencer-worker' into feat/hardware-sequ…
hongquanli Nov 2, 2026
e887b21
test(sequencer): the From Center return is checked in both z-stack or…
hongquanli Nov 2, 2026
607d097
Merge branch 'feat/hardware-sequencer-worker' into feat/hardware-sequ…
hongquanli Nov 2, 2026
e94bb4c
Merge branch 'feat/hardware-sequencer-worker' into feat/hardware-sequ…
hongquanli Nov 3, 2026
43451b1
Merge branch 'feat/hardware-sequencer-worker' into feat/hardware-sequ…
hongquanli Nov 11, 2026
6f570f7
Merge branch 'feat/hardware-sequencer-worker' into feat/hardware-sequ…
hongquanli Nov 12, 2026
ce08725
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 14, 2026
ad7d8f1
fix(worker): build each burst's program when it is uploaded, with the…
hongquanli Nov 14, 2026
db37287
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 15, 2026
d0555fe
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 15, 2026
89afa47
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 15, 2026
6bf5086
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 15, 2026
e6c6f97
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 15, 2026
a7e2137
Merge remote-tracking branch 'origin/feat/hardware-sequencer-worker' …
hongquanli Nov 29, 2026
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
247 changes: 152 additions & 95 deletions software/control/core/multi_point_worker.py

Large diffs are not rendered by default.

40 changes: 30 additions & 10 deletions software/control/core/sequenced_acquisition.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import dataclasses
import enum
from typing import Optional, Sequence, Tuple
from typing import List, Optional, Sequence, Tuple

import numpy as np

Expand Down Expand Up @@ -158,6 +158,29 @@ def camera_record(*, use_ready_line: bool, strobe_delay_ms: float, readout_ms: f
)


@dataclasses.dataclass(frozen=True)
class BurstPlan:
"""One SEQ_RUN: which of the selected channels it images, in program order (indices into the
selected channels, so the frames can carry their configuration index)."""

channel_indices: Tuple[int, ...]


def burst_plans(z_stack_order: str, n_channels: int) -> List[BurstPlan]:
"""How one FOV's stack is split into bursts.

CHANNEL FIRST: one burst, every channel at each z level (the engine's channels-inner order).
Z FIRST: one single-channel burst per channel, in channel order; the host does between the
bursts what the software Z FIRST loop does between channels (that channel's exposure, gain and
emission filter, laser AF per stack). The engine's z-inner order is not used: nothing can move
a serial filter wheel inside a burst, and on a stepper the jump back to the first plane at a
channel boundary would be an uncompensated reversal (Hongquan, 2026-11-01: one burst per channel).
"""
if z_stack_order == "Z FIRST":
return [BurstPlan((index,)) for index in range(n_channels)]
return [BurstPlan(tuple(range(n_channels)))]


def build_program(
channels: Sequence[ChannelPlan],
*,
Expand Down Expand Up @@ -264,14 +287,11 @@ def ineligibility_reason(
f"the z step of {dz_um:.4f} um is below one DAC count of the {piezo_range_um:.0f} um piezo "
f"({piezo_range_um / 65535:.4f} um): the controller would image every layer at the same z"
)
if nz > 1 and z_stack_order != "CHANNEL FIRST":
# The program runs every channel at each z level. A Z FIRST acquisition would write the same
# files, but acquiring them in another physical order is a different experiment (bleaching,
# drift and the timing between channels differ), so the order asked for is honoured instead.
return (
f"the z-stack order is '{z_stack_order}' (the sequencer runs every channel at each z level, CHANNEL FIRST)"
)
if emission_filter_unit is not None:
# Z FIRST runs one single-channel burst per channel (burst_plans): the host applies each channel's
# own gain, emission filter and iris values before its burst, so those need not agree between
# channels. In CHANNEL FIRST every channel shares one burst, and a burst cannot change them.
one_burst_for_all_channels = z_stack_order != "Z FIRST"
if emission_filter_unit is not None and one_burst_for_all_channels:
# The software path moves the unit at every channel switch. A burst cannot, so every channel
# has to image through the same filter: the one position the channels share is set before the
# burst. A channel without a position leaves the wheel where it is in the software path too.
Expand Down Expand Up @@ -321,7 +341,7 @@ def ineligibility_reason(
return f"channel '{channel.name}' has a z offset ({channel.z_offset_um} um), which is not sequenced yet"
if channel.source_code not in SOURCE_CODE_TO_PORT:
return f"channel '{channel.name}' is not strobed from an MCU TTL port (source code {channel.source_code})"
if len(set(camera_gains)) > 1:
if one_burst_for_all_channels and len(set(camera_gains)) > 1:
return f"the channels use different camera gain values ({sorted(set(camera_gains))}); gain is a camera register write"
if byte_budget is not None and 3 * burst_bytes > byte_budget:
# The burst dispatcher holds one burst in flight and queues one behind it (submit() blocks beyond
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ def _channel(name):
def _worker(piezo_at_um, piezo_z_range_um, nz=4, z_stacking_config="FROM BOTTOM"):
w = MultiPointWorker.__new__(MultiPointWorker)
w.z_stack_order = "CHANNEL FIRST"
w._sequenced_program = None # software-sequenced (the sequenced branch is decided once per acquisition)
w._sequenced_bursts = [] # software-sequenced (decided once per acquisition)
w.NZ = nz
# 2 µm; __init__ resolves FROM TOP's sign once, so the fixture carries it the way a built worker does.
w.deltaZ = -0.002 if z_stacking_config == "FROM TOP" else 0.002
Expand Down
69 changes: 1 addition & 68 deletions software/tests/control/test_MultiPointWorker_sequenced_retry.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
import dataclasses
import threading
from types import SimpleNamespace
from unittest.mock import MagicMock, call
from unittest.mock import MagicMock

import pytest

Expand Down Expand Up @@ -244,70 +244,3 @@ def overrun(timeout):

assert events == ["cancel"]
assert w._ready_for_next_trigger.is_set() # the worker is left consistent for whoever runs next


def _stack_worker():
"""A worker ready for _acquire_sequenced_stack with the burst itself mocked."""
w = _bare_worker()
w._applied_config = None
w.selected_configurations = [MagicMock()]
w.z_piezo_um = 20.0
w.deltaZ = 0.001 # mm
w.NZ, w.Nt, w.time_point = 3, 1, 0
w.do_reflection_af = w.do_autofocus = False
w.piezo = MagicMock()
w.piezo.range_um = 300
w.stage = MagicMock()
w._ask_user_to_intervene = MagicMock()
w.handle_acquisition_abort = MagicMock()
return w


def test_a_controller_that_did_not_stop_goes_straight_to_the_user():
"""No retry and no piezo move: the controller may still own both."""
w = _stack_worker()
w._run_sequenced_burst = MagicMock(side_effect=ControllerDidNotStop("the controller did not report it over"))

w._acquire_sequenced_stack("A1", "/tmp", 0)

w._ask_user_to_intervene.assert_called_once()
assert "did not report it over" in w._ask_user_to_intervene.call_args.args[0]
assert w._run_sequenced_burst.call_count == 1
w.piezo.move_to.assert_not_called()


def test_an_abort_after_a_failed_burst_still_puts_the_piezo_back():
"""A failed burst leaves the piezo at the last plane the controller commanded. An abort that lands on that
failure must not skip the move back: the next acquisition starts from where the cache says."""
w = _stack_worker()
w._run_sequenced_burst = MagicMock(return_value=(None, "the controller reported an error"))
w.abort_requested_fn = lambda: w._run_sequenced_burst.call_count >= 1 # the abort lands on the failed burst

w._acquire_sequenced_stack("A1", "/tmp", 0)

assert w.piezo.move_to.call_args_list == [call(20.0)]
w.handle_acquisition_abort.assert_called_once()
w._ask_user_to_intervene.assert_not_called()


def test_a_failed_burst_puts_the_piezo_back_at_the_stack_start():
"""The engine's fail() leaves the piezo at the last plane it commanded (only Done and a cancel return
to the start) while the worker's cache says the stack start; every failed attempt moves it back there,
so the retry and the next acquisition start where the cache says."""
w = _bare_worker()
w._applied_config = None
w.selected_configurations = [MagicMock()]
w.z_piezo_um = 20.0
w.deltaZ = 0.001 # mm
w.NZ, w.Nt, w.time_point = 3, 1, 0
w.do_reflection_af = w.do_autofocus = False
w.piezo = MagicMock()
w.piezo.range_um = 300
w.stage = MagicMock()
w._run_sequenced_burst = MagicMock(return_value=(None, "the controller reported an error"))
w._ask_user_to_intervene = MagicMock()

w._acquire_sequenced_stack("A1", "/tmp", 0)

assert w.piezo.move_to.call_args_list == [call(20.0)] * 3
w._ask_user_to_intervene.assert_called_once()
Loading
Loading