conninfpy.eeg_utils

EEG-specific data structures and reshape helpers.

class conninfpy.eeg_utils.Electrodes(value)[source]

Bases: Enum

Class to return EEG electrode names and respective IDs based on 10-20 EEG system.

>>> Electrodes.Fp2.value
2
>>> Electrodes.P3.name
'P3'
Fp1 = 1
Fp2 = 2
F7 = 3
F3 = 4
Fz = 5
F4 = 6
F8 = 7
T3 = 8
T4 = 9
T5 = 10
T6 = 11
O1 = 12
O2 = 13
C3 = 14
Cz = 15
C4 = 16
P3 = 17
Pz = 18
P4 = 19
class conninfpy.eeg_utils.Bands(value)[source]

Bases: Enum

Class to return EEG frequency bands with respective IDs.

>>> Bands.alpha1.value
3
>>> Bands.get_name_by_id(6)
'beta2'
delta = 1
theta = 2
alpha1 = 3
alpha2 = 4
beta1 = 5
beta2 = 6
gamma = 7
static get_name_by_id(id)[source]
static get_values()[source]
class conninfpy.eeg_utils.PairsElectrodes1020(electrodes: Electrodes)[source]

Bases: object

Class to resolve & return electrode pairs based on 10-20 EEG system.

Methods:

electrode_pairs: Function to resolve indexed electrode names. create_pairs_dict: Function to create mapped dictionary of electodes with pairs.

Attributes:

electrodes (List[Electrodes]): List of electrodes to consider nearest (List[Tuple[str, str]]): List of electrode pairs

>>> electrodes = [Electrodes.Fp1, Electrodes.Fp2, Electrodes.Fz]
>>> pairs_obj = PairsElectrodes1020(electrodes)
>>> pairs_obj.electrode_pairs
[('Fp1', 'Fp2'), ('Fp1', 'Fz'), ('Fp2', 'Fz')]
>>> pairs_dict = pairs_obj.create_pairs_dict(pairs_obj.nearest, filter_by=['Fp1'])
>>> ('Fp1', 'Fp2') in pairs_dict
True
property electrode_pairs

Returns electrode names as an indexed list as per combinations

>>> electrodes = [Electrodes.Fp1, Electrodes.Fp2, Electrodes.Fz]
>>> pairs_obj = PairsElectrodes1020(electrodes)
>>> pairs_obj.electrode_pairs
[('Fp1', 'Fp2'), ('Fp1', 'Fz'), ('Fp2', 'Fz')]
create_pairs_dict(pairs_list, filter_by=None)[source]

Creates a dictionary mapping electrode pairs to pairs_list.

Parameters:

pairs_list (list): List of electrode pairs (tuple) to be mapped. filter_by (list, optional): List of electrodes to be filtered by name (Default = None).

Returns:
pairs_dict (dict) = Dictionary with electrode pairs as keys (from self.electrode) mapped to

electrodes from pairs_list.

>>> electrodes = [Electrodes.Fp1, Electrodes.Fp2, Electrodes.Fz]
>>> pairs_obj = PairsElectrodes1020(electrodes)
>>> pairs_obj.electrode_pairs
[('Fp1', 'Fp2'), ('Fp1', 'Fz'), ('Fp2', 'Fz')]
class conninfpy.eeg_utils.EEGData(data, subj_list, electrodes, el_pairs_list, bands)[source]

Bases: object

Class function for reading EEG data

Parameters:

data (np.ndarray): EEG data of shape (n_subjects, n_channels, num_freqs). subj_list (List[str]): List of subject identifiers corresponding to the first axis of ‘data’. electrodes (Enum): Enum represents electrodes. el_pairs_list (List[Tuple[str, str]]): List of electrode pairs. bands (List[str]): List of EEG frequency bands.

conninfpy.eeg_utils.read_from_eeg_dataframe(path_to_df, cond_prefix='fo', band_list=None)[source]

Function to read EEG data from (.csv) format & load into (n_subjects, n_channels, num_freqs) format.

Parameters:

path_to_df (str): Path to csv file with EEG data. cond_prefix (str, optional): String prefix for identification of condition w.r.t columns (default = ‘fo’). band_list (list[int], optional) : List of frequency bands (default = None).

Returns:
EEGData object with attributes:

data (np.ndarray): EEG data array of shape (n_subjects, n_channels, n_freqs) subj_list (List[str]): List of subject identifiers corresponding to the first axis of ‘data’. Electrodes (Enum): Returned as electrode class for channels with labels. (pairs_dict.keys()) (tuple): Electrode pairs as a tuple. bands (Enum): Returned as Bands class of frequency bands.

>>> eeg_data = read_from_eeg_dataframe('datasets\eeg_dataframe_nansfilled.csv', cond_prefix='fo')
>>> eeg_data.data.shape
(177, 171, 7)
conninfpy.eeg_utils.reshape_eeg_data(data: ndarray, reshape_bands: bool = True) ndarray[source]

Reshape EEG data from (n_subjects, chan_pairs, num_freqs) or (chan_pairs, chan_pairs) to (n_subjects, n_chans, n_chans, n_freq) or to (n_subjects, n_chans*n_freq, n_chans*n_freq,) if reshape_bands is True, where each chansxchans block corresponds to a specific frequency. The number of electrode pairs is considered as 19, for this instance of implementation.

Parameters:

data (np.ndarray): Array of shape (n_subjects, chan_pairs, num_freqs) reshape_bands (bool): Option to return a block diagonal matrix where each block returned as per individual frequency bands (default = True)

Returns:

reshaped_data (np.ndarray): Array of shape (n_subjects, chan_pairs, num_freqs) or (n_subjects, n_chans*n_freq, n_chans*n_freq,) For single subjects = reshaped_data: [np.ndarray] of shape (chan_pairs, chan_pairs, num_freqs) or (chan_pairs*num_freqs, chan_pairs*num_freqs)

>>> n_pairs = np.random.rand(2, len(PairsElectrodes1020(Electrodes).electrode_pairs), 3)  # 1 subject, all pairs, 2 freqs/len(PairsElectrodes1020(Electrodes).electrode_pairs)
>>> reshape_eeg_data(n_pairs, reshape_bands=False).shape
(2, 19, 19, 3)
>>> reshape_eeg_data(n_pairs, reshape_bands=True).shape
(2, 57, 57)
conninfpy.eeg_utils.inverse_reshape_eeg_data(reshaped_data: ndarray, reshape_bands: bool = True) ndarray[source]

Function to perform Inverse reshape of EEG data back to (n_subjects, chan_pairs, num_freqs) format.

Parameters:

reshaped_data (np.ndarray): EEG data of shape (n_subjects, num_els, num_els, num_freqs). or (n_subjects, num_els*n_freqs, num_els*n_freqs) if reshape_bands=True. reshape_bands (bool): To indicate if input is in band-flattened form.

Returns:

original_data (np.ndarray): Original EEG data of shape (n_subjects, chan_pairs, num_freqs) or (chan_pairs, num_freq)

>>> n_pairs = np.random.rand(2, len(PairsElectrodes1020(Electrodes).electrode_pairs), 3)
>>> reshaped_data = reshape_eeg_data(n_pairs, reshape_bands=True)
>>> reshaped_data.shape
(2, 57, 57)
>>> inverse_data = inverse_reshape_eeg_data(reshaped_data, reshape_bands=True)
>>> inverse_data.shape
(2, 171, 3)