Skip to content

Commit

Permalink
Fix some mypy issues
Browse files Browse the repository at this point in the history
  • Loading branch information
stefsmeets committed Oct 25, 2024
1 parent efde4df commit ce7cd54
Show file tree
Hide file tree
Showing 9 changed files with 139 additions and 109 deletions.
6 changes: 6 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ changelog = "https://github.com/instamatic-dev/instamatic/releases"
develop = [
"bump2version",
"check-manifest",
"mypy",
"pre-commit",
"pytest >= 5.4.1",
"coverage",
Expand Down Expand Up @@ -151,6 +152,11 @@ required-imports = ["from __future__ import annotations"]
quote-style = "single"
indent-style = "space"

[tool.mypy]
files = ["src", "tests"]
allow_redefinition = true
ignore_missing_imports = true

[tool.bumpversion]
current_version = "2.0.5"

Expand Down
31 changes: 22 additions & 9 deletions src/instamatic/TEMController/TEMController.py
Original file line number Diff line number Diff line change
Expand Up @@ -524,7 +524,9 @@ def from_dict(self, dct: dict):
except TypeError:
func(v)

def get_raw_image(self, exposure: float = None, binsize: int = None) -> np.ndarray:
def get_raw_image(
self, exposure: Optional[float] = None, binsize: Optional[int] = None
) -> np.ndarray:
"""Simplified function equivalent to `get_image` that only returns the
raw data array.
Expand All @@ -542,7 +544,9 @@ def get_raw_image(self, exposure: float = None, binsize: int = None) -> np.ndarr
"""
return self.cam.get_image(exposure=exposure, binsize=binsize)

def get_future_image(self, exposure: float = None, binsize: int = None) -> 'future':
def get_future_image(
self, exposure: Optional[float] = None, binsize: Optional[int] = None
) -> 'future':
"""Simplified function equivalent to `get_image` that returns the raw
image as a future. This makes the data acquisition call non-blocking.
Expand All @@ -566,7 +570,9 @@ def get_future_image(self, exposure: float = None, binsize: int = None) -> 'futu
future = self._executor.submit(self.get_raw_image, exposure=exposure, binsize=binsize)
return future

def get_rotated_image(self, exposure: float = None, binsize: int = None) -> np.ndarray:
def get_rotated_image(
self, exposure: Optional[float] = None, binsize: Optional[int] = None
) -> np.ndarray:
"""Simplified function equivalent to `get_image` that returns the
rotated image array.
Expand Down Expand Up @@ -598,13 +604,13 @@ def get_rotated_image(self, exposure: float = None, binsize: int = None) -> np.n

def get_image(
self,
exposure: float = None,
binsize: int = None,
exposure: Optional[float] = None,
binsize: Optional[int] = None,
comment: str = '',
out: str = None,
out: Optional[str] = None,
plot: bool = False,
verbose: bool = False,
header_keys: Tuple[str] = 'all',
header_keys: Optional[Tuple[str]] = None,
) -> Tuple[np.ndarray, dict]:
"""Retrieve image as numpy array from camera. If the exposure and
binsize are not given, the default values are read from the config
Expand Down Expand Up @@ -685,7 +691,12 @@ def get_image(
return arr, h

def get_movie(
self, n_frames: int, *, exposure: float = None, binsize: int = None, out: str = None
self,
n_frames: int,
*,
exposure: Optional[float] = None,
binsize: Optional[int] = None,
out: Optional[str] = None,
) -> Tuple[np.ndarray]:
"""Collect a stack of images using the camera's movie mode, if
available.
Expand Down Expand Up @@ -740,7 +751,9 @@ def store_diff_beam(self, name: str = 'beam', save_to_file: bool = False):
keys = 'FunctionMode', 'Brightness', 'GunTilt', 'DiffFocus', 'SpotSize'
self.store(name=name, keys=keys, save_to_file=save_to_file)

def store(self, name: str = 'stash', keys: tuple = None, save_to_file: bool = False):
def store(
self, name: str = 'stash', keys: Optional[tuple] = None, save_to_file: bool = False
):
"""Stores current settings to dictionary.
Multiple settings can be stored under different names. Specify
Expand Down
14 changes: 7 additions & 7 deletions src/instamatic/TEMController/jeol_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
import atexit
import logging
import time
from typing import Tuple
from typing import Optional, Tuple

import comtypes.client

Expand Down Expand Up @@ -279,7 +279,7 @@ def getImageShift2(self) -> Tuple[int, int]:
def setImageShift2(self, x: int, y: int):
self.def3.SetIS2(x, y)

def getStagePosition(self) -> Tuple[int, int, int, int, int]:
def getStagePosition(self) -> Tuple[float, float, float, float, float]:
"""X, y, z in nanometer a and b in degrees."""
x, y, z, a, b, result = self.stage3.GetPos()
return x, y, z, a, b
Expand Down Expand Up @@ -334,11 +334,11 @@ def stopStage(self):

def setStagePosition(
self,
x: int = None,
y: int = None,
z: int = None,
a: int = None,
b: int = None,
x: Optional[float] = None,
y: Optional[float] = None,
z: Optional[float] = None,
a: Optional[float] = None,
b: Optional[float] = None,
wait: bool = True,
):
if z is not None:
Expand Down
8 changes: 4 additions & 4 deletions src/instamatic/TEMController/lenses.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,14 +121,14 @@ def __repr__(self):
def index(self) -> int:
return self._indexgetter()

@property
def absolute_index(self) -> int:
return self._tem.getMagnificationAbsoluteIndex()

@index.setter
def index(self, index: int):
self._indexsetter(index)

@property
def absolute_index(self) -> int:
return self._tem.getMagnificationAbsoluteIndex()

def increase(self) -> None:
try:
self.index += 1
Expand Down
6 changes: 4 additions & 2 deletions src/instamatic/TEMController/microscope_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ def getSpotSize(self) -> int:
pass

@abstractmethod
def getStagePosition(self) -> Tuple[int, int, int, int, int]:
def getStagePosition(self) -> Tuple[float, float, float, float, float]:
pass

@abstractmethod
Expand Down Expand Up @@ -170,7 +170,9 @@ def setSpotSize(self, value: int) -> None:
pass

@abstractmethod
def setStagePosition(self, x: int, y: int, z: int, a: int, b: int, wait: bool) -> None:
def setStagePosition(
self, x: float, y: float, z: float, a: float, b: float, wait: bool
) -> None:
pass

@abstractmethod
Expand Down
30 changes: 15 additions & 15 deletions src/instamatic/TEMController/simu_microscope.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import random
import time
from typing import Tuple
from typing import Optional, Tuple

from instamatic import config
from instamatic.exceptions import TEMValueError
Expand Down Expand Up @@ -87,8 +87,8 @@ def __init__(self, name: str = 'simulate'):
self._HT = 200_000 # V

# self.Magnification_value = random.choice(self.MAGNIFICATIONS)
self.Magnification_value = config.microscope.ranges['mag1'][10]
self.Magnification_value_diff = config.microscope.ranges['diff'][3]
self.Magnification_value: int = config.microscope.ranges['mag1'][10] # type: ignore
self.Magnification_value_diff: int = config.microscope.ranges['diff'][3] # type: ignore

self.beamblank = False

Expand Down Expand Up @@ -372,7 +372,7 @@ def setImageShift2(self, x: int, y: int):
self.ImageShift2_x = x
self.ImageShift2_y = y

def getStagePosition(self) -> Tuple[int, int, int, int, int]:
def getStagePosition(self) -> Tuple[float, float, float, float, float]:
return (
self.StagePosition_x,
self.StagePosition_y,
Expand All @@ -390,32 +390,32 @@ def waitForStage(self, delay: float = 0.1):
while self.isStageMoving():
time.sleep(delay)

def setStageX(self, value: int, wait: bool = True):
def setStageX(self, value: float, wait: bool = True):
self.StagePosition_x = value
if wait:
self.waitForStage()

def setStageY(self, value: int, wait: bool = True):
def setStageY(self, value: float, wait: bool = True):
self.StagePosition_y = value
if wait:
self.waitForStage()

def setStageZ(self, value: int, wait: bool = True):
def setStageZ(self, value: float, wait: bool = True):
self.StagePosition_z = value
if wait:
self.waitForStage()

def setStageA(self, value: int, wait: bool = True):
def setStageA(self, value: float, wait: bool = True):
self.StagePosition_a = value
if wait:
self.waitForStage()

def setStageB(self, value: int, wait: bool = True):
def setStageB(self, value: float, wait: bool = True):
self.StagePosition_b = value
if wait:
self.waitForStage()

def setStageXY(self, x: int, y: int, wait: bool = True):
def setStageXY(self, x: float, y: float, wait: bool = True):
self.StagePosition_x = x
self.StagePosition_y = y
if wait:
Expand All @@ -426,11 +426,11 @@ def stopStage(self):

def setStagePosition(
self,
x: int = None,
y: int = None,
z: int = None,
a: int = None,
b: int = None,
x: Optional[float] = None,
y: Optional[float] = None,
z: Optional[float] = None,
a: Optional[float] = None,
b: Optional[float] = None,
speed: float = -1,
wait: bool = True,
):
Expand Down
Loading

0 comments on commit ce7cd54

Please sign in to comment.