minerva.data.readers.patched_array_reader
Classes
This class is used to read data from a NumPy array. It is designed to generate |
Module Contents
- class minerva.data.readers.patched_array_reader.PatchedArrayReader(data, data_shape, stride=None, pad_width=None, pad_mode='constant', pad_kwargs=None)
Bases:
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
- dataArrayLike
The input array from which patches are generated.
- data_shapeTuple[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__.
- strideTuple[int, …], optional
The stride between consecutive patches. If None, the stide will be the same as data_shape. By default None
- pad_widthTuple[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_modestr, optional
The padding mode, by default “constant”. Check the documentation of numpy.pad for more information.
- pad_kwargsdict, 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]]
- __getitem__(idx)
Fetch a patch from the data array.
Parameters
- idxint
The index of the patch to be fetched.
Returns
- np.ndarray
The patch that was fetched from the data array with shape data_shape
- Parameters:
idx (int)
- Return type:
numpy.ndarray
- __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.
- Return type:
int
- _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.
- Return type:
List[Tuple[int, Ellipsis]]
- Parameters:
data (numpy.typing.ArrayLike)
data_shape (Tuple[int, Ellipsis])
stride (Tuple[int, Ellipsis])
pad_width (Tuple[Tuple[int, int], Ellipsis])
pad_mode (str)
pad_kwargs (dict)