nanopyx.methods.channel_registration.corrector

  1import numpy as np
  2from skimage.io import imread
  3
  4# TODO recheck values
  5from ...core.transform.align_channels import align_channels as new_align_channels
  6
  7
  8class ChannelRegistrationCorrector(object):
  9    """
 10    Corrector class for channel registration in an image stack.
 11
 12    This class provides methods for aligning channels in an image stack using translation masks.
 13
 14    Args:
 15        None
 16
 17    Attributes:
 18        aligned_stack (numpy.ndarray): The aligned image stack after correction.
 19
 20    Methods:
 21        load_translation_masks(path=None): Load translation masks from a file.
 22
 23        align_channels(img_stack, translation_masks=None): Align channels in an image stack using translation masks.
 24
 25    Example:
 26        corrector = ChannelRegistrationCorrector()
 27        translation_masks = corrector.load_translation_masks("translation_masks.tif")
 28        aligned_stack = corrector.align_channels(img_stack, translation_masks)
 29
 30    Note:
 31        The `ChannelRegistrationCorrector` class is used for correcting channel registration in an image stack.
 32        It provides methods for loading translation masks and aligning channels using these masks.
 33    """
 34
 35    def __init__(self):
 36        """
 37        Initialize the `ChannelRegistrationCorrector` object.
 38
 39        Args:
 40            None
 41
 42        Returns:
 43            None
 44
 45        Example:
 46            corrector = ChannelRegistrationCorrector()
 47        """
 48        self.aligned_stack = None
 49
 50    def load_translation_masks(self, path=None):
 51        """
 52        Load translation masks from a file.
 53
 54        Args:
 55            path (str, optional): The file path to load translation masks from. If not provided, a user input prompt will be used to specify the path.
 56
 57        Returns:
 58            numpy.ndarray: The loaded translation masks as a NumPy array.
 59
 60        Example:
 61            translation_masks = corrector.load_translation_masks("translation_masks.tif")
 62
 63        Note:
 64            This method loads translation masks from a file and returns them as a NumPy array.
 65        """
 66        if path is not None:
 67            path = input("Please provide a filepath to the translation masks")
 68
 69        return imread(path)
 70
 71    def align_channels(self, img_stack, translation_masks=None):
 72        """
 73        Align channels in an image stack using translation masks.
 74
 75        Args:
 76            img_stack (numpy.ndarray): The input image stack with shape [n_channels, height, width].
 77            translation_masks (numpy.ndarray, optional): The translation masks to use for alignment. If not provided, they will be loaded.
 78
 79        Returns:
 80            numpy.ndarray: The aligned image stack after correction.
 81
 82        Example:
 83            aligned_stack = corrector.align_channels(img_stack, translation_masks)
 84
 85        Note:
 86            This method aligns channels in an image stack using translation masks.
 87        """
 88        translation_masks = translation_masks
 89
 90        if translation_masks is None:
 91            translation_masks = self.load_translation_masks()
 92
 93        input_d_type = img_stack.dtype
 94
 95        n_channels = img_stack.shape[0]
 96        height = img_stack.shape[1]
 97        width = img_stack.shape[2]
 98
 99        self.aligned_stack = np.empty((n_channels, height, width))
100        channels_list = list(range(n_channels))
101
102        for channel in channels_list:
103            img_slice = img_stack[channel].astype(np.float32)
104            translation_mask = translation_masks[channel].astype(np.float32)
105            if np.sum(translation_mask) == 0:
106                self.aligned_stack[channel] = img_slice
107            else:
108                self.aligned_stack[channel] = new_align_channels(np.ascontiguousarray(img_slice.reshape((1, height, width)), dtype=np.float32), translation_mask)
109
110        return self.aligned_stack.astype(input_d_type)
class ChannelRegistrationCorrector:
  9class ChannelRegistrationCorrector(object):
 10    """
 11    Corrector class for channel registration in an image stack.
 12
 13    This class provides methods for aligning channels in an image stack using translation masks.
 14
 15    Args:
 16        None
 17
 18    Attributes:
 19        aligned_stack (numpy.ndarray): The aligned image stack after correction.
 20
 21    Methods:
 22        load_translation_masks(path=None): Load translation masks from a file.
 23
 24        align_channels(img_stack, translation_masks=None): Align channels in an image stack using translation masks.
 25
 26    Example:
 27        corrector = ChannelRegistrationCorrector()
 28        translation_masks = corrector.load_translation_masks("translation_masks.tif")
 29        aligned_stack = corrector.align_channels(img_stack, translation_masks)
 30
 31    Note:
 32        The `ChannelRegistrationCorrector` class is used for correcting channel registration in an image stack.
 33        It provides methods for loading translation masks and aligning channels using these masks.
 34    """
 35
 36    def __init__(self):
 37        """
 38        Initialize the `ChannelRegistrationCorrector` object.
 39
 40        Args:
 41            None
 42
 43        Returns:
 44            None
 45
 46        Example:
 47            corrector = ChannelRegistrationCorrector()
 48        """
 49        self.aligned_stack = None
 50
 51    def load_translation_masks(self, path=None):
 52        """
 53        Load translation masks from a file.
 54
 55        Args:
 56            path (str, optional): The file path to load translation masks from. If not provided, a user input prompt will be used to specify the path.
 57
 58        Returns:
 59            numpy.ndarray: The loaded translation masks as a NumPy array.
 60
 61        Example:
 62            translation_masks = corrector.load_translation_masks("translation_masks.tif")
 63
 64        Note:
 65            This method loads translation masks from a file and returns them as a NumPy array.
 66        """
 67        if path is not None:
 68            path = input("Please provide a filepath to the translation masks")
 69
 70        return imread(path)
 71
 72    def align_channels(self, img_stack, translation_masks=None):
 73        """
 74        Align channels in an image stack using translation masks.
 75
 76        Args:
 77            img_stack (numpy.ndarray): The input image stack with shape [n_channels, height, width].
 78            translation_masks (numpy.ndarray, optional): The translation masks to use for alignment. If not provided, they will be loaded.
 79
 80        Returns:
 81            numpy.ndarray: The aligned image stack after correction.
 82
 83        Example:
 84            aligned_stack = corrector.align_channels(img_stack, translation_masks)
 85
 86        Note:
 87            This method aligns channels in an image stack using translation masks.
 88        """
 89        translation_masks = translation_masks
 90
 91        if translation_masks is None:
 92            translation_masks = self.load_translation_masks()
 93
 94        input_d_type = img_stack.dtype
 95
 96        n_channels = img_stack.shape[0]
 97        height = img_stack.shape[1]
 98        width = img_stack.shape[2]
 99
100        self.aligned_stack = np.empty((n_channels, height, width))
101        channels_list = list(range(n_channels))
102
103        for channel in channels_list:
104            img_slice = img_stack[channel].astype(np.float32)
105            translation_mask = translation_masks[channel].astype(np.float32)
106            if np.sum(translation_mask) == 0:
107                self.aligned_stack[channel] = img_slice
108            else:
109                self.aligned_stack[channel] = new_align_channels(np.ascontiguousarray(img_slice.reshape((1, height, width)), dtype=np.float32), translation_mask)
110
111        return self.aligned_stack.astype(input_d_type)

Corrector class for channel registration in an image stack.

This class provides methods for aligning channels in an image stack using translation masks.

Args: None

Attributes: aligned_stack (numpy.ndarray): The aligned image stack after correction.

Methods: load_translation_masks(path=None): Load translation masks from a file.

align_channels(img_stack, translation_masks=None): Align channels in an image stack using translation masks.

Example: corrector = ChannelRegistrationCorrector() translation_masks = corrector.load_translation_masks("translation_masks.tif") aligned_stack = corrector.align_channels(img_stack, translation_masks)

Note: The ChannelRegistrationCorrector class is used for correcting channel registration in an image stack. It provides methods for loading translation masks and aligning channels using these masks.

ChannelRegistrationCorrector()
36    def __init__(self):
37        """
38        Initialize the `ChannelRegistrationCorrector` object.
39
40        Args:
41            None
42
43        Returns:
44            None
45
46        Example:
47            corrector = ChannelRegistrationCorrector()
48        """
49        self.aligned_stack = None

Initialize the ChannelRegistrationCorrector object.

Args: None

Returns: None

Example: corrector = ChannelRegistrationCorrector()

aligned_stack
def load_translation_masks(self, path=None):
51    def load_translation_masks(self, path=None):
52        """
53        Load translation masks from a file.
54
55        Args:
56            path (str, optional): The file path to load translation masks from. If not provided, a user input prompt will be used to specify the path.
57
58        Returns:
59            numpy.ndarray: The loaded translation masks as a NumPy array.
60
61        Example:
62            translation_masks = corrector.load_translation_masks("translation_masks.tif")
63
64        Note:
65            This method loads translation masks from a file and returns them as a NumPy array.
66        """
67        if path is not None:
68            path = input("Please provide a filepath to the translation masks")
69
70        return imread(path)

Load translation masks from a file.

Args: path (str, optional): The file path to load translation masks from. If not provided, a user input prompt will be used to specify the path.

Returns: numpy.ndarray: The loaded translation masks as a NumPy array.

Example: translation_masks = corrector.load_translation_masks("translation_masks.tif")

Note: This method loads translation masks from a file and returns them as a NumPy array.

def align_channels(self, img_stack, translation_masks=None):
 72    def align_channels(self, img_stack, translation_masks=None):
 73        """
 74        Align channels in an image stack using translation masks.
 75
 76        Args:
 77            img_stack (numpy.ndarray): The input image stack with shape [n_channels, height, width].
 78            translation_masks (numpy.ndarray, optional): The translation masks to use for alignment. If not provided, they will be loaded.
 79
 80        Returns:
 81            numpy.ndarray: The aligned image stack after correction.
 82
 83        Example:
 84            aligned_stack = corrector.align_channels(img_stack, translation_masks)
 85
 86        Note:
 87            This method aligns channels in an image stack using translation masks.
 88        """
 89        translation_masks = translation_masks
 90
 91        if translation_masks is None:
 92            translation_masks = self.load_translation_masks()
 93
 94        input_d_type = img_stack.dtype
 95
 96        n_channels = img_stack.shape[0]
 97        height = img_stack.shape[1]
 98        width = img_stack.shape[2]
 99
100        self.aligned_stack = np.empty((n_channels, height, width))
101        channels_list = list(range(n_channels))
102
103        for channel in channels_list:
104            img_slice = img_stack[channel].astype(np.float32)
105            translation_mask = translation_masks[channel].astype(np.float32)
106            if np.sum(translation_mask) == 0:
107                self.aligned_stack[channel] = img_slice
108            else:
109                self.aligned_stack[channel] = new_align_channels(np.ascontiguousarray(img_slice.reshape((1, height, width)), dtype=np.float32), translation_mask)
110
111        return self.aligned_stack.astype(input_d_type)

Align channels in an image stack using translation masks.

Args: img_stack (numpy.ndarray): The input image stack with shape [n_channels, height, width]. translation_masks (numpy.ndarray, optional): The translation masks to use for alignment. If not provided, they will be loaded.

Returns: numpy.ndarray: The aligned image stack after correction.

Example: aligned_stack = corrector.align_channels(img_stack, translation_masks)

Note: This method aligns channels in an image stack using translation masks.