Skip to content

Support being able to configure baseline data with DeviceManager #1784

Open

Description

@oliwenmandiamond

Within Bluesky, you are able to record baseline data. This is so that whenever a plan is ran which collects data, these devices will always record data at start and end of scan. This currently done in GDA with before_scan metadata. There are currently two ways (I think) to achieve this

  1. via a plan decorator e.g baseline_decorator
  2. Add preprocessor SupplementalData to RunEngine (doc string below)
   Create an instance of SupplementalData and apply it to a RunEngine.

    >>> sd = SupplementalData(baseline=[some_motor, some_detector]),
    ...                       monitors=[some_signal],
    ...                       flyers=[some_flyer])
    >>> RE = RunEngine({})
    >>> RE.preprocessors.append(sd)

    Now all plans executed by RE will be modified to add baseline readings
    (before and after each run), monitors (during each run), and flyers
    (kicked off before each run and collected afterward).

Currently, the only way for us to add metadata is via option 1. The issue with this is we are now forced to wrap ever single plan we have with this decorator. It would much easier and simpler if we can add global metadata to be added to each run via option 2. The issue is Blueapi has no way of exposing this utils to us.

I've had a thought how we could do this. The first option is to have DeviceManager have an instance of RunEngine inside which blueapi then pulls out. This allows us to then add our own configuration to the RunEngine. My thinking was something like this

devices = DeviceManager()

@devices.factory
def my_device1() -> Device:
     return MyDevice(...)

@devices.factory
def my_device2() -> Device:
     return MyDevice(...)

...
@devices.run_engine
def configure_run_engine(beamline: dict[str: Device]) -> RunEngine:
   run_engine = RunEngine()
   sd = SupplementalData(
        baseline=[beamline["my_device1"]],
        monitors=[beamline["my_device2"]],
        flyers=[beamline["my_device3"]]
    )
    run_engine.preprocessors.append(sd)
    return run_engine

Then on Blueapi side, you update DeviceManager to support a run_engine method and then in BlueskyContext, you update to use the one we provided rather than your own. You then reuse what we have then add your additional blueapi configuration.

Acceptance Criteria

  • We can configure run engine with baseline metadata

Activity

  1. tpoliaw commented on Nov 19, 2026

    @tpoliaw
    Contributor

    This could be useful although I think having a configure method for the run engine would work better, especially if we need to support multiple device managers in a single blueapi instance. Would something like the following work?

    @devices.init_run_engine
    def add_baseline(run_engine: RunEngine, some_motor: Motor, some_monitor: Monitor):
        ...

    The decorated method would have to take the RunEngine and then any devices it requires. Blueapi or the devicemanager then passes the actual devices in the same way it currently handles fixtures?

    This would also allow different configuration to be done in different functions, eg

    @devices.init_run_engine
    def add_baseline(run_engine: RunEngine, ...): ...
    
    @devices.init_run_engine
    def add_beam_monitor(run_engine: RunEngine, ...): ...

    Not replacing the runengine also feels like it would cause fewer problems as well.

    I will have a think about any other repercussions of this. It's starting to look like the DeviceManager is starting to become a general BeamlineConfiguration which might not be a bad thing but it's worth looking at alternatives. Might go some way towards mitigating the need for using blueapi as a library as well.

  2. added
    enhancement
    needs investigation
    on Nov 19, 2026
  3. oliwenmandiamond commented on Nov 19, 2026

    @oliwenmandiamond
    ContributorAuthor

    So the reason I went with this approach is for a couple reasons. The first is that giving access to the RunEngine directly means we don't have to constantly expand the code base to wrap a new aspect of something. For example, we may want to customise md_validator. It just simplifies everything and makes blueapi a lot less restrictive.

    The second point I want to address is

    @devices.init_run_engine
    def add_baseline(run_engine: RunEngine, some_motor: Motor, some_monitor: Monitor): 

    My issue with passing the devices as a direct dependency for a baseline is that this will be huge!!! The metadata and number of devices recorded for a beamline can be massive. This would result in the factory function args to basically duplicate every device already defined of the entire beamline which I can forsee just becoming silly on the size of a method. So a dictionary is much simpler, then each beamline can define

    BASELINE_METADATA_DEVICES = [
         "stage",
         "slit1",
         "slit2",
         ...
    ]

    in some configuration file.

    then we can do

       sd = SupplementalData(
            baseline=[beamline[device_name] for device_name in BASELINE_METADATA],
        )

    (maybe turn the above into some helper util, but you get the idea).

    Do we still have a use case for multiple DeviceManagers? I don't think this is needed because we have the include method so you can chain them together to form one device manager instance. I think we can still support this by by making it this signature instead

    @devices.run_engine
    def configure_iXX(run_engine: RunEngine, beamline: dict[str: Device]) -> RunEngine:
       sd = SupplementalData(
            baseline=[beamline["my_device1"]],
            monitors=[beamline["my_device2"]],
            flyers=[beamline["my_device3"]]
        )
        run_engine.preprocessors.append(sd)
        return run_engine

    DeviceManager creates RunEngine() if one hasn't already been created, if it has it then passes it to DeviceManager and again, were able to chain together configuration via the different functions

    e.g

    @devices.run_engine
    def configure_i09_shared_baseline(run_engine: RunEngine, beamline: dict[str: Device]) -> RunEngine:
       sd = SupplementalData(
            baseline=[beamline["shared-i09-device"]],
        )
        run_engine.preprocessors.append(sd)
        return run_engine

    and then in i09-1 I do

    @devices.run_engine
    def configure_i09_1_baseline(run_engine: RunEngine, beamline: dict[str: Device], fixtures: dict[str, obj]) -> RunEngine:
       sd = SupplementalData(
            baseline=[beamline["i09_1_device"]],
        )
        run_engine.preprocessors.append(sd)
        run_engine.suspenders.append
        return run_engine

    and then with blueapi you add your configuration at the end which rules them all of course! We cannot change those.

    I just think a solution like the above is possible with chaining together configuration, I don't want us support teams to keep raising tickets with blueapi to let us do x with bluesky and you needing to expose it, it would mean less work in long term I think. We can make some standard setup functions in dodal so beamlines look more or less the same with the flexibility of being able to add your super specific use case without bothering core every time

    Happy to discuss more ideas though!

  4. tpoliaw commented on Nov 19, 2026

    @tpoliaw
    Contributor

    Fair point about the number of arguments, it would just be good to be able to validate a configuration without running anything.

    Would be worth discussing this one in person though - I think we're both proposing the same idea.

  5. oliwenmandiamond commented on Nov 19, 2026

    @oliwenmandiamond
    ContributorAuthor

    Yes, after discussing in IRL we are discussing more or less the same, I was in a rush reading your example walking over to the meeting, apologies. The only difference I think we had is I suggested DeviceManager starts with the RunEngine instance and returns it where as you suggested blueapi creates the RunEngine instance and passes to DeviceManager to add additional configuration.

    I personally still lean on my suggestion. The reason being is that then DeviceManager when connecting devices has the RunEngine available for the event loop completely independent of anything else and makes it a part of the API. With your suggestion, it means DeviceManager is still reliant on an external RunEngine. I am not too bothered either way, but just something to consider with the design

    On the note of a BeamlineConfigurationManager, I have mentioned this for awhile, see bottom of DiamondLightSource/dodal#1997 (comment) ;)

  6. fajinyuan commented on Nov 19, 2026

    @fajinyuan
    Contributor

    I am agree with using SupplementalData class.
    Have we thinked to pass a default dictionary as md parameters to run engine directly and add support in blueapi for this default dict being appended to any dynamic metadata dict user specified?

  7. DominicOram commented on Nov 26, 2026

    @DominicOram
    Contributor

    One other usecase for this is that beamline staff will want to add/remove baseline devices easily. i.e. they add it once and now all scans going forward also include this baseline device.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    c: contextenhancementneeds investigation

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions