Dataset Module¶
PyTorch Dataset classes for loading training data.
Overview¶
The dataset module provides PyTorch Dataset implementations for efficient data loading during training.
LeechDataset
¶
LeechDataset(chunk_path: Path | None = None, signal_len: int = 400, kmer_len: int = 11, model_type: str = 'ConvLSTMDwell', dwell_offset: int = 0, chunks: list[dict] | None = None, augmentation: dict | None = None, seq_encoding: str = 'signal_kmer', signal_kmer_context: tuple[int, int] = (4, 4), allow_encoding_fallback: bool = True, left_context: int | None = None, right_context: int | None = None, confound_encoder: ConfoundEncoder | None = None, cl_regression: bool = False, signal_mode: str = 'both', time_mask_bases: int = 0, time_mask_count: int = 1, shift_max_bases: float = 0.0, feature_noise_scale: float = 0.0, dwell_template_table: str | Path | None = None)
Bases: Dataset
PyTorch Dataset for leech training chunks.
Handles loading and preprocessing of training data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chunk_path
|
Path | None
|
Path to .npz file with training chunks |
None
|
signal_len
|
int
|
Expected signal length (will pad/truncate) |
400
|
kmer_len
|
int
|
Expected k-mer length |
11
|
model_type
|
str
|
Model architecture name (e.g., "ConvLSTMDwell", "TransformerDwell") |
'ConvLSTMDwell'
|
dwell_offset
|
int
|
Shift dwell/feature window toward 3' end (bases). Compensates for physical offset between motor protein and sensing region. Requires feature_left >= kmer_context + offset. |
0
|
chunks
|
list[dict] | None
|
Pre-loaded list of chunk dicts. When provided, chunk_path is ignored and no disk I/O occurs (useful for grid search caching). |
None
|
augmentation
|
dict | None
|
Signal augmentation config dict. Keys: - jitter_std (float): Gaussian noise std dev (0 = disabled) - scale_range (tuple[float, float]): Random scale range (1.0, 1.0 = disabled) |
None
|
allow_encoding_fallback
|
bool
|
When |
True
|
left_context
|
int | None
|
Left signal context (samples before focus base). When both left_context and right_context are provided, crop asymmetrically around the focus base instead of center-cropping. |
None
|
right_context
|
int | None
|
Right signal context (samples after focus base). |
None
|
confound_encoder
|
ConfoundEncoder | None
|
Optional :class: |
None
|
cl_regression
|
bool
|
When True, include |
False
|
time_mask_bases
|
int
|
Max width in bases for time masking (0 = disabled). Zeros out contiguous blocks across signal, features, and sequence. |
0
|
time_mask_count
|
int
|
Number of time masks to apply per sample. |
1
|
shift_max_bases
|
float
|
Max cross-layer shift in bases (0 = disabled). Simulates motif anchor offset, applied consistently to all branches. |
0.0
|
feature_noise_scale
|
float
|
Per-channel Gaussian noise multiplier (0 = disabled). Noise std = feature_noise_scale * per-channel empirical std. |
0.0
|
dwell_template_table
|
str | Path | None
|
Path to TSV with per-AA per-position expected
dwell times. When provided, 20 dwell ratio channels are appended
to features: |
None
|
Source code in src/leech/dataset.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 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 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 | |
effective_seq_encoding
property
¶
The encoding this dataset actually yields.
Differs from the requested seq_encoding only when a
signal_kmer request fell back to base_onehot. That is a
different model input — (36, signal_len) against (4, kmer_len)
— so anything that builds a model or writes a config has to read this
rather than what was asked for (#230).
Data Collation¶
LeechDataset also implements __getitems__, so a DataLoader fetches a whole
batch in one call and gets back an already-collated dict rather than a list of
samples -- one gather per field instead of one slice per sample plus a
torch.stack. collate_fn passes such a dict through untouched, and still
stacks a list when it gets one (the per-sample path, used for the list-fallback
dataset and for the cross-layer shift/time-mask augmentations, which roll by a
per-sample offset).
collate_fn
¶
Collate function for DataLoader.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
list[dict[str, Tensor]] | dict[str, Tensor]
|
List of samples from |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Tensor]
|
Batched tensors |
Source code in src/leech/dataset.py
DataLoader Sizing¶
Every leech DataLoader -- training, validation and evaluation -- gets its
worker count from this one function, so the rules (auto on GPU, serial on CPU,
never workers inside a daemonic pool worker) cannot drift between call sites.
resolve_dataloader_workers
¶
Resolve how many DataLoader workers to actually use.
num_workers=0 means AUTO here, not "no workers": on CUDA it becomes
AUTO_DATALOADER_WORKERS, on CPU it stays 0. Feeding a GPU from the main
process serializes collate, host-to-device copy and forward pass onto one
core, which is how eval test sat at 8% GPU on an A5000 (issue #205).
On CPU the workers would compete with the compute for the same cores, and
__getitem__ is trivially fast against pre-tensorized data, so they only
add overhead.
The daemon check is not an optimization: daemonic processes (a
multiprocessing.Pool worker, as in grid search) cannot spawn children,
so a DataLoader with workers raises there. Every caller that builds a
loader goes through this function, so that guard lives in one place.
The auto count is capped by the CPUs this process may actually run on --
sched_getaffinity, which respects the Slurm cpuset -- because a GPU job
allocated 2 cores would otherwise fork 8 workers onto them and thrash. An
explicit request is honoured as given; only "auto" is capped.