Contents

%%%#!/usr/local/bin/matlab%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%O
% MATLAB Script to perform pre-ICA clean-up of the continuous data
% 1. Load the preprocessed re-referenced dataset
% 2. Automatic Bad Channel Rejection, and save the dataset, say as xyz_..._ica_bc.out
%    %==> This will be the dataset we will use after ICA to remove bad components from!
%    %    Let us call this dataset THE_MAIN_PREICA_DATASET for later reference.
% 3. MANUAL PRE-ICA REJECTION:
%    Load the dataset with bad-channels already removed (one by one!)
%    Mark very bad, out-of-the ordinary artefacts to reject
%    Leave good blink artefacts in place for the ICA to take care of them
%    At the end, click reject: this saves the rejections in the dataset
%                    as well as does some book-keeping, just to be sure.
%
% With inputs from Alessandro Tavano
%
% Author: R. Muralikrishnan -- 24.06.2016; Last modified: 07.03.2017
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%m
clear all;
close all;

eeglab;

V_Folder_RawData = '/home/muralir/Documents/AB/Experiments/NeuroFemi/Raw_NeuroFemi_RAW/';
V_Folder_EEGLabData = '/home/muralir/Documents/AB/Experiments/NeuroFemi/EEGLab-Data/';

V_Participant = 'filled_in_below';

% CA_x => CellArray
%CA_Participants = {'NF01','NF02','NF03','NF04','NF05','NF06','NF07','NF08','NF09','NF10','NF11','NF12','NF13','NF14','NF15','NF16','NF17','NF18','NF19','NF20','NF21','NF22','NF23','NF24','NF25','NF26','NF27','NF28','NF29','NF30','NF31','NF32','NF33','NF34','NF35','NF37','NF38','NF39','NF40','NF41','NF42','NF43','NF44','NF45','NF46','NF47','NF48','NF49','NF50','NF51','NF52','NF53','NF54','NF55','NF56'};
CA_Participants = {'NF01','NF02','NF03','NF05','NF06','NF07','NF09','NF10','NF11','NF12','NF13','NF14','NF15','NF16','NF20','NF21','NF22','NF23','NF24','NF25','NF27','NF28','NF30','NF32','NF33','NF34','NF37','NF38','NF39','NF42','NF43','NF45','NF46','NF47','NF48','NF49','NF51','NF52','NF53','NF54','NF55','NF56'};

Participants who are ruled out a priori, and for whom NO ICA needs to be done

% NF41, NF36 : Incomplete/non-existent data
% NF29, NF44, NF50 : Left-handed/handedness unclear
% NF04, NF08, NF17, NF18, NF19, NF26, NF31, NF35, NF40 : Too few correct responses. See Avr-Rej-Details-Filetered.txt (in EEG-Data folder).

ICA Preparation

CA_Participants = {'NF39'};
for V_Counter = 1:length(CA_Participants)

Load the preprocessed re-referenced dataset

      V_Participant = CA_Participants{V_Counter};
      V_DataFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '_bp03_40r_.set']);

      EEG = pop_loadset('filename', V_DataFile);

Automatic Bad Channel Rejection

      [EEG, V_Channels_Excluded] = pop_rejchan(EEG, 'elec',[1:EEG.nbchan],'threshold',2,'norm','on','measure','spec','freqrange',[0.3 40] );

      % Save information about Bad Channels excluded from ICA
      %(Adapted from Katrin Cunitz)
      V_BadChannelsFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '_Bad_Channel_Indices.txt']);
      dlmwrite(V_BadChannelsFile, V_Channels_Excluded, 'delimiter', ',');


      % Save the resultant file as an EEGLab .set

      EEG.filename = [V_Participant '_bp03_40r_ica_bc'];
      EEG.filepath = [V_Folder_EEGLabData, V_Participant];
      EEG.comments = char([V_Participant, '_bp03_40r_ica_bc'], '; Sampling Rate: 500 Hz; BPF: 0.3-40 Hz; ReRef: Linked Mastoids;');
      pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);

Automatic Continuous Rejection -- NOT DONE -- MANUAL CLEANUP DONE BELOW!

[EEG, V_Rejected_Sample_Range] = pop_rejcont(EEG, 'elecrange',[1:EEG.nbchan] ,'freqlimit',[0.3 40] ,'threshold',10,'epochlength',0.5,'contiguous',4,'addlength',0.25,'taper','hamming');
% Save the resultant file as an EEGLab .set
EEG.filename = [V_Participant '_bp03_40r_ica_in'];
EEG.filepath = [V_Folder_EEGLabData, V_Participant];
EEG.comments = char([V_Participant, '_bp03_40r_ica_in'], '; Sampling Rate: 500 Hz; BPF: 0.3-40 Hz; ReRef: Linked Mastoids;');
pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);
close all;
V_DataFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '_bp03_40r_ica_in.set']);
EEG = pop_loadset('filename', V_DataFile);
end;

MANUAL PRE-ICA REJECTION

CA_Participants = {'NF39'};
for V_Counter = 1:length(CA_Participants)

Load the dataset with bad-channels already removed in the previous step

    V_Participant = CA_Participants{V_Counter};
    V_DataFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '_bp03_40r_ica_bc.set']);

    EEG = pop_loadset('filename', V_DataFile);


    % Manual Rejection of Bad Sample Segments in Continuous Data
    %  Manually mark bad segments of continuous data in an interactive eegplot
    %  Returns the marked sample intervals in the variable 'V_Rejected_Sample_Range'

    V_Command = 'V_Rejected_Sample_Range = TMPREJ(:,1:2);';   % REJECT button action
    winlength = 5;                 % time range to display in seconds
    scale = 70;                     % amplitude range for channels (uV)

    eegplot(EEG.data,'command',V_Command,'title',[V_Participant ' - Reject time intervals manually'],'events',EEG.event,'spacing',scale,'eloc_file',EEG.chanlocs);

    waitfor(gcf);

    % Automatic rejection
    %[EEG, V_Rejected_Sample_Range] = pop_rejcont(EEG, 'elecrange',[1:EEG.nbchan] ,'freqlimit',[0.3 40] ,'threshold',10,'epochlength',0.5,'contiguous',4,'addlength',0.25,'taper','hamming');

    fprintf('%d interval(s) marked for pre-ICA rejection\n', size(V_Rejected_Sample_Range,1));

Save rejection information as EEP-style .rej file. (Adapted from Katrin Cunitz)

    V_RejectionsFile = fullfile(V_Folder_EEGLabData, V_Participant, [V_Participant, '.rej_pre_ICA']);
    dlmwrite(V_RejectionsFile,(V_Rejected_Sample_Range-1)/EEG.srate,'delimiter','-','precision','%.3f');
    fprintf('Saved file %s.\n',V_RejectionsFile);

Actually Reject the bad sample segments that were marked just now in the data

    % The following not necessary for the automatic rejection procedure, for
    % things are already rejected as part of pop_rejcont.
    % But absolutely necessary for manual rejection, for things were only
    % marked for rejection, and so not yet out.
    EEG = pop_select(EEG,'nopoint',V_Rejected_Sample_Range);
    clear V_Rejected_Sample_Range;

Save the resultant file as an EEGLab .set

    EEG.filename = [V_Participant '_bp03_40r_ica_in'];
    EEG.filepath = [V_Folder_EEGLabData, V_Participant];
    EEG.comments = char([V_Participant, '_bp03_40r_ica_in'], '; Sampling Rate: 500 Hz; BPF: 0.3-40 Hz; ReRef: Linked Mastoids;');
    pop_saveset(EEG, 'filename', EEG.filename, 'filepath', EEG.filepath);

    close all;


  %Om
end;

The dataset ABxx_bp03_40r_ica_in.set is the one that will be used in the next step!

%# See EEGLab_RinICA_PruneData.m