Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 34 additions & 25 deletions examples/base-device/camera_example.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
from examples.resources.simulated_camera import Camera
from view.widgets.base_device_widget import BaseDeviceWidget
from qtpy.QtWidgets import QApplication
import sys
from qtpy.QtCore import Slot
import threading
from time import sleep

from voxel.devices.camera.simulated import Camera
from qtpy.QtCore import Slot
from qtpy.QtWidgets import QApplication

from view.widgets.base_device_widget import BaseDeviceWidget


def scan_for_properties(device):
"""Scan for properties with setters and getters in class and return dictionary
:param device: object to scan through for properties
"""
"""_summary_

:param device: _description_
:type device: _type_
:return: _description_
:rtype: _type_
"""
prop_dict = {}
for attr_name in dir(device):
attr = getattr(type(device), attr_name, None)
if isinstance(attr, property): #and attr.fset is not None:
if isinstance(attr, property): # and attr.fset is not None:
prop_dict[attr_name] = getattr(device, attr_name)

return prop_dict

def device_change():
"""Simulate changing device properties"""

def device_change():
"""_summary_"""
for i in range(0, 100):
if i == 25:
print('changing exposure_time_ms')
print("changing exposure_time_ms")
base.exposure_time_ms = 2500.0
if i == 50:
print('changing pixel_type')
base.pixel_type = 'mono16'
if i ==75:
print('changing roi')
print("changing pixel_type")
base.pixel_type = "mono16"
if i == 75:
print("changing roi")
# Need to change whole dictionary to trigger update. DOES NOT WORK changing one item
base.roi = {'width_px': 1016, 'height_px': 2032, 'width_offset_px': 0, 'height_offest_px': 0}
if i ==99:
print('changing sensor_height_px')
base.sensor_height_px = 10640/2
sleep(.1)
base.roi = {"width_px": 1016, "height_px": 2032, "width_offset_px": 0, "height_offest_px": 0}
if i == 99:
print("changing sensor_height_px")
base.sensor_height_px = 10640 / 2
sleep(0.1)


@Slot(str)
def widget_property_changed(name):
"""Slot to signal when widget has been changed
:param name: name of attribute and widget"""
"""_summary_

:param name: _description_
:type name: _type_
"""
name_lst = name.split(".")
print(name, " changed to ", getattr(base, name_lst[0]))

name_lst = name.split('.')
print(name, ' changed to ', getattr(base, name_lst[0]))

if __name__ == "__main__":
app = QApplication(sys.argv)
simulated_camera = Camera('camera')
simulated_camera = Camera("camera")
camera_properties = scan_for_properties(simulated_camera)
print(camera_properties)
base = BaseDeviceWidget(Camera, camera_properties)
Expand Down
87 changes: 44 additions & 43 deletions examples/base-device/instrument_example.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,27 @@
from view.widgets.base_device_widget import BaseDeviceWidget
from qtpy.QtWidgets import QApplication
import os
import sys
from pathlib import Path

from qtpy.QtCore import Slot
from voxel.instruments import Instrument
from qtpy.QtWidgets import QApplication

from view.widgets.base_device_widget import BaseDeviceWidget
from voxel.acquisition import Acquisition
from pathlib import Path
import os
from voxel.instruments import Instrument

RESOURCES_DIR = Path(os.path.dirname(os.path.realpath(__file__))) / "resources"
ACQUISITION_YAML = RESOURCES_DIR / "test_acquisition.yaml"
INSTRUMENT_YAML = RESOURCES_DIR / "simulated_instrument.yaml"

RESOURCES_DIR = (
Path(os.path.dirname(os.path.realpath(__file__))) / "resources"
)
ACQUISITION_YAML = RESOURCES_DIR / 'test_acquisition.yaml'
INSTRUMENT_YAML = RESOURCES_DIR / 'simulated_instrument.yaml'

def scan_for_properties(device):
"""Scan for properties with setters and getters in class and return dictionary
:param device: object to scan through for properties
"""
"""_summary_

:param device: _description_
:type device: _type_
:return: _description_
:rtype: _type_
"""
prop_dict = {}
for attr_name in dir(device):
attr = getattr(type(device), attr_name, None)
Expand All @@ -28,38 +32,44 @@ def scan_for_properties(device):


def set_up_guis(devices, device_type):
"""_summary_

:param devices: _description_
:type devices: _type_
:param device_type: _description_
:type device_type: _type_
:return: _description_
:rtype: _type_
"""
guis = {}
for name, device in devices.items():
properties = scan_for_properties(device)
# TODO: better way to find out what module
guis[name] = BaseDeviceWidget(type(device), properties)
guis[name].setWindowTitle(f'{device_type} {name}')
guis[name].setWindowTitle(f"{device_type} {name}")
guis[name].ValueChangedInside[str].connect(
lambda value, dev=device, widget=guis[name],: widget_property_changed(value, dev, widget))
lambda value, dev=device, widget=guis[name],: widget_property_changed(value, dev, widget)
)
guis[name].show()
return guis


@Slot(str)
def widget_property_changed(name, device, widget):
"""Slot to signal when widget has been changed
:param name: name of attribute and widget"""

name_lst = name.split('.')
print('widget', name, ' changed to ', getattr(widget, name_lst[0]))
"""_summary_

:param name: _description_
:type name: _type_
:param device: _description_
:type device: _type_
:param widget: _description_
:type widget: _type_
"""
name_lst = name.split(".")
print("widget", name, " changed to ", getattr(widget, name_lst[0]))
value = getattr(widget, name_lst[0])
setattr(device, name_lst[0], value)
# if len(name_lst) == 1: # name refers to attribute
# setattr(device, name, value)
# else: # name is a dictionary and key pair split by .
# dictionary = getattr(device, name_lst[0])
# # new = {k:getattr(widget, f'{name_lst[0]}.{k}') for k in dictionary.keys()}
# # print(new)
# for k in dictionary.keys():
# print(k, dir(widget))
# print(k, getattr(widget, f'{name_lst[0]}.{k}_'))

print('Device', name, ' changed to ', getattr(device, name_lst[0]))
print("Device", name, " changed to ", getattr(device, name_lst[0]))


if __name__ == "__main__":
Expand All @@ -68,20 +78,11 @@ def widget_property_changed(name, device, widget):
# instrument
instrument = Instrument(INSTRUMENT_YAML)

laser_ui = set_up_guis(instrument.lasers, 'laser')
combiner_ui = set_up_guis(instrument.combiners, 'combiner')
camera_ui = set_up_guis(instrument.cameras, 'camera')
laser_ui = set_up_guis(instrument.lasers, "laser")
combiner_ui = set_up_guis(instrument.combiners, "combiner")
camera_ui = set_up_guis(instrument.cameras, "camera")

# acquisition
acquisition = Acquisition(instrument, ACQUISITION_YAML)

# simulated_camera = Camera('camera')
# camera_properties = scan_for_properties(simulated_camera)
# print(camera_properties)
# base = BaseDeviceWidget(Camera, "examples.resources.simulated_camera", camera_properties)
# base.ValueChangedInside[str].connect(widget_property_changed)

# t1 = threading.Thread(target=device_change, args=())
# t1.start()

sys.exit(app.exec_())
57 changes: 33 additions & 24 deletions examples/base-device/laser_example.py
Original file line number Diff line number Diff line change
@@ -1,55 +1,64 @@
from examples.resources.simulated_laser import SimulatedLaser
from view.widgets.base_device_widget import BaseDeviceWidget
from qtpy.QtWidgets import QApplication
import sys
from qtpy.QtCore import Slot
import threading
from time import sleep

from voxel.devices.laser.simulated import SimulatedLaser
from qtpy.QtCore import Slot
from qtpy.QtWidgets import QApplication

from view.widgets.base_device_widget import BaseDeviceWidget


def scan_for_properties(device):
"""Scan for properties with setters and getters in class and return dictionary
:param device: object to scan through for properties
"""
"""_summary_

:param device: _description_
:type device: _type_
:return: _description_
:rtype: _type_
"""
prop_dict = {}
for attr_name in dir(device):
attr = getattr(type(device), attr_name, None)
if isinstance(attr, property): #and attr.fset is not None:
if isinstance(attr, property): # and attr.fset is not None:
prop_dict[attr_name] = getattr(device, attr_name)

return prop_dict

def device_change():
"""Simulate changing device properties"""

def device_change():
"""_summary_"""
for i in range(0, 100):
if i == 25:
print('changing temperature')
print("changing temperature")
base.temperature = 25.0
if i == 50:
print('changing cdrh')
base.cdrh = 'OFF'
if i ==75:
print('changing test_property')
print("changing cdrh")
base.cdrh = "OFF"
if i == 75:
print("changing test_property")
# Need to change whole dictionary to trigger update. DOES NOT WORK changing one item
base.test_property = {"value0":"internal", "value1":"on"}
if i ==99:
print('changing power')
base.test_property = {"value0": "internal", "value1": "on"}
if i == 99:
print("changing power")
base.power_setpoint_mw = 67.0
sleep(.1)
sleep(0.1)


@Slot(str)
def widget_property_changed(name):
"""Slot to signal when widget has been changed
:param name: name of attribute and widget"""
"""_summary_

:param name: _description_
:type name: _type_
"""
name_lst = name.split(".")
print(name, " changed to ", getattr(base, name_lst[0]))

name_lst = name.split('.')
print(name, ' changed to ', getattr(base, name_lst[0]))

if __name__ == "__main__":
app = QApplication(sys.argv)
simulated_laser = SimulatedLaser(port='COM3')
simulated_laser = SimulatedLaser(port="COM3")
laser_properties = scan_for_properties(simulated_laser)
base = BaseDeviceWidget(laser_properties, laser_properties)
base.ValueChangedInside[str].connect(widget_property_changed)
Expand Down
26 changes: 13 additions & 13 deletions examples/base-device/schema_example.py
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
from view.widgets.base_device_widget import BaseDeviceWidget
from qtpy.QtWidgets import QApplication
import sys
from qtpy.QtCore import Slot

from aind_data_schema.core import acquisition
from qtpy.QtCore import Slot
from qtpy.QtWidgets import QApplication

from view.widgets.base_device_widget import BaseDeviceWidget


@Slot(str)
def widget_property_changed(name):
"""Slot to signal when widget has been changed
:param name: name of attribute and widget"""
"""_summary_

:param name: _description_
:type name: _type_
"""
name_lst = name.split(".")
print(name, " changed to ", getattr(base, name_lst[0]))

name_lst = name.split('.')
print(name, ' changed to ', getattr(base, name_lst[0]))
# if len(name_lst) == 1: # name refers to attribute
# setattr(writer, name, value)
# else: # name is a dictionary and key pair split by .
# getattr(writer, name_lst[0]).__setitem__(name_lst[1], value)
# print(name, ' changed to ', getattr(writer, name_lst[0]))

if __name__ == "__main__":
app = QApplication(sys.argv)
acquisition_properties = {k:'' for k in acquisition.Acquisition.model_fields.keys()}
acquisition_properties = {k: "" for k in acquisition.Acquisition.model_fields.keys()}

base = BaseDeviceWidget(acquisition.Acquisition.model_fields, acquisition_properties)
base.ValueChangedInside[str].connect(widget_property_changed)
Expand Down
Loading