DynamicSingleAug
- class selfeeg.augmentation.compose.DynamicSingleAug(augmentation, discrete_arg: Dict[str, Any] = None, range_arg: Dict[str, list[int, int]] = None, range_type: Dict[str, str] = None)[source]
Single augmentation with randomly chosen arguments.
DynamicSingleAugperforms a single data augmentation where the optional arguments are chosen at random from a given discrete or continuous range of values. Random choice of the arguments is performed at each call.To perform an augmentation, simply call the instantiated class (see provided example or check the introductory notebook)
- Parameters:
augmentation (function) – The augmentation function to apply. It can be a custom function, but the first argument must be the element to augment.
discrete_arg (dict, optional) –
A dictionary specifying arguments whose value must be chosen within a discrete set. The dict must have:
keys as string with the name of one of the optional arguments
values as lists of elements to be randomly chosen. Single elements are allowed if a specific value for an argument needs to be set. In this case it is not mandatory to give it as list, as automatic conversion will be performed internally. In other words, a key-value pair given as
{"arg": value}is allowed, since the conversion to{"arg": [value]}is automatically performed.
Default = None
range_arg (dict, optional) –
A dictionary specifying arguments whose value must be chosen within a continuous range. The dict must have:
keys as string with the name of one of the optional arguments
values as two element lists specifying the range of values where to randomly select the argument value.
Default = None
range_type (dict or list, optional) –
A dictionary or a list specifying if values in range_arg must be given to the augmentation function as integers. If given as a dict, keys must be the same as the one of range_arg argument. If given as a list, the length must be the same of range_arg. In particular:
if range_type is a dict:
keys must be those in range_arg
values must be single element specifying if the argument must be an integer. In this case, use a boolean True or a string ‘int’ to specify if the argument must be converted to an integer.
if range_arg is a list:
values must be set as the values in the dict. The order is the one used when iterating along the range_arg dict.
if None is given, a list of True with length equal to range_arg is automatically created, since int arguments are more compatible compared to float ones.
Default = None
- perform_augmentation(x: ArrayLike)[source]
Apply the augmentation with the given arguments. __call__() will call this method.
Note
At least one of discrete_arg or range_arg arguments must be given, the class simply suggests to use
StaticSingleAug.Example
>>> import selfeeg.augmentation as aug >>> import torch >>> BatchEEG = torch.zeros(16,32,1024) + torch.sin(torch.linspace(0, 8*np.pi,1024)) >>> Aug_warp = aug.DynamicSingleAug( ... aug.warp_signal, ... discrete_arg = {'batch_equal': [True, False]}, ... range_arg= {'segments': [5,15], 'stretch_strength': [1.5,2.5], ... 'squeeze_strength': [0.4,2/3]}, ... range_type={'segments': True, 'stretch_strength': False, ... 'squeeze_strength': False} ... ) >>> BatchEEGaug1 = Aug_warp(BatchEEG) >>> BatchEEGaug2 = Aug_warp(BatchEEG)
plot the augmentations (require matplotlib to be installed)
>>> import matplotlib.pyplot as plt >>> plt.style.use('seaborn-v0_8-white') >>> plt.rcParams['figure.figsize'] = (15.0, 6.0) >>> 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 dynamic aug with warp augmentation', fontsize=15) >>> plt.legend(['original sample', 'augmented sample 1', 'augmented sample 2']) >>> plt.show()