/
spm_uomeeg_twin_avg.m
318 lines (257 loc) · 9.2 KB
/
spm_uomeeg_twin_avg.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
function [twtable,twdatacell,twcolnames] = spm_uomeeg_twin_avg(S)
% Extract time- (and/or frequency-) window averaged data from an
% SPM12-format data file. Output is a table-style cell array with column
% headers, row labels (subject index and filename), and data with
% participants as rows and time-window x channel x condition as columns.
% Optionally, a cell array of 1 cell per participant (chans x twins x conds)
% can also be output.
%
% Input:
% S struct with (optional) subfields:
% D - filename (.mat) of SPM12 M/EEG object
% OR cell array of filenames
% OR leave blank to be prompted
% chans - cell array of channel names (in D.chanlabels)
% fwins - cell array of frequency borders
% twins - cell array of start/end times
% conds - cell array of condition labels (in D.condlist)
% outfname - filename for output (tab-delimited *.txt) - uses
% present working directory if no path given.
%
% Output:
% twtable - table of data, repeated-measures style
% twdatacell - cell array {#files} of time-window averaged data
% each cell is (chans (x twins) (x fwins) x conds)
% twcolnames - cell array of column names
%
% jt (11/09/2020)
%
% Examples
%
%Single file (e.g., grandaverage):
% S = [];
% S.D = 'my_grandaveraged_data_tf.mat';
% S.chans = {'Fz','Cz','Pz'};
% S.fwins = {[3 7],[8 12]};
% S.twins = {[140 200],[300 600]}; % (ms) cell array of start/end times
% S.conds = {'cond1','cond2'};
% S.outfname = 'my_twdata.txt';
%
% [twtable,twdatacell,twcolnames] = spm_uomeeg_twin_avg(S);
%
%Multiple files (e.g., individual participants):
% S = [];
% S.D = {
% 'p1/mrtfeffdspmeeg_p1.mat'
% 'p2/mrtfeffdspmeeg_p2.mat'
% 'p3/mrtfeffdspmeeg_p3.mat'
% 'p4/mrtfeffdspmeeg_p4.mat'
% 'p5/mrtfeffdspmeeg_p5.mat'
% };
% S.chans = {'Fz','Cz','Pz'};
% S.fwins = {[3 7],[8 12]};
% S.twins = {[140 200],[300 600]}; % (ms) cell array of start/end times
% S.conds = {'cond1', 'cond2'};
% S.outfname = 'my_group_twdata.txt';
%
% [twtable,twdatacell,twcolnames] = spm_uomeeg_twin_avg(S);
%% Get params
try fnames = S.D; catch [fnames,dirs] = spm_select(Inf,'mat','Select SPM12 MEEG files',{},pwd); end
if isstr(fnames)
for i=1:size(fnames,1)
D{i} = spm_eeg_load(fnames(i,:));
end
elseif iscell(fnames)
for i=1:size(fnames)
D{i} = spm_eeg_load(fnames{i});
end
end
try conds = S.conds; catch, conds = []; end
try fwins = S.fwins; catch, fwins = []; end
try twins = S.twins; catch, twins = []; end
try chans = S.chans; catch, chans = []; end
try outfname = S.outfname; catch, outfname = []; end
if isempty(conds)
conds = D{1}.condlist';
end
if isempty(fwins)
switch D{1}.transformtype
case {'TF'}
fwins = {D{1}.frequency([1 end])};
end
elseif ~iscell(fwins)
fwins = {fwins};
end
if isempty(twins)
switch D{1}.transformtype
case {'time'}
twins = {D{1}.time([1 end])*1000};
case {'TF'}
if length(D{1}.time)>1
twins = {D{1}.time([1 end])*1000};
else % spectra:
twins = {[]};
end
otherwise
twins = {[]};
end
elseif ~iscell(twins)
twins = {twins};
end
if isempty(chans)
chans = D{1}.chanlabels;
elseif ~iscell(chans)
chans = {chans};
end
%% Load data
cwd = pwd;
for i=1:length(D)
switch D{1}.transformtype
case 'time'
%if i==1
twdatacell{i} = zeros([length(chans) length(twins) length(conds)]);
%end
for tw=1:length(twins)
twin=twins{tw};
d = selectdata(D{i},chans,twin/1000,conds);
md = mean(d,2);
twdatacell{i}(:,tw,:) = md;
end
case 'TF'
if length(D{1}.time)>1 % TF
%if i==1
twdatacell{i} = zeros([length(chans) length(fwins) length(twins) length(conds)]);
%end
for tw=1:length(twins)
twin=twins{tw};
for fq=1:length(fwins)
fwin=fwins{fq};
d = selectdata(D{i},chans,fwin,twin/1000,conds);
md = mean(mean(d,3),2);
twdatacell{i}(:,fq,tw,:) = md;
end
end
else % spectra
%if i==1
twdatacell{i} = zeros([length(chans) length(fwins) length(conds)]);
%end
for fq=1:length(fwins)
fwin=fwins{fq};
d = selectdata(D{i},chans,fwin,[],conds);
md = mean(d,2);
twdatacell{i}(:,fq,:) = md;
end
end
end
end
%% Format table
twtabledata = [];
twcolnames = [];
subjnums = [];
filenames = [];
twcolnames{1}='subjectIndex';
twcolnames{2}='fileName';
switch D{1}.transformtype
case 'time'
for i=1:length(twdatacell)
j=0;
for tw=1:length(twins)
twin=twins{tw};
for ch=1:length(chans)
chan=chans{ch};
for co=1:length(conds)
cond=conds{co};
j=j+1;
twtabledata(i,j) = twdatacell{i}(ch,tw,co);
if i==1
twcolnames{j+2} = sprintf('%s_%g-%gms_%s',chan,twin,cond);
end
if j==1
subjnums(i) = i;
filenames{i} = fullfile(D{i}.path,D{i}.fname);
end
end
end
end
end
case 'TF'
if length(D{1}.time)>1
for i=1:length(twdatacell)
j=0;
for fw=1:length(fwins)
fwin=fwins{fw};
for tw=1:length(twins)
twin=twins{tw};
for ch=1:length(chans)
chan=chans{ch};
for co=1:length(conds)
cond=conds{co};
j=j+1;
twtabledata(i,j) = twdatacell{i}(ch,fw,tw,co);
if i==1
twcolnames{j+2} = sprintf('%s_%g-%gHz_%g-%gms_%s',chan,fwin,twin,cond);
end
if j==1
subjnums(i) = i;
filenames{i} = fullfile(D{i}.path,D{i}.fname);
end
end
end
end
end
end
else % spectra
for i=1:length(twdatacell)
j=0;
for fw=1:length(fwins)
fwin=fwins{fw};
for ch=1:length(chans)
chan=chans{ch};
for co=1:length(conds)
cond=conds{co};
j=j+1;
twtabledata(i,j) = twdatacell{i}(ch,fw,co);
if i==1
twcolnames{j+2} = sprintf('%s_%g-%gHz_%s',chan,fwin,cond);
end
if j==1
subjnums(i) = i;
filenames{i} = fullfile(D{i}.path,D{i}.fname);
end
end
end
end
end
end
end
% Format for output:
twtable = [twcolnames; [num2cell(subjnums') filenames' num2cell(twtabledata)]];
%% Write to file:
if ~isempty(outfname)
[p,f] = fileparts(outfname);
if isempty(p)
outfname = fullfile(cwd,outfname);
end
fid = fopen(outfname,'w');
for j=1:size(twtable,2)
fprintf(fid,'%s',twtable{1,j});
if j<size(twtable,2)
fprintf(fid,'\t');
else
fprintf(fid,'\n');
end
end
for i=2:size(twtable,1)
fprintf(fid,'%d\t%s\t',twtable{i,1:2});
for j=3:size(twtable,2)
fprintf(fid,'%g',twtable{i,j});
if j<size(twtable,2)
fprintf(fid,'\t');
else
fprintf(fid,'\n');
end
end
end
fclose(fid);
fprintf(1,'Time-window data written to %s\n',outfname);
end