abstract_dataloader.abstract
¶
Abstract Dataloader Generic/Abstract Implementations.
The implementations here provide abstract implementations of commonly reusable functions such as multi-trace datasets, and glue logic for synchronization.
- Where applicable, "polyfill" fallbacks also implement some methods in terms of more basic ones to allow for extending implementations to be more minimal, while still covering required functionality.
- In cases where fallbacks are sufficient to provide a minimal, non-crashing
implementation of the spec, we omit the
ABCbase class so that the class is not technically abstract (though it still may be abstract, in the sense that it may not be meaningful to use it directly.)
Some other convenience methods are also provided which are not included in the
core spec; software using the abstract data loader should not rely on these,
and should always base their code on the spec
types.
Fallback
Abstract base classes which provide default or "fallback" behavior,
e.g. implementing some methods in terms of others, are documented with
a Fallback section.
Note
Classes without separate abstract implementations are also aliased to
their original protocol definitions, so that
abstract_dataloader.abstract
exposes an identical set of objects as
abstract_dataloader.spec.
abstract_dataloader.abstract.Metadata
¶
Bases: Protocol
Sensor metadata.
All sensor metadata is expected to be held in memory during training, so great effort should be taken to minimize its memory usage. Any additional information which is not strictly necessary for book-keeping, or which takes more than negligible space, should be loaded as data instead.
Note
This can be a @dataclass, typing.NamedTuple,
or a fully custom type - it just has to expose a timestamps
attribute.
Attributes:
| Name | Type | Description |
|---|---|---|
timestamps |
Float[ndarray, N]
|
measurement timestamps, in seconds. Nominally in epoch
time; must be consistent within each trace (but not necessarily
across traces). Suggested type: |
Source code in src/abstract_dataloader/spec.py
abstract_dataloader.abstract.Sensor
¶
Bases: ABC, Sensor[TSample, TMetadata]
Abstract Sensor Implementation.
Type Parameters
TSample: sample data type which thisSensorreturns. As a convention, we suggest returning "batched" data by default, i.e. with a leading singleton axis.TMetadata: metadata type associated with this sensor; must implementMetadata.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
metadata
|
TMetadata
|
sensor metadata, including timestamp information; must
implement |
required |
name
|
str
|
friendly name; should only be used for debugging and inspection. |
'sensor'
|
Source code in src/abstract_dataloader/abstract.py
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 | |
duration
property
¶
duration: float
Trace duration from the first to last sample, in seconds.
Fallback
Compute using the first and last metadata timestamp.
__getitem__
abstractmethod
¶
__len__
¶
__len__() -> int
Total number of measurements.
Fallback
Return the length of the metadata timestamps.
stream
¶
Stream values recorded by this sensor.
Fallback
Manually iterate through one sample at a time, loaded using the
provided __getitem__ implementation.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
batch
|
int | None
|
batch size; if |
None
|
Returns:
| Type | Description |
|---|---|
Iterator[TSample | list[TSample]]
|
Iterable of samples (or sequences of samples). |
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Synchronization
¶
Bases: ABC, Synchronization
Synchronization protocol for asynchronous time-series.
This base class implements an optional reference sensor abstraction, and
a margin calculation, which allows excluding samples at the start and end
of each sensor recording.
Reference Sensor
Synchronization is performed with respect to the timestamps of a "reference sensor", which must be present in the provided timestamps.
Margin Calculation
Samples at the start and end of each sensor recording can optionally be excluded:
- The
(start, end)of each margin can be freely specified as either a time margin (in seconds;float) or an index margin (in samples;int). - The margin can also be specified for all sensors uniformly (as just a
Sequence: (start, end)) or per-sensor via aMapping[str, ...]. - If specified per sensor, any sensors not included in the
marginwill default to no margin (i.e.,(0, 0)).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
reference
|
str
|
reference sensor to synchronize to. |
required |
margin
|
Mapping[str, Sequence[int | float]] | Sequence[int | float]
|
time/index margin to apply. |
{}
|
Source code in src/abstract_dataloader/abstract.py
490 491 492 493 494 495 496 497 498 499 500 501 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 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 | |
__call__
abstractmethod
¶
Apply synchronization protocol.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamps
|
dict[str, Float[ndarray, _N]]
|
sensor timestamps. Each key denotes a different sensor name, and the value denotes the timestamps for that sensor. |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Integer[ndarray, M]]
|
A dictionary, where keys correspond to each sensor, and values
correspond to the indices which map global indices to sensor
indices, i.e. |
Source code in src/abstract_dataloader/abstract.py
apply_margin
¶
apply_margin(
timestamps: Mapping[str, Float[ndarray, _N]],
) -> tuple[dict[str, int], dict[str, int]]
Apply margin to timestamps, returning start and end indices.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamps
|
Mapping[str, Float[ndarray, _N]]
|
timestamps by sensor name. |
required |
Returns:
| Type | Description |
|---|---|
tuple[dict[str, int], dict[str, int]]
|
|
Source code in src/abstract_dataloader/abstract.py
get_reference
¶
Get valid reference sensor timestamps.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timestamps
|
Mapping[str, Float[ndarray, _N]]
|
input sensor timestamps. |
required |
Returns:
| Type | Description |
|---|---|
Float[ndarray, _M]
|
Reference sensor timestamps. |
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Trace
¶
Bases: Trace[TSample]
A trace, consisting of multiple simultaneously-recording sensors.
Type Parameters
Sample: sample data type which thisSensorreturns. As a convention, we suggest returning "batched" data by default, i.e. with a leading singleton axis.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sensors
|
Mapping[str, Sensor]
|
sensors which make up this trace. |
required |
sync
|
Synchronization | Mapping[str, Integer[ndarray, N]] | None
|
synchronization protocol used to create global samples from
asynchronous time series. If |
None
|
name
|
str
|
friendly name; should only be used for debugging and inspection. |
'trace'
|
Source code in src/abstract_dataloader/abstract.py
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 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 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 | |
__getitem__
¶
Get item from global index (or fetch a sensor by name).
Tip
For convenience, traces can be indexed by a str sensor name,
returning that Sensor.
Fallback
Reference implementation which uses the computed
Synchronization to retrieve the
matching indices from each sensor. The returned samples have
sensor names as keys, and loaded data as values, matching the
format provided as the sensors parameter:
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | integer | str
|
sample index, or sensor name. |
required |
Returns:
| Type | Description |
|---|---|
TSample | Sensor
|
Loaded sample if |
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Dataset
¶
Bases: Dataset[TSample]
A dataset, consisting of multiple traces, nominally concatenated.
Type Parameters
TSample: sample data type which thisSensorreturns.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
traces
|
Sequence[Trace[TSample]]
|
traces which make up this dataset. |
required |
Source code in src/abstract_dataloader/abstract.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 | |
indices
cached
property
¶
indices: Int64[ndarray, N]
End indices of each trace, with respect to global indices.
__getitem__
¶
Fetch item from this dataset by global index.
Unsigned integer subtraction promotes to np.float64
Subtracting unsigned integers may cause numpy to promote the result to a floating point number. Extending implementations should be careful about this behavior!
In the default implementation here, we make sure that the computed
indices are int64 instead of uint64, and always cast the input
to an int64.
Fallback
Supports (and assumes) random accesses; maps to datasets using
np.searchsorted to search against pre-computed trace start
indices (indices), which costs on the order of 10-100us
per call @ 100k traces.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int | integer
|
sample index. |
required |
Returns:
| Type | Description |
|---|---|
TSample
|
loaded sample. |
Raises:
| Type | Description |
|---|---|
IndexError
|
provided index is out of bounds. |
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Transform
¶
Bases: Transform[TRaw, TTransformed]
Sample or batch data transform.
Warning
Transform types are not verified during initialization, and can only be verified using runtime type checkers when the transforms are applied.
Type Parameters
TRaw: Input data type.TTransformed: Output data type.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
transforms
|
Sequence[Transform]
|
transforms to apply sequentially; each output type must be the input type of the next transform. |
required |
Source code in src/abstract_dataloader/abstract.py
__call__
¶
Apply transforms to a batch of samples.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TRaw
|
A |
required |
Returns:
| Type | Description |
|---|---|
TTransformed
|
A |
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Collate
¶
Bases: Collate[TTransformed, TCollated]
Data collation.
Type Parameters
TTransformed: Input data type.TCollated: Output data type.
Source code in src/abstract_dataloader/abstract.py
abstract_dataloader.abstract.Pipeline
¶
Bases: Pipeline[TRaw, TTransformed, TCollated, TProcessed]
Dataloader transform pipeline.
Type Parameters
TRaw: Input data format.TTransformed: Data after the firsttransformstep.TCollated: Data after the secondcollatestep.TProcessed: Output data format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
sample
|
Transform[TRaw, TTransformed] | None
|
sample transform; if |
None
|
collate
|
Collate[TTransformed, TCollated] | None
|
sample collation; if |
None
|
batch
|
Transform[TCollated, TProcessed] | None
|
batch collation; if |
None
|
Source code in src/abstract_dataloader/abstract.py
400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 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 | |
batch
¶
Transform data batch.
Warning
If this Pipeline requires GPU state in Pytorch, use
ext.torch.Pipeline
instead, which implements the pipeline as a
torch.nn.Module instead.
Fallback
The identity transform is provided by default
(TProcessed = TCollated).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TCollated
|
A |
required |
Returns:
| Type | Description |
|---|---|
TProcessed
|
The |
Source code in src/abstract_dataloader/abstract.py
children
¶
collate
¶
collate(data: Sequence[TTransformed]) -> TCollated
Collate a list of data samples into a GPU-ready batch.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
Sequence[TTransformed]
|
A sequence of |
required |
Returns:
| Type | Description |
|---|---|
TCollated
|
A |
Source code in src/abstract_dataloader/abstract.py
sample
¶
Transform single samples.
Fallback
The identity transform is provided by default
(TTransformed = TRaw).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
TRaw
|
A single |
required |
Returns:
| Type | Description |
|---|---|
TTransformed
|
A single |