Data Dataset Io#
dataset_io
#
| FUNCTION | DESCRIPTION |
|---|---|
save_dataset |
A utility function to save [DatasetWeighted][spectre.core.DatasetWeighted] |
load_dataset |
Load DatasetWeighted from text file. |
Classes#
Functions#
save_dataset(filename: str, dataset: DatasetWeighted, fmt: str = '%.6f', header: bool = True, header_prefix: str | None = None, delimiter: str = ' ', validate_path: bool = True) -> None
#
A utility function to save [DatasetWeighted][spectre.core.DatasetWeighted] to text file.
The file will contain data columns, followed by weight columns, and optionally target columns. Column headers can be included.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
Output file path.
TYPE:
|
dataset
|
Dataset to save.
TYPE:
|
fmt
|
Numerical format string.
TYPE:
|
header
|
Include column headers.
TYPE:
|
header_prefix
|
Header prefix string.
TYPE:
|
delimiter
|
Column delimiter.
TYPE:
|
validate_path
|
Validate output path before writing.
TYPE:
|
Examples:
Basic usage:
Examples with target:
>>> dataset_with_target = DatasetWeighted(data, weights, target)
>>> save_dataset("output_with_target.txt", dataset_with_target, header=True)
Source code in spectre/data/dataset_io.py
load_dataset(filename: str, n_data_cols: int, n_weight_cols: int = 1, n_target_cols: int = 0, delimiter: str | None = None, skip_header: bool = False, dtype: torch.dtype = torch.float32) -> DatasetWeighted
#
Load DatasetWeighted from text file.
The file should contain data columns, followed by weight columns, and optionally target columns. The first row can be skipped as a header.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
Input file path.
TYPE:
|
n_data_cols
|
Number of data columns.
TYPE:
|
n_weight_cols
|
Number of weight columns.
TYPE:
|
n_target_cols
|
Number of target columns.
TYPE:
|
delimiter
|
Column delimiter, if None, any whitespace is used.
TYPE:
|
skip_header
|
Skip first row as header.
TYPE:
|
dtype
|
Data type for tensors.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
DatasetWeighted
|
Loaded dataset. |
| RAISES | DESCRIPTION |
|---|---|
OSError
|
If file cannot be read. |
ValueError
|
If file format is invalid or column counts do not match. |
Examples:
1D weights and optional target:
>>> # Assuming the number of samples is 100 in all examples...
>>> dataset = load_dataset("data.txt", n_data_cols=3, n_weight_cols=1)
>>> print(dataset.data.shape, dataset.weights.shape)
(100, 3) (100,)
Examples with target columns:
>>> dataset = load_dataset(
... "data_with_target.txt",
... n_data_cols=3,
... n_weight_cols=1,
... n_target_cols=1,
... )
>>> print(
... dataset.data.shape,
... dataset.weights.shape,
... dataset.target.shape,
... )
(100, 3) (100,) (100,)
Multi-dimensional weights and targets:
>>> dataset = load_dataset(
... "data_with_multi_weights.txt", n_data_cols=3, n_weight_cols=2
... )
>>> print(dataset.data.shape, dataset.weights.shape)
(100, 3) (100, 2)
>>> dataset = load_dataset(
... "data_multi_target.txt",
... n_data_cols=3,
... n_weight_cols=1,
... n_target_cols=2,
... )
>>> print(
... dataset.data.shape,
... dataset.weights.shape,
... dataset.target.shape,
... )
(100, 3) (100,) (100, 2)
Source code in spectre/data/dataset_io.py
395 396 397 398 399 400 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 | |
get_file_info(filename: str) -> dict[str, Any]
#
Get information about a data file.
| PARAMETER | DESCRIPTION |
|---|---|
filename
|
File path to analyze.
TYPE:
|
| RETURNS | DESCRIPTION |
|---|---|
dict[str, Any]
|
File information including shape, size, and sample content. |
Examples:
>>> info = get_file_info("data.txt")
>>> print(f"Shape: {info['shape']}, Size: {info['size_mb']:.2f} MB")