-
Notifications
You must be signed in to change notification settings - Fork 3
/
tfr_grandAverage.m
207 lines (168 loc) · 7.7 KB
/
tfr_grandAverage.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
function [] = tfr_grandAverage(whichIdx, whichSession, doERF, doTFR, doEvoked)
% select subsets of trials that we'll look at and compare,
if ~isdeployed,
addpath(genpath('~/code/MEG'));
addpath(genpath('~/code/Tools'));
addpath('~/Documents/fieldtrip');
ft_defaults; % ft_defaults should work in deployed app?
end
% set defaults
if ~exist('doERF', 'var'), doERF = 0;
end
if ~exist('doTFR', 'var'), doTFR = 1;
end
if ~exist('doEvoked', 'var'), doEvoked = 0;
end
if ~exist('whichIdx', 'var'), whichIdx = 1:31;
end
if ~exist('whichSession', 'var'), whichSession = [1 2];
end
% stopos
if ischar(whichIdx), whichIdx = str2double(whichIdx);
end
if ischar(whichSession), whichSession = str2double(whichSession);
end
if ischar(doERF), doERF = str2double(doERF);
end
if ischar(doTFR), doTFR = str2double(doTFR);
end
if ischar(doEvoked), doEvoked = str2double(doEvoked);
end
% define subjects
GAdata = subjectspecifics('GA');
groups = {'all', 'placebo', 'atomoxetine', 'donepezil'};
groups = {'all', 'repeaters', 'alternators'};
% only use subjects with enough MEG trials
groups = {'clean'};
locks = {'ref', 'stim', 'resp', 'fb'}; % for all the lockings
for g = 1:length(groups),
% find the list of those subjects
subjects = unique(GAdata.(groups{g}));
% ==================================================================
% ERFs
% ==================================================================
if doERF,
for session = whichSession,
for l = 1:length(locks),
for c = whichIdx,
[~, name] = getContrastIdx([], c);
cfg = []; cfg.inputfile = {};
% get data from all subjects
for sj = subjects,
subjectdata = subjectspecifics(sj);
ff = sprintf('%s/P%02d-S%d_bl_%s_%s.mat', ...
subjectdata.lockdir, sj, session, locks{l}, name);
if exist(ff, 'file'),
cfg.inputfile{end+1} = ff;
end
end
% grand average file
cfg.keepindividual = 'yes';
cfg.parameter = 'avg';
cfg.outputfile = sprintf('%s/GA%s-S%d_lockbl_%s_%s.mat', ...
GAdata.lockdir, groups{g}, session, locks{l}, name);
ft_timelockgrandaverage(cfg);
end
end
end
end
% ==================================================================
% TFRs - induced
% ==================================================================
if doTFR,
for session = whichSession,
for c = whichIdx,
[~, name] = getContrastIdx([], c);
for l = 1:length(locks),
cfg = [];
cfg.inputfile = {};
% get data from all subjects
for sj = subjects,
subjectdata = subjectspecifics(sj);
ff = sprintf('%s/P%02d-S%d_bl_%s_%s.mat', ...
subjectdata.tfrdir, sj, session, locks{l}, name);
if exist(ff, 'file'),
cfg.inputfile{end+1} = ff;
end
end
% grand average file - for plots
cfg.keepindividual = 'no';
cfg.parameter = 'powspctrm'; % can contain the avg or var, depends on filename
cfg.outputfile = sprintf('%s/GA%s-S%d_freqbl_%s_%s.mat', ...
GAdata.tfrdir, groups{g}, session, locks{l}, name);
ft_freqgrandaverage(cfg);
% also with all individuals, for stats
cfg.keepindividual = 'yes';
cfg.outputfile = sprintf('%s/GA%s-S%d_freqbl_%s_%s_allindividuals.mat', ...
GAdata.tfrdir, groups{g}, session, locks{l}, name);
ft_freqgrandaverage(cfg);
end
% % ALSO INCLUDE THE POWER BASELINE
% cfg = [];
% cfg.inputfile = {};
% for sj = subjects,
% subjectdata = subjectspecifics(sj);
% ff = sprintf('%s/P%02d-S%d_rawbaseline_%s.mat', ...
% subjectdata.tfrdir, sj, session, name);
% if exist(ff, 'file'),
% cfg.inputfile{end+1} = ff;
% end
% end
% cfg.keepindividual = 'yes';
% cfg.parameter = 'powspctrm'; % can contain the avg or var, depends on filename
% cfg.outputfile = sprintf('%s/GA%s-S%d_rawbaseline_%s_allindividuals.mat', ...
% GAdata.tfrdir, groups{g}, session, name);
% ft_freqgrandaverage(cfg);
% % AND NORMALIZED
% cfg.inputfile = {};
% for sj = subjects,
% subjectdata = subjectspecifics(sj);
% ff = sprintf('%s/P%02d-S%d_normalizedbaseline_%s.mat', ...
% subjectdata.tfrdir, sj, session, name);
% if exist(ff, 'file'),
% cfg.inputfile{end+1} = ff;
% end
% end
% cfg.outputfile = sprintf('%s/GA%s-S%d_normalizedbaseline_%s_allindividuals.mat', ...
% GAdata.tfrdir, groups{g}, session, name);
% ft_freqgrandaverage(cfg);
end
end
end
% ==================================================================
% TFRs - evoked
% ==================================================================
if doEvoked,
for session = whichSession,
for l = 1:2, % only during stimulus
for c = whichIdx,
[~, name] = getContrastIdx([], c);
cfg = [];
cfg.inputfile = {};
% get data from all subjects
for sj = subjects,
subjectdata = subjectspecifics(sj);
ff = sprintf('%s/P%02d-S%d_evoked_%s_%s.mat', ...
subjectdata.tfrdir, sj, session, locks{l}, name);
if exist(ff, 'file'),
cfg.inputfile{end+1} = ff;
end
end
% grand average file
cfg.keepindividual = 'no';
cfg.parameter = 'powspctrm'; % can contain the avg or var, depends on filename
cfg.outputfile = sprintf('%s/GA%s-S%d_evoked_%s_%s.mat', ...
GAdata.tfrdir, groups{g}, session, locks{l}, name);
ft_freqgrandaverage(cfg);
% also with all individuals, for stats
cfg.keepindividual = 'yes';
cfg.outputfile = sprintf('%s/GA%s-S%d_evoked_%s_%s_allindividuals.mat', ...
GAdata.tfrdir, groups{g}, session, locks{l}, name);
ft_freqgrandaverage(cfg);
end
end
end
end
end
disp('DONE');
end