-
Notifications
You must be signed in to change notification settings - Fork 2
/
erf_osc_analysis_glm_erf_tfch_tmp.m
executable file
·223 lines (196 loc) · 7.21 KB
/
erf_osc_analysis_glm_erf_tfch_tmp.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
function erf_osc_analysis_glm_erf_tfch_tmp(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 || isempty(subj)
subj = 1;
end
if nargin<2 || isempty(isPilot)
isPilot = false;
end
if nargin<3 || isempty(freqRange)
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 nargin<4 || isempty(zeropoint)
zeropoint = 'onset'; % other option 'reversal': redefine time axis to stimulus reversal or keep it at stimulus onset
end
if nargin<5 || isempty(erfoi) % erf of interest
erfoi = 'reversal'; % other option 'onset': redefine time axis to stimulus reversal or keep it at stimulus onset
end
if nargin<6 || isempty(doDSS)
doDSS=false;
end
% Initiate Diary
ft_diary('on')
%% load data
erf_osc_datainfo;
if isPilot
load(sprintf('/project/3011085.02/analysis/erf/pilot-%03d/dss.mat', subj), 'data_dss');
%load(sprintf('/project/3011085.02/analysis/freq/pilot-%03d/gamma_virtual_channel.mat', subj), 'gammaChan');
else
%load(sprintf('/project/3011085.02/analysis/freq/sub-%03d/tfa_%s.mat', subj, zeropoint));
if strcmp(erfoi, 'reversal')
if doDSS
[data, 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, 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;
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
clear dataClean;
else
data=data_dss;
clear data_dss;
end
fs=data.fsample;
nTrials = length(data.trial);
%% select p1 amplitude for regression
cfg=[];
cfg.vartrllength=2;
tlck = ft_timelockanalysis(cfg, data);
allchans = tlck.label;
t1p1 = nearest(tlck.time, 0.06);
t2p1 = nearest(tlck.time, 0.12);
cfg=[];
cfg.channel = {'MRO', 'MRP', 'MLO', 'MLP', '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);
maxchanidx = find(strcmp(maxchanid, data.label));
halfwindowlength = 8;
i=1;
for ntrl = halfwindowlength+1:length(time)-halfwindowlength
win(i,:) = [time(ntrl-8), time(ntrl+8)];
avg(i,1) = mean(tlck.avg(maxchan,ntrl-8:ntrl+8),2);
i=i+1;
end
[~, window] = max(abs(avg));
lat = win(window,:);
cfg=[];
cfg.latency = lat;
cfg.avgovertime = 'yes';
cfg.channel = {'MRO', 'MRP', 'MLO', 'MLP', 'MZO', 'MZP'};
data = ft_selectdata(cfg, data);
p1amp_all = cat(2,data.trial{:}); % posterior channels only
p1amp_avgtrl = mean(p1amp_all,2); % average p1 amp over trials
multiplier = sign(p1amp_avgtrl); % get the sign of the average
% find a few representative posterior channels that have highest absolute mean amplitude over trials
[~, idx] = sort(multiplier.*p1amp_avgtrl, 'descend');
num_p1chans = 4;
p1chans = idx(1:num_p1chans);
p1chans_id = data.label(p1chans);
% representative p1 amplitude is the mean of the *absolute* value of
% representative channels. (Note that absolute here means sign-flipped if
% a channel's average < 0);
p1amp = multiplier(p1chans)'*p1amp_all(p1chans,:);
%% Regression p1 amplitude over time-frequency-channel
% load TFA data
if isPilot
load(sprintf('/project/3011085.02/analysis/freq/pilot-%03d/sub-%03d_gamma_virtual_channel.mat', subj, subj), 'gammaChan');
else
load(sprintf('/project/3011085.02/analysis/freq/sub-%03d/sub-%03d_tfa_%s.mat', subj, subj, zeropoint));
end
if strcmp(freqRange, 'high')
tfa = tfaHigh;
clear tfaHigh tfaLow;
elseif strcmp(freqRange, 'low')
tfa = tfaLow;
clear tfaLow tfaHigh;
else
error('freqRange should be specified as *low* (2-30Hz) or *high* (28-100 Hz)')
keyboard
end
% select active window of TFA
if strcmp(zeropoint, 'onset')
cfg=[];
cfg.latency = [-1 1]; % shortest baseline window is 1 second, shortest reversal 1 second as well
tfa = ft_selectdata(cfg, tfa);
else
cfg=[];
cfg.latency = [-1 0];
tfa = ft_selectdata(cfg, tfa);
end
nchan = size(tfa.powspctrm, 2);
nfreq = size(tfa.powspctrm, 3);
ntime = size(tfa.powspctrm, 4);
ntrl = size(p1amp,2);
design = [ones(size(p1amp)); ((p1amp-mean(p1amp))./std(p1amp))];
for freq=1:nfreq
for ch=1:nchan
Y_h = squeeze(squeeze(tfa.powspctrm(:,ch,freq,:)));
betas(freq,ch,:,:) = design'\Y_h;
end
end
numShuffles=100;
all_shuffles = zeros(nchan, nfreq, ntime, numShuffles);
% prepare a data object and cfg for the planar transform
tl=[];
tl.avg = squeeze(betas(:,:,2,:));
tl.time = tfa.time;
tl.dimord = 'rpt_chan_time';
tl.label = tfa.label;
tl.grad = tfa.grad;
cfg = [];
cfg.feedback = 'no';
cfg.method = 'template';
cfg.neighbours = ft_prepare_neighbours(cfg, tl);
cfg.planarmethod = 'sincos';
tlPlCmb = ft_combineplanar([], ft_megplanar(cfg, tl));
for iShuffle = 1:numShuffles
shufvec = randperm(ntrl);
betas_shuf = zeros(nchan, nfreq*ntime); % store betas for this shuffle across channels
for ch = 1:nchan
design_shuf = design;
design_shuf = design_shuf(:,shufvec);
Y1 = reshape(tfa.powspctrm(:,ch,:,:),[],nfreq*ntime);
tmp = design_shuf'\Y1;
betas_shuf(ch,:) = tmp(2,:);
end
tl.time = 1:nfreq*ntime;
tl.avg = betas_shuf;
tmp = ft_combineplanar([], ft_megplanar(cfg, tl));
betas_shuf = tmp.avg;
all_shuffles(:,:,:,iShuffle) = reshape(betas_shuf, [nchan nfreq ntime]);
end
avg_shuffles = mean(all_shuffles,4);
std_shuffles = std(all_shuffles,[],4);
%% Put betas and shuffles in freq-structure
betasPlCmb = rmfield(tfa, 'powspctrm');
betasPlCmb.powspctrm = permute(tlPlCmb.trial, [2,1,3]);
betasPlCmb.dimord = 'chan_freq_time';
shufflesAvgPlCmb = rmfield(betasPlCmb, {'powspctrm', 'cfg'});
shufflesAvgPlCmb.powspctrm = avg_shuffles;
shufflesStdPlCmb = rmfield(betasPlCmb, {'powspctrm', 'cfg'});
shufflesStdPlCmb.powspctrm = std_shuffles;
%% Save
if isPilot
filename = sprintf('/project/3011085.02/analysis/GLM/pilot-%03d/sub-%03d_glm_tf_%s_%s_erf_%s', subj, subj, freqRange, zeropoint, erfoi);
else
filename = sprintf('/project/3011085.02/analysis/GLM/sub-%03d/sub-%03d_glm_tf_%s_%s_erf_%s', subj, subj, freqRange, zeropoint, erfoi);
end
save(fullfile([filename '.mat']), 'betas','betasPlCmb', 'all_shuffles', 'shufflesAvgPlCmb', 'shufflesStdPlCmb', 'lat', 'p1amp', 'maxchanid','p1chans_id', '-v7.3');
ft_diary('off')