Skip to content

Commit

Permalink
Update all Enums to UPPER_CASE across the code base (#673)
Browse files Browse the repository at this point in the history
* update all src files with capitalized enums

* fix all tests with matching capital enum values

* more fixes after running tests

* add checks for lowercase enums in place of ruff

* fix line spacing
  • Loading branch information
jennmald authored and evalott100 committed Nov 27, 2024
1 parent 48ec45f commit 2d23de8
Show file tree
Hide file tree
Showing 44 changed files with 298 additions and 289 deletions.
18 changes: 9 additions & 9 deletions src/ophyd_async/core/_detector.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,13 +30,13 @@ class DetectorTrigger(StrictEnum):
"""Type of mechanism for triggering a detector to take frames"""

#: Detector generates internal trigger for given rate
internal = "internal"
INTERNAL = "internal"
#: Expect a series of arbitrary length trigger signals
edge_trigger = "edge_trigger"
EDGE_TRIGGER = "edge_trigger"
#: Expect a series of constant width external gate signals
constant_gate = "constant_gate"
CONSTANT_GATE = "constant_gate"
#: Expect a series of variable width external gate signals
variable_gate = "variable_gate"
VARIABLE_GATE = "variable_gate"


class TriggerInfo(BaseModel):
Expand All @@ -53,7 +53,7 @@ class TriggerInfo(BaseModel):
#: - 3 times for final flat field images
number_of_triggers: NonNegativeInt | list[NonNegativeInt]
#: Sort of triggers that will be sent
trigger: DetectorTrigger = Field(default=DetectorTrigger.internal)
trigger: DetectorTrigger = Field(default=DetectorTrigger.INTERNAL)
#: What is the minimum deadtime between triggers
deadtime: float | None = Field(default=None, ge=0)
#: What is the maximum high time of the triggers
Expand Down Expand Up @@ -265,14 +265,14 @@ async def trigger(self) -> None:
await self.prepare(
TriggerInfo(
number_of_triggers=1,
trigger=DetectorTrigger.internal,
trigger=DetectorTrigger.INTERNAL,
deadtime=None,
livetime=None,
frame_timeout=None,
)
)
assert self._trigger_info
assert self._trigger_info.trigger is DetectorTrigger.internal
assert self._trigger_info.trigger is DetectorTrigger.INTERNAL
# Arm the detector and wait for it to finish.
indices_written = await self.writer.get_indices_written()
await self.controller.arm()
Expand Down Expand Up @@ -303,7 +303,7 @@ async def prepare(self, value: TriggerInfo) -> None:
Args:
value: TriggerInfo describing how to trigger the detector
"""
if value.trigger != DetectorTrigger.internal:
if value.trigger != DetectorTrigger.INTERNAL:
assert (
value.deadtime
), "Deadtime must be supplied when in externally triggered mode"
Expand All @@ -323,7 +323,7 @@ async def prepare(self, value: TriggerInfo) -> None:
self._describe, _ = await asyncio.gather(
self.writer.open(value.multiplier), self.controller.prepare(value)
)
if value.trigger != DetectorTrigger.internal:
if value.trigger != DetectorTrigger.INTERNAL:
await self.controller.arm()
self._fly_start = time.monotonic()

Expand Down
13 changes: 11 additions & 2 deletions src/ophyd_async/core/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,20 @@
ErrorText = str | Mapping[str, Exception]


class StrictEnum(str, Enum):
class StrictEnumMeta(EnumMeta):
def __new__(metacls, *args, **kwargs):
ret = super().__new__(metacls, *args, **kwargs)
lowercase_names = [x.name for x in ret if not x.name.isupper()] # type: ignore
if lowercase_names:
raise TypeError(f"Names {lowercase_names} should be uppercase")
return ret


class StrictEnum(str, Enum, metaclass=StrictEnumMeta):
"""All members should exist in the Backend, and there will be no extras"""


class SubsetEnumMeta(EnumMeta):
class SubsetEnumMeta(StrictEnumMeta):
def __call__(self, value, *args, **kwargs): # type: ignore
if isinstance(value, str) and not isinstance(value, self):
return value
Expand Down
16 changes: 8 additions & 8 deletions src/ophyd_async/epics/adaravis/_aravis_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,9 +31,9 @@ def get_deadtime(self, exposure: float | None) -> float:

async def prepare(self, trigger_info: TriggerInfo):
if trigger_info.total_number_of_triggers == 0:
image_mode = adcore.ImageMode.continuous
image_mode = adcore.ImageMode.CONTINUOUS
else:
image_mode = adcore.ImageMode.multiple
image_mode = adcore.ImageMode.MULTIPLE
if (exposure := trigger_info.livetime) is not None:
await self._drv.acquire_time.set(exposure)

Expand All @@ -58,20 +58,20 @@ def _get_trigger_info(
self, trigger: DetectorTrigger
) -> tuple[AravisTriggerMode, AravisTriggerSource]:
supported_trigger_types = (
DetectorTrigger.constant_gate,
DetectorTrigger.edge_trigger,
DetectorTrigger.internal,
DetectorTrigger.CONSTANT_GATE,
DetectorTrigger.EDGE_TRIGGER,
DetectorTrigger.INTERNAL,
)
if trigger not in supported_trigger_types:
raise ValueError(
f"{self.__class__.__name__} only supports the following trigger "
f"types: {supported_trigger_types} but was asked to "
f"use {trigger}"
)
if trigger == DetectorTrigger.internal:
return AravisTriggerMode.off, AravisTriggerSource.freerun
if trigger == DetectorTrigger.INTERNAL:
return AravisTriggerMode.OFF, AravisTriggerSource.FREERUN
else:
return (AravisTriggerMode.on, f"Line{self.gpio_number}") # type: ignore
return (AravisTriggerMode.ON, f"Line{self.gpio_number}") # type: ignore

async def disarm(self):
await adcore.stop_busy_record(self._drv.acquire, False, timeout=1)
8 changes: 4 additions & 4 deletions src/ophyd_async/epics/adaravis/_aravis_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
class AravisTriggerMode(StrictEnum):
"""GigEVision GenICAM standard: on=externally triggered"""

on = "On"
off = "Off"
ON = "On"
OFF = "Off"


"""A minimal set of TriggerSources that must be supported by the underlying record.
Expand All @@ -20,8 +20,8 @@ class AravisTriggerMode(StrictEnum):


class AravisTriggerSource(SubsetEnum):
freerun = "Freerun"
line1 = "Line1"
FREERUN = "Freerun"
LINE1 = "Line1"


class AravisDriverIO(adcore.ADBaseIO):
Expand Down
42 changes: 21 additions & 21 deletions src/ophyd_async/epics/adcore/_core_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@


class Callback(StrictEnum):
Enable = "Enable"
Disable = "Disable"
ENABLE = "Enable"
DISABLE = "Disable"


class NDArrayBaseIO(Device):
Expand Down Expand Up @@ -72,17 +72,17 @@ class DetectorState(StrictEnum):
See definition in ADApp/ADSrc/ADDriver.h in https://github.com/areaDetector/ADCore
"""

Idle = "Idle"
Acquire = "Acquire"
Readout = "Readout"
Correct = "Correct"
Saving = "Saving"
Aborting = "Aborting"
Error = "Error"
Waiting = "Waiting"
Initializing = "Initializing"
Disconnected = "Disconnected"
Aborted = "Aborted"
IDLE = "Idle"
ACQUIRE = "Acquire"
READOUT = "Readout"
CORRECT = "Correct"
SAVING = "Saving"
ABORTING = "Aborting"
ERROR = "Error"
WAITING = "Waiting"
INITIALIZING = "Initializing"
DISCONNECTED = "Disconnected"
ABORTED = "Aborted"


class ADBaseIO(NDArrayBaseIO):
Expand All @@ -99,14 +99,14 @@ def __init__(self, prefix: str, name: str = "") -> None:


class Compression(StrictEnum):
none = "None"
nbit = "N-bit"
szip = "szip"
zlib = "zlib"
blosc = "Blosc"
bslz4 = "BSLZ4"
lz4 = "LZ4"
jpeg = "JPEG"
NONE = "None"
NBIT = "N-bit"
SZIP = "szip"
ZLIB = "zlib"
BLOSC = "Blosc"
BSLZ4 = "BSLZ4"
LZ4 = "LZ4"
JPEG = "JPEG"


class NDFileHDFIO(NDPluginBaseIO):
Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/adcore/_core_logic.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
# Default set of states that we should consider "good" i.e. the acquisition
# is complete and went well
DEFAULT_GOOD_STATES: frozenset[DetectorState] = frozenset(
[DetectorState.Idle, DetectorState.Aborted]
[DetectorState.IDLE, DetectorState.ABORTED]
)


Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/adcore/_hdf_writer.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ async def open(self, multiplier: int = 1) -> dict[str, DataKey]:
self.hdf.file_path.set(str(info.directory_path)),
self.hdf.file_name.set(info.filename),
self.hdf.file_template.set("%s/%s.h5"),
self.hdf.file_write_mode.set(FileWriteMode.stream),
self.hdf.file_write_mode.set(FileWriteMode.STREAM),
# Never use custom xml layout file but use the one defined
# in the source code file NDFileHDF5LayoutXML.cpp
self.hdf.xml_file_name.set(""),
Expand Down
2 changes: 1 addition & 1 deletion src/ophyd_async/epics/adcore/_single_trigger.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def __init__(
@AsyncStatus.wrap
async def stage(self) -> None:
await asyncio.gather(
self.drv.image_mode.set(ImageMode.single),
self.drv.image_mode.set(ImageMode.SINGLE),
self.drv.wait_for_plugins.set(True),
)
await super().stage()
Expand Down
70 changes: 35 additions & 35 deletions src/ophyd_async/epics/adcore/_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,42 +11,42 @@


class ADBaseDataType(StrictEnum):
Int8 = "Int8"
UInt8 = "UInt8"
Int16 = "Int16"
UInt16 = "UInt16"
Int32 = "Int32"
UInt32 = "UInt32"
Int64 = "Int64"
UInt64 = "UInt64"
Float32 = "Float32"
Float64 = "Float64"
INT8 = "Int8"
UINT8 = "UInt8"
INT16 = "Int16"
UINT16 = "UInt16"
INT32 = "Int32"
UINT32 = "UInt32"
INT64 = "Int64"
UINT64 = "UInt64"
FLOAT32 = "Float32"
FLOAT64 = "Float64"


def convert_ad_dtype_to_np(ad_dtype: ADBaseDataType) -> str:
ad_dtype_to_np_dtype = {
ADBaseDataType.Int8: "|i1",
ADBaseDataType.UInt8: "|u1",
ADBaseDataType.Int16: "<i2",
ADBaseDataType.UInt16: "<u2",
ADBaseDataType.Int32: "<i4",
ADBaseDataType.UInt32: "<u4",
ADBaseDataType.Int64: "<i8",
ADBaseDataType.UInt64: "<u8",
ADBaseDataType.Float32: "<f4",
ADBaseDataType.Float64: "<f8",
ADBaseDataType.INT8: "|i1",
ADBaseDataType.UINT8: "|u1",
ADBaseDataType.INT16: "<i2",
ADBaseDataType.UINT16: "<u2",
ADBaseDataType.INT32: "<i4",
ADBaseDataType.UINT32: "<u4",
ADBaseDataType.INT64: "<i8",
ADBaseDataType.UINT64: "<u8",
ADBaseDataType.FLOAT32: "<f4",
ADBaseDataType.FLOAT64: "<f8",
}
return ad_dtype_to_np_dtype[ad_dtype]


def convert_pv_dtype_to_np(datatype: str) -> str:
_pvattribute_to_ad_datatype = {
"DBR_SHORT": ADBaseDataType.Int16,
"DBR_ENUM": ADBaseDataType.Int16,
"DBR_INT": ADBaseDataType.Int32,
"DBR_LONG": ADBaseDataType.Int32,
"DBR_FLOAT": ADBaseDataType.Float32,
"DBR_DOUBLE": ADBaseDataType.Float64,
"DBR_SHORT": ADBaseDataType.INT16,
"DBR_ENUM": ADBaseDataType.INT16,
"DBR_INT": ADBaseDataType.INT32,
"DBR_LONG": ADBaseDataType.INT32,
"DBR_FLOAT": ADBaseDataType.FLOAT32,
"DBR_DOUBLE": ADBaseDataType.FLOAT64,
}
if datatype in ["DBR_STRING", "DBR_CHAR"]:
np_datatype = "s40"
Expand All @@ -62,9 +62,9 @@ def convert_pv_dtype_to_np(datatype: str) -> str:

def convert_param_dtype_to_np(datatype: str) -> str:
_paramattribute_to_ad_datatype = {
"INT": ADBaseDataType.Int32,
"INT64": ADBaseDataType.Int64,
"DOUBLE": ADBaseDataType.Float64,
"INT": ADBaseDataType.INT32,
"INT64": ADBaseDataType.INT64,
"DOUBLE": ADBaseDataType.FLOAT64,
}
if datatype in ["STRING"]:
np_datatype = "s40"
Expand All @@ -79,15 +79,15 @@ def convert_param_dtype_to_np(datatype: str) -> str:


class FileWriteMode(StrictEnum):
single = "Single"
capture = "Capture"
stream = "Stream"
SINGLE = "Single"
CAPTURE = "Capture"
STREAM = "Stream"


class ImageMode(StrictEnum):
single = "Single"
multiple = "Multiple"
continuous = "Continuous"
SINGLE = "Single"
MULTIPLE = "Multiple"
CONTINUOUS = "Continuous"


class NDAttributeDataType(StrictEnum):
Expand Down
14 changes: 7 additions & 7 deletions src/ophyd_async/epics/adkinetix/_kinetix_controller.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,10 @@
from ._kinetix_io import KinetixDriverIO, KinetixTriggerMode

KINETIX_TRIGGER_MODE_MAP = {
DetectorTrigger.internal: KinetixTriggerMode.internal,
DetectorTrigger.constant_gate: KinetixTriggerMode.gate,
DetectorTrigger.variable_gate: KinetixTriggerMode.gate,
DetectorTrigger.edge_trigger: KinetixTriggerMode.edge,
DetectorTrigger.INTERNAL: KinetixTriggerMode.INTERNAL,
DetectorTrigger.CONSTANT_GATE: KinetixTriggerMode.GATE,
DetectorTrigger.VARIABLE_GATE: KinetixTriggerMode.GATE,
DetectorTrigger.EDGE_TRIGGER: KinetixTriggerMode.EDGE,
}


Expand All @@ -33,11 +33,11 @@ async def prepare(self, trigger_info: TriggerInfo):
await asyncio.gather(
self._drv.trigger_mode.set(KINETIX_TRIGGER_MODE_MAP[trigger_info.trigger]),
self._drv.num_images.set(trigger_info.total_number_of_triggers),
self._drv.image_mode.set(adcore.ImageMode.multiple),
self._drv.image_mode.set(adcore.ImageMode.MULTIPLE),
)
if trigger_info.livetime is not None and trigger_info.trigger not in [
DetectorTrigger.variable_gate,
DetectorTrigger.constant_gate,
DetectorTrigger.VARIABLE_GATE,
DetectorTrigger.CONSTANT_GATE,
]:
await self._drv.acquire_time.set(trigger_info.livetime)

Expand Down
14 changes: 7 additions & 7 deletions src/ophyd_async/epics/adkinetix/_kinetix_io.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,16 @@


class KinetixTriggerMode(StrictEnum):
internal = "Internal"
edge = "Rising Edge"
gate = "Exp. Gate"
INTERNAL = "Internal"
EDGE = "Rising Edge"
GATE = "Exp. Gate"


class KinetixReadoutMode(StrictEnum):
sensitivity = 1
speed = 2
dynamic_range = 3
sub_electron = 4
SENSITIVITY = 1
SPEED = 2
DYNAMIC_RANGE = 3
SUB_ELECTRON = 4


class KinetixDriverIO(adcore.ADBaseIO):
Expand Down
Loading

0 comments on commit 2d23de8

Please sign in to comment.