Datasets API¶
Dataset generation is controlled by two user-defined builders. Both receive an
ordinary case mapping; one returns the structural system and the other returns
the load. Sensors and axes are supplied separately, and the required record
argument names the leaf fields retained from each completed simulation.
Pass cases for an exact design. For a random or correlated study, provide
n_samples with case_builder(rng). These two case sources are mutually
exclusive.
import dynaresp as dr
dataset = dr.generate_dataset(
cases=cases,
system_builder=build_system,
load_builder=build_load,
sensors=sensor_layout,
time=time,
positions=positions,
record=[
dr.Record.signal,
dr.Record.case_parameter,
],
seed=42,
)
Start with the dataset generation guide, then consult the schema reference for stored dimensions and metadata.
Generation¶
Record
dataclass
¶
One typed dataset field, organized through related selector families.
Use the predefined class attributes; unrecognized paths are rejected by
:func:generate_dataset.
generate_dataset ¶
generate_dataset(
*,
n_samples=None,
system_builder,
load_builder,
sensors,
cases=None,
case_builder=None,
time,
positions,
record,
storage_dtype=float64,
output=None,
seed=None,
workers=1,
progress=False,
)
Generate structural realizations from user-controlled builder strategies.
The system and load builders receive the same plain, mutable parameter dictionary, in that order. Either builder may augment it with numeric derived values; later builders see those additions, and the final dictionary is stored once as ground truth and exposed through both the parameter and target views.
Supply cases for exact, fully controlled realizations. For streamed
random or correlated variability, supply case_builder and n_samples;
it receives the per-sample NumPy generator and returns an ordinary numeric
mapping. The two inputs are mutually exclusive. With neither, n_samples
repeats constant builders with an empty mapping.
Sample seeds are derived only from seed and sample index. Consequently,
case generation, stochastic loads, and sensor effects produce the same
ordered dataset regardless of workers. If output is provided,
records stream to a temporary sibling Zarr store that is validated and
renamed into place only after successful completion.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_samples
|
int | None
|
Number of realizations. Required without |
None
|
system_builder
|
Callable[[dict[str, CaseValue]], StructuralSystem]
|
Function receiving the sample case and returning a system. |
required |
load_builder
|
Callable[[dict[str, CaseValue]], Load]
|
Function receiving the same case and returning a load. |
required |
sensors
|
Sequence[Sensor]
|
Fixed sensor layout applied to every realization. |
required |
cases
|
Iterable[Mapping[str, ArrayLike]] | None
|
Ordered exact parameter mappings, one per realization. All mappings must contain the same names and value shapes. |
None
|
case_builder
|
Callable[[Generator], Mapping[str, ArrayLike]] | None
|
Optional function that creates one numeric case from the
sample generator. It cannot be combined with |
None
|
time
|
ArrayLike
|
Explicit simulation time axis. |
required |
positions
|
ArrayLike
|
Explicit spatial axis. |
required |
record
|
Sequence[Record]
|
Leaf fields from :class: |
required |
storage_dtype
|
DTypeLike
|
Floating dtype used for selected arrays. Integer case values use int64 storage, while sample seeds remain uint64. |
float64
|
output
|
str | Path | None
|
New Zarr path for transactional, incremental storage. Existing destinations are not overwritten. |
None
|
seed
|
int | None
|
Root seed for the case builder, load generation, and sensor effects. A random root seed is created when omitted. |
None
|
workers
|
int
|
Number of builder and simulation threads. This changes throughput, not sample values or ordering. |
1
|
progress
|
bool
|
Display an opt-in |
False
|
Returns:
| Type | Description |
|---|---|
StructuralDataset
|
An in-memory or lazily Zarr-backed structural dataset. |
Raises:
| Type | Description |
|---|---|
RuntimeError
|
If a builder or simulation fails for a sample. |
ValueError
|
If cases, sample shapes, or parameter names are inconsistent. |
FileExistsError
|
If |
Source code in src/dynaresp/datasets/generation.py
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 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 | |
parameter_grid ¶
Build exact cases from the Cartesian product of named dimensions.
This function performs no random sampling. Keyword order is retained in
every case and the rightmost dimension varies fastest. Pass the result to
:func:generate_dataset to run one realization per combination.
Example
parameter_grid(temperature=[0, 20], force=[100, 200]) returns four
cases in the order (0, 100), (0, 200), (20, 100), and
(20, 200).
Source code in src/dynaresp/datasets/generation.py
Dataset access¶
StructuralDataset ¶
Validated, indexable collection of structural simulations.
Schema version 4 stores only explicitly selected arrays. Shared time and
positions coordinates have no sample dimension; every selected sample
array begins with it. Signals use (sample, sensor, time), responses use
(sample, degrees_of_freedom, time), and modal shapes use
(sample, mode, position). metadata["n_samples"] remains authoritative
even when no sample arrays were selected.
In-memory arrays are ordinary NumPy arrays. Arrays opened from Zarr remain
lazy until a property, sample, or analysis adapter materializes them.
Logical parameter and target names are resolved through parameter_keys
and target_keys metadata instead of inferred from path prefixes. This
keeps parameter and target views independent of their physical paths.
Source code in src/dynaresp/datasets/core.py
__getitem__ ¶
Materialize one realization while shared coordinates stay reusable.
Source code in src/dynaresp/datasets/core.py
sample_field ¶
Resolve an adapter field by public alias, target, or parameter name.
Source code in src/dynaresp/datasets/core.py
to_torch ¶
Return a lazy, pickleable PyTorch-compatible view.
inputs, targets, and extras use the same names accepted by
:meth:sample_field. Values are converted to torch.float32 only
when an item is requested, so a Zarr-backed source remains lazy.
Source code in src/dynaresp/datasets/core.py
to_xarray ¶
Materialize signals, frequencies, and scalar parameters as xarray.
Source code in src/dynaresp/datasets/core.py
properties ¶
Materialize scalar parameters and modal frequencies as a dataframe.
Source code in src/dynaresp/datasets/core.py
summary ¶
Return a compact human-readable dataset summary.
Source code in src/dynaresp/datasets/core.py
from_records
classmethod
¶
Construct a dataset from identically shaped per-sample records.
With no output, records are stacked into memory. With output,
each record is written directly to a temporary Zarr directory beside
the destination. The completed store is reopened and validated before
one same-filesystem rename publishes it. Failures remove the temporary
store, and an existing destination is never overwritten.
Source code in src/dynaresp/datasets/core.py
664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 | |
open
classmethod
¶
Open and validate a schema-v4 Zarr store without loading samples.
Selected array locations come from stored metadata rather than naming assumptions, so opening a compact store does not materialize its samples.
Source code in src/dynaresp/datasets/core.py
StructuralSample
dataclass
¶
One realization containing only the fields selected for storage.
signal and clean_signal have shape (sensors, time). System
matrices use (degrees_of_freedom, degrees_of_freedom); modal shapes use
(modes, positions); and the generalized load["force"] uses
(degrees_of_freedom, time). The diagnostic load["values"] uses
(positions, time). target contains the logical targets declared by
dataset metadata, so callers need not know their storage paths.
The dataclass is frozen to keep its fields bound to one realization, but contained NumPy arrays and dictionaries are ordinary mutable objects.
Predefined datasets¶
BeamDamage ¶
BeamDamage(
*,
root,
split="train",
version="1.0",
generate=False,
n_samples=None,
workers=1,
progress=False,
)
Open or reproducibly generate the finite-element beam-damage benchmark.
Version 1.0 varies damaged element, stiffness reduction, and Young's
modulus under a fixed white-noise load configuration. Train and test splits
use distinct fixed root seeds. An existing split is opened lazily;
otherwise, generate=True is required before the transactional Zarr
writer creates it. n_samples overrides the standard 10,000/2,000 split
sizes.
Set progress=True to display completed samples while generating.