RandomAug

class selfeeg.augmentation.compose.RandomAug(*augmentations, p=None, return_index=False)[source]

Random augmentation chosen from a given set.

RandomAug applies an augmentations selected randomly from a given set.

To perform an augmentation, simply call the instantiated class (see provided example or check the introductory notebook)

Parameters:
  • *augmentations ("callable objects") – The set of augmentations to randomly choose at each call. It can be any callable object, but the first arguments to pass must be the ArrayLike object to augment. It is suggested to give a set of StaticSingleAug or DynamicSingleAug instantiations.

  • p (1D ArrayLike, optional) –

    A 1D array or list with the weights associated to each augmentation (higher the weight, higher the frequency of choosing an augmentation of the list). Elements of p must be in the same order as the given augmentations. If given, p will be scaled so to have sum 1 (so you can give any value). If not given, all augmentations will be chosen with equal probability.

    Default = None

  • return_index (bool, optional) –

    Whether to return an index identifying the selected augmentation or not. The index is simply the output of random.choice function used to select the augmentation from the given list. Indeces follow the augmentation order given during class instantiation.

    Default = False

perform_augmentation(X: ArrayLike)[source]

Apply a random augmentation from the given list of augmenters. __call__() will call this method.

Example

>>> import selfeeg.augmentation as aug
>>> import numpy as np
>>> import torch
>>> BatchEEG = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*np.pi,1024))
>>> Aug_eye = aug.StaticSingleAug(
...     aug.add_eeg_artifact,{'Fs': Fs, 'artifact': 'eye', 'amplitude': 0.5})
>>> Aug_warp = aug.DynamicSingleAug(
...     aug.warp_signal,
...     discrete_arg = {'batch_equal': [True, False]},
...     range_arg= {'segments': [5,15], 'stretch_strength': [1.5,1.8],
...                 'squeeze_strength': [0.5,2/3]},
...     range_type={'segments': True, 'stretch_strength': False,
...                 'squeeze_strength': False}
... )
>>> Sequence2= aug.RandomAug(Aug_eye, Aug_warp, p=[0.7, 0.3])
>>> BatchEEGaug1 = Sequence2(BatchEEG)
>>> BatchEEGaug2 = Sequence2(BatchEEG)

plot the augmentations (require matplotlib to be installed)

>>> # simulate 10000 augmentations calls this line is used in RandomAug
>>> # to choose the index of the list of augmentations to call.
>>> # Note that the size argument has been added to make computation faster, in
>>> # the class only 1 value is returned
>>> import matplotlib.pyplot as plt
>>> idx=np.random.choice(Sequence2.nprange_, size= 1000, p=Sequence2.p)
>>> counts=[(1000-len(np.nonzero(idx)[0]))/1000, len(np.nonzero(idx)[0])/1000]
>>> plt.subplot(1,3,(1,2))
>>> plt.plot(BatchEEG[0,0],linewidth=2.5)
>>> plt.plot(BatchEEGaug1[0,0])
>>> plt.plot(BatchEEGaug2[0,0])
>>> plt.tick_params(axis='both', which='major', labelsize=12)
>>> plt.title(
...     'Example of Random aug between eye blinking artifact and warp',
...     fontsize=15)
>>> plt.legend(
...     ['original sample', 'augmented sample 1', 'augmented sample 2'],
...     loc='upper left')
>>> plt.subplot(1,3,3)
>>> plt.bar(['eye blinking', 'warp'],counts)
>>> plt.tick_params(axis='both', which='major', labelsize=12)
>>> plt.title('barplot of chosen augmentations', fontsize=15)
>>> plt.ylabel('probability',fontsize=12)
>>> plt.show()