Update test_camera and test_dummy_server for labthings-fastapi 0.0.12

This commit is contained in:
Julian Stirling 2025-12-14 20:04:48 +00:00
parent 0c3de60629
commit 70fc3516e6
2 changed files with 28 additions and 24 deletions

View file

@ -17,12 +17,12 @@ def camera_server() -> lt.ThingClient:
The test client will be needed for the camera to run async frame generation code. The test client will be needed for the camera to run async frame generation code.
""" """
with tempfile.TemporaryDirectory() as tmpdir: with tempfile.TemporaryDirectory() as tmpdir:
conf = { thing_conf = {
"camera": "openflexure_microscope_server.things.camera.simulation:SimulatedCamera", "camera": "openflexure_microscope_server.things.camera.simulation:SimulatedCamera",
"stage": "openflexure_microscope_server.things.stage.dummy:DummyStage", "stage": "openflexure_microscope_server.things.stage.dummy:DummyStage",
} }
server = lt.ThingServer(things=conf, settings_folder=tmpdir) server = lt.ThingServer(things=thing_conf, settings_folder=tmpdir)
yield server yield server

View file

@ -20,26 +20,30 @@ from PIL import Image
import labthings_fastapi as lt import labthings_fastapi as lt
from openflexure_microscope_server.things.autofocus import AutofocusThing
from openflexure_microscope_server.things.camera.simulation import SimulatedCamera
from openflexure_microscope_server.things.camera_stage_mapping import CameraStageMapper
from openflexure_microscope_server.things.stage.dummy import DummyStage
@pytest.fixture @pytest.fixture
def thing_server(): def thing_server():
"""Yield a server with a very basic configuration.""" """Yield a server with a very basic configuration."""
temp_folder = tempfile.TemporaryDirectory() temp_folder = tempfile.TemporaryDirectory()
server = lt.ThingServer(settings_folder=temp_folder.name) thing_conf = {
server.add_thing( "camera": {
SimulatedCamera( "class": "openflexure_microscope_server.things.camera.simulation:SimulatedCamera",
shape=(240, 320, 3), canvas_shape=(1000, 1500, 3), frame_interval=0.01 "kwargs": {
), "shape": (240, 320, 3),
"camera", "canvas_shape": (1000, 1500, 3),
) "frame_interval": 0.01,
server.add_thing(DummyStage(step_time=0.000001), "stage") },
server.add_thing(AutofocusThing(), "autofocus") },
server.add_thing(CameraStageMapper(), "camera_stage_mapping") "stage": {
"class": "openflexure_microscope_server.things.stage.dummy:DummyStage",
"kwargs": {"step_time": 0.000001},
},
"autofocus": "openflexure_microscope_server.things.autofocus:AutofocusThing",
"camera_stage_mapping": "openflexure_microscope_server.things.camera_stage_mapping:CameraStageMapper",
}
server = lt.ThingServer(things=thing_conf, settings_folder=temp_folder.name)
assert os.path.exists(os.path.join(temp_folder.name, "camera")) assert os.path.exists(os.path.join(temp_folder.name, "camera"))
# Note: yield is important. If return is used the temp folder gets deleted # Note: yield is important. If return is used the temp folder gets deleted
# before the test runs. Silence PT022 as ruff doesn't think yield is needed. # before the test runs. Silence PT022 as ruff doesn't think yield is needed.
@ -68,20 +72,20 @@ def slower_client(thing_server):
def test_autofocus(slower_client): def test_autofocus(slower_client):
"""Test Fast Autofocus can run doesn't raise an exception.""" """Test Fast Autofocus can run doesn't raise an exception."""
client = slower_client client = slower_client
autofocus = lt.ThingClient.from_url("autofocus", client) autofocus = lt.ThingClient.from_url("/autofocus/", client)
_ = autofocus.fast_autofocus() _ = autofocus.fast_autofocus()
def test_grab_jpeg(client): def test_grab_jpeg(client):
"""Check that grab_jpeg returns a blob that can be opened.""" """Check that grab_jpeg returns a blob that can be opened."""
camera = lt.ThingClient.from_url("camera", client) camera = lt.ThingClient.from_url("/camera/", client)
blob = camera.grab_jpeg() blob = camera.grab_jpeg()
_image = Image.open(blob.open()) _image = Image.open(blob.open())
def test_capture_jpeg_metadata(client): def test_capture_jpeg_metadata(client):
"""Check that the position is encoded into the image metadata.""" """Check that the position is encoded into the image metadata."""
camera = lt.ThingClient.from_url("camera", client) camera = lt.ThingClient.from_url("/camera/", client)
blob = camera.capture_jpeg() blob = camera.capture_jpeg()
image = Image.open(blob.open()) image = Image.open(blob.open())
exif_dict = piexif.load(image.info["exif"]) exif_dict = piexif.load(image.info["exif"])
@ -92,7 +96,7 @@ def test_capture_jpeg_metadata(client):
def test_stage(client): def test_stage(client):
"""Test moving th stage forwards and backwards.""" """Test moving th stage forwards and backwards."""
stage = lt.ThingClient.from_url("stage", client) stage = lt.ThingClient.from_url("/stage/", client)
start = stage.position start = stage.position
move = {"x": 1, "y": 2, "z": 3} move = {"x": 1, "y": 2, "z": 3}
stage.move_relative(**move) stage.move_relative(**move)
@ -107,14 +111,14 @@ def test_stage(client):
def test_capture_array(client): def test_capture_array(client):
"""Capture array from simulation and check the size is as expected.""" """Capture array from simulation and check the size is as expected."""
camera = lt.ThingClient.from_url("camera", client) camera = lt.ThingClient.from_url("/camera/", client)
array = np.asarray(camera.capture_array()) array = np.asarray(camera.capture_array())
assert array.shape == (240, 320, 3) assert array.shape == (240, 320, 3)
def test_camera_stage_mapping_calibration(client): def test_camera_stage_mapping_calibration(client):
"""Check that camera stage mapping can run without an exception.""" """Check that camera stage mapping can run without an exception."""
camera = lt.ThingClient.from_url("camera", client) camera = lt.ThingClient.from_url("/camera/", client)
camera.settling_time = 0 camera.settling_time = 0
camera_stage_mapping = lt.ThingClient.from_url("camera_stage_mapping", client) camera_stage_mapping = lt.ThingClient.from_url("/camera_stage_mapping/", client)
camera_stage_mapping.calibrate_xy() camera_stage_mapping.calibrate_xy()