Abstract Base Classes
¶
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
ABC
base 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 thisSensor
returns. 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
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 |
|
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: Protocol
Synchronization protocol for asynchronous time-series.
Defines a rule for creating matching sensor index tuples which correspond to some kind of global index.
Source code in src/abstract_dataloader/spec.py
__call__
¶
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/spec.py
abstract_dataloader.abstract.Trace
¶
Bases: Trace[TSample]
A trace, consisting of multiple simultaneously-recording sensors.
Type Parameters
Sample
: sample data type which this Sensor
returns. As a
convention, we suggest returning "batched" data by default, i.e.
with a leading singleton axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
sensors
|
dict[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
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 249 250 251 252 253 254 255 256 |
|
__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 |
TSample | Sensor
|
|
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
Sample
: sample data type which this Sensor
returns. As a
convention, we suggest returning "batched" data by default, i.e.
with a leading singleton axis.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
traces
|
list[Trace[TSample]]
|
traces which make up this dataset. |
required |
Source code in src/abstract_dataloader/abstract.py
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 |
|
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: Protocol
, Generic[TTransformed, TCollated]
Data collation.
Note
This protocol is a equivalent to
Callable[[Sequence[TTransformed]], TCollated]
. Collate
can also
be viewed as a special case of Transform
, where the input type
TRaw
must be a Sequence[...]
.
Composition Rules
Collate
can only be composed in parallel, and can never be sequentially composed.
Type Parameters
TTransformed
: Input data type.TCollated
: Output data type.
Source code in src/abstract_dataloader/spec.py
abstract_dataloader.abstract.Pipeline
¶
Bases: Pipeline[TRaw, TTransformed, TCollated, TProcessed]
Dataloader transform pipeline.
Composition Rules
- A full
Pipeline
can be sequentially pre-composed and/or post-composed with one or moreTransform
s; this is implemented bygeneric.ComposedPipeline
. Pipeline
s can always be composed in parallel; this is implemented bygeneric.ParallelPipelines
, with a pytorchnn.Module
-compatible version intorch.ParallelPipelines
.
Type Parameters
TRaw
: Input data format.TTransformed
: Data after the firsttransform
step.TCollated
: Data after the secondcollate
step.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
380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 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 |
|
batch
¶
Transform data batch.
- Operates on a batch of data, nominally on the GPU-side of a dataloader.
- This method is both sequentially and parallel composable.
Implementation as torch.nn.Module
If this Pipeline
requires GPU state, and the GPU components
are tied to CPU-side or collation functions (so cannot be
separated and implemented separately) it may be helpful to
implement the Pipeline
as a torch.nn.Module
. In this case,
batch
should redirect to __call__
, which in turn redirects to
nn.Module.forward
in order to handle any registered
pytorch hooks.
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
collate
¶
collate(data: Sequence[TTransformed]) -> TCollated
Collate a list of data samples into a GPU-ready batch.
- Operates on the CPU-side of the dataloader, and is responsible for aggregating individual samples into a batch (but not transferring to the GPU).
- Analogous to the
collate_fn
of a pytorch dataloader. - This method is not sequentially composable.
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.
- Operates on single samples, nominally on the CPU-side of a dataloader.
- This method is both sequentially and parallel composable.
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 |