vectorize.py
import goldener.vectorize
Vectorization of the embeddings into 2D tensors of vectors.
An embedding is a tensor of at least 3 dimensions (batch, channels and e.g. tokens or pixels). The algorithms of Goldener work on 2D tensors of vectors, so the embeddings are flattened into vectors and, optionally, some of these vectors are filtered out (e.g. keeping only the vectors of a region of interest given by a target).
GoldTensorVectorizationTooltransforms a tensor into vectors, usingFilter2DWithCountandFilterLocationto filter them, and returns aVectorizedobject keeping track of the sample of each vector.GoldVectorizerapplies the vectorization on a whole dataset in batches and stores the vectors locally, without recomputing already vectorized samples.
The vectors are the input of the clustering (goldener.clusterize) and selection (goldener.select) steps.
Enum defining filter location strategies for filtering 2D tensor rows.
START: Filter from the start of the tensor. END: Filter from the end of the tensor. RANDOM: Filter randomly from the tensor.
Filter 2D tensor rows based on specified criteria.
Attributes:
- _filter_location: Location to filter from (start, end, random).
- _filter_count: Number of rows to filter.
- _keep: Whether to keep or remove the filtered rows.
- _random_sampler: Sampler for random filtering.
Initialize the Filter2DWithCount.
Arguments:
- filter_count: Number of rows to filter.
- filter_location: Location to filter from (start, end, random).
- keep: Whether to keep or remove the filtered rows.
- generator: Random number generator for random filtering.
Raises:
- ValueError: If
filter_countis not greater than 0.
Check if the filter is random.
Returns:
True if the filter uses random sampling, False otherwise.
Check if the filter is set to keep the filtered rows.
Returns:
True if the filter is set to keep the filtered rows, False if it is set to remove them.
Get the number of rows to filter.
Returns:
The number of rows that the filter is set to filter.
Filter a single 2D tensor.
Arguments:
- x: Input 2D tensor to filter.
Returns:
Filtered tensor.
Filter the input tensor or dictionary of tensors.
Arguments:
- x: Input 2D tensor or dictionary of 2D tensors to filter. In case of a dictionary,
- all tensors must have the same number of rows and this number must be greater than filter_count.
Returns:
Filtered tensor or dictionary of tensors. If the batch size is less than filter_count, the input is returned unchanged.
Raises:
- ValueError: If input tensors do not have the same batch size or are not 2 dimensional.
Dataclass to hold vectorized tensors and the corresponding batch indices.
Attributes:
- vectors: 2D tensor of vectorized data.
- batch_indices: 1D tensor containing information about the origin of each vector.
Transform input as 2D tensor and filter based on target tensor.
Attributes:
- keep: Filter2DWithCount instance to keep specific rows in the input
xoffilter. - remove: Filter2DWithCount instance to remove specific rows in the input
xoffilter. - random: Random Filter2DWithCount instance to randomly filter vectors after
- applying
keep,removeand the targetyon the inputxof filter. - fusion_strategy: Optional strategy to fuse vectors when more than 1 vector is kept for a sample after all filtering steps.
- transform_y: Optional callable to transform the target tensor before transforming it to 2D.
- channel_pos: position of the channel dimension in the input tensor to vectorize.
- in_selection_tool: Optional smart selection tool applied after all other input filters.
- in_selection_size: Number or fraction of vectors to keep with
in_selection_tool.
Initialize the GoldTensorVectorizationTool.
Arguments:
- keep: Optional filter to keep specific rows in the input.
- remove: Optional filter to remove specific rows from the input.
- random: Optional random filter to apply after keep/remove filters.
- fusion_strategy: Optional strategy to fuse vectors when more than 1 vector is kept for a sample after all filtering steps.
- transform_y: Optional transformation to apply to the target tensor.
- channel_pos: Position of the channel dimension in the input tensor. Defaults to 1.
- in_selection_tool: Optional selection tool to choose the most useful remaining vectors.
- in_selection_size: Number or fraction of vectors for
in_selection_toolto keep.
Raises:
- ValueError: If keep, remove and random filters are not having the expected properties.
- See the checks in the code for details.
Vectorize input tensor and filter based on target tensor.
If y is not provided, only filtering and vectorization are performed. If y is provided, it is used to filter the vectorized x tensor, only if at least 1 vector location is non zero in the target.
Arguments:
- x: Input tensor to vectorize.
- y: Optional target tensor to filter the input tensor.
Returns:
Vectorized and filtered input tensor with the information about which element of the batch it corresponds to.
Raises:
- ValueError: If x and y shapes are incompatible. See check_x_and_y_shapes for details.
Extract and flatten vectors from dataset samples and store results in a PixelTable table.
The GoldVectorizer processes a dataset or PixelTable table to extract and flatten vectors using a
GoldTensorVectorizationTool. The computed vectors are stored in a local PixelTable table
(specified by table_path) so that the vectorization process is idempotent: calling the
same operation multiple times will not duplicate or recompute vectors that are already
present in the table.
Assuming all the data will not fit in memory, the dataset is processed in batches. All torch tensors will be converted to numpy arrays before saving.
The vectorization can operate in a sequential (single-process) mode or a
distributed mode (not implemented). The table schema is created/validated automatically and will include
minimal indexing columns (idx, idx_vector) required to link vectors back to
their originating samples.
Attributes:
- table_path: Path to the PixelTable table where vectorized outputs will be stored locally.
- vectorizer: GoldTensorVectorizationTool instance for transforming batched inputs into vectors.
- collate_fn: Optional function to collate dataset samples into batches composed of
dictionaries with at least the key specified by
data_keyreturning a PyTorch Tensor. If None,collate_keeping_sequences_as_sequencesis used, which preserves sequence-valued fields as one sequence per sample. - data_key: Key in the batch dictionary that contains the data to vectorize. Default is "embeddings".
- target_key: Optional key in the batch dictionary containing the target used to filter vectors. Default is "target".
- vectorized_key: Column name to store the resulting vectors in the PixelTable table. Default is "vectorized".
- label_key: Optional key for labels in the batch dictionary. Default is None.
- target_to_label: Optional mapping from target values to label strings. Default is None.
- merge_multilabels: Whether to merge multiple labels into a single label. Default is False.
- exclude_full_zero_target: Whether to exclude samples with a target tensor containing only zeros (in case of multi target). Default is False.
- exclude_labels: Optional set of label strings to exclude from vectorization. Default is None.
- to_keep_schema: Optional dictionary defining additional columns to keep from the original dataset/table into the vectorized table. The keys are the column names and the values are the PixelTable types.
- min_pxt_insert_size: Minimum number of rows to accumulate before inserting into the PixelTable table. Default is 100.
- batch_size: Batch size used when iterating over the data.
- num_workers: Number of workers for the PyTorch DataLoader during iteration on data.
- allow_existing: If False, an error will be raised when the table already exists. Default is True.
- distribute: Whether to use distributed processing for vectorization and table population. Not implemented yet. Default is False.
- drop_table: Whether to drop the vectorized table after creating the dataset with vectorized outputs. It is only applied
when using
vectorize_in_dataset. Default is False. - max_batches: Optional maximum number of batches to process. Useful for testing on a small subset of the dataset.
Initialize the GoldVectorizer.
Arguments:
- table_path: Path to the PixelTable table for storing vectors.
- vectorizer: GoldTensorVectorizationTool instance for transforming tensors.
- collate_fn: Optional collate function for preparing batches.
If None,
collate_keeping_sequences_as_sequencesis used. - data_key: Key for data in the batch dictionary. Defaults to "embeddings".
- target_key: Key for target in the batch dictionary. Defaults to "target".
- vectorized_key: Column name for storing vectors. Defaults to "vectorized".
- label_key: Optional key for labels in the batch dictionary. Default is None.
- target_to_label: Optional mapping from target values to label strings. Default is None.
- merge_multilabels: Whether to merge multiple labels into a single label. Default is False.
- exclude_labels: Optional set of label strings to exclude from vectorization. Default is None.
- exclude_full_zero_target: Whether to exclude samples with a target tensor containing only zeros (in case of multi target). Default is False.
- to_keep_schema: Optional schema for additional columns to preserve.
- min_pxt_insert_size: Minimum number of rows to accumulate before inserting into the PixelTable table. Default is 100.
- batch_size: Batch size used when iterating over the data.
- num_workers: Number of workers for the PyTorch DataLoader during iteration on data.
- allow_existing: Whether to allow using an existing table. Defaults to True.
- distribute: Whether to use distributed processing. Defaults to False.
- drop_table: Whether to drop the table after dataset creation. Defaults to False.
- max_batches: Optional maximum number of batches to process.
- estimation_time_batch_count: Number of initial batches used to estimate the total computation time. Defaults to 2.
Raises:
- NotImplementedError: If
distributeis True. - ValueError: If
estimation_time_batch_countis not a positive integer.
Extract and flatten vectors from samples and return results as a GoldPxtTorchDataset.
The vectorization process extracts and flattens vectors from the provided dataset or table
using the GoldTensorVectorizationTool instance and stores them in a PixelTable table specified by table_path.
The resulting vectors are stored in the column specified by vectorized_key.
This is a convenience wrapper that runs vectorize_in_table to populate
(or resume populating) the PixelTable table, then wraps the table into a
GoldPxtTorchDataset for downstream consumption. If drop_table is True,
the table will be removed after the dataset is created.
Arguments:
- to_vectorize: Dataset or Table to be vectorized. If a Dataset is provided, each item should be a
dictionary with at least the key specified by
data_keyafter applying the collate_fn. If a Table is provided, it should contain both 'idx' anddata_keycolumns. - restrict_to: Optional set of sample indices (
idx) to restrict the vectorization to. If provided, only the selected samples will be vectorized.
Returns:
A GoldPxtTorchDataset containing at least the vectorized data in the
vectorized_keykey andidx(index of the sample) andidx_vector(index of the vector) keys as well.
Extract and flatten vectors from samples and store results in a PixelTable table.
The vectorization process extracts and flattens vectors from the provided dataset or table
using the GoldTensorVectorizationTool instance and stores them in a PixelTable table specified by table_path.
The resulting vectors are stored in the column specified by vectorized_key.
This method is idempotent (i.e., failure proof), meaning that if it is called multiple times on the same dataset or table, it will not duplicate or recompute the vectorization already present in the PixelTable table.
Arguments:
- to_vectorize: Dataset or Table to be vectorized. If a Dataset is provided, each item should be a
dictionary with at least the key specified by
data_keyafter applying the collate_fn. If a Table is provided, it should contain both 'idx' anddata_keycolumns. - restrict_to: Optional set of sample indices (
idx) to restrict the vectorization to. If provided, only the selected samples will be vectorized.
Returns:
A PixelTable Table containing at least the vectorized data in the
vectorized_keycolumn andidx(index of the sample) andidx_vector(index of the vector) columns as well.
Raises:
- ValueError: If
allow_existingis False and the table attable_pathalready exists, or if an existing vectorized table is missing the requiredidx_vectorprimary key.
Estimate the total computation time for vectorizing a dataset.
Times self.estimation_time_batch_count initial batches — including data loading,
preprocessing, and vectorization — running the exact same steps as
_sequential_vectorize (via _compute_batch), then extrapolates if the dataset's
size is known.
Unwrap vectorized output into individual rows for table insertion.
This converts the Vectorized dataclass (which contains batched vectors and their batch indices) into a dictionary format suitable for inserting into the PixelTable table, with one entry per vector.
Arguments:
- vectorized: Vectorized output containing vectors and batch indices.
- vectorized_key: Key under which to store the vectors in the output dictionary.
- batch: Original batch dictionary with metadata.
- starts: Starting index for assigning new
idx_vectorvalues. - to_keep: Optional list of additional keys to preserve from the batch.
- label_key: Optional key for labels in the batch dictionary. Default is None.
- exclude_labels: Optional set of label strings to exclude from vectorization. Default is None.
Returns:
Dictionary with unwrapped vectors and metadata, ready for table insertion.
Raises:
- ValueError: If
exclude_labelsis provided butlabel_keyis None.
Vectorize a batch and insert the results into a PixelTable table.
This function vectorizes the data in the provided batch using the specified GoldTensorVectorizationTool and inserts the resulting vectors into the given PixelTable table.
Arguments:
- batch: The batch dictionary containing data to vectorize.
- vectorizer: The GoldTensorVectorizationTool instance to use for vectorization.
- data_key: Key in the batch dictionary that contains the data to vectorize.
- vectorized_key: Column name to store the resulting vectors in the PixelTable table.
- target_key: Optional key in the batch dictionary containing the target used to filter vectors.
- label_key: Optional key in the batch dictionary containing the labels.
- target_to_label: Optional mapping from target values to labels for binarization.
- merge_multilabels: Whether to merge multiple labels into a single label. Default is False.
- exclude_labels: Optional set of label strings to exclude from vectorization. Default is None.
- to_keep: Optional list of additional keys to preserve from the batch.
- starts: Starting index for assigning new
idx_vectorvalues. - exclude_full_zero_target: Whether to exclude samples with a full zero target when binarizing
- targets based on the target_to_label mapping. Default is True.