random_windows_pytorch_dataset
RandomWindowPytorchDataset
Bases: PytorchDataset
A PyTorch Dataset class that generates random windows on-the-fly for contrastive learning pretraining.
This class extends PytorchDataset to support random window generation without relying on predefined windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
cfg |
DictConfig
|
Configuration options for the dataset. |
required |
split |
str
|
The data split to use (e.g., ‘train’, ‘validation’, ‘test’). |
required |
min_window_size |
int
|
Minimum size of generated windows. |
required |
max_window_size |
int
|
Maximum size of generated windows. |
required |
n_windows |
int
|
Number of windows to generate for each sample. |
required |
Source code in meds_torch/data/components/random_windows_pytorch_dataset.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 | |
collate(batch)
Collate a batch of randomly windowed sequences.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch |
List[dict]
|
A list of dictionaries, each containing windowed sequences. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with collated data for each window. |
Source code in meds_torch/data/components/random_windows_pytorch_dataset.py
generate_random_windows(seq_length)
Generate random windows within a sequence.
This method supports two modes of operation: (a) Random windows: Windows can overlap and be in any order. (b) Consecutive random windows: Windows are side by side and in order.
The window sizes are predetermined to be the same and add up to less than the length of the dataset.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
seq_length |
int
|
Length of the sequence to generate windows from. |
required |
Returns:
| Type | Description |
|---|---|
list[tuple[int, int]]
|
List[Tuple[int, int]]: List of (start, end) indices for each window. |
Raises:
| Type | Description |
|---|---|
ValueError
|
If the sequence length is too short to accommodate all windows. |
Source code in meds_torch/data/components/random_windows_pytorch_dataset.py
get_random_window_size(seq_length)
Calculates the size of the generated random windows.
Source code in meds_torch/data/components/random_windows_pytorch_dataset.py
partition_sequence(sequence, windows)
Partition a sequence into multiple windows.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sequence |
dict
|
The full sequence data. |
required |
windows |
List[Tuple[int, int]]
|
List of (start, end) indices for each window. |
required |
Returns:
| Name | Type | Description |
|---|---|---|
dict |
dict
|
A dictionary with partitioned data for each window. |