Skip to content

Commit 4c3e6d4

Browse files
committed
Make run_number a metadata key in period-per-point mode
1 parent 90aa070 commit 4c3e6d4

File tree

2 files changed

+51
-0
lines changed

2 files changed

+51
-0
lines changed

src/ibex_bluesky_core/plans/__init__.py

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
import bluesky.plans as bp
77
import matplotlib.pyplot as plt
88
from bluesky import plan_stubs as bps
9+
from bluesky.preprocessors import inject_md_decorator
910
from bluesky.protocols import NamedMovable
1011
from bluesky.utils import Msg
1112
from matplotlib.axes import Axes
@@ -31,6 +32,17 @@
3132
]
3233

3334

35+
def _get_additional_md(
36+
dae: "SimpleDae", *, periods: bool, save_run: bool
37+
) -> Generator[Msg, None, dict[str, Any]]:
38+
if periods and save_run:
39+
run_number = yield from bps.rd(dae.current_or_next_run_number_str)
40+
return {"run_number": run_number}
41+
else:
42+
yield from bps.null()
43+
return {}
44+
45+
3446
def scan( # noqa: PLR0913
3547
dae: "SimpleDae",
3648
block: NamedMovable[float],
@@ -66,7 +78,10 @@ def scan( # noqa: PLR0913
6678

6779
icc = _set_up_fields_and_icc(block, dae, model, periods, save_run, ax)
6880

81+
additional_md = yield from _get_additional_md(dae, periods=periods, save_run=save_run)
82+
6983
@icc
84+
@inject_md_decorator(additional_md)
7085
def _inner() -> Generator[Msg, None, None]:
7186
if rel:
7287
plan = bp.rel_scan
@@ -148,7 +163,10 @@ def adaptive_scan( # noqa: PLR0913, PLR0917
148163

149164
icc = _set_up_fields_and_icc(block, dae, model, periods, save_run, ax)
150165

166+
additional_md = yield from _get_additional_md(dae, periods=periods, save_run=save_run)
167+
151168
@icc
169+
@inject_md_decorator(additional_md)
152170
def _inner() -> Generator[Msg, None, None]:
153171
if rel:
154172
plan = bp.rel_adaptive_scan

tests/plans/test_init.py

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
# pyright: reportMissingParameterType=false
2+
import functools
3+
from typing import Any
24
from unittest.mock import patch
35

6+
import bluesky.utils
47
import pytest
58
from bluesky.preprocessors import run_decorator
69
from ophyd_async.plan_stubs import ensure_connected
@@ -121,6 +124,36 @@ def test_scan_does_normal_scan_when_relative_false(RE, dae, block):
121124
assert count == bp_scan.call_args[1]["num"]
122125

123126

127+
@pytest.mark.parametrize(
128+
"scan_func",
129+
[
130+
functools.partial(scan, start=1, stop=2, num=2),
131+
functools.partial(adaptive_scan, start=1, stop=2, min_step=1, max_step=2, target_delta=1),
132+
],
133+
)
134+
def test_if_in_periods_mode_and_run_saved_then_scan_start_doc_contains_run_number(
135+
RE, dae, block, scan_func
136+
):
137+
set_mock_value(dae.current_or_next_run_number_str, "12345678")
138+
139+
start_doc: dict[str, Any] | None = None
140+
141+
def _cb(typ, doc):
142+
if typ == "start":
143+
nonlocal start_doc
144+
start_doc = doc
145+
146+
with (
147+
patch("ibex_bluesky_core.plans.ensure_connected"),
148+
):
149+
# Scan fails because DAE isn't set up right... but it still emits a start doc so that's fine
150+
with pytest.raises(bluesky.utils.FailedStatus):
151+
RE(scan_func(dae, block, rel=False, periods=True, save_run=True), _cb)
152+
153+
assert start_doc is not None
154+
assert start_doc.get("run_number") == "12345678"
155+
156+
124157
def test_scan_does_relative_scan_when_relative_true(RE, dae, block):
125158
start = 0
126159
stop = 2

0 commit comments

Comments
 (0)