Skip to content

Commit

Permalink
Change deserialization based around ComponentConfig instances (#16)
Browse files Browse the repository at this point in the history
* Try to fix #4 for cryostream only

* Fix the mypy error for raise_interrupt

* Try to fix #4 for cryostream only

* Fix the mypy error for raise_interrupt

* Ported everything apart from nested and tests

* Support SystemSimulation

* Fixed remaining tests

* Fix docs

* Update tickit/core/management/event_router.py

Co-authored-by: Garry O'Donnell <[email protected]>

* Update tickit/cli.py

Co-authored-by: Garry O'Donnell <[email protected]>

* Update tickit/cli.py

Co-authored-by: Garry O'Donnell <[email protected]>

* Update tickit/core/components/system_simulation.py

Co-authored-by: Garry O'Donnell <[email protected]>

* Update tickit/core/components/device_simulation.py

Co-authored-by: Garry O'Donnell <[email protected]>

* Fix event_router indentation

* Add test for as_tagged_union and fix bug

* Rename lifetime_runnable to runner

* Fix test_runner to use correct mock library

Co-authored-by: Garry O'Donnell <[email protected]>
Co-authored-by: Garry O'Donnell <garry.o'[email protected]>
  • Loading branch information
3 people authored Oct 20, 2021
1 parent 653cc5f commit 2e41f45
Show file tree
Hide file tree
Showing 58 changed files with 986 additions and 1,831 deletions.
1 change: 1 addition & 0 deletions .vscode/settings.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,4 +15,5 @@
"editor.codeActionsOnSave": {
"source.organizeImports": true
},
"restructuredtext.confPath": "",
}
7 changes: 7 additions & 0 deletions Pipfile.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,8 @@
("py:class", "SimTime"),
("py:class", "immutables._map.Map"),
("py:class", "_asyncio.Task"),
("py:class", "apischema.conversions.conversions.Conversion"),
("py:class", "apischema.conversions.conversions.LazyConversion"),
]

# Both the class’ and the __init__ method’s docstring are concatenated and
Expand Down
14 changes: 7 additions & 7 deletions docs/reference/api.rst
Original file line number Diff line number Diff line change
Expand Up @@ -26,24 +26,24 @@ API

.. automodule:: examples.devices.trampoline
:members:
:exclude-members: RandomTrampoline
:exclude-members: RandomTrampolineDevice

..
RandomTrampoline excluded such that Inputs & Outputs TypedDicts may be
RandomTrampolineDevice excluded such that Inputs & Outputs TypedDicts may be
:noindex:ed to prevent namespace collision with Trampoline Inputs & Outputs
as TypedDict lacks proper __qualname__
``examples.devices.trampoline``
-------------------------------

.. autoclass:: examples.devices.trampoline.RandomTrampoline
.. autoclass:: examples.devices.trampoline.RandomTrampolineDevice
:members:
:exclude-members: Inputs, Outputs

.. autoclass:: examples.devices.trampoline.RandomTrampoline.Inputs
.. autoclass:: examples.devices.trampoline.RandomTrampolineDevice.Inputs
:noindex:

.. autoclass:: examples.devices.trampoline.RandomTrampoline.Outputs
.. autoclass:: examples.devices.trampoline.RandomTrampolineDevice.Outputs
:noindex:

.. automodule:: tickit
Expand Down Expand Up @@ -216,10 +216,10 @@ API
``tickit.core.adapter``
-----------------------

.. automodule:: tickit.core.lifetime_runnable
.. automodule:: tickit.core.runner
:members:

``tickit.core.lifetime_runnable``
``tickit.core.runner``
---------------------------------

.. automodule:: tickit.core.typedefs
Expand Down
71 changes: 45 additions & 26 deletions docs/tutorials/creating-a-device.rst
Original file line number Diff line number Diff line change
Expand Up @@ -15,16 +15,16 @@ will determine the operation of our device.
Device Class
------------

We shall begin by defining the Shutter class which inerits `ConfigurableDevice` - by
We shall begin by defining the Shutter class which inherits `Device` - by
doing so a confiuration dataclass will automatically be created for the device,
allowing for easy YAML configuration.

.. code-block:: python
from tickit.core.device import ConfigurableDevice
from tickit.core.device import Device
class Shutter(ConfigurableDevice):
class ShutterDevice(Device):
Device Constructor and Configuration
------------------------------------
Expand All @@ -41,10 +41,10 @@ the initial ``position`` will be random.
from random import random
from typing import Optional
from tickit.core.device import ConfigurableDevice
from tickit.core.device import Device
class Shutter(ConfigurableDevice):
class ShutterDevice(Device):
def __init__(
self, default_position: float, initial_position: Optional[float] = None
) -> None:
Expand All @@ -53,7 +53,7 @@ the initial ``position`` will be random.
.. note::
Arguments to the ``__init__`` method may be specified in the simulation config file
if the device inherits `ConfigurableDevice`.
if the device inherits `Device`.

Device Logic
------------
Expand All @@ -70,11 +70,11 @@ which consists of ``outputs`` - a mapping of output ports and their value - and
from typing import Optional
from typing_extensions import TypedDict
from tickit.core.device import ConfigurableDevice, DeviceUpdate
from tickit.core.device import Device, DeviceUpdate
from tickit.core.typedefs import SimTime
class Shutter(ConfigurableDevice):
class ShutterDevice(Device):
Inputs = TypedDict("Inputs", {"flux": float})
Outputs = TypedDict("Outputs", {"flux": float})
Expand Down Expand Up @@ -107,6 +107,33 @@ which consists of ``outputs`` - a mapping of output ports and their value - and
output_flux = inputs["flux"] * self.position
return DeviceUpdate(Shutter.Outputs(flux=output_flux), call_at)
Creating a ComponentConfig
--------------------------

In order to create the Device we must create a `ComponentConfig` that knows how
to instantiate a Device.

.. code-block:: python
from tickit.core.components.component import Component, ComponentConfig
from tickit.core.components.device_simulation import DeviceSimulation
@dataclass
class Shutter(ComponentConfig):
default_position: float
initial_position: Optional[float] = None
def __call__(self) -> Component:
return DeviceSimulation(
name=self.name,
device=ShutterDevice(
default_position=self.default_position,
initial_position=self.initial_position,
),
)
Using the Device
----------------

Expand All @@ -118,28 +145,20 @@ per our implementation, and a `Sink` named sink which will recieve the resulting

.. code-block:: yaml
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.source.Source:
value: 42.0
inputs: {}
- tickit.devices.source.Source:
name: source
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
my_shutter.Shutter:
default_position: 0.2
inputs:
flux: source:value
inputs: {}
value: 42.0
- examples.devices.shutter.Shutter:
name: shutter
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.sink.Sink: {}
inputs:
flux: shutter:flux
flux: source:value
default_position: 0.2
initial_position: 0.24
- tickit.devices.sink.Sink:
name: sink
inputs:
flux: shutter:flux
.. seealso::
See the `Creating a Simulation` tutorial for a walk-through of creating simulation
Expand Down
4 changes: 2 additions & 2 deletions docs/tutorials/creating-an-adapter.rst
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ Adapter Class
-------------

We shall begin by defining the ShutterAdapter class which inherits
`ConfigurableAdapter` - by doing so a configuration dataclass will automatically be
`Adapter` - by doing so a configuration dataclass will automatically be
created for the adapter, allowing easy YAML configuration.

.. code-block:: python
Expand Down Expand Up @@ -69,7 +69,7 @@ appending a line break and a ``CommandInterpreter``.
.. note::
Arguments to the ``__init__`` method may be specified in the simulation config file
if the device inherits `ConfigurableAdapter` (excluding ``device`` and
if the device inherits `Adapter` (excluding ``device`` and
``raise_interrupt`` which are injected at run-time).

Adapter Commands
Expand Down
23 changes: 8 additions & 15 deletions examples/configs/attns.yaml
Original file line number Diff line number Diff line change
@@ -1,18 +1,11 @@
- tickit.core.components.device_simulation.DeviceSimulation:
adapters:
- tickit.devices.pneumatic.pneumatic.PneumaticAdapter:
ioc_name: PNEUMATIC
db_file: tickit/devices/pneumatic/db_files/filter1.db
device:
tickit.devices.pneumatic.pneumatic.Pneumatic:
initial_speed: 2.5
initial_state: False
inputs: {}
- tickit.devices.pneumatic.Pneumatic:
name: filter1
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.sink.Sink: {}
inputs: {}
initial_speed: 0.5
initial_state: False
ioc_name: PNEUMATIC
db_file: tickit/devices/pneumatic/db_files/filter1.db
- tickit.devices.sink.Sink:
name: contr_sink
inputs:
input: filter1:output
name: contr_sink
19 changes: 6 additions & 13 deletions examples/configs/cryo-tcp.yaml
Original file line number Diff line number Diff line change
@@ -1,15 +1,8 @@
- tickit.core.components.device_simulation.DeviceSimulation:
adapters:
- tickit.devices.cryostream.cryostream.CryostreamAdapter: {}
device:
tickit.devices.cryostream.cryostream.Cryostream: {}
inputs: {}
- tickit.devices.cryostream.Cryostream:
name: cryo
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.sink.Sink: {}
inputs:
input: cryo:temperature
name: contr_sink
inputs: {}
- tickit.devices.sink.Sink:
name: contr_sink
inputs:
input: cryo:temperature

27 changes: 10 additions & 17 deletions examples/configs/current-monitor.yaml
Original file line number Diff line number Diff line change
@@ -1,19 +1,12 @@
- tickit.core.components.device_simulation.DeviceSimulation:
adapters:
- tickit.devices.femto.femto.FemtoAdapter:
ioc_name: FEMTO
db_file: tickit/devices/femto/record.db
device:
tickit.devices.femto.femto.Femto:
initial_gain: 2.5
initial_current: 0.0
- tickit.devices.femto.Current:
name: current_device
inputs: {}
callback_period: 1000000000
- tickit.devices.femto.Femto:
name: femto
inputs:
input: current_device:output
name: femto
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.femto.femto.CurrentDevice:
callback_period: 1000000000
inputs: {}
name: current_device
initial_gain: 2.5
initial_current: 0.0
db_file: tickit/devices/femto/record.db
ioc_name: FEMTO
21 changes: 7 additions & 14 deletions examples/configs/http-device.yaml
Original file line number Diff line number Diff line change
@@ -1,17 +1,10 @@
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.source.Source:
value: False
inputs: {}
- tickit.devices.source.Source:
name: source
- tickit.core.components.device_simulation.DeviceSimulation:
adapters:
- examples.devices.http_device.ExampleHTTPAdapter: {}
device:
examples.devices.http_device.ExampleHTTPDevice:
foo: False
bar: 10
inputs: {}
value: False
- examples.devices.http_device.ExampleHTTP:
name: http-device
inputs:
foo: source:value
name: http-device
foo: False
bar: 10
43 changes: 14 additions & 29 deletions examples/configs/nested.yaml
Original file line number Diff line number Diff line change
@@ -1,37 +1,22 @@
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
examples.devices.trampoline.RandomTrampoline:
callback_period: 10000000000
inputs: {}
- examples.devices.trampoline.RandomTrampoline:
name: random_trampoline
inputs: {}
callback_period: 10000000000
- tickit.core.components.system_simulation.SystemSimulation:
components:
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.sink.Sink: {}
inputs:
sink_1: external:input_1
name: internal_sink
- tickit.core.components.device_simulation.DeviceSimulation:
adapters:
- examples.devices.remote_controlled.RemoteControlledAdapter:
server:
tickit.adapters.servers.tcp.TcpServer: {}
device:
examples.devices.remote_controlled.RemoteControlled: {}
inputs: {}
name: internal_tcp_controlled
name: internal_tickit
inputs:
input_1: random_trampoline:output
name: internal_tickit
components:
- tickit.devices.sink.Sink:
name: internal_sink
inputs:
sink_1: external:input_1
- examples.devices.remote_controlled.RemoteControlled:
name: internal_tcp_controlled
inputs: {}
expose:
output_1: internal_tcp_controlled:observed
- tickit.core.components.device_simulation.DeviceSimulation:
adapters: []
device:
tickit.devices.sink.Sink: {}
- tickit.devices.sink.Sink:
name: external_sink
inputs:
sink_1: internal_tickit:output_1
name: external_sink
Loading

0 comments on commit 2e41f45

Please sign in to comment.