ml4gw.utils.slicing
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
X
is 1D, kernels will be sampled uniformly fromX
. IfX
is 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_offset
andcoincident
kwargs. IfX
is 3D, one kernel will be sampled coincidentally from each element along the 0th axis ofX
. In this case,N
must either beNone
or be equal tolen(X)
.- Parameters:
X (
Union
[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 (
Optional
[int
]) -- The number of kernels to sample. Can be left asNone
ifX
is 3D, otherwise must be specifiedmax_center_offeset -- If
X
is 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. IfX
is 1D, this argument is ignored.coincident (
bool
) -- IfX
is 2D, determines whether the individual channels ofX
sample the same kernels or different kernels independently, i.e. whether the channels of each batch element in the output will contain coincident data. IfX
is 1D, this argument is ignored.kernel_size (int)
max_center_offset (int | None)
- Return type:
Union
[Float[Tensor, 'batch time']
,Float[Tensor, 'batch channel time']
]- Returns:
A batch of sampled kernels. If
X
is 1D, this will have shape(N, kernel_size)
. IfX
is 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 (
Union
[Float[Tensor, 'time']
,Float[Tensor, 'channel time']
,Float[Tensor, 'batch channel time']
]) -- The timeseries tensor to slice kernels fromidx (
']
) -- The indices inx
of the first sample of each kernel. Ifx
is 1D,idx
must be 1D as well. Ifx
is 2D andidx
is 1D,idx
is assumed to represent the first index of the kernels sliced from _all_ channels (i.e. the channels are sliced coincidentally). Ifx
is 2D andidx
is also 2D,idx
should have shape(batch_size, num_channels)
, and its values are assumed to represent the first index of the kernel sliced from each channel _independently_. Ifx
is 3D,idx
_must_ be 1D, and have the same length asx
. In this case, it is assumed that the elements ofidx
represent the starting index in the last dimension ofx
from 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:
Union
[Float[Tensor, 'batch time']
,Float[Tensor, 'batch channel time']
]- Returns:
A tensor of shape
(batch_size, kernel_size)
ifx
is 1D and(batch_size, num_channels, kernel_size)
ifx
is 2D, wherebatch_size = idx.shape[0]
andnum_channels = x.shape[0]
ifx
is 2D.
- ml4gw.utils.slicing.unfold_windows(x, window_size, stride, drop_last=True)
Unfold a timeseries into windows
- Parameters:
x (
Union
[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:
Union
[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_last
is false, returns the remainder of the timeseries, shaped to be compatible with the returned unfolded tensor