Data Dataset#
dataset
#
| CLASS | DESCRIPTION |
|---|---|
DatasetWeighted |
Dataset with weights and optional targets for enhanced data loading. |
Classes#
DataDict
#
Bases: TypedDict
Type-safe container for converted dataset inputs.
DatasetWeighted(X: ArrayType | list[ArrayType], weights: ArrayType | list[ArrayType] | None = None, target: ArrayType | list[ArrayType] | None = None, reduce: Literal['prod', 'sum'] | Callable[[torch.Tensor], torch.Tensor] = 'prod', labels: list[str] | None = None, dtype: torch.dtype = torch.float32, transform: Callable | None = None, *args, **kwargs)
#
Bases: Dataset
Dataset with weights and optional targets for enhanced data loading.
Supports multiple input formats including tensors, numpy arrays, and lists of arrays. Provides automatic weight generation, label creation, and comprehensive indexing capabilities for machine learning workflows.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data. Can be tensor, numpy array, or list of arrays.
TYPE:
|
weights
|
Data weights. Auto-generated as ones if None.
TYPE:
|
target
|
Target values for supervised learning, if None, dataset is for unsupervised learning.
TYPE:
|
reduce
|
TYPE:
|
optional
|
Weight reduction method for 2D weights only. For 1D weights, this parameter has no effect and weights are used as-is. Use "prod" for product across features, "sum" for sum, or provide a custom callable that accepts a 2D tensor (n_samples, n_features) and returns a 1D tensor (n_samples,).
|
by
|
Weight reduction method for 2D weights only. For 1D weights, this parameter has no effect and weights are used as-is. Use "prod" for product across features, "sum" for sum, or provide a custom callable that accepts a 2D tensor (n_samples, n_features) and returns a 1D tensor (n_samples,).
|
labels
|
Feature labels. Auto-generated as ["x_1", "x_2", ...] if None.
TYPE:
|
dtype
|
Tensor data type for all arrays.
TYPE:
|
transform
|
Optional transform function applied to data during
TYPE:
|
Properties
data : torch.Tensor Input data tensor.
weights : torch.Tensor Weight tensor.
target : torch.Tensor | None Target tensor, if provided.
labels : list[str] Feature labels.
Examples:
Using tensors:
>>> import torch
>>> from spectre.data import DatasetWeighted
>>> X = torch.randn(100, 5)
>>> weights = torch.rand(100)
>>> target = torch.randint(0, 2, (100,))
>>> dataset = DatasetWeighted(X, weights=weights, target=target)
>>> len(dataset)
100
>>> data, w, t = dataset[0]
>>> data.shape
torch.Size([5])
>>> w.shape
torch.Size([])
>>> t.shape
torch.Size([1])
Using list of arrays:
>>> X_list = [torch.randn(50, 5), torch.randn(50, 5)]
>>> dataset = DatasetWeighted(X_list)
>>> len(dataset)
100
>>> data, w = dataset[0]
>>> data.shape
torch.Size([5])
>>> w.shape
torch.Size([])
>>> # Without target
>>> dataset = DatasetWeighted(X, weights=weights)
>>> data, w = dataset[0]
>>> data.shape
torch.Size([5])
>>> w.shape
torch.Size([])
Using multiple weight columns:
>>> weights_2d = torch.rand(100, 3) # Three weight columns
>>> dataset = DatasetWeighted(X, weights=weights_2d, reduce="sum")
>>> data, w = dataset[0]
>>> data.shape
torch.Size([5])
>>> w.shape
torch.Size([])
>>> w # Weight is sum across columns
tensor(1.2345) # Example output
Using custom reduction function:
>>> def geometric_mean(w: torch.Tensor) -> torch.Tensor:
... return torch.exp(torch.log(w).mean(dim=1))
>>> weights_2d = torch.rand(100, 3)
>>> dataset = DatasetWeighted(X, weights=weights_2d, reduce=geometric_mean)
>>> data, w = dataset[0]
>>> w.shape
torch.Size([])
| METHOD | DESCRIPTION |
|---|---|
validate |
Validate dataset for common data quality issues. |
get_statistics |
Compute basic statistics for the dataset. |
check_nan |
Check if dataset contains NaN values in data, weights, or targets. |
check_inf |
Check if dataset contains infinite values in data, weights, or targets. |
has_target |
Check if the dataset has target values. |
Source code in spectre/data/dataset.py
Functions#
validate() -> dict[str, bool]
#
Validate dataset for common data quality issues.
Checks for NaN values, infinite values, and negative weights.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, bool]
|
Dictionary with validation results: |
Source code in spectre/data/dataset.py
get_statistics() -> dict[str, torch.Tensor]
#
Compute basic statistics for the dataset.
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Tensor]
|
Dictionary with statistics: |
Source code in spectre/data/dataset.py
check_nan() -> bool
#
Check if dataset contains NaN values in data, weights, or targets.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if NaN values are found, False otherwise. |
Source code in spectre/data/dataset.py
check_inf() -> bool
#
Check if dataset contains infinite values in data, weights, or targets.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if infinite values are found, False otherwise. |
Source code in spectre/data/dataset.py
has_target() -> bool
#
Check if the dataset has target values.
| RETURNS | DESCRIPTION |
|---|---|
bool
|
True if dataset has targets, False otherwise. |
Functions#
concatenate(arrays: list[ArrayType], tensors: list[torch.Tensor], dtype: torch.dtype, validate_shape: bool = True) -> torch.Tensor
#
Concatenate list of arrays into single tensor with validation.
| PARAMETER | DESCRIPTION |
|---|---|
arrays
|
List of arrays to concatenate.
TYPE:
|
X_tensors
|
Reference tensors for shape validation.
TYPE:
|
dtype
|
Target data type.
TYPE:
|
validate_shape
|
Whether to validate shapes against X_tensors, by default True.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
Tensor
|
Concatenated tensor. |
Source code in spectre/data/dataset.py
data_to_tensors(X: ArrayType | list[ArrayType], weights: ArrayType | list[ArrayType] | None, target: ArrayType | list[ArrayType] | None, dtype: torch.dtype, reduce: Literal['prod', 'sum'] | Callable[[torch.Tensor], torch.Tensor]) -> DataDict
#
Convert input data to tensors with validation.
Handles list of arrays by concatenating, validates shapes, and ensures all outputs are proper torch.Tensor types for type safety.
| PARAMETER | DESCRIPTION |
|---|---|
X
|
Input data.
TYPE:
|
weights
|
Data weights.
TYPE:
|
target
|
Target values.
TYPE:
|
dtype
|
Tensor data type.
TYPE:
|
reduce
|
Weight reduction method for 2D weights. Use "prod" for product, "sum" for sum, or provide a custom callable that accepts a 2D tensor (n_samples, n_features) and returns a 1D tensor (n_samples,).
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DataDict
|
TypedDict with |
Source code in spectre/data/dataset.py
401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 | |