Skip to content

Extend DeviceVector to support string and integer #903

@oliwenmandiamond

Description

@oliwenmandiamond

In the ophyd-async documentation, there is the DeviceVector class:
"
Sometimes, it makes sense to group devices by number, say an array of sensors:

class SensorGroup(StandardReadable):
    def __init__(self, prefix: str, name: str = "", sensor_count: int = 3) -> None:
        with self.add_children_as_readables():
            self.sensors = DeviceVector(
                {i: Sensor(f"{prefix}{i}:") for i in range(1, sensor_count + 1)}
            )

        super().__init__(name)

DeviceVector allows writing maintainable, arbitrary-length device groups instead of fixed classes for each possible grouping. A DeviceVector can be accessed via indices, for example: my_sensor_group.sensors[2]. Here sensors is a dictionary with integer indices rather than a list so that the most semantically sensible indices may be used, the sensor group above may be 1-indexed, for example, because the sensors’ datasheet calls them “sensor 1”, “sensor 2” etc.
"

However, it would also be useful to support str as well as int as the key for DeviceVector e,g

class ExcitationEnergySources(StandardReadable):
    def __init__(self, name: str = "", alias_name_to_device: dict[str, Reference[Device]]) -> None:
        with self.add_children_as_readables():
            self.sources = DeviceVector(
                alias_to_device
            )

        super().__init__(name)

I09 beamline

pgm = PGM(...)
dcm = DCM(...)
energy_sources = DeviceVector("energy_sources", {"source1" : Reference(dcm), "source2" : Reference(pgm)})

P60 beamline

mg_kalpha = DeviceImpl(...)
al_kalpha = DeviceImpl(...)
energy_sources = DeviceVector("energy_sources", {"source1" : Reference(mg_kalpha), "energy_sources", {"source2" : Reference(al_kalpha)})

Common plan every beamline can use

def plan(region: AbstractBaseRegion, energy_sources : DeviceVector[str, Device] = inject("energy_sources")) -> Device:
     source_alias : str = region.excitation_energy_source
     return energy_sources.sources[source_alias]

Metadata

Metadata

Assignees

No one assigned

    Labels

    enhancementNew feature or request

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions