random_slope_scale

selfeeg.augmentation.functional.random_slope_scale(x: ArrayLike, min_scale: float = 0.9, max_scale: float = 1.2, batch_equal: bool = True, keep_memory: bool = False) ArrayLike[source]

randomly scales the first derivative of x.

Given the input ArrayLike object x where the last two dimensions refers to the EEG channels and samples (1D tensor are also accepted), random_slope_scale calculates the first derivatives of each EEG records, here simplified as the difference between two consecutive values of the last dimension, and rescale each of them with a random factor selected from a uniform distribution between min_scale and max_scale. This transformation is similar to adding a random noise, but with the constraint that the first derivatives must keep the same sign of the original EEG (e.g. if a value is bigger than the previous one, then this is also true in the transformed data).

Parameters:
  • x (ArrayLike) – The input Tensor or Array. The last two dimensions must refer to the EEG recording (Channels x Samples).

  • min_scale (float, optional) –

    The minimum rescaling factor to be applied. It must be a value bigger than 0.

    Default = 0.9

  • max_scale (float, optional) –

    The maximum rescaling factor to be applied. It must be a value bigger than min_scale.

    Default = 1.2

  • batch_equal (bool, optional) –

    Whether to apply the same rescale to all EEGs in the batch or not. This apply only if x has more than 2 dimensions, i.e. more than 1 EEG.

    Default: True

  • keep_memory (bool, optional) –

    Whether to keep memory of the previous changes in slope and accumulate them during the transformation or not. Basically, instead of using:

    x_hat(n)= x(n-1) + scaling*( x(n)-x(n-1) )

    with n>1, x_hat transformed signal, x original signal, keep_memory apply the following:

    x_hat(n)= x_hat(n-1) + scaling*( x(n)-x(n-1) )

    Keep in mind that this may completely change the range of values, as consecutive increases in the slopes may cause a strong vertical shift of the signal. If set to True, it is suggested to set the scaling factor in the range [0.8, 1.2]

    Default: False

Returns:

x (ArrayLike) – The augmented version of the input Tensor or Array.

Example

>>> import torch
>>> import selfeeg.augmentation as aug
>>> x = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*torch.pi,1024))
>>> xaug = aug.random_slope_scale(x)
>>> diff1=torch.abs(xaug[0,0,1:] - xaug[0,0,:-1])
>>> diff2=torch.abs(x[0,0,1:] - x[0,0,:-1])
>>> print(
...     torch.logical_or(diff1<=(diff2*1.2),
...     diff1>=(diff2*0.9)).sum()
... ) # should return 1023