ml4gw.utils.slicing
This module contains functions for randomly sampling windows of data from timeseries data, as well as for unfolding timeseries data into potentially overlapping windows.
Functions
|
Randomly sample kernels from a single or multichannel timeseries |
|
Slice kernels from single or multichannel timeseries |
|
Unfold a timeseries into windows |
- ml4gw.utils.slicing.sample_kernels(X, kernel_size, N=None, max_center_offset=None, coincident=True)
Randomly sample kernels from a single or multichannel timeseries
For a tensor representing one or multiple channels of timeseries data, randomly slice kernels of a fixed length from the timeseries. If
Xis 1D, kernels will be sampled uniformly fromX. IfXis 2D, kernels will be sampled from the first dimension ofX(assumed to be the time dimension) in a manner that depends on the values of themax_center_offsetandcoincidentkwargs. IfXis 3D, one kernel will be sampled coincidentally from each element along the 0th axis ofX. In this case,Nmust either beNoneor be equal tolen(X).- Parameters:
X (
Float[Tensor, 'time']|Float[Tensor, 'channel time']|Float[Tensor, 'batch channel time']) -- The timeseries tensor from which to sample kernels kernel_size: The size of the kernels to sampleN (
int|None) -- The number of kernels to sample. Can be left asNoneifXis 3D, otherwise must be specifiedmax_center_offeset -- If
Xis 2D, this indicates the maximum distance from the center of the timeseries the edge of sampled kernels may fall. If left asNone, kernels will be sampled uniformly across all ofX's time dimension. If greater than 0, defines the maximum distance that the rightmost edge of the kernel may fall from the center of the timeseries (the leftmost edge will always be sampled such that the center of the timeseries falls within or afer the kernel). If equal to 0, every kernel sampled will contain the center of the timeseries, which may fall anywhere within the kernel with uniform probability. If less than 0, defines the minimum distance that the center of the timeseries must fall from either edge of the kernel. IfXis 1D, this argument is ignored.coincident (
bool) -- IfXis 2D, determines whether the individual channels ofXsample the same kernels or different kernels independently, i.e. whether the channels of each batch element in the output will contain coincident data. IfXis 1D, this argument is ignored.kernel_size (int)
max_center_offset (int | None)
- Return type:
Float[Tensor, 'batch time']|Float[Tensor, 'batch channel time']- Returns:
A batch of sampled kernels. If
Xis 1D, this will have shape(N, kernel_size). IfXis 2D, this will have shape(N, num_channels, kernel_size), wherenum_channels = X.shape[0].
- ml4gw.utils.slicing.slice_kernels(x, idx, kernel_size)
Slice kernels from single or multichannel timeseries
Given a 1D timeseries or a 2D tensor representing a multichannel timeseries, slice kernels of a given size from the timeseries starting at the indicated indices. Returns a batch of 1D or 2D kernels, and so will have one more dimension than
x.- Parameters:
x (
Float[Tensor, 'time']|Float[Tensor, 'channel time']|Float[Tensor, 'batch channel time']) -- The timeseries tensor to slice kernels fromidx (
']) -- The indices inxof the first sample of each kernel. Ifxis 1D,idxmust be 1D as well. Ifxis 2D andidxis 1D,idxis assumed to represent the first index of the kernels sliced from _all_ channels (i.e. the channels are sliced coincidentally). Ifxis 2D andidxis also 2D,idxshould have shape(batch_size, num_channels), and its values are assumed to represent the first index of the kernel sliced from each channel _independently_. Ifxis 3D,idx_must_ be 1D, and have the same length asx. In this case, it is assumed that the elements ofidxrepresent the starting index in the last dimension ofxfrom which to sample a batch of kernels coincidentally among the channels.kernel_size (
int) -- The length of the kernels to slice from the timeseries
- Return type:
Float[Tensor, 'batch time']|Float[Tensor, 'batch channel time']- Returns:
A tensor of shape
(batch_size, kernel_size)ifxis 1D and(batch_size, num_channels, kernel_size)ifxis 2D, wherebatch_size = idx.shape[0]andnum_channels = x.shape[0]ifxis 2D.
- ml4gw.utils.slicing.unfold_windows(x, window_size, stride, drop_last=True)
Unfold a timeseries into windows
- Parameters:
x (
Float[Tensor, 'time']|Float[Tensor, 'channel time']|Float[Tensor, 'batch channel time']) -- The timeseries to unfold. Can have shape(batch_size, num_channels, length * sample_rate),(num_channels, length * sample_rate), or(length * sample_rate)window_size (
int) -- The size of the windows to unfold from xstride (
int) -- The stride between windowsdrop_last (
bool) -- If true, does not return the remainder that exists when the timeseries cannot be evenly broken up into windows
- Return type:
Float[Tensor, 'window time']|Float[Tensor, 'window channel time']|Float[Tensor, 'window batch channel time']- Returns:
A tensor of shape
(num_windows, batch_size, num_channels, kernel_size),(num_windows, num_channels, kernel_size), or(num_windows, kernel_size)depending on whether the input tensor is 3D, 2D, or 1DIf
drop_lastis false, returns the remainder of the timeseries, shaped to be compatible with the returned unfolded tensor