tomato.driverinterface_3_0 package
Code author: Peter Kraus
- pydantic model tomato.driverinterface_3_0.Attr
Bases:
BaseModelA
BaseModelused to describe device attributes.Show JSON schema
{ "title": "Attr", "type": "object", "properties": { "type": { "default": null, "title": "Type" }, "rw": { "default": false, "title": "Rw", "type": "boolean" }, "status": { "default": false, "title": "Status", "type": "boolean" }, "units": { "anyOf": [ { "type": "string" }, { "type": "null" } ], "default": null, "title": "Units" }, "maximum": { "anyOf": [ { "type": "number" }, { "type": "null" } ], "default": null, "title": "Maximum" }, "minimum": { "anyOf": [ { "type": "number" }, { "type": "null" } ], "default": null, "title": "Minimum" }, "options": { "anyOf": [ { "items": {}, "type": "array", "uniqueItems": true }, { "type": "null" } ], "default": null, "title": "Options" } } }
- Config:
arbitrary_types_allowed: bool = True
- field type: type [Required]
Data type of the attribute
- field rw: bool = False
Is the attribute read-write?
- field status: bool = False
Should the attribute be included in component status?
- field units: str | None = None
Default units for the attribute, optional.
- field maximum: float | Quantity | None = None
Maximum value for the attribute, optional.
- Constraints:
union_mode = left_to_right
- field minimum: float | Quantity | None = None
Minimum value for the attribute, optional.
- Constraints:
union_mode = left_to_right
- field options: set | None = None
Allowed set of values for the attribute, optional.
- pydantic model tomato.driverinterface_3_0.Status
Bases:
BaseModelA
BaseModelused to describe component status.Show JSON schema
{ "title": "Status", "description": "A :class:`~pydantic.BaseModel` used to describe component status.", "type": "object", "properties": { "connected": { "title": "Connected", "type": "boolean" }, "state": { "anyOf": [ { "enum": [ "idle", "meas", "task", "stop" ], "type": "string" }, { "type": "null" } ], "title": "State" }, "can_submit": { "title": "Can Submit", "type": "boolean" }, "attrs": { "additionalProperties": true, "title": "Attrs", "type": "object" } }, "required": [ "connected", "state", "can_submit" ] }
- field connected: bool [Required]
Indicates whether component is communicating correctly.
- field state: Literal['idle', 'meas', 'task', 'stop'] | None [Required]
Indicates device state:
idlewhen component is connected and idle,measwhen component is doing an idle measurement,taskwhen component has a running task,stopwhen component is being torn down or reset,None when component is not connected.
- field can_submit: bool [Required]
Indicates whether a
Taskcan be sent to component queue.
- field attrs: dict[str, Any] [Optional]
Container for any attrs that are returned as part of a status.
- class tomato.driverinterface_3_0.ModelInterface(settings: dict[str, Any] | None = None)
Bases:
objectAn abstract base class specifying the driver interface.
Individual driver modules should expose a
DriverInterfaceas a top-level import, which inherits from this abstract class. Only the methods of this class are used to interact with drivers and their components.All methods of this class should return
Replyobjects (except theComponentFactory()function). However, for better readability, a decorator functionto_reply()is provided, so that the types of the return values can be explicitly defined here.- version: str = '3.0'
Version of the
DriverInterface.
- idle_measurement_interval: int | None = None
The interval (in seconds) after which
self.cmp_measure()will be executed, when idle.
- property name: str
Property that should return the name of this driver.
- devmap: dict[str, ModelComponent]
Map of registered device components, the keys are set from the
tomato.models.Component.name.
- constants: dict[str, Any]
A map that should be populated with driver-specific run-time constants.
- settings: dict[str, Any]
A settings map to contain driver-specific settings such as
dllpathfor BioLogic
- retries: dict[str, int]
Map of components which failed to register, with number of retries as values.
- ComponentFactory(name, **kwargs)
A factory function which is used to pass this instance of the
ModelInterfaceto the newModelDeviceinstance.
- cmp_register(name: str, address: str | None, channel: str | None, **kwargs: dict) tuple[bool, str, str | None]
Register a new device component in this driver.
Creates a
ModelDevicerepresenting a device component, storing it in theself.devmapusing the provided address and channel.Returns the name of the registered component as the
Reply.data.
- cmp_stop(name: str, **kwargs: dict) tuple[bool, str, None]
Component stop function, passthrough to
ModelComponent.stop().Should set the device component into a documented, safe state.
- cmp_quit(name: str, **kwargs: dict) tuple[bool, str, None]
Component quit function, passthrough to
ModelComponent.stop()andModelComponent.quit().Should set the device component into a documented, safe state, then release the component from tomato.
The function is called when the driver is exiting normally.
- cmp_reset(name: str, **kwargs: dict) tuple[bool, str, None]
Component reset function, passthrough to
ModelComponent.stop()andModelComponent.reset().Should set the device component into a safe state and make it ready to accept new
Tasksif possible.The function is called on completion of each
Payload.
- cmp_set_attr(attr: str, val: str | int | float | Quantity, name: str, **kwargs: dict) tuple[bool, str, str | int | float | Quantity]
Set value of the
Attrof the specified device component.Pass-through to the
ModelDevice.set_attr()function. No type or read-write validation performed here! Returns the validated or coerced value as theReply.data.
- cmp_get_attr(attr: str, name: str, **kwargs: dict) tuple[bool, str, str | int | float | Quantity]
Get value of the
Attrfrom the specified device component.Pass-through to the
ModelDevice.get_attr()function. No type coercion is done here. Returns the value as theReply.data.
- cmp_status(name: str, **kwargs: dict) tuple[bool, str, Status]
Get the status report from the specified device component.
Returns a flag in
Reply.data['running']indicating whether the component is running.Passthrough to
ModelDevice.status(). Returns thedictof attribute values marked asstatus=True.
- cmp_capabilities(name: str, **kwargs) tuple[bool, str, set]
Returns the capabilities of the device component.
Pass-through to
ModelDevice.capabilities(). Returns thesetof capabilities inReply.data.
- cmp_attrs(name: str, **kwargs: dict) tuple[bool, str, dict]
Query available
Attrson the specified device component.Pass-through to the
ModelDevice.attrs()function. Returns thedictof attributes as theReply.data.
- cmp_constants(name: str, **kwargs: dict) tuple[bool, str, dict]
Query constants on the specified device component and this driver.
Returns the
dictof constants as theReply.data.
- cmp_last_data(name: str, **kwargs: dict) tuple[bool, str, None | Dataset]
Fetch the last stored data on the component.
Passthrough to
ModelDevice.get_last_data(). The data in the form of axarray.Datasetis returned as theReply.data.
- cmp_measure(name: str, **kwargs: dict) tuple[bool, str, None]
Do a single measurement on the component according to its current configuration.
Fails if the component already has a running task / measurement.
- task_start(name: str, task: Task, **kwargs) tuple[bool, str, set | Task]
Submit a
Taskonto the specified device component.Pushes the supplied
Taskinto theQueueof the component, then starts the worker thread (if not already started). Checks that theTaskis among the capabilities of this component.
- task_status(name: str, **kwargs: dict) tuple[bool, str, dict]
- task_stop(name: str, **kwargs) tuple[bool, str, Dataset | None]
Stops a running task and returns any collected data.
Pass-through to
ModelComponent.stop_task()andModelInterface.task_data().If there is any cached data, it is returned as a
xarray.Datasetin theReply.dataand the cache is cleared.
- task_data(name: str, **kwargs) tuple[bool, str, Dataset | None]
Return cached task data on the device component and clean the cache.
Pass-through for
ModelDevice.get_data(), which should return axarray.Datasetthat is fully annotated.This function gets called by the job thread every device.pollrate, it therefore incurs some IPC cost.
- task_validate(name: str, task: Task, **kwargs) tuple[bool, str, None]
Validate the provided
Taskfor submission on the component identified bykey.
- status() Reply
Returns the driver status. Currently that is the names of the components in the devmap.
- quit() Reply
Driver quit function.
Stops tasks and quits every registered component. Passthrough to
ModelInterface.task_stop()andModelInterface.cmp_quit().Any driver-specific commands (such as releasing serial port etc.) should be performed ehre.
Called when driver process is exiting.
- reset() Reply
Resets the driver.
Called when the driver process is quitting. Instructs all remaining tasks to stop. Warns when devices linger. Passes through to
cmp_reset(). This is not a pass-through tocmp_teardown().
- class tomato.driverinterface_3_0.ModelComponent(driver, name, **kwargs)
Bases:
objectAn abstract base class specifying a manager for an individual component.
This class should handle determining attributes and capabilities of the component, the reading/writing of those attributes, processing of tasks, and caching and returning of task data.
- driver: ModelInterface
The parent
DriverInterfaceinstance.
- name: str
The name in
self.driver.devmapreferring to this object.
- task_list: Queue
A
Queueused to passTasksto the workerThread.
- thread: Thread
The worker
Thread.
- state: str | None = None
A
strholding the component state.
- datalock: RLock
Lock object for thread-safe data manipulation.
- constants: dict[str, Any]
Constant metadata of this component.
- task_runner() None
Target function for the
self.threadwhen handlingTasks.This function waits for a
Taskpassed usingself.task_list, then handles setting allAttrsusing theprepare_task()function, and finally handles the main loop of the task, periodically running thedo_task()function (using task.sampling_interval) until the maximum task duration (i.e. task.max_duration) is exceeded.The
self.threadis reset to None.
- prepare_task(task: Task, **kwargs: dict) None
Given a
Task, prepare this component for execution by setting allAttrsas specified in the task.task_params dictionary.
- do_task(task: Task, t_start: float, t_now: float, t_prev: float, **kwargs: dict) None
Periodically called task execution function.
This function is responsible for updating
self.datawith new data, i.e. performing the measurement. It should also update the value ofself.last_data, so that the component status is consistent with the cached data.
- abstract do_measure(**kwargs: dict) None
One shot execution worker function.
This function is performs a measurement using the current configuration of
self.attrs, and stores the result inself.last_data.
- stop_task(**kwargs: dict) None
Stops the currently running task.
- abstract set_attr(attr: str, val: str | int | float | Quantity, **kwargs: dict) str | int | float | Quantity
Sets the specified
Attrtoval.This function should handle any data type coercion and validation using e.g.
Attr.maximumandAttr.minimum.Returns the coerced value corresponding to
val.
- abstract get_attr(attr: str, **kwargs: dict) str | int | float | Quantity
Reads the value of the specified
Attr.
- get_data(**kwargs: dict) Dataset | None
Returns the cached
self.dataas axarray.Datasetbefore clearing the cache.
- get_last_data(**kwargs: dict) Dataset | None
Returns the
last_dataobject as axarray.Dataset.
- abstract capabilities(**kwargs) set
Returns a
setof all supported techniques.
- abstract status(**kwargs) Status
Function indicating component status.
The implementation of this function in the driver module should perform checks whether the components is still reachable (
Status.connected) and what state is the component in (Status.state).The function should also compile a status report using
Attrsmarked asstatus=Trueand return it asStatus.attrs.
- stop(**kwargs) None
Stops any activity on this component.
This function should set the component to a safe state.
By default a pass-through to
ModelComponent.stop_task().
- abstract quit(**kwargs) None
Quits the component.
This function makes the component ready to quit. When accessed via the
ModelInterface.cmp_quit(), it is always called afterModelComponent.stop(), therefore allTaskson the device can be assumed to be stopped.
- reset(**kwargs) None
Resets the component to an initial status.
This function makes the component ready to accept new
Task. When accessed via theModelInterface.cmp_reset(), it is always called afterModelComponent.stop(), therefore allTaskson the device can be assumed to be stopped.
Submodules
- tomato.driverinterface_3_0.decorators.in_devmap(func)
- tomato.driverinterface_3_0.decorators.to_reply(func)
Helper decorator for coercing tuples into
Reply.
- tomato.driverinterface_3_0.decorators.log_errors(func)
Helper decorator for logging all kinds of errors.
This decorator should be only used on functions in the API of the
ModelInterface, as the caught exceptions will cause the driver process to exit.
- tomato.driverinterface_3_0.decorators.coerce_val(func)
Decorator for coercing
valinto the correct format based onAttrdata.This decorator should be applied to the
ModelDriver.set_attr()function, in order to check whether the supplied value is allowed (notNone, inoptions, betweenminimumandmaximum) as well as coercing it to the right type and unit.