Skip to content

Hardware Autofocus Plan#

Ways to describe a hardware-based autofocus plan.

AutoFocusPlan #

Base class for hardware autofocus plans.

Attributes:

Name Type Description
autofocus_device_name str | None

Optional name of the offset motor device. If None, acquisition engines may attempt to set the offset however they see fit (such as using a current or default autofocus device.)

autofocus_motor_offset float | None

Before autofocus is performed, the autofocus motor should be moved to this offset, if applicable. (Not all autofocus devices have an offset motor.) If None, the autofocus motor should not be moved.

as_action() -> HardwareAutofocus #

Return a useq.HardwareAutofocus for this autofocus plan.

event(event: MDAEvent) -> Optional[MDAEvent] #

Return an autofocus useq.MDAEvent if autofocus should be performed.

The z position of the new useq.MDAEvent is also updated if a relative zplan is provided since autofocus shuld be performed on the home z stack position.

should_autofocus(event: MDAEvent) -> bool #

Method that must be implemented by a subclass.

Should return True if autofocus should be performed (see useq.AxesBasedAF).

AxesBasedAF #

Autofocus plan that performs autofocus when any of the specified axes change.

Attributes:

Name Type Description
axes Tuple[str, ...]

Tuple of axis label to use for hardware autofocus. At every event in which any axis in this tuple is change, autofocus will be performed. For example, if axes is ('p',) then autofocus will be performed every time the p axis is change, (in other words: every time the position is changed.).

Source code in src/useq/_hardware_autofocus.py
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
class AxesBasedAF(AutoFocusPlan):
    """Autofocus plan that performs autofocus when any of the specified axes change.

    Attributes
    ----------
    axes : Tuple[str, ...]
        Tuple of axis label to use for hardware autofocus.  At every event in which
        *any* axis in this tuple is change, autofocus will be performed.  For example,
        if `axes` is `('p',)` then autofocus will be performed every time the `p` axis
        is change, (in other words: every time the position is changed.).
    """

    axes: tuple[str, ...]
    _previous: dict = PrivateAttr(default_factory=dict)

    def should_autofocus(self, event: MDAEvent) -> bool:
        """Return `True` if autofocus should be performed at this event.

        Will return `True` if any of the axes specified in `axes` have changed from the
        previous event.
        """
        self._previous, previous = dict(event.index), self._previous
        return any(
            axis in self.axes and previous.get(axis) != index
            for axis, index in event.index.items()
        )

should_autofocus(event: MDAEvent) -> bool #

Return True if autofocus should be performed at this event.

Will return True if any of the axes specified in axes have changed from the previous event.

Source code in src/useq/_hardware_autofocus.py
77
78
79
80
81
82
83
84
85
86
87
def should_autofocus(self, event: MDAEvent) -> bool:
    """Return `True` if autofocus should be performed at this event.

    Will return `True` if any of the axes specified in `axes` have changed from the
    previous event.
    """
    self._previous, previous = dict(event.index), self._previous
    return any(
        axis in self.axes and previous.get(axis) != index
        for axis, index in event.index.items()
    )