-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathscan_channels.m
271 lines (241 loc) · 8.79 KB
/
scan_channels.m
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
263
264
265
266
267
268
269
270
271
%% Scan Muse channels using 5-s sliding windows and use trained models to
% classify each window as good/bad automatically and quickly. Frontal and
% posterior channels are considered separately (one fine-tuned classifier
% for each because their signal characteristics are pretty different).
%
% The EEG signals in input must be raw (no prior preprocessing), and will
% be band-pass filtered by this function for best classification performance
% (but your EEG file output remains raw).
%
% The only parameter to change (maxTol) how much of a channel should be
% tolerated as bad before it is flagged as bad.
%
% For each window, some features are computed (RMS, SNR, low-frequency
% power), which were selected as most important by a Random Forest model
% during model training and validation.
%
% Various ML models were trained and tested (decision trees, logistic
% regression, LDA, SVM, Naive Bayes, neural networks). They implement
% PCA-dimension reduction, hyperparameter tuning, and 5-fold
% cross-validation. Training was done on 80% of a dataset. After model
% validation, models were tested on the remaining 20% of data
% (different individuals). The best models achieved 93.5% for frontal
% channels (logistic regression) and 91.4% for the posterior channels (decision
% tree).
%
%
% Inputs:
% EEG - EEG structure (EEGLAB format)
% maxTol - max portion of bad windows to tolerate before
% flagging the channel (default = .5 for 50% of
% windows).
% vis - visualize the flagged channels (1) or not (0)
%
% Output:
% badChan - a logical array indicating if channels are bad (1) or
% ok (0). A channel is considered bad when the portion of
% bad sliding segments is > maxBad.
% badChanLabels - cell array with the labels of bad channels
% badSegs - logical array indicating the sliding windows
% classified as bad by the trained models.
%
% Example:
% [badChan, badChanLabels] = scan_channels(EEG, .5, 1);
%
% Cedric Cannard, March 2023
function [badChan, badChanLabels, badSegs] = scan_channels(EEG,maxTol,vis)
pluginPath = fileparts(which('eegplugin_import_muse.m'));
load(fullfile(pluginPath, 'classifier_front.mat')); % default = Logistic regression
load(fullfile(pluginPath, 'classifier_post.mat')); % default = Decision tree
% Defaults
badChanLabels = [];
% if isempty(winSize)
winSize = 5; % should remain 5 seconds (same as for training)
% end % size of sliding windows in s
winSize = winSize * EEG.srate; % convert to samples
nSeg = floor(EEG.pnts/winSize);
if isempty(maxTol)
maxTol = .5;
end
if isempty(vis)
vis = false;
end
% Filter EEG signal (same as for training of classifiers)
EEG = pop_eegfiltnew(EEG,'locutoff',1,'hicutoff',50);
% Design filter for SNR feature
b = design_fir(100,[2*[0 45 50]/EEG.srate 1],[1 1 0 0]);
% Extract features
badChan = false(1,EEG.nbchan);
disp('Lookg for bad channels using trained models...')
for iChan = 1:EEG.nbchan
signal = EEG.data(iChan,:);
flatSeg = nan(nSeg-1,1);
feat1 = nan(nSeg-1,1);
feat2 = nan(nSeg-1,1);
feat3 = nan(nSeg-1,1);
feat4 = nan(nSeg-1,1);
feat5 = nan(nSeg-1,1);
for iWind = 1:nSeg-1
% Sliding window
if iWind == 1
tStart = 1;
else
tStart = tEnd + 1;
end
tEnd = (tStart + winSize)-1;
if tEnd > EEG.pnts
d = tEnd - EEG.pnts;
tEnd = EEG.pnts;
warning('tEnd is %g s beyond the last sample. Replacing with last sample.', round(d*256,1))
end
tSeg = tStart:tEnd; % time index in samples for this segment
%%% Frontal features (see trainedModelFront.RequiredVariables)
% RMS
feat1(iWind,:) = rms(signal(tSeg));
% LF power
tmp = get_psd(signal(tSeg),256,'hamming',50,[],256,[0.2 3],'psd');
feat2(iWind,:) = rms(tmp);
% SNR
tmp = filtfilt_fast(b,1,signal(tSeg)');
feat3(iWind,:) = mad(signal(tSeg) - tmp');
% POST features (see trainedModelPost.RequiredVariables)
if iChan == 1 || iChan == 4
% Kurtosis
feat4(iWind,:) = kurtosis(signal(tSeg));
% HF power
tmp = get_psd(signal(tSeg),256,'hamming',50,[],256,[70 100],'psd');
feat5(iWind,:) = rms(tmp);
end
% Additional classification: if more than 50% of segment is flat
flatPortion = sum( abs(diff(signal(tSeg)))<(20*eps) ) / (length(tSeg)-1);
if flatPortion > .5
flatSeg(iWind,:) = true; % 2 for bad
else
flatSeg(iWind,:) = false; % 1 for good
end
end
if iChan == 2 || iChan == 3
features = table(feat1,feat2,feat3,'variablenames',{ 'RMS' 'LF_power' 'SNR'});
prediction = trainedModelFront.predictFcn(features); % classify windows
else
features = table(feat1,feat2,feat3,feat4,feat5,'variablenames',{ 'RMS' 'LF_power' 'SNR' 'Kurtosis' 'HF_power'});
prediction = trainedModelPost.predictFcn(features); % classify windows
end
% Tag if good or bad
badSegs(iChan,:) = or(single(prediction) == 2, flatSeg);
% Bad channels
if sum(badSegs(iChan,:))/(nSeg-1) > maxTol
badChan(iChan) = true;
end
end
if sum(badChan) == 0
disp('No bad channels detected by trained classifier')
else
badChanLabels = {EEG.chanlocs(badChan).labels};
fprintf('Bad channels detected by trained classifier: '); fprintf('%s ', badChanLabels{:}); fprintf('\n')
end
if vis
% eegplot(EEG.data,'winlength',30,'srate',EEG.srate,'spacing',1000);
TMPEEG = pop_select(EEG,'nochannel', badChanLabels);
TMPEEG.etc.clean_channel_mask = ~badChan;
vis_artifacts(TMPEEG,EEG);
end
%% Subfunctions
% FIR filter design from Christian's clean_artifacts code
function B = design_fir(N,F,A,nfft,W)
if nargin < 4 || isempty(nfft)
nfft = max(512,2^ceil(log(N)/log(2)));
end
if nargin < 5
W = 0.54 - 0.46*cos(2*pi*(0:N)/N);
end
F = interp1(round(F*nfft),A,(0:nfft),'pchip');
F = F .* exp(-(0.5*N)*sqrt(-1)*pi*(0:nfft)./nfft);
B = real(ifft([F conj(F(end-1:-1:2))]));
B = B(1:N+1).*W(:)';
% filtfilt_fast from Christian's clean_artifacts code
function X = filtfilt_fast(varargin)
% if nargin == 3
[B, A, X] = deal(varargin{:});
% elseif nargin == 4
% [N, F, M, X] = deal(varargin{:});
% B = design_fir(N,F,sqrt(M)); A = 1;
% end
if A == 1
was_single = strcmp(class(X),'single');
w = length(B); t = size(X,1);
X = double([bsxfun(@minus,2*X(1,:),X(1+mod(((w+1):-1:2)-1,t),:)); X; bsxfun(@minus,2*X(t,:),X(1+mod(((t-1):-1:(t-w))-1,t),:))]);
X = filter_fast(B,A,X); X = X(length(X):-1:1,:);
X = filter_fast(B,A,X); X = X(length(X):-1:1,:);
X([1:w t+w+(1:w)],:) = [];
if was_single
X = single(X); end
else
X = filtfilt(B,A,X);
end
% filter_fast from Christian's clean_artifacts code
function [X,Zf] = filter_fast(B,A,X,Zi,dim)
if nargin <= 4
dim = find(size(X)~=1,1);
end
if nargin <= 3
Zi = [];
end
lenx = size(X,dim);
lenb = length(B);
if lenx == 0
Zf = Zi;
elseif lenb < 256 || lenx<1024 || lenx <= lenb || lenx*lenb < 4000000 || ~isequal(A,1)
if nargout > 1
[X,Zf] = filter(B,A,X,Zi,dim);
else
X = filter(B,A,X,Zi,dim);
end
else
was_single = strcmp(class(X),'single');
if isempty(Zi)
if nargout < 2
X = unflip(oct_fftfilt(B,flip(double(X),dim)),dim);
else
X = flip(X,dim);
[dummy,Zf] = filter(B,1,X(end-length(B)+1:end,:),Zi,1); %#ok<ASGLU>
X = oct_fftfilt(B,double(X));
X = unflip(X,dim);
end
else
X = flip(X,dim);
tmp = filter(B,1,X(1:length(B),:),Zi,1);
if nargout > 1
[dummy,Zf] = filter(B,1,X(end-length(B)+1:end,:),Zi,1); %#ok<ASGLU>
end
X = oct_fftfilt(B,double(X));
X(1:length(B),:) = tmp;
X = unflip(X,dim);
end
if was_single
X = single(X);
end
end
function X = flip(X,dim)
if dim ~= 1
order = 1:ndims(X);
order = order([dim 1]);
X = permute(X,order);
end
function X = unflip(X,dim)
if dim ~= 1
order = 1:ndims(X);
order = order([dim 1]);
X = ipermute(X,order);
end
% Compute power spectral density (PSD)
function [pxx, f] = get_psd(eegData,winSize,taperM,overlap,nfft,Fs,fRange,type)
fh = str2func(taperM);
overlap = winSize/(100/overlap); % convert overlap to samples
for iChan = 1:size(eegData,1)
[pxx(iChan,:), f] = pwelch(eegData(iChan,:),fh(winSize),overlap,nfft,Fs,type);
end
freq = dsearchn(f,fRange(1)):dsearchn(f, fRange(2)); % extract frequencies of interest
f = f(freq(2:end))';
pxx = pxx(:,freq(2:end));
pxx = 10*log10(pxx); % normalize to deciBels (dB)