bci_essentials.paradigm.ssvep_paradigm
1import numpy as np 2 3from .paradigm import Paradigm 4 5 6class SsvepParadigm(Paradigm): 7 """ 8 SSVEP paradigm. 9 10 TODO Determine how to automatically set the target frequencies in the classifier from here. 11 """ 12 13 def __init__( 14 self, 15 filters=[5, 30], 16 iterative_training=False, 17 live_update=False, 18 buffer_time=0.01, 19 ): 20 """ 21 Parameters 22 ---------- 23 filters : list of floats, *optional* 24 Filter bands. 25 - Default is `[5, 30]`. 26 iterative_training : bool, *optional* 27 Flag to indicate if the classifier will be updated iteratively. 28 - Default is `False`. 29 live_update : bool, *optional* 30 Flag to indicate if the classifier will be used to provide 31 live updates on trial classification. 32 - Default is `False`. 33 buffer_time : float, *optional* 34 Defines the time in seconds after an epoch for which we require EEG data to ensure that all EEG is present in that epoch. 35 - Default is `0.01`. 36 """ 37 super().__init__(filters) 38 39 self.live_update = live_update 40 self.iterative_training = iterative_training 41 42 if self.live_update: 43 self.classify_each_epoch = True 44 self.classify_each_trial = False 45 else: 46 self.classify_each_trial = True 47 self.classify_each_epoch = False 48 49 self.buffer_time = buffer_time 50 51 self.paradigm_name = "SSVEP" 52 53 def get_eeg_start_and_end_times(self, markers, timestamps): 54 """ 55 Get the start and end times of the EEG data based on the markers. 56 57 Parameters 58 ---------- 59 markers : list of str 60 List of markers. 61 timestamps : list of float 62 List of timestamps. 63 64 Returns 65 ------- 66 float 67 Start time. 68 float 69 End time. 70 """ 71 start_time = timestamps[0] - self.buffer_time 72 73 end_time = timestamps[-1] + float(markers[-1].split(",")[3]) + self.buffer_time 74 75 return start_time, end_time 76 77 def process_markers(self, markers, marker_timestamps, eeg, eeg_timestamps, fsample): 78 """ 79 This takes in the markers and EEG data and processes them into epochs according to the SSVEP paradigm. 80 81 Parameters 82 ---------- 83 markers : list of str 84 List of markers. 85 marker_timestamps : list of float 86 List of timestamps. 87 eeg : np.array 88 EEG data. Shape is (n_channels, n_samples). 89 eeg_timestamps : np.array 90 EEG timestamps. Shape is (n_samples). 91 fsample : float 92 Sampling frequency. 93 94 Returns 95 ------- 96 np.array 97 Processed EEG data. Shape is (n_epochs, n_channels, n_samples). 98 np.array 99 Labels. Shape is (n_epochs). 100 """ 101 102 # Initialize y 103 y = np.zeros(len(markers), dtype=int) 104 105 for i, marker in enumerate(markers): 106 marker = marker.split(",") 107 label = int(marker[2]) 108 epoch_length = float(marker[3]) 109 self.target_freqs = [float(x) for x in marker[4:]] 110 111 n_channels, _ = eeg.shape 112 113 marker_timestamp = marker_timestamps[i] 114 115 # Subtract the marker timestamp from the EEG timestamps so that 0 becomes the marker onset 116 marker_eeg_timestamps = eeg_timestamps - marker_timestamp 117 118 # Create the epoch time vector 119 epoch_time = np.arange(0, epoch_length, 1 / fsample) 120 121 # Initialize the EEG data array 122 epoch_eeg = np.zeros((1, n_channels, len(epoch_time))) 123 124 # Interpolate the EEG data to the epoch time vector for each channel 125 for c in range(n_channels): 126 epoch_eeg[0, c, :] = np.interp( 127 epoch_time, marker_eeg_timestamps, eeg[c, :] 128 ) 129 130 epoch_eeg[0, :, :] = super()._preprocess( 131 epoch_eeg[0, :, :], fsample, self.lowcut, self.highcut 132 ) 133 134 if i == 0: 135 X = epoch_eeg 136 else: 137 X = np.concatenate((X, epoch_eeg), axis=0) 138 139 y[i] = label 140 return X, y 141 142 # TODO: Implement this 143 def check_compatibility(self): 144 pass
7class SsvepParadigm(Paradigm): 8 """ 9 SSVEP paradigm. 10 11 TODO Determine how to automatically set the target frequencies in the classifier from here. 12 """ 13 14 def __init__( 15 self, 16 filters=[5, 30], 17 iterative_training=False, 18 live_update=False, 19 buffer_time=0.01, 20 ): 21 """ 22 Parameters 23 ---------- 24 filters : list of floats, *optional* 25 Filter bands. 26 - Default is `[5, 30]`. 27 iterative_training : bool, *optional* 28 Flag to indicate if the classifier will be updated iteratively. 29 - Default is `False`. 30 live_update : bool, *optional* 31 Flag to indicate if the classifier will be used to provide 32 live updates on trial classification. 33 - Default is `False`. 34 buffer_time : float, *optional* 35 Defines the time in seconds after an epoch for which we require EEG data to ensure that all EEG is present in that epoch. 36 - Default is `0.01`. 37 """ 38 super().__init__(filters) 39 40 self.live_update = live_update 41 self.iterative_training = iterative_training 42 43 if self.live_update: 44 self.classify_each_epoch = True 45 self.classify_each_trial = False 46 else: 47 self.classify_each_trial = True 48 self.classify_each_epoch = False 49 50 self.buffer_time = buffer_time 51 52 self.paradigm_name = "SSVEP" 53 54 def get_eeg_start_and_end_times(self, markers, timestamps): 55 """ 56 Get the start and end times of the EEG data based on the markers. 57 58 Parameters 59 ---------- 60 markers : list of str 61 List of markers. 62 timestamps : list of float 63 List of timestamps. 64 65 Returns 66 ------- 67 float 68 Start time. 69 float 70 End time. 71 """ 72 start_time = timestamps[0] - self.buffer_time 73 74 end_time = timestamps[-1] + float(markers[-1].split(",")[3]) + self.buffer_time 75 76 return start_time, end_time 77 78 def process_markers(self, markers, marker_timestamps, eeg, eeg_timestamps, fsample): 79 """ 80 This takes in the markers and EEG data and processes them into epochs according to the SSVEP paradigm. 81 82 Parameters 83 ---------- 84 markers : list of str 85 List of markers. 86 marker_timestamps : list of float 87 List of timestamps. 88 eeg : np.array 89 EEG data. Shape is (n_channels, n_samples). 90 eeg_timestamps : np.array 91 EEG timestamps. Shape is (n_samples). 92 fsample : float 93 Sampling frequency. 94 95 Returns 96 ------- 97 np.array 98 Processed EEG data. Shape is (n_epochs, n_channels, n_samples). 99 np.array 100 Labels. Shape is (n_epochs). 101 """ 102 103 # Initialize y 104 y = np.zeros(len(markers), dtype=int) 105 106 for i, marker in enumerate(markers): 107 marker = marker.split(",") 108 label = int(marker[2]) 109 epoch_length = float(marker[3]) 110 self.target_freqs = [float(x) for x in marker[4:]] 111 112 n_channels, _ = eeg.shape 113 114 marker_timestamp = marker_timestamps[i] 115 116 # Subtract the marker timestamp from the EEG timestamps so that 0 becomes the marker onset 117 marker_eeg_timestamps = eeg_timestamps - marker_timestamp 118 119 # Create the epoch time vector 120 epoch_time = np.arange(0, epoch_length, 1 / fsample) 121 122 # Initialize the EEG data array 123 epoch_eeg = np.zeros((1, n_channels, len(epoch_time))) 124 125 # Interpolate the EEG data to the epoch time vector for each channel 126 for c in range(n_channels): 127 epoch_eeg[0, c, :] = np.interp( 128 epoch_time, marker_eeg_timestamps, eeg[c, :] 129 ) 130 131 epoch_eeg[0, :, :] = super()._preprocess( 132 epoch_eeg[0, :, :], fsample, self.lowcut, self.highcut 133 ) 134 135 if i == 0: 136 X = epoch_eeg 137 else: 138 X = np.concatenate((X, epoch_eeg), axis=0) 139 140 y[i] = label 141 return X, y 142 143 # TODO: Implement this 144 def check_compatibility(self): 145 pass
SSVEP paradigm.
TODO Determine how to automatically set the target frequencies in the classifier from here.
SsvepParadigm( filters=[5, 30], iterative_training=False, live_update=False, buffer_time=0.01)
14 def __init__( 15 self, 16 filters=[5, 30], 17 iterative_training=False, 18 live_update=False, 19 buffer_time=0.01, 20 ): 21 """ 22 Parameters 23 ---------- 24 filters : list of floats, *optional* 25 Filter bands. 26 - Default is `[5, 30]`. 27 iterative_training : bool, *optional* 28 Flag to indicate if the classifier will be updated iteratively. 29 - Default is `False`. 30 live_update : bool, *optional* 31 Flag to indicate if the classifier will be used to provide 32 live updates on trial classification. 33 - Default is `False`. 34 buffer_time : float, *optional* 35 Defines the time in seconds after an epoch for which we require EEG data to ensure that all EEG is present in that epoch. 36 - Default is `0.01`. 37 """ 38 super().__init__(filters) 39 40 self.live_update = live_update 41 self.iterative_training = iterative_training 42 43 if self.live_update: 44 self.classify_each_epoch = True 45 self.classify_each_trial = False 46 else: 47 self.classify_each_trial = True 48 self.classify_each_epoch = False 49 50 self.buffer_time = buffer_time 51 52 self.paradigm_name = "SSVEP"
Parameters
- filters (list of floats, optional):
Filter bands.
- Default is
[5, 30].
- Default is
- iterative_training (bool, optional):
Flag to indicate if the classifier will be updated iteratively.
- Default is
False.
- Default is
- live_update (bool, optional):
Flag to indicate if the classifier will be used to provide
live updates on trial classification.
- Default is
False.
- Default is
- buffer_time (float, optional):
Defines the time in seconds after an epoch for which we require EEG data to ensure that all EEG is present in that epoch.
- Default is
0.01.
- Default is
def
get_eeg_start_and_end_times(self, markers, timestamps):
54 def get_eeg_start_and_end_times(self, markers, timestamps): 55 """ 56 Get the start and end times of the EEG data based on the markers. 57 58 Parameters 59 ---------- 60 markers : list of str 61 List of markers. 62 timestamps : list of float 63 List of timestamps. 64 65 Returns 66 ------- 67 float 68 Start time. 69 float 70 End time. 71 """ 72 start_time = timestamps[0] - self.buffer_time 73 74 end_time = timestamps[-1] + float(markers[-1].split(",")[3]) + self.buffer_time 75 76 return start_time, end_time
Get the start and end times of the EEG data based on the markers.
Parameters
- markers (list of str): List of markers.
- timestamps (list of float): List of timestamps.
Returns
- float: Start time.
- float: End time.
def
process_markers(self, markers, marker_timestamps, eeg, eeg_timestamps, fsample):
78 def process_markers(self, markers, marker_timestamps, eeg, eeg_timestamps, fsample): 79 """ 80 This takes in the markers and EEG data and processes them into epochs according to the SSVEP paradigm. 81 82 Parameters 83 ---------- 84 markers : list of str 85 List of markers. 86 marker_timestamps : list of float 87 List of timestamps. 88 eeg : np.array 89 EEG data. Shape is (n_channels, n_samples). 90 eeg_timestamps : np.array 91 EEG timestamps. Shape is (n_samples). 92 fsample : float 93 Sampling frequency. 94 95 Returns 96 ------- 97 np.array 98 Processed EEG data. Shape is (n_epochs, n_channels, n_samples). 99 np.array 100 Labels. Shape is (n_epochs). 101 """ 102 103 # Initialize y 104 y = np.zeros(len(markers), dtype=int) 105 106 for i, marker in enumerate(markers): 107 marker = marker.split(",") 108 label = int(marker[2]) 109 epoch_length = float(marker[3]) 110 self.target_freqs = [float(x) for x in marker[4:]] 111 112 n_channels, _ = eeg.shape 113 114 marker_timestamp = marker_timestamps[i] 115 116 # Subtract the marker timestamp from the EEG timestamps so that 0 becomes the marker onset 117 marker_eeg_timestamps = eeg_timestamps - marker_timestamp 118 119 # Create the epoch time vector 120 epoch_time = np.arange(0, epoch_length, 1 / fsample) 121 122 # Initialize the EEG data array 123 epoch_eeg = np.zeros((1, n_channels, len(epoch_time))) 124 125 # Interpolate the EEG data to the epoch time vector for each channel 126 for c in range(n_channels): 127 epoch_eeg[0, c, :] = np.interp( 128 epoch_time, marker_eeg_timestamps, eeg[c, :] 129 ) 130 131 epoch_eeg[0, :, :] = super()._preprocess( 132 epoch_eeg[0, :, :], fsample, self.lowcut, self.highcut 133 ) 134 135 if i == 0: 136 X = epoch_eeg 137 else: 138 X = np.concatenate((X, epoch_eeg), axis=0) 139 140 y[i] = label 141 return X, y
This takes in the markers and EEG data and processes them into epochs according to the SSVEP paradigm.
Parameters
- markers (list of str): List of markers.
- marker_timestamps (list of float): List of timestamps.
- eeg (np.array): EEG data. Shape is (n_channels, n_samples).
- eeg_timestamps (np.array): EEG timestamps. Shape is (n_samples).
- fsample (float): Sampling frequency.
Returns
- np.array: Processed EEG data. Shape is (n_epochs, n_channels, n_samples).
- np.array: Labels. Shape is (n_epochs).