zea.data.datasets¶
zea.data.datasets¶
This module provides classes and utilities for loading, validating, and managing ultrasound datasets stored in HDF5 format. It supports both local and Hugging Face Hub datasets, and offers efficient file handle caching for large collections of files.
Main Classes¶
H5FileHandleCache: Caches open HDF5 file handles to optimize repeated access.
Folder: Represents a group of HDF5 files in a directory, with optional validation.
- Dataset: Provides an iterable interface over multiple HDF5 files or folders, with
support for directory-based splitting and validation.
Functions¶
split_files_by_directory: Splits files among directories according to specified ratios.
count_samples_per_directory: Counts the number of files per directory.
Features¶
Validation of dataset integrity with flag files and error logging.
Support for Hugging Face Hub datasets with local caching.
Utilities for dataset splitting and sample counting.
Example usage provided in the module’s main block.
Functions
|
Condition for |
|
Normalize |
|
Count number of samples per directory. |
|
Split files according to their parent directories and given split ratios. |
Classes
|
Dataset class managing multiple |
|
Group of HDF5 files in a folder that can be validated. |
|
Cache for HDF5 file handles. |
- zea.data.datasets.EXISTS(value)[source]¶
Condition for
compile_file_filter()dict filters: field is present.Use as the condition for a dotted path to keep only files where that field is present (i.e. resolves to a non-
Nonevalue). For example{"metadata.subject.fat_percentage": EXISTS}keeps only files that record a subject fat percentage.It is a plain callable (so it survives module reloads and needs no special casing), evaluated like any other value-level predicate.
- Return type:
bool
- class zea.data.datasets.Folder(folder_path, validate=False, hf_cache_dir=PosixPath('/home/docs/.cache/zea/huggingface/datasets'), revision=None)[source]¶
Bases:
objectGroup of HDF5 files in a folder that can be validated. Mostly used internally, you might want to use the Dataset class instead.
- copy(to_path, key, mode=None)[source]¶
Copy the data for all or a specific key to a new location.
Has the option to copy all keys or only a specific key. By default, it only copies if the destination file does not already contain the key. You can change the mode to ‘w’ to overwrite the destination file. Will always copy metadata such as dataset attributes and scan object.
- Parameters:
to_path (
str|Path) – The destination path where files will be copied.key (
str) – The key to copy from the source files. If ‘all’ or ‘*’, all keys will be copied.mode (
str|None) – The mode in which to open the destination files. Defaults to ‘a’ (append mode), and ‘w’ (write mode) if key is ‘all’ or ‘*’. See: https://docs.h5py.org/en/stable/high/file.html#opening-creating-files
- property n_files¶
Return number of files in dataset.
- class zea.data.datasets.H5FileHandleCache(file_handle_cache_capacity=128)[source]¶
Bases:
objectCache for HDF5 file handles.
This class manages a cache of HDF5 file handles to avoid reopening files multiple times. It uses an OrderedDict to maintain the order of file access and closes the least recently used file when the cache reaches its capacity.
- zea.data.datasets.compile_file_filter(file_filter)[source]¶
Normalize
file_filterinto aFile -> boolpredicate (orNone).Accepts either a callable predicate over an open
File, or a declarative dotted-path dict (see_compile_dict_filter()). ReturnsNonewhenfile_filterisNone(no filtering).- Return type:
Optional[Callable[[File],bool]]
- zea.data.datasets.count_samples_per_directory(file_names, directories)[source]¶
Count number of samples per directory.
- Parameters:
file_names (list) – List of file paths
directories (str or list) – Directory or list of directories
- Returns:
Dictionary with directory paths as keys and sample counts as values
- Return type:
dict
- zea.data.datasets.split_files_by_directory(file_names, directory_list, directory_splits)[source]¶
Split files according to their parent directories and given split ratios.
- Parameters:
file_names (list) – List of file paths.
directory_list (list) – List of directory paths to split by.
directory_splits (list) – List of split ratios (0-1) for each directory.
- Returns:
(split_file_names, split_file_shapes)
- Return type:
tuple