sampler
sampler
¶
SequenceSampler
¶
SequenceSampler(replay_buffer, sequence_length, pad_before=0, pad_after=0, keys=None, key_first_k=None, episode_mask=None, skip_initial=0, pad_with_zeros=True)
Tool to pull out fixed-length chunks (sequences) from a big dataset of episodes.
First, it pre-calculates all possible starting points for sequences using create_indices. This handles episode boundaries, padding if sequences overlap edges, and skipping episodes or initial steps. When you ask for a sequence by index, it slices the data, adds padding (by repeating edge values), and returns a dict of arrays (one per data key like 'observations', 'actions'). For some keys, it can load only the first K steps to save time/memory, filling the rest with defaults (0 or nan).
Example use: In training ML models on time-series data from robot episodes, where you want overlapping windows of 10 steps each.
Attributes:
| Name | Type | Description |
|---|---|---|
indices |
List of precomputed sequence starts/ends/pads. |
|
keys |
Which data fields to include (e.g., ['obs', 'action']). |
|
sequence_length |
Length of each chunk. |
|
replay_buffer |
The big dataset holder. |
|
key_first_k |
For certain keys, load only first K steps (perf hack). |
dict str: int
Only take first k data from these keys (to improve perf)
Source code in src/versatil/data/preprocessing/sampler.py
__len__
¶
sample_sequence
¶
Gets one sequence as a dict of arrays.
Uses the precomputed indices to slice the data.
If padding is needed, pads with zeros when pad_with_zeros is set
(the default) and repeats the first or last value otherwise.
For limited keys, loads partial data and fills rest.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
idx
|
int
|
Which sequence (0 to len-1). |
required |
Returns:
| Type | Description |
|---|---|
dict[str, ndarray]
|
Dict like {'obs': array(seq_len, obs_dim), 'action': array(seq_len, act_dim)}. |
Source code in src/versatil/data/preprocessing/sampler.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 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 | |
create_indices
¶
create_indices(episode_ends, sequence_length, episode_mask, pad_before=0, pad_after=0, skip_initial=0, debug=True)
Builds a list of starting points for fixed-length sequences across multiple episodes.
Episodes are sections of data defined by their ending positions in episode_ends (cumulative, like [5, 10, 15] for episodes of length 5 each). This function finds all possible starting positions where you can pull a sequence of 'sequence_length' steps, allowing some overhang (padding) at the start or end of an episode. It skips episodes marked False in episode_mask. For each valid start, it calculates: - Where the sequence actually starts and ends in the full data buffer. - How much padding is needed at the beginning or end if the sequence hangs over the episode edges. The output is an array of these details for every possible sequence.
Example: If an episode has 10 steps, sequence_length=3, pad_before=1, pad_after=1: - Possible starts range from -1 (pads 1 at start) to 8 (pads 1 at end). - For start=-1: buffer 0 to 2, pad 1 at beginning.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
episode_ends
|
ndarray
|
Array like [5, 12, 20] where each number is the end index of an episode in the full data. |
required |
sequence_length
|
int
|
How long each sequence should be (e.g., 10 steps). |
required |
episode_mask
|
ndarray
|
Boolean array same size as episode_ends; True means include this episode. |
required |
pad_before
|
int
|
Max steps to allow overhanging at the start (will pad with repeats). |
0
|
pad_after
|
int
|
Max steps to allow overhanging at the end. |
0
|
skip_initial
|
int
|
Ignore the first N steps of each episode (e.g., for warm-up periods). |
0
|
debug
|
bool
|
If True, checks that calculations make sense (like no negative pads). |
True
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Array with rows [buffer_start, buffer_end, pad_start, pad_end] for each sequence. |
Source code in src/versatil/data/preprocessing/sampler.py
get_val_mask
¶
Makes a boolean array to pick which episodes are for validation.
Randomly chooses a fraction (val_ratio) of episodes to mark as True (validation set). Uses a random seed for repeatability. If val_ratio=0, all False.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
n_episodes
|
int
|
Total number of episodes. |
required |
val_ratio
|
float
|
Fraction (0.0 to 1.0) to use for validation. |
required |
seed
|
int
|
Random seed for selection. |
0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
Boolean array where True means validation episode. |
Source code in src/versatil/data/preprocessing/sampler.py
downsample_mask
¶
Reduces the number of True values in a mask to at most max_n.
If there are more than max_n Trues, randomly pick max_n of them to keep True, rest False. Useful for limiting training data size.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
mask
|
ndarray
|
Boolean array (e.g., training mask). |
required |
max_n
|
int | None
|
Max number of Trues to keep (if None, no change). |
required |
seed
|
int
|
Random seed. |
0
|
Returns:
| Type | Description |
|---|---|
ndarray
|
New mask with limited Trues. |