Torch-based Transforms
transforms.ToTensor
- class torcheeg.transforms.ToTensor(apply_to_baseline: bool = False)[source]
Convert a
numpy.ndarray
to tensor. Different fromtorchvision
, tensors are returned without scaling.transform = ToTensor() transform(eeg=np.random.randn(32, 128))['eeg'].shape >>> (32, 128)
- __call__(*args, eeg: ndarray, baseline: Optional[ndarray] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (np.ndarray) – The input EEG signals.
baseline (np.ndarray, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
If baseline is passed and apply_to_baseline is set to True, then {‘eeg’: …, ‘baseline’: …}, else {‘eeg’: …}. The output is represented by
torch.Tensor
.- Return type
dict
transforms.Resize
- class torcheeg.transforms.Resize(size: Union[Sequence[int], int], interpolation: str = 'bilinear', apply_to_baseline: bool = False)[source]
Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension.
transform = ToTensor(size=(64, 64)) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 64, 64)
- Parameters
size (tuple) – The output spatial size.
interpolation (str) – The interpolation algorithm used for upsampling, can be nearest, linear, bilinear, bicubic, trilinear, and area. (defualt:
'nearest'
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal in shape of [height of grid, width of grid, number of data points].
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The scaled EEG signal at the saptial dimension.
- Return type
torch.Tensor[new height of grid, new width of grid, number of subbands]
transforms.RandomNoise
- class torcheeg.transforms.RandomNoise(mean: float = 0.0, std: float = 1.0, p: float = 0.5, apply_to_baseline: bool = False)[source]
Add random noise conforming to the normal distribution on the EEG signal.
transform = RandomNoise(p=0.5) transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128)
- Parameters
mean (float) – The mean of the normal distribution of noise. (defualt:
0.0
)std (float) – The standard deviation of the normal distribution of noise. (defualt:
0.0
)p (float) – Probability of adding noise to EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no noise is added to every sample and 1.0 means that noise is added to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after adding random noise.
- Return type
torch.Tensor
transforms.RandomMask
- class torcheeg.transforms.RandomMask(ratio: float = 0.5, p: float = 0.5, apply_to_baseline: bool = False)[source]
Overlay the EEG signal using a random mask, and the value of the overlaid data points was set to 0.0.
transform = RandomMask() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128)
- Parameters
ratio (float) – The proportion of data points covered by the mask out of all data points for each EEG signal sample. (defualt:
0.5
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random mask.
- Return type
torch.Tensor
transforms.RandomWindowSlice
- class torcheeg.transforms.RandomWindowSlice(window_size: int = 120, series_dim: int = - 1, p: float = 0.5, apply_to_baseline: bool = False)[source]
Randomly applies a slice transformation with a given probability, where the original time series is sliced by a window, and the sliced data is scaled to the original size. It is worth noting that the random position where each channel slice starts is the same.
transform = RandomWindowSlice() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128) transform = RandomWindowSlice(window_size=100) transform(eeg=torch.randn(1, 32, 128))['eeg'].shape >>> (1, 32, 128) transform = RandomWindowSlice(p=1.0, series_dim=0) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 9, 9)
- Parameters
window_size (int) – The window size of the slice, the original signal will be sliced to the window_size size, and then adaptively scaled to the input shape.
series_dim (int) – Dimension of the time series in the input tensor. (defualt:
-1
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random window slicing.
- Return type
torch.Tensor
transforms.RandomWindowWarp
- class torcheeg.transforms.RandomWindowWarp(window_size: int = 12, warp_size: int = 24, series_dim: int = - 1, p: float = 0.5, apply_to_baseline: bool = False)[source]
Apply the window warping with a given probability, where a part of time series data is warpped by speeding it up or down.
transform = RandomWindowWarp() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128) transform = RandomWindowWarp(window_size=24, warp_size=48) transform(eeg=torch.randn(1, 32, 128))['eeg'].shape >>> (1, 32, 128) transform = RandomWindowWarp(p=1.0, series_dim=0) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 9, 9)
- Parameters
window_size (int) – Randomly pick a window of size window_size on the time series to transform. (defualt:
-1
)warp_size (int) – The size of the window after the warp. If warp_size is larger than window_size, it means slowing down, and if warp_size is smaller than window_size, it means speeding up. (defualt:
24
)series_dim (int) – Dimension of the time series in the input tensor. (defualt:
-1
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random window warping.
- Return type
torch.Tensor
transforms.RandomPCANoise
- class torcheeg.transforms.RandomPCANoise(mean: float = 0.0, std: float = 1.0, n_components: int = 2, series_dim: int = - 1, p: float = 0.5, apply_to_baseline: bool = False)[source]
Add noise with a given probability, where the noise is added to the principal components of each channel of the EEG signal. In particular, the noise added by each channel is different.
transform = RandomPCANoise() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128) transform = RandomPCANoise(mean=0.5, std=2.0, n_components=4) transform(eeg=torch.randn(1, 32, 128))['eeg'].shape >>> (1, 32, 128) transform = RandomPCANoise(p=1.0, series_dim=0) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 9, 9)
- Parameters
mean (float) – The mean of the normal distribution of noise. (defualt:
0.0
)std (float) – The standard deviation of the normal distribution of noise. (defualt:
0.0
)series_dim (int) – Dimension of the time series in the input tensor. (defualt:
-1
)n_components (int) – Number of components to add noise. if n_components is not set, the first two components are used to add noise.
p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random PCA noise.
- Return type
torch.Tensor
transforms.RandomFlip
- class torcheeg.transforms.RandomFlip(dim=- 1, p: float = 0.5, apply_to_baseline: bool = False)[source]
Applies a random transformation with a given probability to reverse the direction of the input signal in the specified dimension, commonly used for left-right and bottom-up reversal of EEG caps and reversal of timing.
transform = RandomFlip(dim=-1) transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128) transform = RandomFlip(dim=1) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 9, 9)
- Parameters
dim (int) – Dimension to be flipped in the input tensor. (defualt:
-1
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random flipping.
- Return type
torch.Tensor
transforms.RandomSignFlip
- class torcheeg.transforms.RandomSignFlip(p: float = 0.5, apply_to_baseline: bool = False)[source]
Apply a random transformation such that the input signal becomes the opposite of the reversed sign with a given probability
transform = RandomSignFlip() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128)
- Parameters
p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random sign flipping.
- Return type
torch.Tensor
transforms.RandomShift
- class torcheeg.transforms.RandomShift(p: float = 0.5, shift_min: int = 8, shift_max: int = 12, dim: int = - 1, apply_to_baseline: bool = False)[source]
Apply a shift with a specified probability, after which the specified dimension is shifted backward, and the part shifted out of the Tensor is added to the front of that dimension.
transform = RandomShift(dim=-1, shift_min=8, shift_max=24) transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128)
- Parameters
shift_min (float or int) – The minimum shift in the random transformation. (defualt:
-2.0
)shift_max (float or int) – The maximum shift in random transformation. (defualt:
2.0
)dim (int) – Dimension to be shifted in the input tensor. (defualt:
-1
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random shift.
- Return type
torch.Tensor
transforms.RandomChannelShuffle
- class torcheeg.transforms.RandomChannelShuffle(p: float = 0.5, apply_to_baseline: bool = False)[source]
Apply a shuffle with a specified probability, after which the order of the channels is randomly shuffled.
transform = RandomChannelShuffle() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128)
- Parameters
p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random channel shuffle.
- Return type
torch.Tensor
transforms.RandomFrequencyShift
- class torcheeg.transforms.RandomFrequencyShift(p: float = 0.5, frequency: int = 128, shift_min: Union[float, int] = - 2.0, shift_max: Union[float, int] = 2.0, series_dim: int = 0, apply_to_baseline: bool = False)[source]
Apply a frequency shift with a specified probability, after which the EEG signals of all channels are equally shifted in the frequency domain.
transform = RandomFrequencyShift() transform(eeg=torch.randn(32, 128))['eeg'].shape >>> (32, 128) transform = RandomFrequencyShift(frequency=128, shift_min=4.0) transform(eeg=torch.randn(1, 32, 128))['eeg'].shape >>> (1, 32, 128) transform = RandomFrequencyShift(p=1.0, series_dim=0) transform(eeg=torch.randn(128, 9, 9))['eeg'].shape >>> (128, 9, 9)
- Parameters
frequency (int) – The sample frequency in Hz. (defualt:
128
)shift_min (float or int) – The minimum shift in the random transformation. (defualt:
-2.0
)shift_max (float or int) – The maximum shift in random transformation. (defualt:
2.0
)series_dim (int) – Dimension of the time series in the input tensor. (defualt:
-1
)p (float) – Probability of applying random mask on EEG signal samples. Should be between 0.0 and 1.0, where 0.0 means no mask is applied to every sample and 1.0 means that masks are applied to every sample. (defualt:
0.5
)apply_to_baseline – (bool): Whether to act on the baseline signal at the same time, if the baseline is passed in when calling. (defualt:
False
)
- __call__(*args, eeg: Tensor, baseline: Optional[Tensor] = None, **kwargs) Dict[str, Tensor] [source]
- Parameters
eeg (torch.Tensor) – The input EEG signal.
baseline (torch.Tensor, optional) – The corresponding baseline signal, if apply_to_baseline is set to True and baseline is passed, the baseline signal will be transformed with the same way as the experimental signal.
- Returns
The output EEG signal after applying a random frequency shift.
- Return type
torch.Tensor