Torch-based Transforms

transforms.ToTensor

class torcheeg.transforms.ToTensor(apply_to_baseline: bool = False)[source]

Convert a numpy.ndarray to tensor. Different from torchvision, 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