-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathfigure_TF_3way_paper.m
258 lines (244 loc) · 9.27 KB
/
figure_TF_3way_paper.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
function figure_TF_3way(varargin)
validPlotModes = {'channel','topo'};
p = inputParser;
addOptional(p,'plotMode','channel',@(x) ismember(x,validPlotModes));
parse(p,varargin{:})
plotMode = p.Results.plotMode;
expStage = 'final';
sourceDf = DEC_2_setupdir(expStage,'anal_eeg_group_tf');
saveDf = cd(sourceDf);
% Loading statistics
temp = load('stat_TF-pow-low_3way.mat');
stats = struct();
stats.low = temp.stats;
temp = load('stat_TF-pow-high_3way.mat');
stats.high = temp.stats;
% Loading TF data and plotting
conds = struct();
freqBands = {'low','high'};
for iFreq = 1:numel(freqBands)
% Loading the 16 conditions of the 2x2x2x2 design
listing = dir;
listing = regexp({listing.name}',...
['fteeg_TF-pow-',freqBands{iFreq},'_',...
'bis-r[hl]-d[hl]-[av]','_gravg.mat'],'match','once');
fNames = listing(~strcmp(listing,''));
listing = dir;
tokens = regexp({listing.name}',...
['fteeg_TF-pow-',freqBands{iFreq},'_',...
'bis-(r[hl]-d[hl]-[av])','_gravg.mat'],'tokens','once');
tokens = tokens(~cellfun(@isempty,tokens));
tokens = [tokens{:}]';
temp = struct2cell(cellfun(@load,fNames));
conds.(freqBands{iFreq}) = cat(2,strrep(tokens,'-','_'),temp');
% Main effects
matchStr = {...
'VR',{'rh','rl'};...
'Task',{'a','v'};...
'Disp',{'dh','dl'}};
c = cell(3,size(matchStr,1));
for i = 1:size(matchStr,1)
for j = 1:size(matchStr{i,2},2)
idx = ~cellfun(@isempty,regexp(conds.(freqBands{iFreq})(:,1),...
matchStr{i,2}{j},'match','once'));
c{j,i} = avgFTdata(conds.(freqBands{iFreq}){idx,2});
end
c{3,i} = c{1,i};
c{3,i}.powspctrm = c{2,i}.powspctrm-c{1,i}.powspctrm;
% Averaging over subjects
c(:,i) = cellfun(@(x) ft_freqdescriptives([],x),c(:,i),'UniformOutput',false);
% Baseline correcting main effect levels
cfg = struct();
cfg.baseline = [-0.4,-0.2];
c(1:2,i) = cellfun(@(x) ft_freqbaseline(cfg,x),c(1:2,i),'UniformOutput',false);
% Adding mask from stats
c{1,i}.mask = false(size(stats.(freqBands{iFreq}).(matchStr{i,1}).mask));
c{2,i}.mask = false(size(stats.(freqBands{iFreq}).(matchStr{i,1}).mask));
c{3,i}.mask = stats.(freqBands{iFreq}).(matchStr{i,1}).mask;
end
main.(freqBands{iFreq}) = array2table(c,'VariableNames',matchStr(:,1),...
'RowNames',{'A1','A2','eff'});
% 2-way interactions
matchStr = {...
'VR_X_Task',{'rh_.*_a','rh_.*_v','rl_.*_a','rl_.*_v'};...
'VR_X_Disp',{'rh_dh','rh_dl','rl_dh','rl_dl'};...
'Disp_X_Task',{'dh_a','dh_v','dl_a','dl_v'}};
c = cell(9,size(matchStr,1));
for i = 1:size(matchStr,1)
for j = 1:size(matchStr{i,2},2)
idx = ~cellfun(@isempty,regexp(conds.(freqBands{iFreq})(:,1),...
matchStr{i,2}{j},'match','once'));
c{j,i} = avgFTdata(conds.(freqBands{iFreq}){idx,2});
end
c{5,i} = c{1,i};
c{5,i}.powspctrm = c{3,i}.powspctrm-c{1,i}.powspctrm;
c{6,i} = c{2,i};
c{6,i}.powspctrm = c{4,i}.powspctrm-c{2,i}.powspctrm;
c{7,i} = c{1,i};
c{7,i}.powspctrm = c{2,i}.powspctrm-c{1,i}.powspctrm;
c{8,i} = c{3,i};
c{8,i}.powspctrm = c{4,i}.powspctrm-c{3,i}.powspctrm;
c{9,i} = c{1,i};
c{9,i}.powspctrm = (c{4,i}.powspctrm-c{3,i}.powspctrm) - ...
(c{2,i}.powspctrm-c{1,i}.powspctrm);
% Averaging over subjects
c(:,i) = cellfun(@(x) ft_freqdescriptives([],x),c(:,i),'UniformOutput',false);
% Adding mask from stats
for k = 1:size(c,1)-1
c{k,i}.mask = false(size(stats.(freqBands{iFreq}).(matchStr{i,1}).mask));
end
c{9,i}.mask = stats.(freqBands{iFreq}).(matchStr{i,1}).mask;
end
int2way.(freqBands{iFreq}) = array2table(c,'VariableNames',matchStr(:,1),...
'RowNames',{'A1B1','A1B2','A2B1','A2B2','AinB1','AinB2','BinA1',...
'BinA2','eff'});
end
cd(saveDf);
% % Plotting figures
if strcmp(plotMode,'channel')
chanClusters = {...
{'FC5','FC3','FC1','FC2','FC4','FC6','F5','F3','F1','Fz','F2','F4','F6'},...
{'P5','P3','P1','Pz','P2','P4','P6','CP5','CP3','CPz','CP1','CP2',...
'CP4','CP6','C5','C3','C1','Cz','C2','C4','C6'},...
{'O1','Oz','O2','PO7','PO3','POz','PO4','PO8'}};
chanClusters = {...
{'F1','F3','F5','FC1','FC3','FC5','F2','F4','F6','FC2','FC4','FC6'};...
{'C1','C3','C5','CP1','CP3','CP5','C2','C4','C6','CP2','CP4','CP6'};...
{'O1','PO3','PO7','P1','P3','P5','O2','PO4','PO8','P2','P4','P6'}}';
chanClusters = {{'Fz'},{'Cz'},{'Pz'},{'Oz'}};
clusterNames = {'Frontal','Central','Parietal','Occipital'};
% freqRanges = {[4,8],[8,12],[13,30]};
freqRanges = {[8,30]};
effs = {'VR','Task','Disp','VR_X_Task'};
figure();
for iEff = 1:numel(effs)
for i = 1:numel(freqRanges)
ax = {};
for j = 1:numel(chanClusters)
cfg = struct();
cfg.frequency = freqRanges{i};
cfg.channel = chanClusters{j}';
cfg.avgoverfreq = 'yes';
cfg.nanmean = 'yes';
temp = {};
if iEff < 4
for k = 1:3
temp{k} = ft_selectdata(cfg,main.low.(effs{iEff}){k});
end
else
rowsToPlot = {'AinB1','AinB2','eff'};
for k = 1:3
temp{k} = ft_selectdata(cfg,int2way.low.(effs{iEff}){rowsToPlot{k}});
end
end
% ax{j} = subplot(numel(effs),6,(6*(iEff-1))+(3*(i-1))+j);
ax{j} = subplot(numel(effs),numel(chanClusters),...
(numel(chanClusters)*(iEff-1))+j);
cfg = struct();
cfg.ylim = 'maxabs';
cfg.xlim = [-0.4,0.7];
cfg.baseline = 'no';
cfg.maskparameter = 'mask';
cfg.graphcolor = 'grb';
ft_singleplotER(cfg,temp{numel(temp):-1:1});
if i == 1 && j == 1
ylabel(strrep(effs{iEff},'_',' '));
end
if iEff > 1
title('');
end
if j > 1
set(gca,'YTick',[]);
end
end
ax = cat(2,ax{:});
for iAx = 1:size(ax,2)
setYlimits(ax);
end
end
end
else
% Clusterplots
% Flip the sign of statistics because the contrast is computed the other
% way around here
effs = {'VR','Task','Disp'};
for i = 1:numel(effs)
temp = stats.low.(effs{i}).stat;
stats.low.(effs{i}).stat = -temp;
end
effs = {'VR','Task','Task','VR_X_Task'};
freqRanges = {[8,30],[8,30],[8,30],[8,30]};
freqStr = {'alpha-beta','alpha-beta','alpha-beta','alpha-beta'};
latencies = {[0.2,0.4],[-0.2,0],[0.35,0.55],[0.4,0.6]};
figure();
colormap('redblue');
% colormap('parula');
plotOrder = [1,3,4,5];
ax = {};
for i = 1:numel(effs)
cfg = struct();
cfg.frequency = freqRanges{i};
cfg.latency = 'all';
cfg.avgoverfreq = 'yes';
cfg.latency = latencies{i};
cfg.avgovertime = 'yes';
cfg.nanmean = 'yes';
temp = ft_selectdata(cfg,stats.low.(effs{i}));
% Selecting statistics within the time window of interest. If a
% channel has a significant value somewhere within the time window,
% the channel will be significant.
cfg = struct();
cfg.frequency = freqRanges{i};
cfg.latency = latencies{i};
tempStat = ft_selectdata(cfg,stats.low.(effs{i}));
tempMask = any(any(tempStat.mask,3),2);
temp.mask = tempMask;
cfg = struct();
cfg.layout = 'acticap-64ch-standard2';
cfg.parameter = 'stat';
cfg.colorbar = 'no';
cfg.zlim = 'maxabs';
cfg.comment = 'no';
cfg.highlight = 'on';
cfg.highlightchannel = temp.label(temp.mask);
cfg.highlightsymbol = '*';
cfg.highlightsize = 4;
ax{i} = subplot(3,2,plotOrder(i));
ft_topoplotTFR(cfg,temp);
title(sprintf('%s band\n%d - %d ms',...
freqStr{i},...
latencies{i}(1)*1000,latencies{i}(2)*1000));
if mod(plotOrder(i),2) == 1
text(-1,0,strrep(effs{i},'_',' '),'Rotation',90,...
'HorizontalAlignment','center');
end
end
c = colorbar('EastOutside');
c.Label.String = 't-value';
% Setting color
ax = cat(2,ax{:});
for iAx = 1:size(ax,2)
setColorLimits(ax);
end
end
end
function ylim = setYlimits(ax,varargin)
if isempty(varargin)
temp = arrayfun(@(i) get(ax(i),'ylim'),1:numel(ax),'UniformOutput',false);
temp = cat(2,temp{:});
ylim = max(abs(min(temp)),abs(max(temp)));
else
ylim = varargin{1};
end
arrayfun(@(i) set(ax(i),'ylim',[-ylim,ylim]),1:numel(ax));
end
function clim = setColorLimits(ax,varargin)
if isempty(varargin)
temp = arrayfun(@(i) get(ax(i),'clim'),1:numel(ax),'UniformOutput',false);
temp = cat(2,temp{:});
clim = max(abs(min(temp)),abs(max(temp)));
else
clim = varargin{1};
end
arrayfun(@(i) set(ax(i),'clim',[-clim,clim]),1:numel(ax));
end