Skip to content

Commit

Permalink
simplify AttributeData dataclass
Browse files Browse the repository at this point in the history
  • Loading branch information
jsouter committed Feb 7, 2025
1 parent 8de4777 commit 3918d9f
Show file tree
Hide file tree
Showing 3 changed files with 41 additions and 120 deletions.
8 changes: 6 additions & 2 deletions src/ophyd_async/tango/testing/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
from ._one_of_everything import ExampleStrEnum, OneOfEverythingTangoDevice
from ._one_of_everything import (
ExampleStrEnum,
OneOfEverythingTangoDevice,
everything_attrs,
)

__all__ = ["ExampleStrEnum", "OneOfEverythingTangoDevice"]
__all__ = ["ExampleStrEnum", "OneOfEverythingTangoDevice", "everything_attrs"]
128 changes: 25 additions & 103 deletions src/ophyd_async/tango/testing/_one_of_everything.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,13 @@ class ExampleStrEnum(StrictEnum):
def int_image_value(
dtype: type[DTypeScalar_co],
):
# how do we type this?
array_1d = int_array_value(dtype)
return np.vstack((array_1d, array_1d))


def float_image_value(
dtype: type[DTypeScalar_co],
):
# how do we type this?
array_1d = float_array_value(dtype)
return np.vstack((array_1d, array_1d))

Expand All @@ -46,7 +44,6 @@ class AttributeData(Generic[T]):
initial_value: T
random_put_values: tuple[T, ...]
dformat = AttrDataFormat.SCALAR
# initial_spectrum: Array1D[T] # type: ignore

def random_value(self):
return choice(self.random_put_values)
Expand All @@ -56,42 +53,38 @@ class SpectrumData(AttributeData):
dformat = AttrDataFormat.SPECTRUM

def random_value(self):
array = self.initial_value.copy()
for idx in range(len(array)):
array[idx] = choice(self.random_put_values)
return array
# make array of one value from provided random choices
return np.full(
shape=self.initial_value.shape,
fill_value=choice(self.random_put_values),
dtype=self.initial_value.dtype,
)


class ImageData(AttributeData):
class ImageData(SpectrumData):
dformat = AttrDataFormat.IMAGE

def random_value(self):
array = self.initial_value.copy()
for idx in range(array.shape[1]):
array[0, idx] = choice(self.random_put_values)
array[1, idx] = choice(self.random_put_values)
return array


attribute_datas = []
everything_attrs = []


def add_ads(
my_list: list[AttributeData],
name: str,
tango_type: str,
py_type: type,
initial_scalar,
initial_spectrum,
choices,
):
my_list.append(AttributeData(name, tango_type, py_type, initial_scalar, choices))
my_list.append(
everything_attrs.append(
AttributeData(name, tango_type, py_type, initial_scalar, choices)
)
everything_attrs.append(
SpectrumData(
f"{name}_spectrum", tango_type, Array1D[py_type], initial_spectrum, choices
)
)
my_list.append(
everything_attrs.append(
ImageData(
f"{name}_image",
tango_type,
Expand All @@ -103,7 +96,6 @@ def add_ads(


add_ads(
attribute_datas,
"str",
"DevString",
str,
Expand All @@ -112,91 +104,23 @@ def add_ads(
("four", "five", "six"),
)
add_ads(
attribute_datas,
"bool",
"DevBoolean",
bool,
True,
np.array([False, True], dtype=bool),
(False, True),
)
add_ads("strenum", "DevEnum", StrictEnum, 1, np.array([0, 1, 2]), (0, 1, 2))
add_ads("int8", "DevShort", int, 1, int_array_value(np.int8), (1, 2, 3, 4, 5))
add_ads("uint8", "DevUChar", int, 1, int_array_value(np.uint8), (1, 2, 3, 4, 5))
add_ads("int16", "DevShort", int, 1, int_array_value(np.int16), (1, 2, 3, 4, 5))
add_ads("uint16", "DevUShort", int, 1, int_array_value(np.uint16), (1, 2, 3, 4, 5))
add_ads("int32", "DevLong", int, 1, int_array_value(np.int32), (1, 2, 3, 4, 5))
add_ads("uint32", "DevULong", int, 1, int_array_value(np.uint32), (1, 2, 3, 4, 5))
add_ads("int64", "DevLong64", int, 1, int_array_value(np.int64), (1, 2, 3, 4, 5))
add_ads("uint64", "DevULong64", int, 1, int_array_value(np.uint64), (1, 2, 3, 4, 5))
add_ads(
attribute_datas, "strenum", "DevEnum", StrictEnum, 1, np.array([0, 1, 2]), (0, 1, 2)
) # right py_type?
add_ads(
attribute_datas,
"int8",
"DevShort",
int,
1,
int_array_value(np.int8),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"uint8",
"DevUChar",
int,
1,
int_array_value(np.uint8),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"int16",
"DevShort",
int,
1,
int_array_value(np.int16),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"uint16",
"DevUShort",
int,
1,
int_array_value(np.uint16),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"int32",
"DevLong",
int,
1,
int_array_value(np.int32),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"uint32",
"DevULong",
int,
1,
int_array_value(np.uint32),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"int64",
"DevLong64",
int,
1,
int_array_value(np.int64),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"uint64",
"DevULong64",
int,
1,
int_array_value(np.uint64),
(1, 2, 3, 4, 5),
)
add_ads(
attribute_datas,
"float32",
"DevFloat",
float,
Expand All @@ -205,7 +129,6 @@ def add_ads(
(1.234, 2.345, 3.456),
)
add_ads(
attribute_datas,
"float64",
"DevDouble",
float,
Expand All @@ -214,7 +137,6 @@ def add_ads(
(1.234, 2.345, 3.456),
)
add_ads(
attribute_datas,
"my_state",
"DevState",
DevState,
Expand All @@ -229,7 +151,7 @@ class OneOfEverythingTangoDevice(Device):

def initialize_dynamic_attributes(self):
self.reset_values()
for attr_data in attribute_datas:
for attr_data in everything_attrs:
attr = attribute(
name=attr_data.name,
dtype=attr_data.tango_type,
Expand Down Expand Up @@ -269,7 +191,7 @@ def initialize_dynamic_attributes(self):

@command
def reset_values(self):
for attr_data in attribute_datas:
for attr_data in everything_attrs:
self.attr_values[attr_data.name] = attr_data.initial_value

def read(self, attr):
Expand All @@ -288,6 +210,6 @@ def {}(self, arg):
"""
)

for attr_data in attribute_datas:
for attr_data in everything_attrs:
if attr_data.dformat != AttrDataFormat.IMAGE:
exec(echo_command_code.format(f"{attr_data.name}_cmd"))
25 changes: 10 additions & 15 deletions tests/tango/test_tango_signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
import pytest
from test_base_device import TestDevice

from ophyd_async.core import SignalR, SignalRW, SignalW, SignalX, StrictEnum, T
from ophyd_async.core import SignalR, SignalRW, SignalW, SignalX, T
from ophyd_async.tango.core import (
TangoReadable,
TangoSignalBackend,
Expand All @@ -16,15 +16,10 @@
tango_signal_w,
tango_signal_x,
)
from ophyd_async.tango.core._tango_transport import (
TangoEnumConverter,
TangoEnumImageConverter,
TangoEnumSpectrumConverter,
)
from ophyd_async.tango.testing._one_of_everything import (
from ophyd_async.tango.testing import (
ExampleStrEnum,
OneOfEverythingTangoDevice,
attribute_datas,
everything_attrs,
)
from ophyd_async.testing import MonitorQueue, assert_reading, assert_value
from tango import AttrDataFormat, DevState
Expand Down Expand Up @@ -148,7 +143,7 @@ async def assert_monitor_then_put(
@pytest.mark.asyncio
async def test_backend_get_put_monitor_attr(echo_device: str):
try:
for attr_data in attribute_datas:
for attr_data in everything_attrs:
if "state" in attr_data.name:
print("skipping for now", attr_data.name)
continue
Expand Down Expand Up @@ -204,7 +199,7 @@ async def assert_put_read(
async def test_backend_get_put_monitor_cmd(
echo_device: str,
):
for cmd_data in attribute_datas:
for cmd_data in everything_attrs:
if (
cmd_data.dformat == AttrDataFormat.IMAGE
or cmd_data.tango_type == "DevUChar"
Expand Down Expand Up @@ -241,7 +236,7 @@ async def test_tango_signal_r(
timeout = 0.2
for use_proxy in [True, False]:
proxy = await DeviceProxy(echo_device) if use_proxy else None
for attr_data in attribute_datas:
for attr_data in everything_attrs:
if "state" in attr_data.name:
print("skipping for now", attr_data.name)
continue
Expand Down Expand Up @@ -269,7 +264,7 @@ async def test_tango_signal_w(
for use_proxy in [True, False]:
proxy = await DeviceProxy(echo_device) if use_proxy else None
timeout = 0.2
for attr_data in attribute_datas:
for attr_data in everything_attrs:
if "state" in attr_data.name:
print("skipping for now", attr_data.name)
continue
Expand Down Expand Up @@ -312,7 +307,7 @@ async def test_tango_signal_rw(
timeout = 0.2
for use_proxy in [True, False]:
proxy = await DeviceProxy(echo_device) if use_proxy else None
for attr_data in attribute_datas:
for attr_data in everything_attrs:
if "state" in attr_data.name:
print("skipping for now", attr_data.name)
continue
Expand Down Expand Up @@ -366,7 +361,7 @@ async def test_tango_signal_auto_attrs(
timeout = 0.2
for use_proxy in [True, False]:
proxy = await DeviceProxy(echo_device) if use_proxy else None
for attr_data in attribute_datas:
for attr_data in everything_attrs:
await prepare_device(echo_device, attr_data.name, attr_data.initial_value)
source = echo_device + "/" + attr_data.name

Expand Down Expand Up @@ -421,7 +416,7 @@ async def test_tango_signal_auto_cmds(
timeout = 0.2
proxy = await DeviceProxy(echo_device) if use_proxy else None

for cmd_data in attribute_datas:
for cmd_data in everything_attrs:
source = echo_device + "/" + cmd_data.name + "_cmd"

async def _test_signal(dtype, proxy, source, put_value):
Expand Down

0 comments on commit 3918d9f

Please sign in to comment.