-
Notifications
You must be signed in to change notification settings - Fork 1
/
spm_eeg_grandmean_tw.m
executable file
·364 lines (304 loc) · 10.3 KB
/
spm_eeg_grandmean_tw.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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
function Do = spm_eeg_grandmean(S)
% average over multiple data sets
% FORMAT Do = spm_eeg_grandmean(S)
%
% S - struct (optional)
% fields of S:
% D - filenames (char matrix) of EEG mat-file containing epoched
% data
% weighted - average weighted by number of replications in inputs (1)
% or not (0).
% outfile - name of the output file (default - 'grand_mean')
%
% Output:
% Do - EEG data struct, result files are saved in the same
% directory as first input file.
%__________________________________________________________________________
%
% spm_eeg_grandmean averages data over multiple files. The data must have
% the same trialtype numbering and sampling rate. This function can be used
% for grand mean averaging, i.e. computing the average over multiple subjects.
% Missing event types and bad channels are taken into account properly.
% The output is written to a user-specified new file. The default name is
% the same name as the first selected input file, but prefixed with a 'g'.
% The output file is written to the current working directory.
%__________________________________________________________________________
% Copyright (C) 2008-2012 Wellcome Trust Centre for Neuroimaging
% Stefan Kiebel
% $Id: spm_eeg_grandmean.m 6625 2015-12-03 21:49:24Z vladimir $
SVNrev = '$Rev: 6625 $';
%-Startup
%--------------------------------------------------------------------------
spm('FnBanner', mfilename, SVNrev);
spm('FigName','M/EEG Grand Mean'); spm('Pointer','Watch');
if ~isfield(S, 'weighted'), S.weighted = 0; end
if ~isfield(S, 'outfile'), S.outfile = 'grand_mean'; end
%-Load MEEG data
%--------------------------------------------------------------------------
D = S.D;
if ischar(D)
F = cell(1,size(D,1));
try
for i = 1:size(D, 1)
F{i} = spm_eeg_load(deblank(D(i, :)));
end
D = F;
catch
error('Trouble reading files');
end
end
Nfiles = length(D);
%-Check dimension of the data files
%--------------------------------------------------------------------------
estr = [];
for i = 1:Nfiles
flist = [];
if ~strcmp(D{i}.type, 'evoked');
flist = [flist ' ' D{i}.fname];
end
if ~isempty(flist)
estr = ['The files' flist ' do not contain averaged (evoked) data.'];
end
end
% This applies to # of channels, # of samples, # of conditions, sampling
% rate, and # of frequencies (if time-frequency data).
nc = zeros(Nfiles,1);
ns = zeros(Nfiles,1);
fs = zeros(Nfiles,1);
ne = zeros(Nfiles,1);
if strncmp(D{1}.transformtype, 'TF',2)
nf = zeros(Nfiles,1);
end
megsens = [];
eegsens = [];
fid = [];
for i = 1:Nfiles
nc(i) = D{i}.nchannels;
ns(i) = D{i}.nsamples;
fs(i) = D{i}.fsample;
ne(i) = D{i}.nconditions;
if strncmp(D{1}.transformtype, 'TF',2)
nf(i) = D{i}.nfrequencies;
end
if ~isempty(D{i}.sensors('MEG')) || ~isempty(D{i}.sensors('EEG'))
D{i} = check(D{i}, '3d');
end
if ~isempty(D{i}.sensors('MEG'))
megsens = spm_cat_struct(megsens, D{i}.sensors('MEG'));
end
if ~isempty(D{i}.sensors('EEG'))
% tw: somehow SPM puts bad channels at the end, so we need to reorder them so we can have the same channel order across participants
E = struct(D{i}); %tw
[E.sensors.eeg.label,ind] = sort(D{i}.sensors('EEG').label); %tw
E.sensors.eeg.chanpos = D{i}.sensors('EEG').chanpos(ind,:); %tw
eegsens = spm_cat_struct(eegsens, E.sensors.eeg); %tw
end
if ~isempty(megsens) || ~isempty(eegsens)
fid = spm_cat_struct(fid, D{i}.fiducials);
end
end
unc = unique(nc);
if length(unc)~=1
ind = zeros(Nfiles,1);
fna = cell(length(unc),1);
for i=1:Nfiles
ind(i) = find(unc==nc(i));
fna{ind(i)} = [fna{ind(i)},'-',D{i}.fname,'\n'];
end
fprintf('\n')
for i=1:length(unc)
fprintf(['\nThose files have ',num2str(unc(i)),' channels:\n',...
fna{i}])
end
estr = [estr,'Data don''t have the same number of channels.\n'];
end
uns = unique(ns);
if length(uns)~=1
ind = zeros(Nfiles,1);
fna = cell(length(uns),1);
for i=1:Nfiles
ind(i) = find(uns==ns(i));
fna{ind(i)} = [fna{ind(i)},'-',D{i}.fname,'\n'];
end
fprintf('\n')
for i=1:length(uns)
fprintf(['\nThose files have ',num2str(uns(i)),' time points:\n',...
fna{i}])
end
estr = [estr,'Data don''t have the same number of time points.\n'];
end
ufs = unique(fs);
if length(ufs)~=1
ind = zeros(Nfiles,1);
fna = cell(length(ufs),1);
for i=1:Nfiles
ind(i) = find(ufs==fs(i));
fna{ind(i)} = [fna{ind(i)},'-',D{i}.fname,'\n'];
end
fprintf('\n')
for i=1:length(ufs)
fprintf(['\nThose files have a sampling rate of ',num2str(ufs(i)),' Hz:\n',...
fna{i}])
end
estr = [estr,'Data don''t have the same sampling rate.\n'];
end
une = unique(ne);
if length(une)~=1
ind = zeros(Nfiles,1);
fna = cell(length(une),1);
for i=1:Nfiles
ind(i) = find(une==ne(i));
fna{ind(i)} = [fna{ind(i)},'-',D{i}.fname,'\n'];
end
fprintf('\n')
for i=1:length(unc)
fprintf(['\nThose files have ',num2str(une(i)),' conditions:\n',...
fna{i}])
end
%estr = [estr,'Data don''t have the same number of conditions.\n'];
end
if strncmp(D{1}.transformtype, 'TF',2)
unf = unique(nf);
if length(unf)~=1
ind = zeros(Nfiles,1);
fna = cell(length(unf),1);
for i=1:Nfiles
ind(i) = find(unf==nf(i));
fna{ind(i)} = [fna{ind(i)},'-',D{i}.fname,'\n'];
end
fprintf('\n')
for i=1:length(unf)
fprintf(['\nThose files have ',num2str(unf(i)),' frequency bins:\n',...
fna{i}])
end
estr = [estr,'Data don''t have the same number of frequency bins.\n'];
end
end
% send message error (if any)
if ~isempty(estr)
error(estr)
return
else
fprintf('Ok: All data files have the same dimensions.\n')
end
%-Initialise output
%--------------------------------------------------------------------------
Do = D{1};
% how many different trial types and bad channels
types = {};
for i = 1:Nfiles
types = unique([types, D{i}.condlist]);
end
% The order of the conditions will be consistent with the first file
[sel1, sel2] = spm_match_str(D{1}.condlist, types);
sel2 = sel2(:)';
types = types([sel2, setdiff(1:length(types), sel2)]);
Ntypes = numel(types);
% how many repetitons per trial type
nrepl = zeros(Nfiles, Ntypes);
for i = 1:Nfiles
for j = 1:numel(types)
ind = D{i}.indtrial(types{j});
if ~isempty(ind)
nrepl(i, j) = D{i}.repl(ind);
end
end
end
if ~S.weighted
nrepl = ones(Nfiles, Ntypes);
end
% generate new meeg object with new filenames
if strncmp(D{1}.transformtype, 'TF',2)
Do = clone(Do, S.outfile, [Do.nchannels Do.nfrequencies Do.nsamples Ntypes]);
else
Do = clone(Do, S.outfile, [Do.nchannels Do.nsamples Ntypes]);
end
% for determining bad channels of the grandmean
w = zeros(Do.nchannels, Ntypes);
badchans = cellfun(@badchannels, D, 'UniformOutput', 0);
trialinds = cellfun(@(x) x.indtrial(types, 'GOOD'), D, 'UniformOutput', 0);
%-Do the averaging
%--------------------------------------------------------------------------
spm_progress_bar('Init', Ntypes, 'responses averaged');
if Ntypes > 100, Ibar = floor(linspace(1, Ntypes, 100));
else Ibar = [1:Ntypes]; end
if strncmp(D{1}.transformtype, 'TF',2)
for i = 1:Ntypes
d = zeros(D{1}.nchannels, D{1}.nfrequencies, D{1}.nsamples);
for j = 1:D{1}.nchannels
for k = 1:Nfiles
if ~any(badchans{k}==j)
ind = trialinds{k}(i);
if ~isempty(ind)
d(j, :, :) = d(j, :, :) + nrepl(k, i)*D{k}(j, :, :, ind);
w(j, i) = w(j, i) + nrepl(k, i);
end
end
end
if w(j, i) > 0
d(j, :, :) = d(j, :, :)/w(j, i);
end
end
Do(1:Do.nchannels, 1:Do.nfrequencies, 1:Do.nsamples, i) = d;
if ismember(i, Ibar), spm_progress_bar('Set', i); end
end
else
for i = 1:Ntypes
d = zeros(D{1}.nchannels, D{1}.nsamples);
for j = 1:D{1}.nchannels
for k = 1:Nfiles
if ~any(badchans{k}==j)
ind = trialinds{k}(i);
if ~isempty(ind)
d(j, :) = d(j, :) + nrepl(k, i)*D{k}(j, :, ind);
w(j, i) = w(j, i) + nrepl(k, i);
end
end
end
if w(j, i) > 0
d(j, :) = d(j, :)/w(j, i);
end
end
Do(1:Do.nchannels, 1:Do.nsamples, i) = d;
if ismember(i, Ibar), spm_progress_bar('Set', i); end
end
end
spm_progress_bar('Clear');
%-Average sensor locations
%--------------------------------------------------------------------------
Ntrials = sum(nrepl, 2);
if ~isempty(megsens)
spm_figure('GetWin','Graphics');clf;
if ~isempty(eegsens)
h = subplot(2, 1, 1);
aeegsens = ft_average_sens(eegsens, 'weights', Ntrials, 'feedback', h);
Do = sensors(Do, 'EEG', aeegsens);
h = subplot(2, 1, 2);
else
h = axes;
end
[amegsens,afid] = ft_average_sens(megsens, 'fiducials', fid, 'weights', Ntrials, 'feedback', h);
Do = sensors(Do, 'MEG', amegsens);
Do = fiducials(Do, afid);
elseif ~isempty(eegsens)
spm_figure('GetWin','Graphics');clf;
h = axes;
[aeegsens,afid] = ft_average_sens(eegsens, 'fiducials', fid, 'weights', Ntrials, 'feedback', h);
Do = sensors(Do, 'EEG', aeegsens);
Do = fiducials(Do, afid);
end
%-Save Grand Mean to disk
%--------------------------------------------------------------------------
Do = type(Do, 'evoked');
nrepl = sum(nrepl, 1);
Do = badchannels(Do, ':', ~any(w, 2));
Do = conditions(Do, ':', types);
Do = repl(Do, ':', nrepl);
Do = badtrials(Do, ':', 0);
Do = trialonset(Do, ':', []);
Do = trialtag(Do, ':', []);
Do = Do.history('spm_eeg_grandmean', S, 'reset');
save(Do);
%-Cleanup
%--------------------------------------------------------------------------
spm('FigName','M/EEG Grand Mean: done'); spm('Pointer','Arrow');