[422372]: / functions / popfunc / pop_autorej.m

Download this file

263 lines (239 with data), 13.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
% POP_AUTOREJ - perform automatic artifact epoch detection and rejection
%
% Usage:
% >> [EEG, rmepochs] = pop_autorej( EEG, 'key', 'val');
%
% Inputs:
% EEG - input EEG structure where epochs have been extracted.
%
% Optional inputs:
% 'threshold' - [float] Threshold limit for detection of extremely large
% fluctuations (uV) {default: 1000}
% 'electrodes' - [integer] Use these channel indices for detection
% of improbable data {default: all channels}
% 'icacomps' - [integer] Use these component activities (instead of
% channel data) for detection of improbable data.
% {default: none}
% 'startprob' - [float] Probability threshold (in std. dev.) for
% detection of improbable data {default: 5}
% 'maxrej' - [float] Maximum % of total trials to reject per
% iteration {default: 5}
% 'eegplot' - ['on'|'off'] Allow for visual inspection of rejected
% trials {default: 'off' if called from the command line,
% 'on' if called from the gui}
% 'nogui' - ['on'|'off'] Do not pop up a gui window to ask for
% input parameters {default: 'off'}
%
% Outputs:
% EEG - EEGLAB data structure
% rmepochs - [integer] rejected trial indices
%
% Function description:
% POP_AUTOREJ first detects extremely large potential fluctuations;
% this is mostly to detect artifacts from electrical surges or other
% unreasonably large amplitude events. Then it applies the following.
% The function rejects data epochs containing data values outside a
% given standard deviation (s.d.) threshold entered by the user (e.g.,
% 3 s.d.'s). In each iteration, if the number of epochs that are
% thus marked for rejection arefewer than 'maxrej' (by default, 5%), it
% then rejects thebeyond-threshold dataepochsand iterates. If the number
% of epochs marked for rejection is more than 5% of the total number
% of data epochs, it does not reject them, but instead increases the
% s.d.threshold by 0.5 s.d.and iterates. When no more data epochs are
% found toexceed the currents.d. threshold, it lowersthe threshold
% by 0.5 s.d. and continues to iterate until either nomore epochs are
% rejected or until 8 iterations have been performed.
%
% Authors: Julie Onton and Arnaud Delorme, SCCN/INC/UCSD, 2007-
% Copyright (C) Arnaud Delorme, SCCN/INC/UCSD, 2007
%
% This file is part of EEGLAB, see http://www.eeglab.org
% for the documentation and details.
%
% Redistribution and use in source and binary forms, with or without
% modification, are permitted provided that the following conditions are met:
%
% 1. Redistributions of source code must retain the above copyright notice,
% this list of conditions and the following disclaimer.
%
% 2. Redistributions in binary form must reproduce the above copyright notice,
% this list of conditions and the following disclaimer in the documentation
% and/or other materials provided with the distribution.
%
% THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
% AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
% IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
% ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
% LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
% CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
% SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
% INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
% CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
% ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
% THE POSSIBILITY OF SUCH DAMAGE.
function [EEG, rmep, com ] = pop_autorej(EEG, varargin);
DEFAULT_STARTPROB = 5; % std devs
DEFAULT_MAXREJ = 5; % std devs
DEFAULT_THRESH = 1000; % uV
if nargin < 1
help pop_autorej;
return;
end
rmep = [];
com = '';
if EEG.trials == 1
error('This function requires data epochs');
end
opt = finputcheck(varargin, { 'startprob' 'real' [] DEFAULT_STARTPROB; ...
'electrodes' 'real' [] [1:EEG.nbchan]; ...
'icacomps' 'real' [] []; ...
'maxrej' 'real' [] DEFAULT_MAXREJ; ...
'eegplot' 'string' { 'on';'off' } 'off'; ...
'nogui' 'string' { 'on';'off' } 'off'; ...
'threshold' 'real' [] DEFAULT_THRESH }, 'pop_autorej');
if isstr(opt), error(opt); end
% pop-up GUI for rejecting artifacts
% ----------------------------------
if strcmpi(opt.nogui, 'off')
promptstr = { { 'style' 'text' 'string' 'Detection of extremely large fluctuations (channels only)' 'fontweight' 'bold' } ...
{ 'style' 'text' 'string' 'Threshold limit (microV)' } ...
{ 'style' 'edit' 'string' '1000' } ...
{ 'style' 'text' 'string' ' ' } ...
{ 'style' 'text' 'string' 'Detection of improbable activity (channels or ICA)' 'fontweight' 'bold' } ...
{ 'style' 'text' 'string' 'Do not use these channel indices (default=all)' } ...
{ 'style' 'edit' 'string' '' } ...
{ 'style' 'text' 'string' 'Use these ICA components instead of data channels' } ...
{ 'style' 'edit' 'string' '' } ...
{ 'style' 'text' 'string' 'Probability threshold (std. dev.)' } ...
{ 'style' 'edit' 'string' '5' } ...
{ 'style' 'text' 'string' 'Maximum % of total trials to reject per iteration' } ...
{ 'style' 'edit' 'string' '5' } ...
{ 'style' 'text' 'string' ' ' } ...
{ 'style' 'text' 'string' 'Check box for visual inspection of results' } ...
{ 'style' 'checkbox' 'string' '' 'value' 1 } };
geometry = { [1] [2 1] [1] [1] [2 1] [2 1] [2 1] [2 1] [1] [2 1]};
result = inputgui( 'geometry', geometry, 'uilist', promptstr, ...
'helpcom', 'pophelp(''pop_autorej'')', ...
'title', 'Automatic artifact rejection -- pop_autorej()');
if isempty(result), return; end
options = { 'nogui' 'on' };
if ~strcmpi(result{1}, '1000'), options = { options{:} 'threshold' str2num(result{1}) }; end
if ~isempty(result{2}), options = { options{:} 'electrodes' setdiff([1:EEG.nbchan], str2num(result{2})) }; end
if ~isempty(result{3}), options = { options{:} 'icacomps' str2num(result{3}) }; end
if ~strcmpi(result{4}, '5'), options = { options{:} 'startprob' str2num(result{4}) }; end
if ~strcmpi(result{5}, '5'), options = { options{:} 'maxrej' str2num(result{5}) }; end
if result{6}, options = { options{:} 'eegplot' 'on' }; end
[ EEG rmep com ] = pop_autorej(EEG, options{:});
return;
end
EEGIN = EEG; % backup EEG structure
if ~isempty(opt.icacomps),
processdat = 0;
complist = opt.icacomps;
else
processdat = 1;
complist = opt.electrodes;
end
% rejection of extremely large fluctuations
% ------------------------------------------
fprintf('\nRunning auto-rejection protocol...\n');
rmep = zeros(1,0);
alleps = [1:EEG.trials];
EEG = pop_eegthresh(EEG,1,[1:size(EEG.data,1)],-opt.threshold,opt.threshold,EEG.xmin,EEG.xmax,0,0);
numrej = length(find(EEG.reject.rejthresh)); % count number of epochs marked
% if numrej > 0
% rmep(1,end+1:end+length(find(EEG.reject.rejthresh))) = alleps(find(EEG.reject.rejthresh));
% alleps(find(EEG.reject.rejthresh)) = [];
% EEG = pop_rejepoch( EEG,EEG.reject.rejthresh,0); % actually reject high prob epochs
% fprintf('\nRe-baselining after large amplitude artifact removed (does not affect the data)...\n');
% EEG = pop_rmbase( EEG, [EEG.xmin*1000 EEG.xmax*1000]);
% end
%--------------------------------------------
EEG = pop_jointprob(EEG, processdat, complist ,opt.startprob,opt.startprob,0,0);% calculate component probabilities
if processdat % if rejection based on channels
numrej = length(find(EEG.reject.rejjp)); % count number of epochs marked
else% if rejection based on ICs
numrej = length(find(EEG.reject.icarejjp)); % count number of epochs marked
end
if (numrej/EEG.trials) < opt.maxrej/100
if processdat
rmep(1,end+1:end+length(find(EEG.reject.rejjp))) = alleps(EEG.reject.rejjp);
alleps(EEG.reject.rejjp) = [];
EEG = pop_rejepoch( EEG,EEG.reject.rejjp,0); % actually reject high prob epochs
else
rmep(1,end+1:end+length(find(EEG.reject.icarejjp))) = alleps(EEG.reject.icarejjp);
alleps(EEG.reject.icarejjp) = [];
EEG = pop_rejepoch( EEG,EEG.reject.icarejjp,0); % actually reject high prob epochs
end
else
fprintf('Re-adjusting probability limits and running again...*********\n');
opt.startprob = opt.startprob + .5;
end
repeat = 1; maxiter = 0;
while repeat == 1 % keep running probability until there are no epochs above threshold
if numrej > 0
EEG = pop_jointprob(EEG,processdat,complist ,opt.startprob,opt.startprob,0,0);
if processdat % if rejection based on channels
numrej = length(find(EEG.reject.rejjp)); % count number of epochs marked
else% if rejection based on ICs
numrej = length(find(EEG.reject.icarejjp)); % count number of epochs marked
end
if (numrej/EEG.trials) < opt.maxrej/100
if processdat
rmep(1,end+1:end+length(find(EEG.reject.rejjp))) = alleps(EEG.reject.rejjp);
alleps(EEG.reject.rejjp) = [];
EEG = pop_rejepoch( EEG,EEG.reject.rejjp,0); % actually reject high prob epochs
else
rmep(1,end+1:end+length(find(EEG.reject.icarejjp))) = alleps(EEG.reject.icarejjp);
alleps(EEG.reject.icarejjp) = [];
EEG = pop_rejepoch( EEG,EEG.reject.icarejjp,0);
end
else
opt.startprob = opt.startprob + 0.5; EEG.reject.icarejjp = [];EEG.reject.rejjpE = [];
fprintf('Re-adjusting probability limits and running again...*********\n');
end
else
if opt.startprob > 5 && maxiter < 8 % don't decrease and startover more than 8 times
fprintf('Decreasing probability limits for final pruning...######\n');
opt.startprob = opt.startprob - 0.5; numrej = 1; maxiter = maxiter+1; % repeat process back to 5 stds
else
if maxiter > 8
opt.maxrej = 15; % go through last round with a high threshold
else
repeat = 0;
end
end
end
end
% run kurtosis check
% ------------------
disp('Final kurotsis reject...');
EEG = pop_rejkurt(EEG,processdat,complist ,6,6,0,0);
numrej = length(find(EEG.reject.icarejkurt)); % count number of epochs marked
if numrej > 0
rmep(1,end+1:end+length(find(EEG.reject.icarejkurt))) = alleps(EEG.reject.icarejkurt);
alleps(EEG.reject.icarejjp) = [];
EEG = pop_rejepoch( EEG,EEG.reject.icarejkurt,0);
end
%--------------------------------------------
EEG = EEGIN;
if strcmpi(opt.eegplot, 'on')
EEG = EEGIN;
EEG.reject.rejauto = zeros(1, length(EEG.trials));
EEG.reject.rejauto(rmep) = 1;
EEG.reject.rejautoE = zeros(EEG.nbchan, EEG.trials);
colrej = EEG.reject.rejmanualcol;
rej = EEG.reject.rejauto;
rejE = EEG.reject.rejautoE;
elecrange = complist;
superpose = 0;
icacomp = processdat;
macrorej = 'EEG.reject.rejauto';
macrorejE = 'EEG.reject.rejautoE';
reject = 1;
eeg_rejmacro; % script macro for generating command and old rejection arrays
eegplot( EEG.data(elecrange,:,:), 'srate', EEG.srate, 'limits', [EEG.xmin EEG.xmax]*1000 , 'command', command, eegplotoptions{:});
else
EEG = pop_rejepoch( EEG, rmep, 0); % actually reject high prob epochs
end
com = sprintf('EEG = pop_autorej(EEG, %s);', vararg2str( varargin ));