-
Notifications
You must be signed in to change notification settings - Fork 2
/
erf_osc_analysis_glm_gamma_time.m
executable file
·174 lines (150 loc) · 5.23 KB
/
erf_osc_analysis_glm_gamma_time.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
function erf_osc_analysis_glm_gamma_time(subj, isPilot, erfoi, doDSS)
% do a linear regression of pre-change gamma power over time.
if nargin<1 || isempty(subj)
subj = 1;
end
if nargin<2 || isempty(isPilot)
isPilot = false;
end
if nargin<3 || isempty(erfoi)
erfoi = 'reversal'; % can be *onset*, *reversal*, *motor*
end
if nargin<4 || 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/sub-%03d_dss.mat', subj, subj), 'data_dss');
load(sprintf('/project/3011085.02/analysis/freq/pilot-%03d/sub-%03d_gamma_virtual_channel.mat', subj, subj), 'gammaPow');
else
load(sprintf('/project/3011085.02/analysis/freq/sub-%03d/sub-%03d_gamma_virtual_channel.mat', subj, subj), 'gammaPow');
% load(sprintf('/project/3011085.02/analysis/eye/sub%03d.mat', subj), 'X', 'Y'); %FIXME get eyedata from cleandata
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));
data = dataClean;
clear dataClean
end
end
fs=data.fsample;
if ~doDSS
idxM = find(data.trialinfo(:,5)>0 & data.trialinfo(:,6)>0 & data.trialinfo(:,6)>data.trialinfo(:,5));
nTrials = length(idxM);
cfg=[];
cfg.trials = idxM;
cfg.channel = 'MEG';
data = ft_selectdata(cfg, data);
% find out which trials have response after end of trial, so you can
% exclude them
cfg=[];
cfg.offset = -(data.trialinfo(:,5)-data.trialinfo(:,4));
data_reversal_tmp = ft_redefinetrial(cfg, data);
for iTrial=1:nTrials
trlLatency(iTrial) = data_reversal_tmp.time{iTrial}(end);
end
idx_trials = find(trlLatency'>((data.trialinfo(:,6)-data.trialinfo(:,5))/1200));
idx_trials_invalid = find(trlLatency'<((data.trialinfo(:,6)-data.trialinfo(:,5))/1200));
cfg=[];
cfg.trials = idx_trials;
cfg.channel = 'MEG';
data = ft_selectdata(cfg, data);
data_orig = data;
if strcmp(erfoi, 'reversal')
cfg=[];
cfg.offset = -(data.trialinfo(:,5)-data.trialinfo(:,4));
data=ft_redefinetrial(cfg, data);
elseif strcmp(erfoi, 'motor')
cfg=[];
cfg.offset = -(data.trialinfo(:,6)-data.trialinfo(:,4));
data=ft_redefinetrial(cfg, data);
end
clear data_reversal_tmp trlLatency
end
nTrials = length(data.trial);
[~, idxMax] = sort(gammaPow, 2, 'descend');
%% GLM on all trials
% first select data epochs. Filtering the data might introduce past data to
% future when using a forward filter). Therefore apply reverse filter.
% filter before data-cutting to avoid edge-effects.
cfg=[];
cfg.lpfilter = 'yes';
cfg.lpfilttype = 'firws';
cfg.lpfreq = 30;
cfg.lpfiltdir = 'onepass-reverse-zerophase';
cfg.preproc.demean = 'yes';
cfg.preproc.baselinewindow = [-0.1 0];
data = ft_preprocessing(cfg, data);
% now cut out the segment of interest.
cfg=[];
if strcmp(erfoi, 'motor')
cfg.lpfilter = 'yes';
cfg.lpfilttype = 'firws';
cfg.lpfreq = 30;
cfg.lpfiltdir = 'onepass-reverse-zerophase';
cfg.preproc.demean = 'yes';
cfg.preproc.baselinewindow = [-0.1 0];
data = ft_preprocessing(cfg, data);
cfg.lpfilter = 'yes';
cfg.lpfilttype = 'firws';
cfg.lpfreq = 30;
cfg.lpfiltdir = 'onepass-reverse-zerophase';
cfg.preproc.demean = 'yes';
cfg.preproc.baselinewindow = [-0.1 0];
data = ft_preprocessing(cfg, data);
cfg.latency = [-0.5 0];
active = ft_selectdata(cfg, data);
else
cfg.latency = [0 0.5];
active = ft_selectdata(cfg, data);
cfg.latency = [-0.5 0];
baseline = ft_selectdata(cfg, data);
end
active.trial = cat(3,active.trial{:});
active.trial = permute(active.trial, [3,1,2]);
active.time = active.time{1};
if ~strcmp(erfoi, 'motor')
baseline.trial = cat(3,baseline.trial{:});
baseline.trial = permute(baseline.trial, [3,1,2]);
baseline.time = active.time;
end
design = [gammaPow;((data.trialinfo(:,5)-data.trialinfo(:,4))/1200)'];
% design = gammaPow;
cfg=[];
cfg.glm.statistic = 'beta';
cfg.glm.standardise = false;
for k=1:length(active.label)
dat = [squeeze(active.trial(:,k,:))]';
dat = (dat - repmat(mean(dat,2),[1 length(data.trialinfo)]));
tmp = statfun_glm(cfg, dat, design);
betas_tmp(k,:) = tmp.stat(:,1);
if ~strcmp(erfoi, 'motor')
dat_bl = [squeeze(baseline.trial(:,k,:))]';
dat_bl = (dat_bl - repmat(mean(dat_bl,2),[1 length(data.trialinfo)]));
tmp_bl = statfun_glm(cfg, dat_bl, design);
betas_bl_tmp(k,:) = tmp_bl.stat(:,1);
end
end
% put beta weights in timelock structure
betas = rmfield(data,{'trial', 'cfg'});
betas.avg = betas_tmp;
betas.time = active.time;
betas.dimord = 'chan_time';
if ~strcmp(erfoi, 'motor')
betas_bl = rmfield(betas, 'avg');
betas_bl.avg = betas_bl_tmp;
end
if strcmp(erfoi, 'motor')
betas_bl = 'use baseline in erfoi = reversal';
end
%% Save
if isPilot
filename = sprintf('/project/3011085.02/analysis/glm/pilot-%03d/sub-%03d_glm_gamma_time_%s', subj, subj, erfoi);
else
filename = sprintf('/project/3011085.02/analysis/glm/sub-%03d/sub-%03d_glm_gamma_time_%s', subj, subj, erfoi);
end
save(fullfile([filename '.mat']), 'betas','betas_bl', '-v7.3');
ft_diary('off')