Torch-based Transforms

transforms.ToTensor

class torcheeg.transforms.ToTensor[source]

Bases: object

Convert a numpy.ndarray to tensor. Different from torchvision, tensors are returned without scaling.

transform = ToTensor()
transform(np.random.randn(32, 128)).shape
>>> (32, 128)
__call__(x: ndarray) Tensor[source]
Parameters

x (np.ndarray) – The input EEG signals.

Returns

The output represented by torch.Tensor.

Return type

torch.Tensor

transforms.Resize

class torcheeg.transforms.Resize(size: Union[Sequence[int], int], interpolation: str = 'bilinear')[source]

Bases: object

Use an interpolation algorithm to scale a grid-like EEG signal at the spatial dimension.

transform = ToTensor(size=(64, 64))
transform(torch.randn(128, 9, 9)).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')

__call__(x: Tensor) Tensor[source]
Parameters

x (torch.Tensor) – The input EEG signal in shape of [height of grid, width of grid, number of data points].

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)[source]

Bases: object

Add random noise conforming to the normal distribution on the EEG signal.

transform = RandomNoise(p=0.5)
transform(torch.randn(32, 128)).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)

__call__(x: Tensor) Tensor[source]
Parameters

x (torch.Tensor) – The input EEG 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)[source]

Bases: object

Overlay the EEG signal using a random mask, and the value of the overlaid data points was set to 0.0.

transform = RandomMask()
transform(torch.randn(32, 128)).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)

__call__(x: Tensor) Tensor[source]
Parameters

x (torch.Tensor) – The input EEG signal.

Returns

The output EEG signal after applying a random mask.

Return type

torch.Tensor