Data Dataset Utils#
dataset_utils
#
| CLASS | DESCRIPTION |
|---|---|
ConcatDatasetWeighted |
Concatenate multiple DatasetWeighted instances into a single dataset. |
ChainDatasetWeighted |
Chain multiple datasets for sequential iteration without concatenation. |
Classes#
ConcatDatasetWeighted(datasets: list[DatasetWeightedType], labels: list[str] | None = None)
#
Bases: Dataset
Concatenate multiple DatasetWeighted instances into a single dataset.
Combines multiple datasets sequentially, maintaining weights and targets across all datasets. Useful for combining data from different sources or experimental conditions.
| PARAMETER | DESCRIPTION |
|---|---|
datasets
|
List of datasets to concatenate.
TYPE:
|
labels
|
Feature labels for concatenated dataset. If None, uses labels from first dataset.
TYPE:
|
Properties
datasets : list[DatasetWeightedType] List of component datasets.
cumulative_sizes : list[int] Cumulative sizes for efficient indexing.
labels : list[str] Feature labels.
Examples:
Concatenate datasets:
>>> import torch
>>> from spectre.data import DatasetWeighted, ConcatDatasetWeighted
>>> ds1 = DatasetWeighted(torch.randn(100, 10), weights=torch.rand(100))
>>> ds2 = DatasetWeighted(torch.randn(50, 10), weights=torch.rand(50))
>>> concat_ds = ConcatDatasetWeighted([ds1, ds2])
>>> len(concat_ds)
150
>>> batch = concat_ds[0]
>>> batch.data.shape
torch.Size([10])
With targets:
>>> ds1 = DatasetWeighted(torch.randn(100, 10), target=torch.randint(0, 2, (100,)))
>>> ds2 = DatasetWeighted(torch.randn(50, 10), target=torch.randint(0, 2, (50,)))
>>> concat_ds = ConcatDatasetWeighted([ds1, ds2])
>>> batch = concat_ds[0]
>>> batch.has_target()
True
Notes
- All datasets must have the same number of features
- All datasets must have targets or none should have targets
- Labels consistency is checked across datasets
Source code in spectre/data/dataset_utils.py
ChainDatasetWeighted(datasets: list[DatasetWeightedType])
#
Bases: Dataset
Chain multiple datasets for sequential iteration without concatenation.
Unlike ConcatDatasetWeighted, ChainDatasetWeighted does not validate feature consistency and allows iteration over heterogeneous datasets. Useful for streaming data from multiple sources.
| PARAMETER | DESCRIPTION |
|---|---|
datasets
|
List of datasets to chain.
TYPE:
|
Properties
datasets : list[DatasetWeightedType] List of component datasets.
cumulative_sizes : list[int] Cumulative sizes for efficient indexing.
Examples:
Chain datasets:
>>> import torch
>>> from spectre.data import DatasetWeighted, ChainDatasetWeighted
>>> ds1 = DatasetWeighted(torch.randn(100, 10))
>>> ds2 = DatasetWeighted(torch.randn(50, 15)) # Different features
>>> chain_ds = ChainDatasetWeighted([ds1, ds2])
>>> len(chain_ds)
150
>>> batch1 = chain_ds[0] # From ds1
>>> batch1.data.shape
torch.Size([10])
>>> batch2 = chain_ds[100] # From ds2
>>> batch2.data.shape
torch.Size([15])
Notes
- No validation of feature dimensions across datasets
- Useful for streaming data or heterogeneous dataset access
- For homogeneous datasets, prefer ConcatDatasetWeighted