PyG-based Transforms

transforms.ToG

class torcheeg.transforms.ToG(adj: List[List], add_self_loop: bool = True, threshold: Optional[float] = None, top_k: Optional[int] = None, binary: bool = False, complete_graph: bool = False, apply_to_baseline: bool = False)[source]

A transformation method for constructing a graph representation of EEG signals, the results of which are applied to the input of the torch_geometric model. In the graph, nodes correspond to electrodes, and edges correspond to associations between electrodes (eg, spatially adjacent or functionally connected)

TorchEEG provides some common graph structures. Consider using the following adjacency matrices depending on the dataset (with different EEG acquisition systems):

  • datasets.constants.emotion_recognition.deap.DEAP_ADJACENCY_MATRIX

  • datasets.constants.emotion_recognition.dreamer.DREAMER_ADJACENCY_MATRIX

  • datasets.constants.emotion_recognition.seed.SEED_ADJACENCY_MATRIX

transform = ToG(adj=DEAP_ADJACENCY_MATRIX)
transform(eeg=np.random.randn(32, 128))['eeg']
>>> torch_geometric.data.Data
Parameters
  • adj (list) – An adjacency matrix represented by a 2D array, each element in the adjacency matrix represents the electrode-to-electrode edge weight. Please keep the order of electrodes in the rows and columns of the adjacency matrix consistent with the EEG signal to be transformed.

  • add_self_loop (bool) – Whether to add self-loop edges to the graph. (default: True)

  • threshold (float, optional) – Used to cut edges when not None. Edges whose weights exceed a threshold are retained. (defualt: None)

  • top_k (int, optional) – Used to cut edges when not None. Keep the k edges connected to each node with the largest weights. (defualt: None)

  • binary (bool) – Whether to binarize the weights on the edges to 0 and 1. If set to True, binarization are done after topk and threshold, the edge weights that still have values are set to 1, otherwise they are set to 0. (defualt: False)

  • complete_graph (bool) – Whether to build as a complete graph. If False, only construct edges between electrodes based on non-zero elements; if True, construct variables between all electrodes and set the weight of non-existing edges to 0. (defualt: False)

  • 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: Union[ndarray, Tensor], baseline: Optional[ndarray] = None, **kwargs) Dict[str, Data][source]
Parameters
  • eeg (np.ndarray) – The input EEG signals in shape of [number of electrodes, 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 graph representation data types that torch_geometric can accept. Nodes correspond to electrodes, and edges are determined via the given adjacency matrix.

Return type

torch_geometric.data.Data

transforms.ToDynamicG

class torcheeg.transforms.ToDynamicG(edge_func: Union[str, Callable] = 'gaussian_distance', add_self_loop: bool = True, threshold: Optional[float] = None, top_k: Optional[int] = None, binary: bool = False, complete_graph: bool = False, apply_to_baseline: bool = False, **kwargs)[source]

A transformation method for dynamically constructing the functional connections between electrodes according to the input EEG signals. The obtained graph structure based on functional connections can be applied to the input of the torch_geometric model. In the graph, nodes correspond to electrodes, and edges correspond to associations between electrodes (functionally connected)

TorchEEG provides algorithms to dynamically calculate functional connections between electrodes:

  • Gaussian Distance

  • Absolute Pearson Correlation Coefficient

  • Phase Locking Value

transform = ToDynamicG(edge_func='gaussian_distance', sigma=1.0, top_k=10, complete_graph=False)
transform(eeg=np.random.randn(32, 128))['eeg']
>>> Data(edge_index=[2, 320], x=[32, 128], edge_weight=[320])

transform = ToDynamicG(edge_func='absolute_pearson_correlation_coefficient', threshold=0.1, binary=True)
transform(eeg=np.random.randn(32, 128))['eeg']
>>> Data(edge_index=[2, 310], x=[32, 128], edge_weight=[310])

transform = ToDynamicG(edge_func='phase_locking_value')
transform(eeg=np.random.randn(32, 128))['eeg']
>>> Data(edge_index=[2, 992], x=[32, 128], edge_weight=[992])

transform = ToDynamicG(edge_func=lambda x, y: (x * y).mean())
transform(eeg=np.random.randn(32, 128))['eeg']
>>> Data(edge_index=[2, 1024], x=[32, 128], edge_weight=[1024])
Parameters
  • edge_func (str or Callable) – Algorithms for computing functional connections. You can use the algorithms provided by TorchEEG, including gaussian_distance, absolute_pearson_correlation_coefficient and phase_locking_value. Or you can use custom functions by passing a callable object containing two parameters representing the signal of the two electrodes, and other named parameters (passed in when initializing the transform), and outputs the value of the functional connection between the two electrodes. (defualt: gaussian_distance)

  • add_self_loop (bool) – Whether to add self-loop edges to the graph. (default: True)

  • threshold (float, optional) – Used to cut edges when not None. Edges whose weights exceed a threshold are retained. (defualt: None)

  • top_k (int, optional) – Used to cut edges when not None. Keep the k edges connected to each node with the largest weights. (defualt: None)

  • binary (bool) – Whether to binarize the weights on the edges to 0 and 1. If set to True, binarization are done after topk and threshold, the edge weights that still have values are set to 1, otherwise they are set to 0. (defualt: False)

  • complete_graph (bool) – Whether to build as a complete graph. If False, only construct edges between electrodes based on non-zero elements; if True, construct variables between all electrodes and set the weight of non-existing edges to 0. (defualt: False)

  • 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: Union[ndarray, Tensor], baseline: Optional[ndarray] = None, **kwargs) Dict[str, Data][source]
Parameters
  • eeg (np.ndarray) – The input EEG signals in shape of [number of electrodes, 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 graph representation data types that torch_geometric can accept. Nodes correspond to electrodes, and edges are determined via the given adjacency matrix.

Return type

torch_geometric.data.Data