Skip to content

Commit f1ef9cd

Browse files
committed
Add type annotations for ABC DeformableMirror and Mirao52e (issue #108)
1 parent a0d6706 commit f1ef9cd

File tree

2 files changed

+26
-24
lines changed

2 files changed

+26
-24
lines changed

microscope/devices.py

Lines changed: 16 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -28,20 +28,19 @@
2828
import functools
2929
import itertools
3030
import logging
31+
import queue
32+
import threading
3133
import time
34+
import typing
35+
3236
from ast import literal_eval
33-
from collections import OrderedDict
37+
from collections import OrderedDict, namedtuple
3438
from threading import Thread
35-
import threading
36-
import Pyro4
37-
import numpy
38-
import queue
39-
4039
from enum import Enum, EnumMeta
4140

4241
import numpy
42+
import Pyro4
4343

44-
from collections import namedtuple
4544
# A tuple that defines a region of interest.
4645
ROI = namedtuple('ROI', ['left', 'top', 'width', 'height'])
4746
# A tuple containing parameters for horizontal and vertical binning.
@@ -948,7 +947,7 @@ class DeformableMirror(Device):
948947
__metaclass__ = abc.ABCMeta
949948

950949
@abc.abstractmethod
951-
def __init__(self, **kwargs):
950+
def __init__(self, **kwargs) -> None:
952951
"""Constructor.
953952
954953
Subclasses must define the following properties during
@@ -962,14 +961,14 @@ def __init__(self, **kwargs):
962961
"""
963962
super(DeformableMirror, self).__init__(**kwargs)
964963

965-
self._patterns = None
966-
self._pattern_idx = None
964+
self._patterns = None # type: typing.Optional[numpy.ndarray]
965+
self._pattern_idx = -1 # type: int
967966

968967
@property
969-
def n_actuators(self):
968+
def n_actuators(self) -> int:
970969
return self._n_actuators
971970

972-
def _validate_patterns(self, patterns):
971+
def _validate_patterns(self, patterns: numpy.ndarray) -> None:
973972
"""Validate the shape of a series of patterns.
974973
975974
Only validates the shape of the patterns, not if the values
@@ -987,12 +986,12 @@ def _validate_patterns(self, patterns):
987986
% (patterns.shape[-1], self._n_actuators)))
988987

989988
@abc.abstractmethod
990-
def apply_pattern(self, pattern):
989+
def apply_pattern(self, pattern: numpy.ndarray) -> None:
991990
"""Apply this pattern.
992991
"""
993992
pass
994993

995-
def queue_patterns(self, patterns):
994+
def queue_patterns(self, patterns: numpy.ndarray) -> None:
996995
"""Send values to the mirror.
997996
998997
Parameters
@@ -1009,7 +1008,7 @@ def queue_patterns(self, patterns):
10091008
self._patterns = patterns
10101009
self._pattern_idx = -1 # none is applied yet
10111010

1012-
def next_pattern(self):
1011+
def next_pattern(self) -> None:
10131012
"""Apply the next pattern in the queue.
10141013
10151014
A convenience fallback is provided.
@@ -1019,10 +1018,10 @@ def next_pattern(self):
10191018
self._pattern_idx += 1
10201019
self.apply_pattern(self._patterns[self._pattern_idx,:])
10211020

1022-
def initialize(self):
1021+
def initialize(self) -> None:
10231022
pass
10241023

1025-
def _on_shutdown(self):
1024+
def _on_shutdown(self) -> None:
10261025
pass
10271026

10281027

microscope/mirror/mirao52e.py

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,16 @@
3939
"""
4040

4141
import ctypes
42+
import typing
43+
44+
import numpy
4245

4346
import microscope.devices
4447
import microscope._wrappers.mirao52e as mro
4548

4649

4750
class Mirao52e(microscope.devices.DeformableMirror):
48-
def __init__(self, **kwargs):
51+
def __init__(self, **kwargs) -> None:
4952
super().__init__(**kwargs)
5053
## Status is not the return code of the function calls.
5154
## Status is where we can find the error code in case a
@@ -57,34 +60,34 @@ def __init__(self, **kwargs):
5760

5861
## super class needs this, but maybe it should be calling the
5962
## property directly?
60-
self._n_actuators = mro.NB_COMMAND_VALUES
63+
self._n_actuators = mro.NB_COMMAND_VALUES # type: int
6164

6265
@property
63-
def n_actuators(self):
66+
def n_actuators(self) -> int:
6467
return mro.NB_COMMAND_VALUES
6568

6669
@staticmethod
67-
def _normalize_patterns(patterns):
70+
def _normalize_patterns(patterns: numpy.ndarray) -> numpy.ndarray:
6871
"""
6972
mirao52e SDK expects values in the [-1 1] range, so we normalize
7073
them from the [0 1] range we expect in our interface.
7174
"""
7275
patterns = (patterns * 2) -1
7376
return patterns
7477

75-
def apply_pattern(self, pattern):
78+
def apply_pattern(self, pattern: numpy.ndarray) -> None:
7679
self._validate_patterns(pattern)
7780
pattern = self._normalize_patterns(pattern)
7881
command = pattern.ctypes.data_as(mro.Command)
7982
if not mro.applyCommand(command, mro.FALSE, self._status):
8083
self._raise_status(mro.applyCommand)
8184

82-
def _raise_status(self, func):
85+
def _raise_status(self, func: typing.Callable) -> None:
8386
error_code = self._status.contents.value
8487
raise RuntimeError('mro_%s() failed (error code %d)'
8588
% (func.__name__, error_code))
8689

87-
def __del__(self):
90+
def __del__(self) -> None:
8891
if not mro.close(self._status):
8992
self._raise_status(mro.close)
9093
super().__del__()

0 commit comments

Comments
 (0)