Skip to content

Commit

Permalink
(DiamondLightSource/dodalDiamondLightSource/hyperion#442) Update for …
Browse files Browse the repository at this point in the history
…ophyd_async attenuator (DiamondLightSource/hyperion#1399)
  • Loading branch information
DominicOram authored May 22, 2024
1 parent 8c46f4f commit 3ee4b6e
Show file tree
Hide file tree
Showing 12 changed files with 36 additions and 42 deletions.
2 changes: 1 addition & 1 deletion setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ install_requires =
ophyd-async >= 0.3a5
bluesky >= 1.13.0a3
blueapi >= 0.4.3-a1
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@ba2e0da424cf7a82e724f278b668987575cf8a52
dls-dodal @ git+https://github.com/DiamondLightSource/dodal.git@4f1376592e8e2a155b903147e869efe82c3b8288

[options.entry_points]
console_scripts =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def _handle_ispyb_transmission_flux_read(self, doc) -> Sequence[ScanDataInfo]:
hwscan_data_collection_info = DataCollectionInfo(
flux=doc["data"]["flux_flux_reading"]
)
if transmission := doc["data"]["attenuator_actual_transmission"]:
if transmission := doc["data"]["attenuator-actual_transmission"]:
# Ispyb wants the transmission in a percentage, we use fractions
hwscan_data_collection_info.transmission = transmission * 100
event_energy = doc["data"]["dcm-energy_in_kev"]
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def activity_gated_event(self, doc: Event):
) = create_beam_and_attenuator_parameters(
data["dcm-energy_in_kev"],
data["flux_flux_reading"],
data["attenuator_actual_transmission"],
data["attenuator-actual_transmission"],
)
if event_descriptor.get("name") == CONST.DESCRIPTORS.NEXUS_READ:
NEXUS_LOGGER.info(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,7 +88,7 @@ def activity_gated_event(self, doc: Event) -> Event | None:
) = create_beam_and_attenuator_parameters(
data["dcm-energy_in_kev"],
data["flux_flux_reading"],
data["attenuator_actual_transmission"],
data["attenuator-actual_transmission"],
)
if event_descriptor.get("name") == CONST.DESCRIPTORS.NEXUS_READ:
NEXUS_LOGGER.info(
Expand Down
20 changes: 11 additions & 9 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -370,15 +370,17 @@ def robot(done_status):


@pytest.fixture
def attenuator():
with patch(
"dodal.devices.attenuator.await_value",
return_value=Status(done=True, success=True),
autospec=True,
):
attenuator = i03.attenuator(fake_with_ophyd_sim=True)
attenuator.actual_transmission.sim_put(0.49118047952) # type: ignore
yield attenuator
def attenuator(RE):
attenuator = i03.attenuator(fake_with_ophyd_sim=True)
set_mock_value(attenuator.actual_transmission, 0.49118047952)

@AsyncStatus.wrap
async def fake_attenuator_set(val):
set_mock_value(attenuator.actual_transmission, val)

attenuator.set = MagicMock(side_effect=fake_attenuator_set)

yield attenuator


@pytest.fixture
Expand Down
25 changes: 9 additions & 16 deletions tests/unit_tests/device_setup_plans/test_xbpm_feedback.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,20 +15,12 @@


@pytest.fixture
def fake_devices():
def fake_devices(attenuator: Attenuator):
xbpm_feedback: XBPMFeedback = make_fake_device(XBPMFeedback)(name="xbpm")
attenuator: Attenuator = make_fake_device(Attenuator)(name="atten")

def fake_attenuator_set(val):
attenuator.actual_transmission.sim_put(val) # type: ignore
return Status(done=True, success=True)

attenuator.set = MagicMock(side_effect=fake_attenuator_set)

return xbpm_feedback, attenuator


def test_given_xpbm_checks_pass_when_plan_run_with_decorator_then_run_as_expected(
async def test_given_xpbm_checks_pass_when_plan_run_with_decorator_then_run_as_expected(
fake_devices,
):
xbpm_feedback: XBPMFeedback = fake_devices[0]
Expand All @@ -39,7 +31,8 @@ def test_given_xpbm_checks_pass_when_plan_run_with_decorator_then_run_as_expecte
xbpm_feedback, attenuator, expected_transmission
)
def my_collection_plan():
assert attenuator.actual_transmission.get() == expected_transmission
read_transmission = yield from bps.rd(attenuator.actual_transmission)
assert read_transmission == expected_transmission
assert xbpm_feedback.pause_feedback.get() == xbpm_feedback.PAUSE
yield from bps.null()

Expand All @@ -48,11 +41,11 @@ def my_collection_plan():
RE = RunEngine()
RE(my_collection_plan())

assert attenuator.actual_transmission.get() == 1.0
assert await attenuator.actual_transmission.get_value() == 1.0
assert xbpm_feedback.pause_feedback.get() == xbpm_feedback.RUN


def test_given_xbpm_checks_fail_when_plan_run_with_decorator_then_plan_not_run(
async def test_given_xbpm_checks_fail_when_plan_run_with_decorator_then_plan_not_run(
fake_devices,
):
xbpm_feedback: XBPMFeedback = fake_devices[0]
Expand All @@ -75,11 +68,11 @@ def my_collection_plan():
RE(my_collection_plan())

mock.assert_not_called()
assert attenuator.actual_transmission.get() == 1.0
assert await attenuator.actual_transmission.get_value() == 1.0
assert xbpm_feedback.pause_feedback.get() == xbpm_feedback.RUN


def test_given_xpbm_checks_pass_and_plan_fails_when_plan_run_with_decorator_then_cleaned_up(
async def test_given_xpbm_checks_pass_and_plan_fails_when_plan_run_with_decorator_then_cleaned_up(
fake_devices,
):
xbpm_feedback: XBPMFeedback = fake_devices[0]
Expand All @@ -101,5 +94,5 @@ def my_collection_plan():
with pytest.raises(MyException):
RE(my_collection_plan())

assert attenuator.actual_transmission.get() == 1.0
assert await attenuator.actual_transmission.get_value() == 1.0
assert xbpm_feedback.pause_feedback.get() == xbpm_feedback.RUN
2 changes: 1 addition & 1 deletion tests/unit_tests/experiment_plans/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ def make_event_doc(data, descriptor="abc123") -> Event:
}

BASIC_POST_SETUP_DOC = {
"attenuator_actual_transmission": 0,
"attenuator-actual_transmission": 0,
"flux_flux_reading": 10,
"dcm-energy_in_kev": 11.105,
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -187,8 +187,8 @@ def test_read_hardware_for_ispyb_updates_from_ophyd_devices(
)

transmission_test_value = 0.01
fake_fgs_composite.attenuator.actual_transmission.sim_put( # type: ignore
transmission_test_value
set_mock_value(
fake_fgs_composite.attenuator.actual_transmission, transmission_test_value
)

current_energy_kev_test_value = 12.05
Expand Down Expand Up @@ -260,7 +260,7 @@ def test_read_hardware_for_ispyb_updates_from_ophyd_devices(
assert_event(
test_ispyb_callback.activity_gated_event.mock_calls[1], # pyright: ignore
{
"attenuator_actual_transmission": transmission_test_value,
"attenuator-actual_transmission": transmission_test_value,
"flux_flux_reading": flux_test_value,
"dcm-energy_in_kev": current_energy_kev_test_value,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -41,8 +41,6 @@ def fake_create_devices() -> OptimizeAttenuationComposite:
)
xspress3mini = i03.xspress3mini(fake_with_ophyd_sim=True, wait_for_connection=True)
attenuator = i03.attenuator(fake_with_ophyd_sim=True, wait_for_connection=True)
for device in attenuator.get_actual_filter_state_list():
device.sim_put(0) # pyright: ignore

return OptimizeAttenuationComposite(
sample_shutter=sample_shutter, xspress3mini=xspress3mini, attenuator=attenuator
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -138,8 +138,8 @@ def test_read_hardware_for_ispyb_updates_from_ophyd_devices(
)

transmission_test_value = 0.01
fake_fgs_composite.attenuator.actual_transmission.sim_put( # type: ignore
transmission_test_value
set_mock_value(
fake_fgs_composite.attenuator.actual_transmission, transmission_test_value
)
ap_sg_test_value = {
"name": "Small",
Expand Down Expand Up @@ -196,7 +196,7 @@ def test_read_hardware_for_ispyb_updates_from_ophyd_devices(
assert_event(
test_ispyb_callback.activity_gated_event.mock_calls[1], # pyright: ignore
{
"attenuator_actual_transmission": transmission_test_value,
"attenuator-actual_transmission": transmission_test_value,
"flux_flux_reading": flux_test_value,
},
)
Expand Down
4 changes: 2 additions & 2 deletions tests/unit_tests/external_interaction/callbacks/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ class TestData:
"descriptor": "bd45c2e5-2b85-4280-95d7-a9a15800a78b",
"time": 2666604299.928203,
"data": {
"attenuator_actual_transmission": 0.98,
"attenuator-actual_transmission": 0.98,
"flux_flux_reading": 9.81,
"dcm-energy_in_kev": 11.105,
},
Expand Down Expand Up @@ -210,7 +210,7 @@ class TestData:
"descriptor": "bd45c2e5-2b85-4280-95d7-a9a15800a78b",
"time": 2666604299.928203,
"data": {
"attenuator_actual_transmission": 1,
"attenuator-actual_transmission": 1,
"flux_flux_reading": 10,
"dcm-energy_in_kev": 11.105,
},
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
from dodal.devices.flux import Flux
from event_model import RunStart
from ophyd.sim import make_fake_device
from ophyd_async.core import set_mock_value
from ophyd_async.core import DeviceCollector, set_mock_value

from hyperion.device_setup_plans.read_hardware_for_setup import (
read_hardware_for_ispyb_during_collection,
Expand Down Expand Up @@ -85,7 +85,8 @@ def fake_rotation_scan(
after_open_do: Callable | None = None,
after_main_do: Callable | None = None,
):
attenuator = make_fake_device(Attenuator)(name="attenuator")
with DeviceCollector(mock=True):
attenuator = Attenuator("", "attenuator")
flux = make_fake_device(Flux)(name="flux")
eiger = make_fake_device(EigerDetector)(name="eiger")
dcm = i03.dcm(fake_with_ophyd_sim=True)
Expand Down

0 comments on commit 3ee4b6e

Please sign in to comment.