nanopyx.core.io.zip_image_loader

  1import os
  2import zipfile
  3
  4import numpy as np
  5import skimage.io
  6from skimage import transform
  7
  8
  9class ZipTiffIterator:
 10    _shape = None
 11    _dtype = None
 12    _im0 = None
 13
 14    def __init__(self, zip_file_path: str):
 15        """
 16        Iterator to sequentially open tiff files contained in a zip file
 17        :param zip_file_path: path to the zip file
 18        :type zip_file_path: str
 19        """
 20        self.zip_file_path = zip_file_path
 21        self.zip_file = zipfile.ZipFile(zip_file_path)
 22        self.tiff_file_names = [
 23            name for name in self.zip_file.namelist() if name.endswith(".tif") and not name.startswith("_")
 24        ]
 25        self.tiff_file_names = sorted(self.tiff_file_names)
 26        # print(self.tiff_file_names)
 27
 28    def __getitem__(self, index: int) -> np.ndarray:
 29        """
 30        Get the item at the specified index.
 31
 32        Parameters:
 33            index (int): The index of the item to get.
 34
 35        Returns:
 36            np.ndarray: The item at the specified index.
 37
 38        Raises:
 39            IndexError: If the index is out of range.
 40        """
 41        if index >= len(self.tiff_file_names):
 42            raise IndexError
 43        else:
 44            with self.zip_file.open(self.tiff_file_names[index]) as tiff_file:
 45                image = skimage.io.imread(tiff_file)
 46                return np.array(image)
 47
 48    def __len__(self) -> int:
 49        """
 50        Returns the length of the `tiff_file_names` list.
 51
 52        :return: An integer representing the length of the `tiff_file_names` list.
 53        :rtype: int
 54        """
 55        return len(self.tiff_file_names)
 56
 57    def __iter__(self):
 58        """
 59        Returns an iterator object that iterates over the elements of the class.
 60
 61        Yields:
 62            The elements of the class.
 63        """
 64        for index in range(len(self.tiff_file_names)):
 65            yield self[index]
 66
 67    def _get_im0(self):
 68        """
 69        Get the first image in the stack.
 70
 71        :return: The first image in the stack.
 72        """
 73        if self._im0 is None:
 74            self._im0 = self[0]
 75            self._shape = (
 76                len(self.tiff_file_names),
 77                self._im0.shape[0],
 78                self._im0.shape[1],
 79            )
 80            self._dtype = self._im0.dtype
 81        return self._im0
 82
 83    def get_shape(self) -> list:
 84        """
 85        Returns the shape of the image stack
 86        :return: shape of the image stack
 87        :rtype: list
 88        """
 89        self._get_im0()
 90        return self._shape
 91
 92    def get_dtype(self) -> str:
 93        """
 94        Returns the data type of the image stack
 95        :return: data type of the image stack
 96        :rtype: str
 97        """
 98        self._get_im0()
 99        return self._dtype
100
101    def get_thumb(self, save_path=None):
102        """
103        Saves a thumbnail (first frame) of the image stack
104        :param save_path: path to save the thumbnail
105        :type save_path: str
106        """
107        thumb = transform.resize(self._get_im0(), (64, 64))
108        _max = thumb.max()
109        _min = thumb.min()
110        thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255
111        if save_path != None:
112            # Save the thumbnail as a JPEG file
113            skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb)
114
115    def close(self):
116        """
117        Closes the zip file
118        """
119        self.zip_file.close()
class ZipTiffIterator:
 10class ZipTiffIterator:
 11    _shape = None
 12    _dtype = None
 13    _im0 = None
 14
 15    def __init__(self, zip_file_path: str):
 16        """
 17        Iterator to sequentially open tiff files contained in a zip file
 18        :param zip_file_path: path to the zip file
 19        :type zip_file_path: str
 20        """
 21        self.zip_file_path = zip_file_path
 22        self.zip_file = zipfile.ZipFile(zip_file_path)
 23        self.tiff_file_names = [
 24            name for name in self.zip_file.namelist() if name.endswith(".tif") and not name.startswith("_")
 25        ]
 26        self.tiff_file_names = sorted(self.tiff_file_names)
 27        # print(self.tiff_file_names)
 28
 29    def __getitem__(self, index: int) -> np.ndarray:
 30        """
 31        Get the item at the specified index.
 32
 33        Parameters:
 34            index (int): The index of the item to get.
 35
 36        Returns:
 37            np.ndarray: The item at the specified index.
 38
 39        Raises:
 40            IndexError: If the index is out of range.
 41        """
 42        if index >= len(self.tiff_file_names):
 43            raise IndexError
 44        else:
 45            with self.zip_file.open(self.tiff_file_names[index]) as tiff_file:
 46                image = skimage.io.imread(tiff_file)
 47                return np.array(image)
 48
 49    def __len__(self) -> int:
 50        """
 51        Returns the length of the `tiff_file_names` list.
 52
 53        :return: An integer representing the length of the `tiff_file_names` list.
 54        :rtype: int
 55        """
 56        return len(self.tiff_file_names)
 57
 58    def __iter__(self):
 59        """
 60        Returns an iterator object that iterates over the elements of the class.
 61
 62        Yields:
 63            The elements of the class.
 64        """
 65        for index in range(len(self.tiff_file_names)):
 66            yield self[index]
 67
 68    def _get_im0(self):
 69        """
 70        Get the first image in the stack.
 71
 72        :return: The first image in the stack.
 73        """
 74        if self._im0 is None:
 75            self._im0 = self[0]
 76            self._shape = (
 77                len(self.tiff_file_names),
 78                self._im0.shape[0],
 79                self._im0.shape[1],
 80            )
 81            self._dtype = self._im0.dtype
 82        return self._im0
 83
 84    def get_shape(self) -> list:
 85        """
 86        Returns the shape of the image stack
 87        :return: shape of the image stack
 88        :rtype: list
 89        """
 90        self._get_im0()
 91        return self._shape
 92
 93    def get_dtype(self) -> str:
 94        """
 95        Returns the data type of the image stack
 96        :return: data type of the image stack
 97        :rtype: str
 98        """
 99        self._get_im0()
100        return self._dtype
101
102    def get_thumb(self, save_path=None):
103        """
104        Saves a thumbnail (first frame) of the image stack
105        :param save_path: path to save the thumbnail
106        :type save_path: str
107        """
108        thumb = transform.resize(self._get_im0(), (64, 64))
109        _max = thumb.max()
110        _min = thumb.min()
111        thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255
112        if save_path != None:
113            # Save the thumbnail as a JPEG file
114            skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb)
115
116    def close(self):
117        """
118        Closes the zip file
119        """
120        self.zip_file.close()
ZipTiffIterator(zip_file_path: str)
15    def __init__(self, zip_file_path: str):
16        """
17        Iterator to sequentially open tiff files contained in a zip file
18        :param zip_file_path: path to the zip file
19        :type zip_file_path: str
20        """
21        self.zip_file_path = zip_file_path
22        self.zip_file = zipfile.ZipFile(zip_file_path)
23        self.tiff_file_names = [
24            name for name in self.zip_file.namelist() if name.endswith(".tif") and not name.startswith("_")
25        ]
26        self.tiff_file_names = sorted(self.tiff_file_names)
27        # print(self.tiff_file_names)

Iterator to sequentially open tiff files contained in a zip file

Parameters
  • zip_file_path: path to the zip file
zip_file_path
zip_file
tiff_file_names
def get_shape(self) -> list:
84    def get_shape(self) -> list:
85        """
86        Returns the shape of the image stack
87        :return: shape of the image stack
88        :rtype: list
89        """
90        self._get_im0()
91        return self._shape

Returns the shape of the image stack

Returns

shape of the image stack

def get_dtype(self) -> str:
 93    def get_dtype(self) -> str:
 94        """
 95        Returns the data type of the image stack
 96        :return: data type of the image stack
 97        :rtype: str
 98        """
 99        self._get_im0()
100        return self._dtype

Returns the data type of the image stack

Returns

data type of the image stack

def get_thumb(self, save_path=None):
102    def get_thumb(self, save_path=None):
103        """
104        Saves a thumbnail (first frame) of the image stack
105        :param save_path: path to save the thumbnail
106        :type save_path: str
107        """
108        thumb = transform.resize(self._get_im0(), (64, 64))
109        _max = thumb.max()
110        _min = thumb.min()
111        thumb = (thumb.astype("float32") - _min / (_max - _min)) * 255
112        if save_path != None:
113            # Save the thumbnail as a JPEG file
114            skimage.io.imsave(os.path.join(save_path, "thumbnail.jpg"), thumb)

Saves a thumbnail (first frame) of the image stack

Parameters
  • save_path: path to save the thumbnail
def close(self):
116    def close(self):
117        """
118        Closes the zip file
119        """
120        self.zip_file.close()

Closes the zip file