Datatype-independent Transforms

transforms.Compose

class torcheeg.transforms.Compose(transforms: List[Callable])[source]

Compose several transforms together. Consistent with torchvision.transforms.Compose’s behavior.

transform = Compose([
    ToTensor(),
    Resize(size=(64, 64)),
    RandomNoise(p=0.1),
    RandomMask(p=0.1)
])
transform(eeg=torch.randn(128, 9, 9))['eeg'].shape
>>> (128, 64, 64)

:obj`Compose` supports transformers with different data dependencies. The above example combines multiple torch-based transformers, the following example shows a sequence of numpy-based transformer.

transform = Compose([
    BandDifferentialEntropy(),
    MeanStdNormalize(),
    ToGrid(DEAP_CHANNEL_LOCATION_DICT)
])
transform(eeg=np.random.randn(32, 128))['eeg'].shape
>>> (128, 9, 9)
Parameters

transforms (list) – The list of transforms to compose.

__call__(*args, **kwargs) any[source]
Parameters

x (any) – The input.

Returns

The transformed output.

Return type

any

transforms.Lambda

class torcheeg.transforms.Lambda(lambd: Callable, targets: List[str] = ['eeg', 'baseline', 'y'])[source]

Apply a user-defined lambda as a transform.

transform = Lambda(targets=['y'], lambda x: x + 1)
transform(y=1)['y']
>>> 2
Parameters
  • targets (list) – What data to transform via the Lambda. (default: ['eeg', 'baseline', 'y'])

  • lambd (Callable) – Lambda/function to be used for transform.

__call__(*args, **kwargs) Dict[str, any][source]
Parameters

x (any) – The input.

Returns

The transformed output.

Return type

any

transforms.BaselineRemoval

class torcheeg.transforms.BaselineRemoval[source]

A transform method to subtract the baseline signal (the signal recorded before the emotional stimulus), the nosie signal is removed from the emotional signal unrelated to the emotional stimulus.

TorchEEG recommends using this class in online_transform for higher processing speed. Even though, this class is also supported in offline_transform. Usually, the baseline needs the same transformation as the experimental signal, please add :obj`apply_to_baseline=True` to all transforms before this operation to ensure that the transformation is performed on the baseline signal

transform = Compose([
    BandDifferentialEntropy(apply_to_baseline=True),
    ToTensor(apply_to_baseline=True),
    BaselineRemoval(),
    ToGrid(DEAP_CHANNEL_LOCATION_DICT)
])

transform(eeg=np.random.randn(32, 128), baseline=np.random.randn(32, 128))['eeg'].shape
>>> (4, 9, 9)
__call__(*args, eeg: any, baseline: Optional[any] = None, **kwargs) Dict[str, any][source]
Parameters
  • eeg (any) – The input EEG signal.

  • baseline (any) – The corresponding baseline signal.

Returns

The transformed result after removing the baseline signal.

Return type

any