-
Notifications
You must be signed in to change notification settings - Fork 2
/
procFinal.m
330 lines (291 loc) · 12.1 KB
/
procFinal.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
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
function procFinal(s, p)
% s = subject number; p = parameters struct
% This function is called by procFinal_wrapper.m, which contains its parameters etc.
% This performs final preprocessing using outputs from procPREP.m, procICAdipfit.m
% Kevin Tan - Jun 8, 2016
% Load logFile
logFile = fopen([int2str(s) '_' p.task '_log.txt'], 'w+');
fprintf(logFile, ['START: ' datestr(now) '\n \n']);
% % Load Psychtoolbox paradigm data
% load(p.PTB);
% if strcmp(p.task, 'jig')
% PTB = jigResults; %#ok<*NODEF>
% elseif strcmp(p.task, 'mem')
% PTB = memResults;
% end
% Load structs from ICA stream
amica = load(p.AMICA);
pd = load(p.p_decomp);
pd = pd.preprocstruct;
EEG.subject = s;
EEG.etc.preproc_decomp = pd;
EEG.etc.preproc = p;
try
dipfit = load(p.dipfit);
dipfit = dipfit.dipfit;
catch
end
%% Start of EEGlab
% Load EEGlab
tic;
eeglab;
pop_editoptions('option_storedisk', 0, 'option_savetwofiles', 1, 'option_saveversion6', 1,...
'option_single', 0, 'option_memmapdata', 0, 'option_eegobject', 0, 'option_computeica', 1,...
'option_scaleicarms', 1, 'option_rememberfolder', 1, 'option_donotusetoolboxes', 0,...
'option_checkversion', 1, 'option_chat', 0);
% Load PREP'd datset
EEG = pop_loadset('filename', p.prepFile);
disp(['Loading ' p.prepFile]);
fprintf(logFile, ['%.2f - Loading ' p.prepFile '\n'], toc);
% Get interpolated channels from PREP
interpCh = EEG.etc.noiseDetection.reference.interpolatedChannels.all;
[~, interpChNames] = eeg_decodechan(EEG.chanlocs, interpCh);
EEG.etc.PREPinterpCh = interpCh; EEG.etc.PREPinterpChNames = interpChNames;
disp('PREP interpolated channels:');
disp(interpChNames);
disp(interpCh);
fprintf(logFile, '%.2f - PREP interpolated channels: ', toc);
fprintf(logFile, '%s\t', interpChNames{:});
fprintf(logFile, '\n');
% Remove large 'etc.noiseDetection' field from PREP
EEG.etc = rmfield(EEG.etc, 'noiseDetection');
disp('Removed large EEG.etc.noiseDetection field from PREP pipeline');
fprintf(logFile, '%.2f - removed large EEG.etc.noiseDetection field from PREP pipeline \n', toc);
% Remove mastoids to reduce rank and align with ICA processing
EEG = pop_select(EEG, 'nochannel', {'M1', 'M2'});
chanlocs = EEG.chanlocs;
disp('Removed mastoids to reduce rank');
fprintf(logFile, '%.2f - Removed mastoids to reduce rank \n', toc);
% Correct channel indices
[~, exChNames] = eeg_decodechan(EEG.chanlocs, p.exCh(1):size(EEG.data, 1));
[ECGch, ~] = eeg_decodechan(EEG.chanlocs, 'ECG'); %#ok<ASGLU>
EEG.etc.preproc.exCh_ChRm = exChNames;
% High pass filter FIR1
EEG = pop_eegfiltnew(EEG, p.HP.freq, 0);
EEG.setname = [EEG.setname '_' p.HP.name];
fprintf(logFile, ['%.2f - Hi-passed ' p.HP.name ' FIR1 zero-order \n'], toc);
% Epoch no baseline correction
EEG = pop_epoch(EEG, p.epEvents, [p.epMin p.epMax], 'newname', [EEG.setname '_ep']);
disp(['Epoched from ' num2str(p.epMin) ' to ' num2str(p.epMax) ', no baseline correction']);
fprintf(logFile, ['%.2f - Epoched from ' num2str(p.epMin) ' to ' num2str(p.epMax)...
', no baseline correction \n'], toc);
% % Insert psychtoolbox trial data in epoch struct
% if s == 2 && strcmp(p.task, 'jig')
% epOffset = 1;
% else
% epOffset = 0;
% end
%
% epCheck = zeros(2, size(EEG.epoch, 2));
% for e = 1:size(EEG.epoch, 2)
% for fn = fieldnames(PTB)' % don't get rid of transpose!
% EEG.epoch(1, e).(fn{1}) = PTB(1, e+epOffset).(fn{1});
% end
% % Save bdf trigger and ptb stimtype for consistency check
% if iscell(EEG.epoch(1, e).eventlatency) % bdf event type
% eLock = find(cell2mat(EEG.epoch(1, e).eventlatency) == 0);
% epCheck(1, e) = EEG.epoch(1, e).eventtype{1, eLock}; %#ok<*FNDSB>
% else
% epCheck(1, e) = EEG.epoch(1, e).eventtype;
% end
% epCheck(2, e) = EEG.epoch(1, e).stimID;
% if isfield(PTB, 'jiggle') % stim ID
% if EEG.epoch(1, e).jiggle == 1;
% epCheck(2, e) = EEG.epoch(1, e).stimID + 100; % add 100 for jiggles
% end
% end
% clear eLock
% end
% epConsist = find(diff(epCheck, 1, 1)); % Consistency check
% EEG.etc.preproc.epCheck = epCheck; % Save to struct vv
% EEG.etc.preproc.epConsist = epConsist;
%
% if isempty(epConsist)
% disp('Imported Psychtoolbox trial data, BDF/PTB data consistent');
% fprintf(logFile, '%.2f - Imported Psychtoolbox trial data, BDF/PTB data consistent \n', toc);
% else
% disp(['Imported Psychtoolbox trial data, found ' num2str(length(epConsist)) ' inconsistent trials:']);
% disp(num2str(epConsist));
% fprintf(logFile, '%.2f - Imported Psychtoolbox trial data, Found %d inconsistent trials: \n', toc, length(epConsist));
% fprintf(logFile, ' %d', epConsist);
% fprintf(logFile, '\n');
% end
% pop_saveset(EEG, 'filename', [EEG.setname]); %save
% fprintf(logFile, ['Saved dataset: ' EEG.setname '\n \n']);
% Remove PREP-interpolated chans, to be interpolated from ICA'd and cleaned data
EEG = pop_select(EEG, 'nochannel', interpChNames);
disp('Removed PREP-interpolated channels');
fprintf(logFile, '%.2f - Removed PREP-interpolated channels \n', toc);
% Temporarily remove 1 scalp channel if no PCA
if isfield(pd, 'scalpRm')
scalpRm = pd.scalpRm;
EEG = pop_select(EEG, 'nochannel', scalpRm); % Remove chan
EEG.etc.preproc.scalpRm = scalpRm; % Save to EEG struct
disp('Removed 1 scalp channel for ICA');
disp(scalpRm);
fprintf(logFile, '%.2f - Removed 1 scalp channel for ICA: ', toc);
fprintf(logFile, '%s\n', scalpRm{:});
end
% Determine current # of chans
currentCh = size(EEG.data, 1);
disp(['Current # of channels: ' num2str(currentCh)]);
fprintf(logFile, ['%.2f - Current # of channels: ' num2str(currentCh) '\n'], toc);
% Generate ICA activations from ica matrices of ICA dataset
EEG.icaweights = amica.weights;
EEG.icasphere = amica.sphere;
EEG.etc.amica = amica.mods;
EEG = eeg_checkset(EEG, 'ica');
EEG.setname = [EEG.setname '_ICA']; %rename
disp(['Generated ' num2str(size(EEG.icaweights, 1)) ' IC activations using ICA matricies from ICA dataset']);
fprintf(logFile, ['%.2f - Generated ' num2str(size(EEG.icaweights, 1)) ' IC activations using ICA matricies from ICA dataset \n'], toc);
pop_saveset(EEG, 'filename', [EEG.setname]); %save
fprintf(logFile, ['Saved dataset: ' EEG.setname '\n \n']);
% Reject bad ICs
if isfield(pd, 'badICs') || (p.ICrej.hiRV.on == 1)
% Reject MARA/FASTER bad ICs
if isfield(pd, 'badICs')
if ~isempty(pd.badICs)
EEG = pop_subcomp(EEG, pd.badICs, 0);
disp('Rejected bad ICs');
fprintf(logFile, '%.2f - Rejected bad ICs \n', toc);
end
else
disp('Rejected no bad ICs');
fprintf(logFile, '%.2f - Rejected no bad ICs \n', toc);
end
% Reject high DIPFIT residual variance ICs
if p.ICrej.hiRV.on == 1
hiRV = find([dipfit.model.rv] >= p.ICrej.hiRV.rv);
if ~isempty(hiRV)
% Prepare for rejection
EEG.etc.preproc.ICrej.hiRV.ICs = hiRV;
remICs = setdiff(1:size(EEG.icaweights, 1), hiRV);
% Rejection
EEG = pop_subcomp(EEG, hiRV, 0);
disp('Rejected high RV ICs:');
disp(num2str(hiRV));
fprintf(logFile, '%.2f - Rejected %d high-RV ICs: ', toc, length(hiRV));
fprintf(logFile, ' %d', hiRV);
fprintf(logFile, '\n');
end
else
disp('Rejected no high-RV ICs');
fprintf(logFile, '%.2f - Rejected no high-RV ICs \n', toc);
end
EEG.setname = [EEG.setname '_ICrej'];
end
% ICs to use for epoch rejection
goodICs = find([dipfit.model(remICs).rv] <= p.ICrej.hiRV.rvRej);
EEG.etc.preproc.goodICs = goodICs;
% Interpolate removed channels
EEG = pop_interp(EEG, chanlocs, 'spherical');
disp('Re-interpolated removed channels');
fprintf(logFile, '%.2f - Re-interpolated removed channels \n', toc);
% Baseline correction
EEG = pop_rmbase(EEG, [p.bMin 0]);
EEG.setname = [EEG.setname '_BL']; %rename
disp(['Baseline corrected from: ' num2str(p.bMin) ' to 0']);
fprintf(logFile, ['%.2f - baseline corrected from: ' num2str(p.bMin) ' to 0 \n'], toc);
pop_saveset(EEG, 'filename', [EEG.setname]); %save
fprintf(logFile, ['Saved dataset: ' EEG.setname '\n \n']);
% Epoch rejection based on good ICA components
if p.epRej.on == 1
% Find epochs w/ extreme voltages
EEG = pop_eegthresh(EEG, 0, goodICs, p.epRej.uvMin, p.epRej.uvMax,...
EEG.xmin, EEG.xmax, 0, 0);
thresh = find(EEG.reject.icarejthresh);
EEG.etc.preproc.epRej.thresh = thresh;
fprintf(logFile, '%.2f - Found %d epochs with extreme voltages:',...
toc, length(thresh));
fprintf(logFile, ' %d', thresh);
fprintf(logFile, '\n');
% Find improbable epochs
[EEG, ~, ~, ~, ~] = pop_jointprob(EEG, 0, goodICs,...
p.epRej.probLoc, p.epRej.probGlb, 0, 0, 0);
prob = find(EEG.reject.icarejjp);
EEG.etc.preproc.epRej.prob = prob;
fprintf(logFile, '%.2f - Found %d improbable epochs:',...
toc, length(prob));
fprintf(logFile, ' %d', prob);
fprintf(logFile, '\n');
% Find epochs with high temporal kurtosis
EEG = pop_rejkurt(EEG, 0, goodICs, p.epRej.kurtLoc, p.epRej.kurtGlb, 0, 0, 0);
kurt = find(EEG.reject.icarejkurt);
EEG.etc.preproc.epRej.kurt = kurt;
fprintf(logFile, '%.2f - Found %d epochs with high kurtosis:',...
toc, length(kurt));
fprintf(logFile, ' %d', kurt);
fprintf(logFile, '\n');
% Find trials via muscle frequency range deviation
[EEG, ~] = pop_rejspec(EEG, 0, 'elecrange', goodICs,...
'freqlimits', p.epRej.fLim, 'threshold', p.epRej.fThresh,...
'method', 'fft', 'eegplotreject', 0);
freqrej = find(EEG.reject.icarejfreq);
EEG.etc.preproc.epRej.freq = freqrej;
fprintf(logFile, '%.2f - Found %d epochs with high muscle noise:',...
toc, length(freqrej));
fprintf(logFile, ' %d', freqrej);
fprintf(logFile, '\n');
% Concactenate bad epochs
EEG.etc.preproc.epRej.all = horzcat(EEG.etc.preproc.epRej.thresh,...
EEG.etc.preproc.epRej.prob, EEG.etc.preproc.epRej.kurt,...
EEG.etc.preproc.epRej.freq);
EEG.etc.preproc.epRej.all = unique(EEG.etc.preproc.epRej.all, 'sorted'); % Remove duplicates
% Reject bad epochs
EEG = pop_select(EEG, 'notrial', EEG.etc.preproc.epRej.all);
EEG.setname = [EEG.setname '_epRej']; %rename
fprintf(logFile, '%.2f - Removed %d bad epochs \n', toc, length(EEG.etc.preproc.epRej.all));
pop_saveset(EEG, 'filename', [EEG.setname]); %save
fprintf(logFile, ['Saved dataset: ' EEG.setname '\n \n']);
end
% Import IC dipole locations from ICA/DIPFIT dataset
if pd.dipfit == 1
EEG.dipfit = dipfit;
EEG.dipfit.model = dipfit.model(remICs);
EEG = eeg_checkset(EEG, 'ica');
disp('Imported IC dipole locations from ICA/DIPFIT dataset');
fprintf(logFile, '%.2f - Imported IC dipole locations from ICA/DIPFIT dataset \n', toc);
EEG.setname = [EEG.setname '_DIPFIT']; %rename
pop_saveset(EEG, 'filename', [EEG.setname]); %save
fprintf(logFile, ['Saved dataset: ' EEG.setname '\n \n']);
end
%% Separate into conditions
% Conditions need to be separated into their own .set files for EEGLAB's STUDY
% structure for group/statistical analysis. To do this you need to import
% Psychtoolbox data into the EEG.epochs structure
% % Make conds dir
% mkdir('conds');
% cd('conds');
% disp('Making condition datasets:');
% fprintf(logFile, '%.2f - Making condition datasets: \n', toc);
%
% load('/data2/tarrlab/kevinalt/Elissa_NEIL/indoorStim.mat');
% outdoorStim = find(~ismember(1:100, indoorStim));
%
% % Separate the conditions for eeglab STUDY structure
% for c = 1:2
%
% if c == 1
% cName = 'indoor';
% cStim = indoorStim;
% elseif c == 2
% cName = 'outdoor';
% cStim = outdoorStim;
% end
%
% condEps = zeros(size(EEG.epoch, 2));
% for e = 1:size(EEG.epoch, 2)
% if any(EEG.epoch(1, e).stimID == cStim)
% condEps(e) = 1;
% end
% end
% tempEEG = pop_select(EEG, 'trial', find(condEps));
% tempEEG.condition = cName;
% tempEEG.setname = [EEG.setname '_' cName];
% pop_saveset(tempEEG, 'filename', [tempEEG.setname]);
% fprintf(logFile, ['Saved dataset: ' tempEEG.setname '\n']);
% clear tempEEG e eLock condEps
% end
fprintf(logFile, '\n \n');
fprintf(logFile, ['END: ' datestr(now)]);
end