Skip to content

How to run plans from the window

Add the built-in acquisition stack to a session, and you can run plans from the window. Its presenter runs the plans your components offer, one at a time, and its view is where you choose a plan, fill its parameters, and run, pause and stop it. ADR 22 explains the design.

Prerequisites

You need components offering plans, which means anything with a plan_map method, as HasPlans describes. The Python blocks below are parts of one script, and the whole script is at the end.

class MyPlans:
    def __init__(self, name: str) -> None:
        self.name = name

    def plan_map(self) -> Mapping[str, PlanEntry]:
        return {"walk": {"plan": self.walk}}

    def walk(
        self, motor: Movable[float], steps: int = 3, size: float = 1.0
    ) -> MsgGenerator[None]:
        """Move *motor* to *size*, then twice *size*, and so on for *steps* moves."""
        for step in range(1, steps + 1):
            yield from bps.mv(motor, step * size)

The presenter gathers the plans of every such component when the session sets it up. It leaves out, with a warning naming it, a plan whose signature no plan widget can show.

Declare it in a session file

The acquisition stack is built into redsun, under the plugin id acquisition, and one entry declares it:

stacks:
  acquisition:
    plugin_name: redsun
    plugin_id: acquisition

The session builds the presenter acquisition and the view acquisition_view, and pairs them, which makes the fourteen links between the two. Those are the names wiring, shortcuts and the settings use.

The same stack declared one by one
presenters:
  acquisition:
    plugin_name: redsun
    plugin_id: acquisition
views:
  acquisition_view:
    plugin_name: redsun
    plugin_id: acquisition
pairs:
  - [acquisition_view, acquisition]

Written out, the links are:

The same links under wiring
wiring:
  acquisition_view.sig_launch: acquisition.launch
  acquisition_view.sig_pause: acquisition.pause
  acquisition_view.sig_resume: acquisition.resume
  acquisition_view.sig_stop: acquisition.stop
  acquisition_view.sig_action: acquisition.request_action
  acquisition_view.sig_base_dir: acquisition.set_base_dir
  acquisition_view.sig_label: acquisition.set_label
  acquisition.sig_plan_started: acquisition_view.set_started
  acquisition.sig_plan_done: acquisition_view.set_done
  acquisition.sig_plan_stopped: acquisition_view.set_stopped
  acquisition.sig_plan_failed: acquisition_view.set_failed
  acquisition.sig_progress: acquisition_view.update_progress
  acquisition.sig_action_changed: acquisition_view.update_action
  acquisition.sig_base_dir_changed: acquisition_view.update_base_dir

Declare it in Python

In a session class, declare the stack with AsStack, and the session pairs the presenter and the view:

class MyApp(QtSession):
    config: ClassVar[dict[str, Any]] = {"session": "my-lab"}

    motor: AsDevice[MyMotor]
    plans: AsPresenter[MyPlans]
    acquisition: AsStack[AcquisitionStack]


if __name__ == "__main__":
    MyApp().run()

Use the view

The acquisition view of the example session: the walk plan chosen, the base
directory, the motor to move and the plan's parameters

  • Choose a plan in the list at the top, and the button beside it shows the plan's documentation. The plan's parameters, devices and the callbacks to attach follow below.
  • The Run button, with a play icon, starts the plan. The view shows it running once the presenter reports that it started, and the button turns into Stop. The plan's form then shows the values the run was launched with, also when another component or a script started it. A plan marked @continuous(pausable=True) also has Pause, which turns into Resume. The buttons show icons only; pointing at one shows its name. See Write a plan that runs until stopped.
  • Run stays disabled while a list of devices the plan needs is empty.
  • Ctrl+R runs the chosen plan and Ctrl+. stops the running one, from anywhere in the window; on macOS they are Command-R and Command-period. The shortcuts section of the session file changes them as acquisition_view.run_plan and acquisition_view.stop_plan.
  • When a plan raises, its plan widget shows "failed:" and the error until it runs again, and the session log holds the full traceback. A plan you stopped shows "stopped" the same way.
  • Each run's files are named after its plan. "Choose root..." sets the directory runs write under, and "Browse root" opens it in the system's file browser.
  • A label typed in the field below the root, such as a sample name, leads the file names of every later run: liver-03_walk_00000 instead of walk_00000. A run already going keeps its names, so the field is locked while a plan runs. Leave it empty for no label.
  • The session's Settings keep the plan you chose last, under the view's name, and the next session offers it again.

Share the engine

The presenter owns the session's RunEngine and its Deferrals, and shares both. Any component that asks for a RunEngine or Deferrals in setup receives the presenter's own.

class MyController:
    def setup(self, engine: RunEngine, deferrals: Deferrals) -> None:
        self.engine = engine
        self.deferrals = deferrals

A second acquisition presenter stops the session from building

Two acquisition presenters would both share a RunEngine, and the session refuses to build. Declare one.

The presenter also passes on the engine's locks. The views and presenters of the light stack and the positioner take them through a pairing each:

pairs:
  - [acquisition, lights_view]
  - [acquisition, lights]
  - [acquisition, positioner_view]
  - [acquisition, positioner]

Offer actions

A component whose plans wait for the user, through an ActionManager held as actions, is a HasActions. The presenter passes on the state of every such component's actions, so their buttons in the plan widget follow them with no link of their own, and a press reaches the component of the running plan.

The example in full

The whole script
"""A session running plans from the window with the built-in acquisition stack."""

from __future__ import annotations

from collections.abc import Mapping
from functools import cached_property
from typing import Any, ClassVar

import bluesky.plan_stubs as bps
from bluesky.protocols import Movable
from bluesky.utils import MsgGenerator
from ophyd_async.core import (
    MovableLogic,
    StandardMovable,
    StandardReadable,
    soft_signal_rw,
)

from redsun import AsDevice, AsPresenter, AsStack, PlanEntry
from redsun.qt import QtSession
from redsun.view.qt.builtins import AcquisitionStack


class MyMotor(StandardReadable, StandardMovable[float]):
    def __init__(self, name: str = "") -> None:
        with self.add_children_as_readables():
            self.position = soft_signal_rw(float, 0.0, units="mm")
        super().__init__(name=name)

    @cached_property
    def movable_logic(self) -> MovableLogic[float]:
        return MovableLogic(setpoint=self.position, readback=self.position)


class MyPlans:
    def __init__(self, name: str) -> None:
        self.name = name

    def plan_map(self) -> Mapping[str, PlanEntry]:
        return {"walk": {"plan": self.walk}}

    def walk(
        self, motor: Movable[float], steps: int = 3, size: float = 1.0
    ) -> MsgGenerator[None]:
        """Move *motor* to *size*, then twice *size*, and so on for *steps* moves."""
        for step in range(1, steps + 1):
            yield from bps.mv(motor, step * size)


class MyApp(QtSession):
    config: ClassVar[dict[str, Any]] = {"session": "my-lab"}

    motor: AsDevice[MyMotor]
    plans: AsPresenter[MyPlans]
    acquisition: AsStack[AcquisitionStack]


if __name__ == "__main__":
    MyApp().run()