Skip to content

Commit

Permalink
Make AravisDetector facility generic
Browse files Browse the repository at this point in the history
  • Loading branch information
DiamondJoseph committed Apr 11, 2024
1 parent 4758850 commit cfca527
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 28 deletions.
10 changes: 6 additions & 4 deletions src/ophyd_async/epics/areadetector/aravis.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,16 @@ class ADAravisDetector(StandardDetector, HasHints):
def __init__(
self,
prefix: str,
directory_provider: DirectoryProvider,
name: str,
directory_provider: DirectoryProvider,
driver: ADAravisDriver,
hdf: NDFileHDF,
gpio_number: ADAravisController.GPIO_NUMBER = 1,
**scalar_sigs: str,
):
# Must be child of Detector to pick up connect()
self.drv = ADAravisDriver(prefix + "DET:")
self.hdf = NDFileHDF(prefix + "HDF5:")
self.drv = driver
self.hdf = hdf

super().__init__(
ADAravisController(self.drv, gpio_number=gpio_number),
Expand All @@ -50,7 +52,7 @@ def __init__(
)

async def _prepare(self, value: TriggerInfo) -> None:
await self._controller._fetch_deadtime()
await self.drv._fetch_deadtime()
await super()._prepare(value)

def get_external_trigger_gpio(self):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,9 @@ class ADAravisController(DetectorControl):
def __init__(self, driver: ADAravisDriver, gpio_number: GPIO_NUMBER) -> None:
self._drv = driver
self.gpio_number = gpio_number
self.dead_time: Optional[float] = None

def get_deadtime(self, exposure: float) -> float:
return self.dead_time or 0

async def _fetch_deadtime(self) -> None:
self.dead_time = await self._drv._fetch_deadtime()
return self._drv.dead_time or 0

async def arm(
self,
Expand Down Expand Up @@ -76,7 +72,7 @@ def _get_trigger_info(
else:
return (
ADAravisTriggerMode.on,
ADAravisTriggerSource(f"line_{self.gpio_number}"),
ADAravisTriggerSource[f"line_{self.gpio_number}"],
)

async def disarm(self):
Expand Down
5 changes: 3 additions & 2 deletions src/ophyd_async/epics/areadetector/drivers/aravis_driver.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
from enum import Enum
from math import nan
from typing import Callable, Dict, Tuple
from typing import Callable, Dict, Optional, Tuple

from ophyd_async.epics.areadetector.drivers import ADBase
from ophyd_async.epics.areadetector.utils import (
Expand Down Expand Up @@ -147,10 +147,11 @@ def __init__(self, prefix: str, name: str = "") -> None:
self.trigger_source = ad_rw(ADAravisTriggerSource, prefix + "TriggerSource")
self.model = ad_r(str, prefix + "Model")
self.pixel_format = ad_rw(str, prefix + "PixelFormat")
self.dead_time: Optional[float] = None
super().__init__(prefix, name=name)

async def _fetch_deadtime(self) -> float:
# All known in-use version B/C have same deadtime as non-B/C
model: str = (await self.model.get_value()).removesuffix("B").removesuffix("C")
pixel_format: str = await self.pixel_format.get_value()
return _deadtimes.get(model, lambda _: nan)(pixel_format)
self.dead_time = _deadtimes.get(model, lambda _: nan)(pixel_format)
7 changes: 7 additions & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
from pathlib import Path
from typing import Any, Callable

from ophyd_async.core import StaticDirectoryProvider

import pytest
from bluesky.run_engine import RunEngine, TransitionError

Expand Down Expand Up @@ -89,3 +91,8 @@ async def inner_coroutine():
raise ValueError()

return inner_coroutine


@pytest.fixture
def static_directory_provider(tmp_path: Path):
return StaticDirectoryProvider(directory_path=tmp_path)
40 changes: 24 additions & 16 deletions tests/epics/areadetector/test_aravis.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,12 @@
set_sim_value,
)
from ophyd_async.epics.areadetector.aravis import ADAravisDetector
from ophyd_async.epics.areadetector.controllers.aravis_controller import ADAravisController
from ophyd_async.epics.areadetector.drivers.aravis_driver import ADAravisDriver, ADAravisTriggerSource
from ophyd_async.epics.areadetector.drivers.aravis_driver import (
ADAravisDriver,
ADAravisTriggerSource,
)
from ophyd_async.epics.areadetector.writers.nd_file_hdf import NDFileHDF


@pytest.fixture
async def adaravis_driver(RE: RunEngine) -> ADAravisDriver:
Expand All @@ -22,24 +26,27 @@ async def adaravis_driver(RE: RunEngine) -> ADAravisDriver:


@pytest.fixture
async def adaravis_controller(
RE: RunEngine, adaravis_driver: ADAravisDriver
) -> ADAravisController:
async def hdf(RE: RunEngine) -> NDFileHDF:
async with DeviceCollector(sim=True):
controller = ADAravisController(adaravis_driver, gpio_number=1)
hdf = NDFileHDF("HDF:")

return controller
return hdf


@pytest.fixture
async def adaravis(
RE: RunEngine, static_directory_provider: DirectoryProvider
RE: RunEngine,
static_directory_provider: DirectoryProvider,
adaravis_driver: ADAravisDriver,
hdf: NDFileHDF,
) -> ADAravisDetector:
async with DeviceCollector(sim=True):
adaravis = ADAravisDetector(
"ADARAVIS:",
"adaravis",
static_directory_provider,
name="adaravis",
driver=adaravis_driver,
hdf=hdf,
)

return adaravis
Expand All @@ -65,16 +72,15 @@ async def test_deadtime_read_from_file(
model: str,
pixel_format: str,
deadtime: float,
adaravis_controller: ADAravisController,
adaravis: ADAravisDetector,
):
set_sim_value(adaravis_controller._drv.model, model)
set_sim_value(adaravis_controller._drv.pixel_format, pixel_format)
set_sim_value(adaravis.drv.model, model)
set_sim_value(adaravis.drv.pixel_format, pixel_format)

await adaravis.drv._fetch_deadtime()
# deadtime invariant with exposure time
await adaravis_controller._fetch_deadtime()
assert adaravis_controller.get_deadtime(0) == deadtime
await adaravis_controller._fetch_deadtime()
assert adaravis_controller.get_deadtime(500) == deadtime
assert adaravis.controller.get_deadtime(0) == deadtime
assert adaravis.controller.get_deadtime(500) == deadtime


async def test_trigger_source_set_to_gpio_line(adaravis: ADAravisDetector):
Expand Down Expand Up @@ -126,6 +132,8 @@ async def test_hints_from_hdf_writer(adaravis: ADAravisDetector):


async def test_unsupported_trigger_excepts(adaravis: ADAravisDetector):
set_sim_value(adaravis.drv.model, "Manta G-125")
set_sim_value(adaravis.drv.pixel_format, "Mono12Packed")
with pytest.raises(
ValueError,
# str(EnumClass.value) handling changed in Python 3.11
Expand Down

0 comments on commit cfca527

Please sign in to comment.