utils.py
import goldener.utils
Generic utilities shared by the Goldener features.
This module gathers helpers to validate inputs (shapes, sampling sizes, types), to compute sampling counts from ratios and to distribute them among chunks, and to filter or transform batches (e.g. binarizing multiple targets, or splitting multilabel samples into independent labels).
Check compatibility of shapes of x and y tensors.
Arguments:
- x_shape: Shape of the input tensor x.
- y_shape: Shape of the target tensor y.
Raises:
- ValueError: If the shapes are incompatible. See the conditions below for details.
Conditions:
- If x is 1D, x and y must have the same shape.
- If x is 2D or more, y must have a channel dimension of 1 (i.e., y_shape[1] == 1).
- If y has a batch size greater than 1 (i.e., y_shape[0] > 1), x and y must have the same batch size (i.e., x_shape[0] == y_shape[0]).
- If x has more than 2 dimensions, the additional (after channel) dimensions of x and y must match.
Get the sum of a list of ratios and validate it (total between 0 and 1).
Arguments:
- ratios: A list of float ratios.
Returns:
The sum of the ratios.
Raises:
- ValueError: If the sum of the ratios is not greater than 0 and at most 1.
Get ratios for a list of counts.
Arguments:
- counts: A list of integer counts.
Returns:
A list of float ratios corresponding to the input counts.
Raises:
- ValueError: If
countsis an empty list or if the total count is zero.
Split a total sampling count among chunks based on their sizes.
Arguments:
- to_split: The total number of samples to split among the chunks.
- chunk_sizes: A list of integers representing the sizes of each chunk.
Returns:
A list of integers representing the number of samples to draw from each chunk, summing up to to_split.
Raises:
- ValueError: If the input parameters are invalid. See the conditions below for details.
Conditions:
- At least one chunk size must be provided.
- If only one chunk size is provided, to_split must not be greater than that chunk size.
- The total size of the chunks must be greater than 0.
Filter a batch dictionary to only include items at specified indices.
Arguments:
- batch: A dictionary representing a batch of data (each key corresponds to stacked information).
- to_remove: A set of indices to filter out from the batch.
- index_key: The key in the batch dictionary that contains the indices.
Returns:
A filtered batch dictionary without the specified indices.
Get the set of index values for batch items whose sharing label values with exclude_labels.
Arguments:
- batch: A dictionary representing a batch of data. It must contain the keys specified by label_key and index_key,
- where the values are lists or tensors of the same length. Label values can be strings or iterables of strings.
- label_key: The key in the batch dictionary that contains the labels.
- exclude_labels: A set of label strings to exclude.
- index_key: The key in the batch dictionary that contains the indices.
Returns:
A set of index values corresponding to items with excluded labels.
Get sizes and sampling sizes per chunk.
Arguments:
- total_size: Total size of the data to sample from.
- sampling_size: Total number of samples to draw.
- max_chunk_size: Maximum size of each chunk.
Returns:
A tuple of two lists: - A list of integers representing the size of each chunk. - A list of integers representing the number of samples to draw from each chunk.
Raises:
- ValueError: If sampling_size is greater than or equal to total_size.
Check the validity of the sampling size.
Arguments:
- sampling_size: The sampling size to check (can be int or float).
- total_size: The total size of the data (Optional).
- force_max: Whether the sampling size is expected to be the maximum possible (1.0 for float, total for int)
Raises:
- ValueError: If the sampling size is invalid based on its type and total size.
- If sampling_size is a float, it must be in the range (0.0, 1.0].
- If sampling_size is an int, it must be in the range (0, total_size].
Check if all elements in an iterable are of the same type.
Arguments:
- iterable: An iterable containing elements to check.
Raises:
- TypeError: If any element in the iterable is not of the expected type.
Get the sampling count from the sampling size.
Arguments:
- sampling_size: The sampling size (can be int or float). If int, it represents the exact number of samples
- and it is required to be less than total_size. If float, it represents the fraction of total size, rounded
- down with a minimum of one sample, and it is required to be in the range (0.0, 1.0].
- total_size: The total size of the data (Optional, required if sampling_size is float).
Returns:
The calculated sampling count as an integer.
Raises:
- ValueError: If the sampling size is invalid based on its type and total size.
Transform a batch with multiple targets into a batch with binarized targets for each label.
Arguments:
- batch: A dictionary representing a batch of data, where the target key contains a target with multiple values corresponding to the labels in target_to_label.
- target_key: The key in the batch dictionary that contains the target.
- label_key: The key in the batch dictionary that containes the labels.
- target_to_label: A mapping from unique target tuples to label values.
- merge_labels: Whether to merge the labels into a single label (default: False).
- exclude_labels: An optional set of labels to exclude from the transformation (default: None).
- exclude_full_zero: Whether to exclude the all-zero target from the transformation (default: False).
Returns:
A new batch dictionary where the target key contains binarized targets for each label and the label key contains the corresponding labels, with other batch elements duplicated accordingly.
Raises:
- ValueError: If a unique target tuple is not found in
target_to_label. - ValueError: If no valid targets remain after applying the
exclude_full_zerofilter.
Transform a batch with multilabel into a batch with one entry per label.
Arguments:
- batch: A dictionary representing a batch of data, where the label key contains several labels.
- label_key: The key in the batch dictionary that contains the labels.
- merge_labels: Whether to merge the labels into a single label (default: False). If False, the batch will be duplicated for each label. If True, the labels will be merged into a single label by concatenating them with a separator (e.g., "_").
- exclude_labels: An optional set of labels to exclude from the transformation (default: None).
Returns:
A new batch dictionary where samples with multiple labels are split/merge in single entries.
Get the sampling count for each key from the ratios.
Arguments:
- ratios: A dictionary mapping keys to their corresponding ratios (values between 0 and 1). the initial counts are computed by multiplying the sampling size by the ratios and taking the floor of the result. If the sum of the initial counts is less than the sampling size, the remaining samples are distributed one by one starting from the most represented key until the total count matches the sampling size.
- sampling_size: The total number of samples to draw.
- force_non_zero: Whether to force a non-zero sampling count for each key (default: False). If activated, the least represented keys will have at least 1 sample, and the most represented keys will have their count reduced accordingly to maintain the total sampling size.
Returns:
A dictionary mapping keys to their corresponding sampling counts (integers). The dictionary is sorted by count in ascending order.
Raises:
- ValueError: If the ratios do not sum to 1.0.
- ValueError: If
force_non_zerois True and the count for the highest-count key becomes zero while redistributing counts.