Skip to content

Commit 9641af2

Browse files
authored
Merge pull request #68 from DiamondLightSource/66-update-system-testp99
66 update system test and bug fixes for test.
2 parents b2b1ac3 + d29eb82 commit 9641af2

File tree

18 files changed

+717
-174
lines changed

18 files changed

+717
-174
lines changed

demo/p99/MVP_demo.ipynb

Lines changed: 376 additions & 0 deletions
Large diffs are not rendered by default.

demo/p99/stxm_demo_fast_step.ipynb

Lines changed: 11 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
},
1111
{
1212
"cell_type": "code",
13-
"execution_count": 1,
13+
"execution_count": null,
1414
"id": "2121d7fe-965b-4fed-94aa-897c7133d689",
1515
"metadata": {},
1616
"outputs": [],
@@ -19,7 +19,8 @@
1919
"from blueapi.client.event_bus import AnyEvent\n",
2020
"from blueapi.config import ApplicationConfig, RestConfig, StompConfig\n",
2121
"from blueapi.worker.task import Task\n",
22-
"from bluesky_stomp.models import BasicAuthentication"
22+
"from bluesky_stomp.models import BasicAuthentication\n",
23+
"from pydantic import HttpUrl"
2324
]
2425
},
2526
{
@@ -36,18 +37,18 @@
3637
},
3738
{
3839
"cell_type": "code",
39-
"execution_count": 3,
40+
"execution_count": null,
4041
"id": "424f8c5c-98df-4e5e-a0ab-356f3e3b6111",
4142
"metadata": {},
4243
"outputs": [],
4344
"source": [
44-
"p99Config = ApplicationConfig(\n",
45-
" stomp=StompConfig(\n",
46-
" host=\"172.23.177.208\",\n",
47-
" auth=BasicAuthentication(username=\"p99\", password=\"\"), # type: ignore\n",
48-
" ),\n",
49-
" api=RestConfig(host=\"p99-blueapi.diamond.ac.uk\", port=443, protocol=\"https\"),\n",
50-
" )"
45+
"p99Config =ApplicationConfig(\n",
46+
" stomp=StompConfig(\n",
47+
" url=HttpUrl(\"http://172.23.177.208\"),\n",
48+
" auth=BasicAuthentication(username=\"p99\", password=password), # type: ignore\n",
49+
" ),\n",
50+
" api=RestConfig(url=HttpUrl(\"https://p99-blueapi.diamond.ac.uk:443\")),\n",
51+
" )"
5152
]
5253
},
5354
{

docs/how-to/42_trobleshooting.rst

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
Troubleshooting
2+
===============
3+
4+
This section provides solutions to common problems you may encounter.
5+
6+
.. contents::
7+
:local:
8+
:depth: 2
9+
10+
Data Writing Issues
11+
~~~~~~~~~~~~~~~~~~~
12+
13+
**Problem:**
14+
The Nexus writer is not creating a .nxs file.
15+
16+
**Solution:**
17+
18+
- Ensure that the output directory exists and that the nexus-file-converter service has write permissions.
19+
- Verify that the RabbitMQ configuration is correct and able to handle messages from BlueAPI.
20+
- Add detectors as metadata so that the nexus-file-converter can identify them, rather than relying on discovery from the data stream.
21+
22+
.. code-block:: python
23+
24+
md["detectors"] = [det.name for det in dets]
25+
md["motors"] = [scan_motor.name, step_motor.name]
26+
27+
Common Issues
28+
~~~~~~~~~~~~~
29+
30+
.. _installation-issues:
31+
32+
Installation Issues
33+
~~~~~~~~~~~~~~~~~~~
34+
35+
.. _dependency-errors:
36+
37+
Dependency Errors
38+
~~~~~~~~~~~~~~~~~
39+
40+
.. _runtime-errors:

src/sm_bluesky/beamlines/p99/plans/__init__.py

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
P99_DEFAULT_METADATA = {
55
"energy": {"value": 1.8, "unit": "eV"},
66
"detector_dist": {"value": 88, "unit": "mm"},
7+
"pixel_size": {"value": 16, "unit": "µm"},
78
}
89

910
stxm_step = add_default_metadata(grid_step_scan, P99_DEFAULT_METADATA)
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
1-
from .add_meta import add_default_metadata
1+
from .add_meta import add_default_metadata, add_extra_names_to_meta
22

3-
__all__ = ["add_default_metadata"]
3+
__all__ = ["add_default_metadata", "add_extra_names_to_meta"]
Lines changed: 30 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,24 +1,46 @@
11
from collections.abc import Callable
22
from functools import wraps
3-
from typing import TypeVar, cast
3+
from typing import Any, TypeVar, cast
44

55
from blueapi.core import MsgGenerator
66

77
TCallable = TypeVar("TCallable", bound=Callable[..., MsgGenerator])
88

99

1010
def add_default_metadata(
11-
funcs: TCallable, extra_metadata: dict | None = None
11+
func: TCallable, extra_metadata: dict[str, Any] | None = None
1212
) -> TCallable:
13-
@wraps(funcs)
13+
"""
14+
Decorator to add or update default metadata in the 'md' keyword argument.
15+
16+
If 'md' is not provided, it will be set to extra_metadata.
17+
If 'md' is provided and not None, it will be updated with extra_metadata.
18+
If 'md' is provided and is None, it will be set to extra_metadata.
19+
"""
20+
21+
@wraps(func)
1422
def inner(
1523
*args,
1624
**kwargs,
1725
) -> MsgGenerator:
18-
if "md" in kwargs:
19-
kwargs["md"].update(extra_metadata)
20-
else:
21-
kwargs["md"] = extra_metadata
22-
yield from (funcs(*args, **kwargs))
26+
md = kwargs.get("md")
27+
if extra_metadata:
28+
if md is None:
29+
kwargs["md"] = extra_metadata
30+
elif isinstance(md, dict):
31+
kwargs["md"] = {**md, **extra_metadata}
32+
else:
33+
raise ValueError("md is reserved for meta data.")
34+
elif md is None:
35+
kwargs["md"] = {}
36+
return func(*args, **kwargs)
2337

2438
return cast(TCallable, inner)
39+
40+
41+
def add_extra_names_to_meta(md: dict, key: str, names: list[str]):
42+
if key in md:
43+
md[key] = md[key] + names
44+
return md
45+
md[key] = names
46+
return md

src/sm_bluesky/common/plan_stubs/motions.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -168,7 +168,7 @@ def get_velocity_and_step_size(
168168
max_velocity = yield from bps.rd(scan_motor.max_velocity) # type: ignore
169169
# if motor does not move fast enough increase step_motor step size
170170
if ideal_velocity > max_velocity:
171-
ideal_step_size = ideal_step_size / (ideal_velocity / max_velocity)
171+
ideal_step_size = ideal_step_size * (ideal_velocity / max_velocity)
172172
ideal_velocity = round(max_velocity, 3)
173173

174174
return ideal_velocity, ideal_step_size

src/sm_bluesky/common/plans/__init__.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
66
"""
77

8-
from .ad_plans import tigger_img
8+
from .ad_plans import trigger_img
99
from .alignments import (
1010
StatPosition,
1111
align_slit_with_look_up,
@@ -24,5 +24,5 @@
2424
"fast_scan_grid",
2525
"grid_fast_scan",
2626
"grid_step_scan",
27-
"tigger_img",
27+
"trigger_img",
2828
]

src/sm_bluesky/common/plans/ad_plans.py

Lines changed: 13 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,18 @@
1+
from typing import Any
2+
13
from blueapi.core import MsgGenerator
24
from bluesky import plan_stubs as bps
35
from bluesky import preprocessors as bpp
46
from bluesky.utils import Msg, plan
7+
from dodal.plan_stubs.data_session import attach_data_session_metadata_decorator
58
from ophyd_async.epics.adandor import Andor2Detector
69

710

811
@plan
9-
def tigger_img(dets: Andor2Detector, value: int) -> MsgGenerator:
12+
@attach_data_session_metadata_decorator()
13+
def trigger_img(
14+
dets: Andor2Detector, acquire_time: int, md: dict[str, Any] | None = None
15+
) -> MsgGenerator:
1016
"""
1117
Set the acquire time and trigger the detector to read data.
1218
@@ -22,11 +28,13 @@ def tigger_img(dets: Andor2Detector, value: int) -> MsgGenerator:
2228
MsgGenerator
2329
A Bluesky generator for triggering the detector.
2430
"""
25-
yield Msg("set", dets.driver.acquire_time, value)
31+
if md is None:
32+
md = {}
33+
yield Msg("set", dets.driver.acquire_time, acquire_time)
2634

2735
@bpp.stage_decorator([dets])
28-
@bpp.run_decorator()
29-
def innertigger_img():
36+
@bpp.run_decorator(md=md)
37+
def inner_trigger_img():
3038
return (yield from bps.trigger_and_read([dets]))
3139

32-
yield from innertigger_img()
40+
yield from inner_trigger_img()

src/sm_bluesky/common/plans/alignments.py

Lines changed: 11 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from collections.abc import Callable
22
from enum import Enum
33
from functools import wraps
4-
from typing import TypeVar, cast
4+
from typing import ParamSpec, TypeVar
55

66
from bluesky import preprocessors as bpp
77
from bluesky.callbacks.fitting import PeakStats
@@ -38,10 +38,14 @@ class StatPosition(tuple, Enum):
3838
D_MAX = ("derivative_stats", "max")
3939

4040

41-
TCallable = TypeVar("TCallable", bound=Callable)
41+
P = ParamSpec("P")
42+
T = TypeVar("T")
43+
TCallable = Callable[
44+
P, T
45+
] # Type for callable functions with parameters P and return type T
4246

4347

44-
def scan_and_move_to_fit_pos(funcs: TCallable) -> TCallable:
48+
def scan_and_move_to_fit_pos(func: TCallable) -> TCallable:
4549
"""
4650
Wrapper to add a PeakStats callback before performing a scan
4751
and move to the fitted position after the scan.
@@ -57,30 +61,30 @@ def scan_and_move_to_fit_pos(funcs: TCallable) -> TCallable:
5761
The wrapped scan function.
5862
"""
5963

60-
@wraps(funcs)
64+
@wraps(func)
6165
def inner(
6266
det: StandardReadable,
6367
motor: Motor,
6468
fitted_loc: StatPosition,
6569
detname_suffix: str,
6670
*args,
6771
**kwargs,
68-
):
72+
) -> MsgGenerator:
6973
ps = PeakStats(
7074
f"{motor.name}",
7175
f"{det.name}-{detname_suffix}",
7276
calc_derivative_and_stats=True,
7377
)
7478
yield from bpp.subs_wrapper(
75-
funcs(det, motor, fitted_loc, detname_suffix, *args, **kwargs),
79+
func(det, motor, fitted_loc, detname_suffix, *args, **kwargs),
7680
ps,
7781
)
7882
peak_position = get_stat_loc(ps, fitted_loc)
7983

8084
LOGGER.info(f"Fit info {ps}")
8185
yield from abs_set(motor, peak_position, wait=True)
8286

83-
return cast(TCallable, inner)
87+
return inner
8488

8589

8690
@plan

0 commit comments

Comments
 (0)