Evaluation#

per_suggestion#

bluesky_adaptive.evaluation.per_suggestion(evaluate, fetch)[source]#

Build an evaluation function from a function of one suggestion’s data.

The evaluation function applies evaluate to each value of fetch(uid) and sets the outcome’s "_id" to its key.

Parameters:
evaluateCallable[[_TData], Mapping[str, Any]]

Returns the outcome of one suggestion’s data.

fetchCallable[[TUid], Mapping[Any, _TData]]

Returns each suggestion’s data keyed by suggestion ID, e.g. RunEventBuffer.pop.

Returns:
EvaluationFunction[TUid]

The evaluation function.

Examples

>>> from bluesky_adaptive.evaluation import per_suggestion
>>> def score(data):
...     return {"objective": 10 * data["x"]}
>>> evaluation_function = per_suggestion(score, lambda uid: {"a": {"x": 1.0}, "b": {"x": 2.0}})
>>> evaluation_function("run-uid", [])
[{'objective': 10.0, '_id': 'a'}, {'objective': 20.0, '_id': 'b'}]

RunEventBuffer#

class bluesky_adaptive.evaluation.documents.RunEventBuffer[source]#

Bases: CallbackBase

Buffer the "primary" event data of acquisition runs by suggestion ID.

Subscribe with RE.subscribe(buffer) and pass pop() to per_suggestion(). Buffers runs that record start["bluesky-adaptive"]["acquisition_order"], such as from default_acquire(), pairing the n-th event with the n-th suggestion ID.

start(doc)[source]#

Begin buffering the run if it records its acquisition order.

descriptor(doc)[source]#

Track the "primary" descriptor of buffered runs.

event(doc)[source]#

Buffer "primary" event data.

pop(uid)[source]#

Remove a buffered run and return its event data keyed by suggestion ID.

Parameters:
uidstr

Run UID returned by the acquisition plan.

Returns:
dict[Hashable, dict[str, Any]]

Event data keyed by suggestion ID, in acquisition order.

Raises:
KeyError

If the run is not buffered.

ValueError

If the number of "primary" events differs from the number of suggestion IDs.

TiledRunPoller#

class bluesky_adaptive.evaluation.tiled.TiledRunPoller(client, *, poll_interval=0.5, timeout=60.0)[source]#

Bases: object

Poll Tiled for a stopped acquisition run and return its "primary" data by suggestion ID.

Pass an instance to per_suggestion(). Every call polls Tiled until the run has a stop document, so the run’s documents may reach Tiled after the acquisition plan returns, e.g. through a separate TiledWriter service. Reads runs that record start["bluesky-adaptive"]["acquisition_order"], such as from default_acquire(), pairing the n-th "primary" row with the n-th suggestion ID.

Parameters:
clientContainer

Tiled container keyed by run UID that a TiledWriter writes to, e.g. tiled.client.from_uri(...) or a CatalogOfBlueskyRuns.

poll_intervalfloat, optional

Seconds between polls. Default is 0.5.

timeoutfloat | None, optional

Seconds to wait for the run’s stop document before raising TimeoutError, or None to wait indefinitely. Default is 60.

Raises:
ValueError

If poll_interval is not positive or timeout is negative.

__call__(uid)[source]#

Wait for a run to stop and return its "primary" data keyed by suggestion ID.

Parameters:
uidstr

Run UID returned by the acquisition plan.

Returns:
dict[Hashable, dict[str, Any]]

Each "primary" row as a mapping of data key to value, keyed by suggestion ID in acquisition order.

Raises:
TimeoutError

If the run has no stop document in Tiled within timeout seconds.

KeyError

If the run does not record its acquisition order.

ValueError

If the number of "primary" rows differs from the number of suggestion IDs.