Add more tests, speed up dummy stage

I've sped up the stage motion, to run tests in a more reasonable time.

CSM doesn't yet calibrate in the test - I suspect debugging of capture_array
is needed.
This commit is contained in:
Richard Bowman 2024-12-03 00:16:26 +00:00
parent 728aa12d07
commit 1e7069b375
3 changed files with 66 additions and 77 deletions

View file

@ -58,49 +58,13 @@ build-backend = "setuptools.build_meta"
# don't believe it's critical at present, because we don't use # don't believe it's critical at present, because we don't use
# the Python package - we distribute a tarball directly. # the Python package - we distribute a tarball directly.
[tool.black]
exclude = '(\.eggs|\.git|\.venv|\venv|node_modules/)'
[tool.isort]
multi_line_output = 3
include_trailing_comma = true
force_grid_wrap = 0
use_parentheses = true
ensure_newline_before_comments = true
line_length = 88
[tool.pylint.'MESSAGES CONTROL']
# W1203 warns about using f strings in logging statements.
# I'm really not concerned about the performance implications of this,
# particularly as we often have the log level set quite high. I think
# for our current purposes, using f strings in logging statements is
# more readable and thus a good idea in many places - I've disabled
# this warning code globally for that reason.
disable = "fixme,C,R,W1203"
max-line-length = 88
[tool.mypy] [tool.mypy]
plugins = ["pydantic.mypy"] plugins = ["pydantic.mypy"]
[tool.ruff] [tool.ruff]
target-version = "py39" target-version = "py39"
[tool.poe.executor] [tool.pytest.ini_options]
type = "virtualenv" addopts = [
location = ".venv" "--import-mode=importlib",
]
[tool.poe.tasks]
black = "black ."
black_check = "black --check ."
ruff = "ruff . --fix"
ruff_check = "ruff ."
isort = "isort openflexure_microscope"
pylint = "pylint openflexure_microscope"
mypy = "mypy --cobertura-xml-report openflexure_microscope openflexure_microscope"
test = "pytest . --junitxml=pytest_report.xml"
serve = "python -m openflexure_microscope.api.app"
format = ["black", "isort", "ruff"]
lint = ["ruff_check", "pylint", "mypy"]
check = ["format", "lint", "test"]

View file

@ -13,6 +13,9 @@ class DummyStage(BaseStage):
This stage should work similarly to a Sangaboard stage, but without any This stage should work similarly to a Sangaboard stage, but without any
hardware attached. hardware attached.
""" """
def __init__(self, step_time: float=0.001, **kwargs):
super().__init__(**kwargs)
self.step_time = step_time
def __enter__(self): def __enter__(self):
self.instantaneous_position = self.position self.instantaneous_position = self.position
@ -27,7 +30,7 @@ class DummyStage(BaseStage):
self.moving = True self.moving = True
try: try:
fraction_complete = 0.0 fraction_complete = 0.0
dt = 0.001 dt = self.step_time
max_displacement = max(abs(v) for v in displacement) max_displacement = max(abs(v) for v in displacement)
start_time = time.time() start_time = time.time()
while time.time() - start_time < dt * max_displacement: while time.time() - start_time < dt * max_displacement:

View file

@ -1,4 +1,5 @@
import json import json
import os
import tempfile import tempfile
from fastapi import Depends, FastAPI from fastapi import Depends, FastAPI
@ -6,49 +7,70 @@ from fastapi.testclient import TestClient
from labthings_fastapi.client import ThingClient from labthings_fastapi.client import ThingClient
from PIL import Image from PIL import Image
import piexif import piexif
import pytest
from openflexure_microscope_server.server import ThingServer from openflexure_microscope_server.server import ThingServer
from openflexure_microscope_server.things.camera.simulation import SimulatedCamera from openflexure_microscope_server.things.camera.simulation import SimulatedCamera
from openflexure_microscope_server.things.stage.dummy import DummyStage from openflexure_microscope_server.things.stage.dummy import DummyStage
from openflexure_microscope_server.things.autofocus import AutofocusThing from openflexure_microscope_server.things.autofocus import AutofocusThing
from openflexure_microscope_server.things.camera_stage_mapping import CameraStageMapper
temp_folder = tempfile.TemporaryDirectory() @pytest.fixture
server = ThingServer(temp_folder.name) def thing_server():
server.add_thing(SimulatedCamera(), "/camera/") temp_folder = tempfile.TemporaryDirectory()
server.add_thing(DummyStage(), "/stage/") server = ThingServer(settings_folder=temp_folder.name)
server.add_thing(AutofocusThing(), "/autofocus/") server.add_thing(SimulatedCamera(), "/camera/")
server.add_thing(DummyStage(step_time=0.000001), "/stage/")
server.add_thing(AutofocusThing(), "/autofocus/")
server.add_thing(CameraStageMapper(), "/camera_stage_mapping/")
assert os.path.exists(os.path.join(temp_folder.name, "camera/"))
# NB yield is important: otherwise, the temp folder gets deleted before the test runs
yield server
def test_autofocus(): @pytest.fixture
with TestClient(server.app) as client: def client(thing_server):
autofocus = ThingClient.from_url("/autofocus/", client) with TestClient(thing_server.app) as client:
_ = autofocus.fast_autofocus() yield client
def test_grab_jpeg(): @pytest.fixture
with TestClient(server.app) as client: def slower_client(thing_server):
camera = ThingClient.from_url("/camera/", client) thing_server.things["/stage/"].step_time = 0.0002
blob = camera.grab_jpeg() with TestClient(thing_server.app) as client:
_image = Image.open(blob.open()) yield client
def test_capture_jpeg_metadata(): def test_autofocus(slower_client):
with TestClient(server.app) as client: client = slower_client
camera = ThingClient.from_url("/camera/", client) autofocus = ThingClient.from_url("/autofocus/", client)
blob = camera.capture_jpeg() _ = autofocus.fast_autofocus()
image = Image.open(blob.open())
exif_dict = piexif.load(image.info["exif"])
encoded_metadata = exif_dict["Exif"][piexif.ExifIFD.UserComment]
metadata = json.loads(encoded_metadata)
assert "position" in metadata["/stage/"]
def test_stage(): def test_grab_jpeg(client):
with TestClient(server.app) as client: camera = ThingClient.from_url("/camera/", client)
stage = ThingClient.from_url("/stage/", client) blob = camera.grab_jpeg()
start = stage.position _image = Image.open(blob.open())
move = {"x": 1, "y": 2, "z": 3}
stage.move_relative(**move) def test_capture_jpeg_metadata(client):
pos = stage.position camera = ThingClient.from_url("/camera/", client)
for s, m, p in zip(start.values(), move.values(), pos.values()): blob = camera.capture_jpeg()
assert s + m == p image = Image.open(blob.open())
stage.move_relative(**{k: -v for k, v in move.items()}) exif_dict = piexif.load(image.info["exif"])
pos = stage.position encoded_metadata = exif_dict["Exif"][piexif.ExifIFD.UserComment]
for s, p in zip(start.values(), pos.values()): metadata = json.loads(encoded_metadata)
assert s == p assert "position" in metadata["/stage/"]
def test_stage(client):
stage = ThingClient.from_url("/stage/", client)
start = stage.position
move = {"x": 1, "y": 2, "z": 3}
stage.move_relative(**move)
pos = stage.position
for s, m, p in zip(start.values(), move.values(), pos.values()):
assert s + m == p
stage.move_relative(**{k: -v for k, v in move.items()})
pos = stage.position
for s, p in zip(start.values(), pos.values()):
assert s == p
# Currently this fails, not yet sure why.
#def test_camera_stage_mapping_calibration(client):
# camera_stage_mapping = ThingClient.from_url("/camera_stage_mapping/", client)
# camera_stage_mapping.calibrate_xy()