minerva.data.readers.patched_array_reader ========================================= .. py:module:: minerva.data.readers.patched_array_reader Classes ------- .. autoapisummary:: minerva.data.readers.patched_array_reader.LazyPaddedPatchedArrayReader minerva.data.readers.patched_array_reader.NumpyArrayReader minerva.data.readers.patched_array_reader.PatchedArrayReader Module Contents --------------- .. py:class:: LazyPaddedPatchedArrayReader(data, data_shape, stride = None, pad_width = None, pad_mode = 'constant', pad_kwargs = None) Bases: :py:obj:`PatchedArrayReader` Reads patches from a NumPy array. This class is a subclass of `PatchedArrayReader` and is designed to perform padding only when the patch consumed by `__get_item__` is in a region that uses the padding (boundary regions). If no padding is necessary, use PatchedArrayReader. Reads data from a NumPy array and generates patches from it. Parameters ---------- data : ArrayLike The input array from which patches are generated. data_shape : Tuple[int, ...] The shape of the patches to be extracted. This will be the shape of the subarray that is returned when a patch is accessed using __getitem__. stride : Tuple[int, ...], optional The stride between consecutive patches. If `None`, the stide will be the same as `data_shape`. By default None pad_width : Tuple[Tuple[int, int], ...], optional The width of padding to be applied to the data array. By default `None`, that is, no padding is applied. Check the documentation of `numpy.pad` for more information. pad_mode : str, optional The padding mode, by default "constant". Check the documentation of `numpy.pad` for more information. pad_kwargs : dict, optional Additional keyword arguments for padding, by default None Examples -------- ```python >>> import numpy as np >>> # Generate a 10x10 array >>> data = np.arange(100).reshape(10, 10) >>> # Create a reader that generates 5x5 patches with a stride of 2 in the >>> # first dimension and 5 in the second dimension. >>> reader = PatchedArrayReader( >>> data, >>> data_shape=(5, 5), >>> stride=(2, 5), >>> ) >>> # Printing the number of patches that can be extracted from the data >>> print(len(reader)) 6 >>> # Printing the indices of the patches >>> print(reader.indices) [(0, 0), (0, 5), (2, 0), (2, 5), (4, 0), (4, 5)] >>> # Fetch the first patch and print its shape >>> print(reader[0].shape) (5, 5) >>> # Fetch the third patch and print its content >>> print(reader[2]) [[20 21 22 23 24] [30 31 32 33 34] [40 41 42 43 44] [50 51 52 53 54] [60 61 62 63 64]] ``` .. py:method:: __getitem__(idx) Fetch a patch from the data array. Parameters ---------- idx : int The index of the patch to be fetched. Returns ------- np.ndarray The patch that was fetched from the data array with shape `data_shape` .. py:method:: _get_patches() Compute the left upper corner indices of the patches that will be extracted from the data array. The patches are extracted with a stride between them. A list of indices is returned, where each index is a tuple of integers representing the coordinates of the left upper corner of the patches. Returns ------- List[Tuple[int, ...]] A list of indices (coordinates) representing the left upper corner of the patches. .. py:method:: _pad_data(pad_width, mode='constant', **kwargs) Apply padding to the data array. Parameters ---------- pad_width : Tuple[Tuple[int, int], ...] The width of padding to be applied to the data array mode : str, optional The padding mode, by default "constant" .. py:class:: NumpyArrayReader(data, data_shape, stride = None, pad_width = None, pad_mode = 'constant', pad_kwargs = None, allow_pickle = True, npz_key = None) Bases: :py:obj:`PatchedArrayReader` This class is used to read data from a NumPy array. It is designed to generate patches from the data and provides sequential access to them. This class can serve as a base class for other readers. Assumptions: - The input data is expected to be a NumPy-like array, that is, it should support NumPy-like indexing. - Patches are fixed-size subarrays of the data. - Patches can have overlap between them. Reads data from a NumPy array and generates patches from it. Parameters ---------- data : ArrayLike The input array from which patches are generated. data_shape : Tuple[int, ...] The shape of the patches to be extracted. This will be the shape of the subarray that is returned when a patch is accessed using __getitem__. stride : Tuple[int, ...], optional The stride between consecutive patches. If `None`, the stide will be the same as `data_shape`. By default None pad_width : Tuple[Tuple[int, int], ...], optional The width of padding to be applied to the data array. By default `None`, that is, no padding is applied. Check the documentation of `numpy.pad` for more information. pad_mode : str, optional The padding mode, by default "constant". Check the documentation of `numpy.pad` for more information. pad_kwargs : dict, optional Additional keyword arguments for padding, by default None Examples -------- ```python >>> import numpy as np >>> # Generate a 10x10 array >>> data = np.arange(100).reshape(10, 10) >>> # Create a reader that generates 5x5 patches with a stride of 2 in the >>> # first dimension and 5 in the second dimension. >>> reader = PatchedArrayReader( >>> data, >>> data_shape=(5, 5), >>> stride=(2, 5), >>> ) >>> # Printing the number of patches that can be extracted from the data >>> print(len(reader)) 6 >>> # Printing the indices of the patches >>> print(reader.indices) [(0, 0), (0, 5), (2, 0), (2, 5), (4, 0), (4, 5)] >>> # Fetch the first patch and print its shape >>> print(reader[0].shape) (5, 5) >>> # Fetch the third patch and print its content >>> print(reader[2]) [[20 21 22 23 24] [30 31 32 33 34] [40 41 42 43 44] [50 51 52 53 54] [60 61 62 63 64]] ``` .. py:class:: PatchedArrayReader(data, data_shape, stride = None, pad_width = None, pad_mode = 'constant', pad_kwargs = None) Bases: :py:obj:`minerva.data.readers.reader._Reader` This class is used to read data from a NumPy array. It is designed to generate patches from the data and provides sequential access to them. This class can serve as a base class for other readers. Assumptions: - The input data is expected to be a NumPy-like array, that is, it should support NumPy-like indexing. - Patches are fixed-size subarrays of the data. - Patches can have overlap between them. Reads data from a NumPy array and generates patches from it. Parameters ---------- data : ArrayLike The input array from which patches are generated. data_shape : Tuple[int, ...] The shape of the patches to be extracted. This will be the shape of the subarray that is returned when a patch is accessed using __getitem__. stride : Tuple[int, ...], optional The stride between consecutive patches. If `None`, the stide will be the same as `data_shape`. By default None pad_width : Tuple[Tuple[int, int], ...], optional The width of padding to be applied to the data array. By default `None`, that is, no padding is applied. Check the documentation of `numpy.pad` for more information. pad_mode : str, optional The padding mode, by default "constant". Check the documentation of `numpy.pad` for more information. pad_kwargs : dict, optional Additional keyword arguments for padding, by default None Examples -------- ```python >>> import numpy as np >>> # Generate a 10x10 array >>> data = np.arange(100).reshape(10, 10) >>> # Create a reader that generates 5x5 patches with a stride of 2 in the >>> # first dimension and 5 in the second dimension. >>> reader = PatchedArrayReader( >>> data, >>> data_shape=(5, 5), >>> stride=(2, 5), >>> ) >>> # Printing the number of patches that can be extracted from the data >>> print(len(reader)) 6 >>> # Printing the indices of the patches >>> print(reader.indices) [(0, 0), (0, 5), (2, 0), (2, 5), (4, 0), (4, 5)] >>> # Fetch the first patch and print its shape >>> print(reader[0].shape) (5, 5) >>> # Fetch the third patch and print its content >>> print(reader[2]) [[20 21 22 23 24] [30 31 32 33 34] [40 41 42 43 44] [50 51 52 53 54] [60 61 62 63 64]] ``` .. py:method:: __getitem__(idx) Fetch a patch from the data array. Parameters ---------- idx : int The index of the patch to be fetched. Returns ------- np.ndarray The patch that was fetched from the data array with shape `data_shape` .. py:method:: __len__() Return the number of patches that can be extracted from the data array. Returns ------- int The number of patches that can be extracted from the data array. .. py:method:: __str__() .. py:method:: _get_patches() Compute the left upper corner indices of the patches that will be extracted from the data array. The patches are extracted with a stride between them. A list of indices is returned, where each index is a tuple of integers representing the coordinates of the left upper corner of the patches. Returns ------- List[Tuple[int, ...]] A list of indices (coordinates) representing the left upper corner of the patches. .. py:method:: _pad_data(pad_width, mode='constant', **kwargs) Apply padding to the data array. Parameters ---------- pad_width : Tuple[Tuple[int, int], ...] The width of padding to be applied to the data array mode : str, optional The padding mode, by default "constant" .. py:attribute:: data .. py:attribute:: data_shape .. py:attribute:: indices :value: [] .. py:attribute:: pad_kwargs .. py:attribute:: pad_mode :value: 'constant' .. py:attribute:: pad_width :value: None .. py:attribute:: shape .. py:attribute:: stride