minerva.data.readers.numpy_reader¶
Classes¶
This class is used to read data from a NumPy-like array. It is designed |
|
A base class for iterating over files in a directory in a custom sorted |
Module Contents¶
- class minerva.data.readers.numpy_reader.NumpyArrayReader(data, data_shape, stride=None, pad_width=None, pad_mode='constant', pad_kwargs=None, allow_pickle=True, npz_key=None)[source]¶
Bases:
minerva.data.readers.patched_array_reader.PatchedArrayReaderThis class is used to read data from a NumPy-like 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
- index_boundsTuple[Tuple[int, …], Tuple[int, …]], optional
A tuple of two tuples specifying the start and end indices for each dimension to create a sub-region of the Zarr array. If None, the entire array is used.
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]]
- Parameters:
data (Union[numpy.typing.ArrayLike, minerva.utils.typing.PathLike])
data_shape (Tuple[int, Ellipsis])
stride (Optional[Tuple[int, Ellipsis]])
pad_width (Optional[Tuple[Tuple[int, int], Ellipsis]])
pad_mode (str)
pad_kwargs (Optional[Dict])
allow_pickle (bool)
npz_key (Optional[str])
- class minerva.data.readers.numpy_reader.NumpyFolderReader(path, sort_method=None, delimiter=None, key_index=0, reverse=False, filters=None, allow_pickle=True, array_key=None)[source]¶
Bases:
minerva.data.readers.base_file_iterator.BaseFileIteratorA base class for iterating over files in a directory in a custom sorted order.
Load image files from a directory.
Parameters¶
- pathUnion[Path, str]
The path to the directory containing the image files. Files will be searched recursively.
- sort_methodOptional[List[str]], optional
A list specifying how to sort each part of the filename. Each element can be either “text” (lexicographical) or “numeric” (numerically). By default, None, which will use “numeric” if numeric parts are detected.
- delimiterOptional[str], optional
The delimiter to split filenames into components, by default None.
- key_indexUnion[int, List[int]], optional
The index (or list of indices) of the part(s) of the filename to use for sorting. If a list is provided, files will be sorted based on multiple parts in sequence. Thus, first by the part at index 0, then by the part at index 1, and so on. By default 0.
- reversebool, optional
Whether to sort in reverse order, by default False.
- filters: Optional[Union[List[str], str]]
An optional string or list of strings containing regular expressions with which to filter files by their stems. Files that match at least one pattern are kept, and the others are excluded. Defaults to None, which means no files are excluded.
Raises¶
- NotADirectoryError
If the path is not a directory.
- __getitem__(index)[source]¶
Retrieve the PNG file at the specified index.
- Parameters:
index (int)
- Return type:
numpy.ndarray
- allow_pickle = True¶
- array_key = None¶
- root_dir¶
- Parameters:
path (minerva.utils.typing.PathLike)
sort_method (Optional[List[str]])
delimiter (Optional[str])
key_index (Union[int, List[int]])
reverse (bool)
filters (Optional[Union[List[str], str]])
allow_pickle (bool)
array_key (Optional[str])