add type hints to transformations

This commit is contained in:
2021-07-14 11:59:35 +02:00
parent eeca8b7ad9
commit d9ab104612
2 changed files with 39 additions and 9 deletions

View File

@@ -1,4 +1,5 @@
import abc import abc
from situr.image.situ_image import SituImage
import numpy as np import numpy as np
import scipy import scipy
@@ -9,18 +10,26 @@ class ChannelTransform(Transform):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@abc.abstractmethod @abc.abstractmethod
def apply_transformation(self, situ_img, channel): def apply_transformation(self, situ_img: SituImage, channel: int):
"""Performs a transformation on one channel, all focus_levels are transformed the same way""" """Performs a transformation on one channel, all focus_levels are transformed the same way"""
raise NotImplementedError( raise NotImplementedError(
self.__class__.__name__ + '.apply_transformation') self.__class__.__name__ + '.apply_transformation')
class IdentityChannelTransform(ChannelTransform): class IdentityChannelTransform(ChannelTransform):
def apply_transformation(self, situ_img, channel): def apply_transformation(self, situ_img: SituImage, channel: int):
pass pass
class ScaleRotateTranslateChannelTransform(ChannelTransform): class ScaleRotateTranslateChannelTransform(ChannelTransform):
def __init__(self, transform_matrix, scale=1, offset=np.array([0, 0])): def __init__(self, transform_matrix: np.ndarray, scale: float = 1, offset: np.ndarray = np.array([0, 0])):
"""Constructor for a Transformation that supports rotation, translation and scaling on a channel
Args:
transform_matrix (np.ndarray): A matrix of shape (2,2)
scale (float, optional): The scale factor. Defaults to 1.
offset (np.ndarray, optional): The offset of shape (2,). Defaults to np.array([0, 0]).
"""
# TODO: check # TODO: check
# * transform matrix is 2x2 # * transform matrix is 2x2
# * offset is array (2,) # * offset is array (2,)
@@ -28,7 +37,7 @@ class ScaleRotateTranslateChannelTransform(ChannelTransform):
self.offset = offset self.offset = offset
self.scale = scale self.scale = scale
def apply_tranformation(self, situ_img, channel): def apply_tranformation(self, situ_img: SituImage, channel: int):
channel_img = situ_img.get_channel(channel) channel_img = situ_img.get_channel(channel)
focus_levels = channel_img.shape[0] focus_levels = channel_img.shape[0]

View File

@@ -1,4 +1,5 @@
import abc import abc
from situr.image.situ_tile import Tile
import scipy import scipy
import numpy as np import numpy as np
from situr.image import situ_image from situr.image import situ_image
@@ -9,19 +10,39 @@ class RoundTransform(Transform):
__metaclass__ = abc.ABCMeta __metaclass__ = abc.ABCMeta
@abc.abstractmethod @abc.abstractmethod
def apply_transformation(self, situ_tile, round): def apply_transformation(self, situ_tile: Tile, round: int):
"""Performs a transformation on one round, all channels and focus_levels are transformed the same way""" """Performs a transformation on one round, all channels and focus_levels are transformed the same way
Args:
situ_tile (Tile): The tile the transformation is applied to.
round (int): The round that the transformation is to be applied to.
Raises:
NotImplementedError: This method is abstract and therefore raises an error
"""
raise NotImplementedError( raise NotImplementedError(
self.__class__.__name__ + '.apply_transformation') self.__class__.__name__ + '.apply_transformation')
class IdentityRoundTransform(RoundTransform): class IdentityRoundTransform(RoundTransform):
def apply_transformation(self, situ_tile, round): def apply_transformation(self, situ_tile: Tile, round: Tile):
"""Performs the identity transformation (meaning no transformation)
Args:
situ_tile (Tile): The tile the transformation is applied to.
round (Tile): The round that the transformation is to be applied to.
"""
pass pass
class ScaleRotateTranslateRoundTransform(RoundTransform): class ScaleRotateTranslateRoundTransform(RoundTransform):
def __init__(self, transform_matrix, scale=1, offset=np.array([0, 0])): def __init__(self, transform_matrix: np.ndarray, scale: int = 1, offset: np.array = np.array([0, 0])):
"""Constructor for a Transformation that supports rotation, translation and scaling on a channel
Args:
transform_matrix (np.ndarray): A matrix of shape (2,2)
scale (int, optional): The scale factor. Defaults to 1.
offset (np.array, optional): The offset of shape (2,). Defaults to np.array([0, 0]).
"""
# TODO: check # TODO: check
# * transform matrix is 2x2 # * transform matrix is 2x2
# * offset is array (2,) # * offset is array (2,)
@@ -29,7 +50,7 @@ class ScaleRotateTranslateRoundTransform(RoundTransform):
self.offset = offset self.offset = offset
self.scale = scale self.scale = scale
def apply_tranformation(self, situ_tile, round): def apply_tranformation(self, situ_tile: Tile, round: int):
situ_image = situ_tile.get_image_round(round) situ_image = situ_tile.get_image_round(round)
for channel in range(situ_image.get_channel_count()): for channel in range(situ_image.get_channel_count()):