def test_load_camera(self, tmp_path): """Test loading a camera from class file directly.""" name = "Dummy Camera" class_name = "DummyCamera" config_defaults = { "intval": "1", "boolval": "No", "strval": "This is a test string" } device_py_path = tmp_path / "dummy_camera.py" with open(device_py_path, "w+") as f: f.write( dummy_device.format(class_name=class_name, name=name, parent_class="CameraInterface")) loader = pylo.DeviceLoader() loader.addDeviceFromFile("camera", name, device_py_path, class_name, config_defaults, "Test description") controller = pylo.Controller(None, pylo.AbstractConfiguration()) device = self.check_loaded_device(loader, name, class_name, "dummy-camera", kind="camera", controller=controller) assert hasattr(device, "controller") assert isinstance(device.controller, pylo.Controller) assert device.controller == controller assert isinstance(device, pylo.CameraInterface)
import os if __name__ == "__main__": # For direct call only import sys sys.path.insert(0, os.path.dirname(os.path.dirname(__file__))) import random import pytest import math import pylo controller = pylo.Controller(pylo.AbstractView(), pylo.AbstractConfiguration()) ############################################################################### ### ### ### Change only this for testing a different microscope ### ### ### ############################################################################### # How to use # ********** # # 1. Create a new function `create_your_microscope()` that returns your # microscope object instance # 2. Add your `create_your_microscope()` to the configurations tuple # # For an example check the commented code below. # def create_your_microscope() -> plyo.MicroscopeInterface
print("Initializing python environment...") import pprint import time import random import threading import importlib import traceback try: import pylo print("Preparing...") view = pylo.DMView() configuration = pylo.AbstractConfiguration() controller = pylo.Controller(view, configuration) controller.microscope = pylo.loader.getDevice("Dummy Microscope", controller) controller.camera = pylo.loader.getDevice("Dummy Camera", controller) tests = [ # "error", # "hint", # "create-measurement", "create-measurement-series", # "ask-for-decision", # "show-settings", # "show-custom-tags",
def test_expand_vars(self): controller = pylo.Controller(pylo.AbstractView(), pylo.AbstractConfiguration()) controller.microscope = pylo.loader.getDevice("Dummy Microscope", controller) controller.camera = pylo.loader.getDevice("Dummy Camera", controller) text = [] check = [] series = {} start = {} s = series for i, var in enumerate(controller.microscope.supported_measurement_variables): s["variable"] = var.unique_id s["start"] = pylolib.parse_value(var.format, random.uniform(var.min_value, var.max_value / 2)) s["end"] = pylolib.parse_value(var.format, random.uniform(var.max_value / 2, var.max_value)) s["step-width"] = pylolib.parse_value(var.format, (s["end"] - s["start"]) / 5) start[var.unique_id] = s["start"] if var.has_calibration and isinstance(var.calibrated_name, str): name = var.calibrated_name else: name = var.name if var.has_calibration and isinstance(var.calibrated_unit, str): unit = var.calibrated_unit else: unit = var.unit text.append("id: {{varname[{}]}}".format(var.unique_id)) check.append(("id", name)) text.append("unit: {{varunit[{}]}}".format(var.unique_id)) check.append(("unit", unit)) text.append("step: {{step[{}]}}".format(var.unique_id)) check.append(("step", ("step", var.unique_id))) text.append("hstep: {{humanstep[{}]}}".format(var.unique_id)) check.append(("hstep", ("humanstep", var.unique_id))) text.append("start: {{start[{}]}}".format(var.unique_id)) check.append(("start", start[var.unique_id])) text.append("hstart: {{humanstart[{}]}}".format(var.unique_id)) check.append(("hstart", self.formatVar(var, start[var.unique_id]))) j = i text.append("series-start: {{series[{}][start]}}".format(j)) check.append(("series-start", s["start"])) text.append("series-end: {{series[{}][end]}}".format(j)) check.append(("series-end", s["end"])) text.append("series-width: {{series[{}][step-width]}}".format(j)) check.append(("series-width", s["step-width"])) text.append("series-id: {{series[{}][variable]}}".format(j)) check.append(("series-id", var.unique_id)) text.append("hseries-start: {{humanseries[{}][start]}}".format(j)) check.append(("hseries-start", self.formatVar(var, s["start"]))) text.append("hseries-end: {{humanseries[{}][end]}}".format(j)) check.append(("hseries-end", self.formatVar(var, s["end"]))) text.append("hseries-width: {{humanseries[{}][step-width]}}".format(j)) check.append(("hseries-width", self.formatVar(var, s["step-width"]))) text.append("hseries-var: {{humanseries[{}][variable]}}".format(j)) check.append(("hseries-var", name)) if i + 1 < len(controller.microscope.supported_measurement_variables): s["on-each-point"] = {} s = s["on-each-point"] steps = pylo.MeasurementSteps(controller, start, series) counter = random.randint(0, len(steps) - 1) step = steps[counter] text.append("counter: {counter}") check.append(("counter", counter)) time_format = "%Y%m%d%H%M" text.append("time: {{time:{}}}".format(time_format)) check.append(("time", datetime.datetime.now().strftime(time_format))) tags = { "test 1": "Value", "group": { "test 2": 1, "test 3": False } } text.append("tag: {tags[test 1]}") check.append(("tag", tags["test 1"])) text.append("tag: {tags[group][test 2]}") check.append(("tag", tags["group"]["test 2"])) text.append("tag: {tags[group][test 3]}") check.append(("tag", tags["group"]["test 3"])) formatted_text = pylolib.expand_vars(*text, controller=controller, step=step, start=start, series=series, tags=tags, counter=counter) print("step", step) print("start", start) print("series", series) print("tags", tags) print("counter", counter) print("check", check) print(formatted_text) for i, (name, val) in enumerate(check): if (isinstance(val, tuple) and len(val) == 2 and val[0] in ("step", "humanstep")): if val[0] == "step": val = step[val[1]] else: var = controller.microscope.getMeasurementVariableById(val[1]) val = self.formatVar(var, step[val[1]]) val = str(val) print("searching for",name, "with value", val) assert val in formatted_text[i]
def test_for_not_implemented(self): camera = pylo.CameraInterface(controller=pylo.Controller( pylo.AbstractView(), pylo.AbstractConfiguration())) with pytest.raises(NotImplementedError): camera.recordImage()
def test_show_settings_raise_not_implemented(self, view): """Test if all the functions raise NotImplementedErrors""" with pytest.raises(NotImplementedError): view.showSettings( pylo.Controller(view, pylo.AbstractConfiguration()))
def test_show_create_measurement_raise_not_implemented(self, view): """Test if all the functions raise NotImplementedErrors""" with pytest.raises(NotImplementedError): view.showCreateMeasurement( pylo.Controller(view, pylo.AbstractConfiguration()))