From d9ab10461205def1bc74f4c778d8b53eaa132e92 Mon Sep 17 00:00:00 2001 From: "Hannes F. Kuchelmeister" Date: Wed, 14 Jul 2021 11:59:35 +0200 Subject: [PATCH] add type hints to transformations --- .../transformation/channel_transformation.py | 17 +++++++--- situr/transformation/round_transformation.py | 31 ++++++++++++++++--- 2 files changed, 39 insertions(+), 9 deletions(-) diff --git a/situr/transformation/channel_transformation.py b/situr/transformation/channel_transformation.py index 7c9b783..48e0e5d 100644 --- a/situr/transformation/channel_transformation.py +++ b/situr/transformation/channel_transformation.py @@ -1,4 +1,5 @@ import abc +from situr.image.situ_image import SituImage import numpy as np import scipy @@ -9,18 +10,26 @@ class ChannelTransform(Transform): __metaclass__ = abc.ABCMeta @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""" raise NotImplementedError( self.__class__.__name__ + '.apply_transformation') class IdentityChannelTransform(ChannelTransform): - def apply_transformation(self, situ_img, channel): + def apply_transformation(self, situ_img: SituImage, channel: int): pass + 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 # * transform matrix is 2x2 # * offset is array (2,) @@ -28,7 +37,7 @@ class ScaleRotateTranslateChannelTransform(ChannelTransform): self.offset = offset 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) focus_levels = channel_img.shape[0] diff --git a/situr/transformation/round_transformation.py b/situr/transformation/round_transformation.py index 14f6852..ea13ace 100644 --- a/situr/transformation/round_transformation.py +++ b/situr/transformation/round_transformation.py @@ -1,4 +1,5 @@ import abc +from situr.image.situ_tile import Tile import scipy import numpy as np from situr.image import situ_image @@ -9,19 +10,39 @@ class RoundTransform(Transform): __metaclass__ = abc.ABCMeta @abc.abstractmethod - def apply_transformation(self, situ_tile, round): - """Performs a transformation on one round, all channels and focus_levels are transformed the same way""" + 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 + + 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( self.__class__.__name__ + '.apply_transformation') 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 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 # * transform matrix is 2x2 # * offset is array (2,) @@ -29,7 +50,7 @@ class ScaleRotateTranslateRoundTransform(RoundTransform): self.offset = offset 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) for channel in range(situ_image.get_channel_count()):