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
13 changes: 7 additions & 6 deletions software/control/core/auto_focus_worker.py
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,13 @@ def run_autofocus(self):
# trigger acquisition (including turning on the illumination) and read frame
if self.liveController.trigger_mode == control._def.TriggerMode.SOFTWARE:
self.liveController.turn_on_illumination()
self.wait_till_operation_is_completed()
self.camera.send_trigger()
image = self.camera.read_frame()
try:
self.wait_till_operation_is_completed()
self.camera.send_trigger()
image = self.camera.read_frame()
finally:
# The light goes off after each plane, also when its frame does not come or the camera raises.
self.liveController.turn_off_illumination()
elif self.liveController.trigger_mode == control._def.TriggerMode.HARDWARE:
if (
"Fluorescence" in self.liveController.currentConfiguration.name
Expand All @@ -98,9 +102,6 @@ def run_autofocus(self):
image = self.camera.read_frame()
if image is None:
continue
# tunr of the illumination if using software trigger
if self.liveController.trigger_mode == control._def.TriggerMode.SOFTWARE:
self.liveController.turn_off_illumination()

image = utils.crop_image(image, self.crop_width, self.crop_height)
self._image_to_display_fn(image)
Expand Down
90 changes: 90 additions & 0 deletions software/tests/control/test_autofocus_light_off.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,90 @@
"""The sweep of the contrast autofocus turns its light off after each plane, in the software trigger mode.

Also when the frame of a plane does not come, and when the camera raises."""

import threading
from types import SimpleNamespace
from unittest.mock import MagicMock

import numpy as np
import pytest

from control._def import TriggerMode
from control.core.auto_focus_worker import AutofocusWorker

PLANES = 5


class Light:
"""The illumination as the live controller switches it. `on` is its state now."""

trigger_mode = TriggerMode.SOFTWARE

def __init__(self):
self.on = False

def turn_on_illumination(self):
self.on = True

def turn_off_illumination(self):
self.on = False


def make_worker(read_frame):
light = Light()
board = MagicMock()
board.is_busy.return_value = False
camera = SimpleNamespace(send_trigger=lambda: None, read_frame=read_frame)
controller = SimpleNamespace(
camera=camera,
microcontroller=board,
stage=MagicMock(),
liveController=light,
nl5=None,
N=PLANES,
deltaZ=0.001,
crop_width=64,
crop_height=64,
)
keep_running = threading.Event()
keep_running.set()
return AutofocusWorker(controller, lambda: None, lambda image: None, keep_running), light


def frames(missing):
"""A camera whose frames at the plane numbers in `missing` do not come."""
count = iter(range(PLANES))
rng = np.random.default_rng(1)

def read_frame():
return None if next(count) in missing else rng.integers(0, 4000, (64, 64)).astype(np.uint16)

return read_frame


def test_the_light_is_off_after_a_sweep_with_all_its_frames():
worker, light = make_worker(frames(missing=()))
worker.run_autofocus()
assert light.on is False


def test_the_light_is_off_when_the_last_frame_does_not_come():
worker, light = make_worker(frames(missing=(PLANES - 1,)))
worker.run_autofocus()
assert light.on is False # before: the light stayed on after the sweep


def test_the_light_is_off_when_no_frame_comes():
worker, light = make_worker(frames(missing=range(PLANES)))
worker.run_autofocus()
assert light.on is False


def test_the_light_is_off_when_the_camera_raises():
def read_frame():
raise RuntimeError("the camera lost its connection")

worker, light = make_worker(read_frame)
with pytest.raises(RuntimeError, match="lost its connection"):
worker.run_autofocus()
assert light.on is False
Loading