Mega‐Grant Separating Eyes‐Open and Eyes‐Closed by 'S 2' Marker - LeoLedesma237/LeoWebsite GitHub Wiki

Overview

The function of this code is to split the initial BrainVision Recorder EEG files into eyes-closed and eyes-open .set recordings. To do this we will need to use this main function below.

You basically need the 'S 2' marker in the data for this to work. It goes inside the { }. The information in the brackets is a time window that functions as the range of EEG data that will be kept around the marker. Below we are saying we want to keep all of the EEG data that starts 180 seconds before 'S 2' up to where 'S 2' starts (0). All of the time window values are in reference to the 'S 2' latency.

   % Use the marker to segment the data for eyes open and eyes closed
    Closed_EEG = pop_epoch( EEG, {  'S  2'  }, [-180  0], 'epochinfo', 'yes');
    Closed_EEG = eeg_checkset( Closed_EEG );
    Closed_EEG = pop_rmbase( Closed_EEG, [],[]);

Part 1: Load in EEG Raw File Names.xlsx

  • Make sure we are loading 'Sheet 2'
% Set the working directory
cd('Y:\STUDY 1\All EEG Files Organized\Preprocessed_RAW')

% Specify the filename
filename = 'EEG Raw File Names.xlsx';

% Read the data from the Excel file
data2 = readtable(filename, 'Sheet', 2);

Part 2: Set Input and Save Pathways

% 1. Set Input filepath 
input_filepath = 'Y:\STUDY 1\All EEG Files Organized\RAW';

% 2. Set the folder path that you want the EEG data saved in
save_pathway = 'Y:\STUDY 1\All EEG Files Organized\Preprocessed_RAW\RAW_eyes_open_and_eyes_closed';

Part 3: Remove files that have already been preprocessed

 % % % % % REMAINING CODE IS AUTOMATIC % % % % % % % % 
eegFiles = data2.all_eeg;

% Remove redundancies
% Only unprocessed files will be ran by the for loop below
Already_Processed= dir(save_pathway);

% Extract .set files
Already_Processed = {Already_Processed(contains({Already_Processed.name}, ".set")).name};

% Change the .set files to .eeg files
Already_Processed = append(erase(Already_Processed, '.set'), '.eeg');

% Remove the 'EyesOpen' and 'EyesClosed' part from the file name
Already_Processed = erase(Already_Processed, 'EyesOpen');
Already_Processed = erase(Already_Processed, 'EyesClosed');

% Keep unique strings
Already_Processed = unique(Already_Processed);

% Keep eegFiles that have not been processed yet
eegFiles = eegFiles(~ismember(eegFiles, Already_Processed));

filesToRemove = {'example1'};

% Remove problematic EEG files
eegFiles = eegFiles(~ismember(eegFiles, filesToRemove));

Part 4: Split the EEG data by eyes-closed or open condition and save them

% Start the for loop
for iii = 1:length(eegFiles)
    % iii = 1
    CurrentFileName = eegFiles{iii}
    fullpath = strcat(input_filepath,'\',CurrentFileName);
    
    %Import data - change the name of the ID
    EEG = pop_fileio(fullpath, 'dataformat','auto');
    [ALLEEG EEG CURRENTSET] = pop_newset(ALLEEG, EEG, 0,'setname','ID#_Imported','gui','off');
    
    % The markers show the latency of when they start, meaning at what data
    % sample it begins. We have to turn this into seconds so we can segment the
    % data. To do this, take the data sample and divide it by the sampling rate
    % (1000). 
    % Extract latency information from markers of interest
    Marker_Type = extractfield(EEG.event ,'type');
    Marker_Latency = num2cell(extractfield(EEG.event ,'latency'));
    
    % Take markers and their latency and merge them into a cell array
    Marker_array = [Marker_Type; Marker_Latency];
    Marker_array = Marker_array';
    
    % Use this formula to bring forth the latency for the marker of interest
    S2rowIdx = strcmp(Marker_array(:,1), 'S  2');
    
    S2Row = Marker_array(S2rowIdx,:);
    
    % Take the latencies and divide by 1000 to convert them into seconds
    S2_Latency = S2Row{2}/EEG.srate; % The start (sec) of the eyes opened condition
    
    % Use the marker to segment the data for eyes open and eyes closed
    Closed_EEG = pop_epoch( EEG, {  'S  2'  }, [-3  0], 'epochinfo', 'yes');
    Closed_EEG = eeg_checkset( Closed_EEG );
    Closed_EEG = pop_rmbase( Closed_EEG, [],[]);
    
    Open_EEG = pop_epoch( EEG, {  'S  2'  }, [0  3], 'newname', 'ID#_Imported epochs', 'epochinfo', 'yes');
    Open_EEG = eeg_checkset( Open_EEG );
    Open_EEG = pop_rmbase( Open_EEG, [],[]);
    
    % Name eyes closed data
    ID_string = eegFiles{iii};
    Eyes_Closed = 'EyesClosed';
    Eyes_Open = 'EyesOpen';
    FileName1 = strcat(Eyes_Closed,ID_string);
    FileName2 = strcat(Eyes_Open,ID_string);
    
    % save data
    Closed_EEG = pop_saveset( Closed_EEG, 'filename',FileName1,'filepath',save_pathway);
    [ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
    
    Open_EEG = pop_saveset( Open_EEG, 'filename',FileName2,'filepath',save_pathway);
    [ALLEEG EEG] = eeg_store(ALLEEG, EEG, CURRENTSET);
    
    % File Tracker
    Current_file = eegFiles{iii};

end