Skip to content

Commit

Permalink
signal: init value can be a ValueInfo structure with dtype, shape and…
Browse files Browse the repository at this point in the history
… default value to characterize the value
  • Loading branch information
Mathias Guijarro committed Jun 4, 2024
1 parent 5c03c3f commit f1849e0
Showing 1 changed file with 27 additions and 6 deletions.
33 changes: 27 additions & 6 deletions ophyd/signal.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
import weakref

import numpy as np
from collections import namedtuple

from . import get_cl
from .ophydobj import Kind, OphydObject
Expand Down Expand Up @@ -50,14 +51,19 @@ class ConnectionTimeoutError(TimeoutError):
...


ValueInfo = namedtuple(
"ValueInfo", ("dtype", "shape", "default"), defaults=(float, (), None)
)


class Signal(OphydObject):
r"""A signal, which can have a read-write or read-only value.
Parameters
----------
name : string, keyword only
value : any, optional
The initial value
value : any acceptable value or a ValueInfo object, optional
The initial value, or the ValueInfo structure to characterize the value
kind : a member the Kind IntEnum (or equivalent integer), optional
Default is Kind.normal. See Kind for options.
parent : Device, optional
Expand Down Expand Up @@ -85,6 +91,7 @@ class Signal(OphydObject):
rtolerance : any, optional
The relative tolerance associated with the value
"""

SUB_VALUE = "value"
SUB_META = "meta"
_default_sub = SUB_VALUE
Expand All @@ -95,7 +102,7 @@ def __init__(
self,
*,
name,
value=0.0,
value=DEFAULT_EPICSSIGNAL_VALUE,
timestamp=None,
parent=None,
labels=None,
Expand All @@ -116,7 +123,19 @@ def __init__(
self.cl = cl
self._dispatcher = cl.get_dispatcher()
self._metadata_thread_ctx = self._dispatcher.get_thread_context("monitor")
self._readback = value
if isinstance(value, ValueInfo):
self._value_dtype_str = np.dtype(value.dtype).name
self._value_shape = value.shape
if value.default is None:
self._readback = DEFAULT_EPICSSIGNAL_VALUE
else:
# ensure default value corresponds to dtype and shape...
self._readback = np.asanyarray(value.default, value.dtype)
self._readback.shape = value.shape
else:
self._value_dtype_str = None
self._value_shape = None
self._readback = value

if timestamp is None:
timestamp = time.time()
Expand Down Expand Up @@ -480,8 +499,10 @@ def describe(self):
return {
self.name: {
"source": "SIM:{}".format(self.name),
"dtype": data_type(val),
"shape": data_shape(val),
"dtype": (
data_type(val) if self._value_dtype_str is None else self._value_dtype_str
),
"shape": data_shape(val) if self._value_shape is None else self._value_shape,
}
}
except ValueError as ve:
Expand Down

0 comments on commit f1849e0

Please sign in to comment.