-
Notifications
You must be signed in to change notification settings - Fork 2
/
erf_osc_analysis_glm_erf_tfch_bugtest.m
executable file
·262 lines (231 loc) · 8.71 KB
/
erf_osc_analysis_glm_erf_tfch_bugtest.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
function erf_osc_analysis_analysis_erf_tfch_bugtest(subj, isPilot, freqRange, zeropoint, erfoi, doDSS)
% linear regression of peak amplitude over time-frequency (with fixed
% channel) or over frequency-channel (with fixed (avg) time).
if nargin<1
subj = 1;
end
if isempty(subj)
subj = 1;
end
if nargin<2
isPilot = false;
end
if isempty(isPilot);
isPilot = false;
end
if nargin<3
freqRange = 'high'; % can be 'high' or 'low'; depending on which frequency range you want to do regression (2-30 Hz or 28-100 Hz)
end
if isempty(freqRange)
freqRange = 'high';
end
if nargin<4
zeropoint = 'onset'; % other option 'reversal': redefine time axis to stimulus reversal or keep it at stimulus onset
end
if isempty(zeropoint);
zeropoint = 'onset';
end
if nargin<5 % erf of interest
erfoi = 'reversal'; % other option 'onset': redefine time axis to stimulus reversal or keep it at stimulus onset
end
if isempty(erfoi);
erfoi = 'reversal';
end
% Initiate Diary
ft_diary('on')
%% load data
erf_osc_datainfo;
if isPilot
load(sprintf('/project/3011085.02/results/erf/pilot-%03d/sub-%03d_dss.mat', subj, subj), 'data_dss');
load(sprintf('/project/3011085.02/results/freq/pilot-%03d/sub-%03d_gamma_virtual_channel.mat', subj, subj), 'gammaChan');
else
load(sprintf('/project/3011085.02/results/freq/sub-%03d/sub-%03d_tfa_%s.mat', subj,subj, zeropoint));
if strcmp(erfoi, 'reversal')
if doDSS
[data_dss, nComp_keep] = erf_osc_analysis_dss(subj,isPilot, 'reversal', false);
else
load(sprintf('/project/3011085.02/processed/sub-%03d/ses-meg01/sub-%03d_cleandata.mat', subj, subj));
end
else
if doDSS
[data_dss, nComp_keep] = erf_osc_analysis_dss(subj,isPilot, 'onset', false);
else
load(sprintf('/project/3011085.02/processed/sub-%03d/ses-meg01/sub-%03d_cleandata.mat', subj, subj));
end
end
end
%% if no data_dss cleaning is done beforehand, do this
if ~doDSS
data=dataClean;
cfg=[];
idxM = find(data.trialinfo(:,5)>0 & data.trialinfo(:,6)>0);
nTrials = length(idxM);
cfg=[];
cfg.trials = idxM;
cfg.channel = 'MEG';
data = ft_selectdata(cfg, data);
if strcmp(erfoi, 'reversal')
cfg=[];
cfg.offset = -(data.trialinfo(:,5)-data.trialinfo(:,4));
data = ft_redefinetrial(cfg, data);
end
data_dss=data;
end
fs=data_dss.fsample;
nTrials = length(data_dss.trial);
%% select p1 window for regression
cfg=[];
cfg.vartrllength=2;
tlck = ft_timelockanalysis(cfg, data_dss);
t1p1 = nearest(tlck.time, 0.06);
t2p1 = nearest(tlck.time, 0.12);
cfg=[];
% cfg.channel = {'MRO', 'MRP', 'MLO', 'MRO', 'MZO', 'MZP'};
cfg.latency = [tlck.time(t1p1) tlck.time(t2p1)];
tlck = ft_selectdata(cfg, tlck);
time = tlck.time;
[~, maxchan] = max(abs(mean(tlck.avg,2))); % find channel with max amplitude
% calculate mean over every window to find out which window has the maximum
% mean amplitude (for the maximum channel!). Take the mean amplitude in
% this latency window as regression weight.
maxchanid = tlck.label(maxchan);
halfwindowlength = 8;
i=1;
for t = halfwindowlength+1:length(time)-halfwindowlength;
win(i,:) = [time(t-8), time(t+8)];
avg(i,1) = mean(tlck.avg(maxchan,t-8:t+8),2);
i=i+1;
end
[~, window] = max(abs(avg));
lat = win(window,:);
%% Regression p1 amplitude over time-frequency-channel
cfg=[];
cfg.latency = [-1.5 0.65];
data_dss = ft_selectdata(cfg, data_dss);
trialdata = cat(3,data_dss.trial{:});
idxtime = [nearest(data_dss.time{1}, lat(1)) : nearest(data_dss.time{1}, lat(2))];
p1amp = squeeze(mean(trialdata(:,idxtime,:),2));
% baselinecorrect with average baseline over trials
if strcmp(freqRange, 'high');
if strcmp(zeropoint, 'onset')
cfg=[];
cfg.latency = [-1 1.75]; % shortest baseline window is 1 second
tfaHigh = ft_selectdata(cfg, tfaHigh);
else
cfg=[];
cfg.latency = [-1.5 0.5];
tfaHigh = ft_selectdata(cfg, tfaHigh);
end
load(sprintf('/project/3011085.02/results/freq/sub-%03d/sub-%03d_tfa_onset.mat', subj, subj), 'baselineH');
baselineH.time = tfaHigh.time;
baselineH.dimord = tfaHigh.dimord;
baselineH.powspctrm = repmat(baselineH.powspctrm, [1,1,length(baselineH.time), size(tfaHigh.powspctrm, 1)]);
baselineH.powspctrm = permute(baselineH.powspctrm, [4, 1, 2, 3]);
cfg=[];
cfg.parameter = 'powspctrm';
cfg.operation = 'subtract';
tfaHigh = ft_math(cfg, tfaHigh, baselineH);
for freq=1:19
for ch=1:length(data_dss.label);
design = [ones(size(p1amp(ch,:))); p1amp(ch,:)];
design(2,:) = (design(2,:)-mean(design(2,:)))./std(design(2,:));
Y_h = squeeze(squeeze(tfaHigh.powspctrm(:,ch,freq,:)));
betas_h(freq,ch,:,:) = design'\Y_h;
end
end
elseif strcmp(freqRange, 'low')
if strcmp(zeropoint, 'onset')
cfg=[];
cfg.latency = [-1 1.75]; % shortest baseline window is 1 second
tfaLow = ft_selectdata(cfg, tfaLow);
end
for freq=1:15
for ch=1:length(data_dss.label);
design = [ones(size(p1amp(ch,:))); p1amp(ch,:)];
design(2,:) = (design(2,:)-mean(design(2,:)))./std(design(2,:));
Y_l = squeeze(squeeze(tfaLow.powspctrm(:,ch,freq,:)));
betas_l(freq,ch,:,:) = design'\Y_l;
end
end
end
%% planar gradiant transformation of beta weights
% make timelocked structure where planar gradient transformation can be
% applied to (that's why dimord is strange)
if strcmp(freqRange, 'high')
tlh=[];
tlh.avg = squeeze(betas_h(:,:,2,:));
tlh.time = tfaHigh.time;
tlh.dimord = 'subj_chan_time';
tlh.label = tfaHigh.label;
tlh.grad = tfaHigh.grad;
% also put betas in a time-freq structure
tfh = rmfield(tlh,'avg');
tfh.dimord = 'chan_freq_time';
tfh.powspctrm = permute(tlh.avg, [2,1,3]);
tfh.freq = tfaHigh.freq;
% planar combination
cfg = [];
cfg.feedback = 'no';
cfg.method = 'template';
cfg.neighbours = ft_prepare_neighbours(cfg, tlh);
cfg.planarmethod = 'sincos';
tlhPlanar = ft_megplanar(cfg, tlh);
cfg = [];
bhPlanarCmb = ft_combineplanar(cfg,tlhPlanar);
% put it back in a freq-data structure
bhPlanarCmb.powspctrm = permute(bhPlanarCmb.trial, [2,1,3]);
bhPlanarCmb = rmfield(bhPlanarCmb, 'trial');
bhPlanarCmb.freq = tfaHigh.freq;
bhPlanarCmb.dimord = 'chan_freq_time';
elseif strcmp(freqRange, 'low')
% Do the same for low frequencies.
tll=[];
tll.avg = betas_l;
tll.time = tfaLow.time;
tll.dimord = 'subj_chan_time';
tll.label = tfaLow.label;
tll.grad = tfaLow.grad;
% also put betas in a time-freq structure
tfl = rmfield(tll,'avg');
tfl.dimord = 'chan_freq_time';
tfl.powspctrm = permute(tll.avg, [2,1,3]);
tfl.freq = tfaLow.freq;
% planar combination
cfg = [];
cfg.feedback = 'no';
cfg.method = 'template';
cfg.neighbours = ft_prepare_neighbours(cfg, tll);
cfg.planarmethod = 'sincos';
tllPlanar = ft_megplanar(cfg, tll);
cfg = [];
blPlanarCmb = ft_combineplanar(cfg,tllPlanar);
% put it back in a freq-data structure
blPlanarCmb.powspctrm = permute(blPlanarCmb.trial, [2,1,3]);
blPlanarCmb = rmfield(blPlanarCmb, 'trial');
blPlanarCmb.freq = tfaLow.freq;
blPlanarCmb.dimord = 'chan_freq_time';
end
t5 = nearest(bhPlanarCmb.time, -0.6);
t6 = nearest(bhPlanarCmb.time, -0.1);
muH = rmfield(bhPlanarCmb, 'powspctrm');
muH.powspctrm = nanmean(bhPlanarCmb.powspctrm(:,:,t5:t6), 3);
muH.powspctrm = repmat(muH.powspctrm, [1, 1, length(bhPlanarCmb.time)]);
sigmaH = rmfield(bhPlanarCmb, 'powspctrm');
sigmaH.powspctrm = nanstd(bhPlanarCmb.powspctrm(:,:,t5:t6),[],3);
sigmaH.powspctrm = repmat(sigmaH.powspctrm, [1,1, length(bhPlanarCmb.time)]);
cfg=[];
cfg.parameter = 'powspctrm';
cfg.operation = '(x1-x2)./x3';
bhPlanarCmbZ = ft_math(cfg, bhPlanarCmb, muH, sigmaH);
%% Save
if isPilot
filename = sprintf('/project/3011085.02/results/erf/pilot-%03d/sub-%03d_analysis_tf_%s_%s_erf_%s_bugtest', subj, subj,subj, freqRange, zeropoint, erfoi);
else
filename = sprintf('/project/3011085.02/results/erf/sub-%03d/sub-%03d_analysis_tf_%s_%s_erf_%s_bugtest', subj, freqRange, zeropoint, erfoi);
end
if strcmp(freqRange, 'high')
save(fullfile([filename '.mat']), 'betas_h','bhPlanarCmb','bhPlanarCmbZ','tfh','lat','maxchanid', '-v7.3');
elseif strcmp(freqRange, 'low')
save(fullfile([filename '.mat']), 'betas_l','bhPlanarCmb','tfl', 'lat','maxchanid', '-v7.3');
end
ft_diary('off')