kups.core.storage
¶
HDF5-backed storage for simulation trajectories.
Writing: :class:HDF5StorageWriter (a Logger) extracts per-group :class:View
data from simulation state and writes it asynchronously on a background thread,
buffering steps into contiguous block writes. Logging frequency is controlled
via :class:LoggingFrequency implementations (:class:Once, :class:EveryNStep);
per-group chunking and compression are controlled via :class:Compression.
Reading: :class:HDF5StorageReader opens a logged file and exposes each group
through a :class:GroupReader, which reconstructs the stored pytree and offers
array-like indexing (reader[idx]) as well as :meth:GroupReader.read_chunked
for streaming a trajectory in fixed-size chunks along the leading (time) axis.
BackgroundWriter
dataclass
¶
Background thread worker that asynchronously writes pre-extracted data to HDF5.
When a group's batch size is >1 its per-step data is buffered and written in contiguous blocks, amortising per-write overhead. Buffers are flushed when full and once more when the thread stops.
Source code in src/kups/core/storage.py
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 | |
start()
¶
Main loop for the background writer thread.
Source code in src/kups/core/storage.py
stop()
¶
write(state, step)
¶
Queue state data for asynchronous writing to HDF5.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Full simulation state to extract and queue. |
required |
step
|
int
|
The current simulation step. |
required |
Source code in src/kups/core/storage.py
Compression
dataclass
¶
HDF5 compression and chunking policy for a logging group.
Datasets are chunked along the leading (time) axis so each chunk is roughly
target_chunk_bytes: large per-frame arrays get about single-frame chunks
(keeping random-frame reads cheap) while small or repeated-across-time fields
get many-frame chunks (capturing cross-step redundancy without per-chunk
bloat). Scalar datasets are stored uncompressed.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
codec
|
Literal['zstd', 'gzip', 'lzf']
|
HDF5 compression filter: |
'zstd'
|
level
|
int
|
Compression level, |
1
|
shuffle
|
bool
|
Enable the byte-shuffle filter. Helps float arrays; may slightly reduce the ratio on highly repetitive data. |
True
|
target_chunk_bytes
|
int
|
Approximate per-chunk size in bytes. |
_DEFAULT_CHUNK_BYTES
|
Source code in src/kups/core/storage.py
dataset_kwargs(leading_dims, shape, itemsize)
¶
Build create_dataset keyword arguments for one array leaf.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
leading_dims
|
tuple[int, ...]
|
Leading (time) dimensions prepended to |
required |
shape
|
tuple[int, ...]
|
Shape of a single logged value. |
required |
itemsize
|
int
|
Bytes per element. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Keyword arguments for |
dict[str, Any]
|
shuffle); empty for scalar datasets, which are stored uncompressed. |
Source code in src/kups/core/storage.py
EveryNStep
dataclass
¶
Bases: LoggingFrequency
Logs data every N steps, creating datasets with a time dimension.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n
|
int
|
The interval between logging steps (e.g., n=10 logs at steps 0, 10, 20, ...). |
required |
Source code in src/kups/core/storage.py
GroupReader
dataclass
¶
Reader for a single HDF5 logging group, providing array-like access.
Source code in src/kups/core/storage.py
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 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 | |
paths
cached
property
¶
Dataset names for this group's array leaves, in pytree-leaf order.
tree_def
cached
property
¶
Pytree structure of the stored value, for reassembling reads into Storage.
__getitem__(index)
¶
Array-like access; equivalent to :meth:read.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Index
|
Index applied to each leaf dataset. |
required |
Returns:
| Type | Description |
|---|---|
Storage
|
The stored value with each leaf sliced by |
Source code in src/kups/core/storage.py
read(index=slice(None), *, select=None)
¶
Read index from each leaf dataset and reassemble the stored pytree.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
Index
|
Index applied to each leaf dataset (int, slice, ellipsis, or tuple thereof). Defaults to the whole leading axis. |
slice(None)
|
select
|
View[Storage, Field] | None
|
Optional lens focusing a field (or sub-pytree) of the stored
value. When given, only the datasets backing that field are read
and the focused value is returned; when |
None
|
Returns:
| Type | Description |
|---|---|
Storage | Field
|
The stored value ( |
Storage | Field
|
leaf sliced by |
Source code in src/kups/core/storage.py
read_chunked(chunk_size, *, select=None)
¶
Yield successive chunks along the leading (time) axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_size
|
int
|
Number of leading-axis frames per chunk; must be positive. |
required |
select
|
View[Storage, Field] | None
|
Optional lens focusing a field of the stored value; when
given each chunk reads only that field's datasets (see
:meth: |
None
|
Yields:
| Type | Description |
|---|---|
Iterator[Storage] | Iterator[Field]
|
Each chunk as a read of at most |
Iterator[Storage] | Iterator[Field]
|
value or the focused field); the final chunk may be shorter. |
Source code in src/kups/core/storage.py
GroupWriters
dataclass
¶
Bases: WriterGroupConfig[State, Storage]
Internal class combining a WriterGroupConfig with its initialized HDF5 writer.
Source code in src/kups/core/storage.py
batch_size
cached
property
¶
Steps to buffer before a block write, sized so the largest leaf writes
about one chunk per flush (capped so cheap groups still flush periodically).
1 for groups logged once (no time axis).
HDF5StorageReader
dataclass
¶
Reader for HDF5 files created by HDF5StorageWriter.
Usage
Source code in src/kups/core/storage.py
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 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 | |
file
property
¶
The open HDF5 file; requires use as a context manager.
__enter__()
¶
Open the HDF5 file for reading.
Returns:
| Type | Description |
|---|---|
Self
|
The reader, for use within the |
__exit__(*exc)
¶
Close the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exc
|
object
|
Context-manager exception info; unused. |
()
|
focus_group(view_or_name)
¶
Return a reader for a specific logging group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view_or_name
|
View[Config, WriterGroupConfig[Any, Storage]] | str
|
Either a string group name or a |
required |
Returns:
| Type | Description |
|---|---|
GroupReader[Storage]
|
A reader for the focused group. |
Source code in src/kups/core/storage.py
list_groups()
¶
List the names of all logging groups in the file.
Returns:
| Type | Description |
|---|---|
list[str]
|
Group names, in config-pytree leaf order. |
Source code in src/kups/core/storage.py
HDF5StorageWriter
dataclass
¶
Logs simulation state to HDF5 files. Implements the Logger protocol.
Usage as context manager (preferred):
writer = HDF5StorageWriter(out_path, config, initial_state, total_steps=1000)
with writer:
for step in range(1000):
state = simulate_step(state)
writer.log(state, step)
The writer opens the file and starts a background I/O thread on __enter__,
and flushes, records actual_steps, and closes the file on __exit__.
The background thread buffers each group's steps and writes them in contiguous
blocks to amortise per-write overhead (the dominant cost of single-step writes).
The block size is chosen per group so the largest leaf writes about one chunk
per flush, bounding the buffer to roughly one chunk regardless of system size;
buffers are flushed on __exit__.
Source code in src/kups/core/storage.py
251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 | |
__enter__()
¶
Open the file, create datasets, and start the background writer thread.
Returns:
| Type | Description |
|---|---|
Self
|
The writer, for use within the |
Source code in src/kups/core/storage.py
__exit__(*exc)
¶
Stop the writer thread, record actual_steps, and close the file.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
*exc
|
object
|
Context-manager exception info; unused. |
()
|
Source code in src/kups/core/storage.py
log(state, step)
¶
Queue state for async background writing.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
State
|
Full simulation state to extract and log. |
required |
step
|
int
|
The current simulation step. |
required |
Source code in src/kups/core/storage.py
Hdf5ObjWriter
dataclass
¶
Low-level writer for a single HDF5 group that stores a pytree of JAX arrays.
Source code in src/kups/core/storage.py
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 | |
init(hdf5_group, state, leading_dims, compression=None)
staticmethod
¶
Create one dataset per array leaf of state and persist its pytree structure.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
hdf5_group
|
Group
|
Group to populate with datasets and metadata. |
required |
state
|
S
|
Sample value whose leaves define dataset shapes and dtypes. |
required |
leading_dims
|
tuple[int, ...]
|
Leading (time) dimensions prepended to each leaf shape. |
required |
compression
|
Compression | None
|
Compression/chunking policy, or |
None
|
Returns:
| Type | Description |
|---|---|
Hdf5ObjWriter[S]
|
A writer bound to the created datasets. |
Source code in src/kups/core/storage.py
write(state, index)
¶
Write a single value's leaves to index of their datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
state
|
Storage
|
Value whose leaves are written. |
required |
index
|
Index
|
Dataset index to write each leaf to. |
required |
Source code in src/kups/core/storage.py
write_block(states, start)
¶
Write a contiguous block of states to [start : start + len(states)].
Stacks each leaf across states and writes one slice per dataset,
amortising the per-call overhead of single-step writes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
states
|
list[Storage]
|
Per-step values to stack and write, in order. |
required |
start
|
int
|
Leading-axis index of the first state. |
required |
Source code in src/kups/core/storage.py
LoggingFrequency
¶
Bases: Protocol
Protocol for defining when and how data should be logged during simulation.
Implementations control logging frequency, determine HDF5 dataset dimensions, and map simulation steps to dataset indices.
Source code in src/kups/core/storage.py
dataset_index(step)
¶
Index at which step's data is stored.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
The current simulation step. |
required |
Returns:
| Type | Description |
|---|---|
Index
|
The leading-axis index, or |
leading_shape(total_steps)
¶
Leading (time) dimensions of this group's datasets.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
total_steps
|
int
|
Total number of steps in the run. |
required |
Returns:
| Type | Description |
|---|---|
tuple[int, ...]
|
Dimensions prepended to each leaf's shape; empty for once-logged data. |
Source code in src/kups/core/storage.py
should_log(step)
¶
Whether to log at this simulation step.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
step
|
int
|
The current simulation step. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
|
Once
¶
Bases: LoggingFrequency
Logs data only at step 0, creating scalar datasets without time dimension.
Source code in src/kups/core/storage.py
WriterGroupConfig
dataclass
¶
Configuration for a single logging group.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
view
|
View[State, Storage]
|
A lens that extracts Storage data from the full State. |
required |
logging_frequency
|
LoggingFrequency
|
Controls when this data should be logged. |
required |
compression
|
Compression | None
|
Compression/chunking policy, or |
Compression()
|