Repository navigation
Description
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
- via a plan decorator e.g baseline_decorator
- 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_engineThen 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
tpoliaw commentedon Nov 19, 2026
This could be useful although I think having a
configuremethod 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?The decorated method would have to take the
RunEngineand 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
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
DeviceManageris starting to become a generalBeamlineConfigurationwhich 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.oliwenmandiamond commentedon Nov 19, 2026
So the reason I went with this approach is for a couple reasons. The first is that giving access to the
RunEnginedirectly means we don't have to constantly expand the code base to wrap a new aspect of something. For example, we may want to customisemd_validator. It just simplifies everything and makes blueapi a lot less restrictive.The second point I want to address is
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
in some configuration file.
then we can do
(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
DeviceManagercreatesRunEngine()if one hasn't already been created, if it has it then passes it toDeviceManagerand again, were able to chain together configuration via the different functionse.g
and then in i09-1 I do
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!
tpoliaw commentedon Nov 19, 2026
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.
oliwenmandiamond commentedon Nov 19, 2026
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
DeviceManagerstarts with theRunEngineinstance and returns it where as you suggested blueapi creates theRunEngineinstance and passes toDeviceManagerto add additional configuration.I personally still lean on my suggestion. The reason being is that then
DeviceManagerwhen connecting devices has theRunEngineavailable for the event loop completely independent of anything else and makes it a part of the API. With your suggestion, it meansDeviceManageris still reliant on an externalRunEngine. I am not too bothered either way, but just something to consider with the designOn the note of a
BeamlineConfigurationManager, I have mentioned this for awhile, see bottom of DiamondLightSource/dodal#1997 (comment) ;)fajinyuan commentedon Nov 19, 2026
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?
DominicOram commentedon Nov 26, 2026
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.