-
Notifications
You must be signed in to change notification settings - Fork 1
/
ExampleScriptforPhD.m
328 lines (313 loc) · 13.4 KB
/
ExampleScriptforPhD.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
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
%% MAIN SCRIPT
%% Two groups of subjects, based on behavioral data: Low aggression vs High aggression
liste=importdata ('D:\Program Files\MATLAB\fieldtrip-20200109\go_grup_data\subject_names.txt'); % A custom function to upload data
%%
agg_condition = [1 1 2 1 1 1 1 2 2 1 1 1 2 1 1 1 2 2 2 2 2 2]; %% 1 = low, 2 = high
phy_condition = [1 1 2 1 1 1 1 1 2 1 1 1 2 1 1 1 2 1 2 2 2 1];
all_subjects = [];
for i=1:length(liste)
all_subjects{i} = fieldtrip_transformer(liste{i}); % A custom function to adjust data structure to be compatible with fieldtrip
all_subjects{i}.agg_condition = agg_condition(i);
all_subjects{i}.phy_condition = phy_condition(i);
end
load ('D:\Program Files\MATLAB\fieldtrip-20200109\go_grup_data\yeni_subjects.mat');
for k = 1:22
for i = 1:150
all_subjects{k}.trial{i}=yeni_subjects{k}.trial{i};
end
end
%% preprocessing ve visualization
cd('D:\Program Files\MATLAB\fieldtrip-20200109');
load ('layout_capa'); %Custom layout for electrode positions
load ('neighbours'); %Neighboring structure for future interpolation and spatial clustering
ft_defaults
for i=1:length(liste)
all_subjects{i}.cfg.preproc = [];
all_subjects{i}.cfg.preproc.hpfilter= 'yes' ;
all_subjects{i}.cfg.preproc.hpfilter=[0.1]; %
all_subjects{i}.cfg.preproc.lpfilter = 'yes';
all_subjects{i}.cfg.preproc.lpfreq = [30];
all_subjects{i}.cfg.preproc.cfg.dftfreq = [50 100 150 200 250];
all_subjects{i}.cfg.preproc.baselinewindow = [-0.2 0];
all_subjects{i}.cfg.preproc.cfg.detrend = 'yes';
all_subjects{i}.cfg.preproc.cfg.demean = 'yes';
[all_subjects{i}.preprocessed_data] = ft_preprocessing(all_subjects{i}.cfg, all_subjects{i} );
end
%% Missing trialinfos to be fixed
Trial_infos = {};
for i = 1:22;
Trial_infos{1,i} = all_subjects{1, i}.trialinfo.Names;
end
% Subjects 8,9 and 10 have wrong tiralinfo raws. Adjusting the vectors in line with other subjects.
%Subject 8 transformation
all_subjects{1, 8}.trialinfo.Data(5,:) = all_subjects{1, 8}.trialinfo.Data(1,:);
all_subjects{1, 8}.trialinfo.Data(1,:) = all_subjects{1, 8}.trialinfo.Data(2,:);
all_subjects{1, 8}.trialinfo.Data(2,:) = all_subjects{1, 8}.trialinfo.Data(3,:);
all_subjects{1, 8}.trialinfo.Data(3,:) = all_subjects{1, 8}.trialinfo.Data(4,:);
all_subjects{1, 8}.trialinfo.Data(4,:) = all_subjects{1, 8}.trialinfo.Data(5,:);
%Subject 9 transformation
all_subjects{1, 9}.trialinfo.Data(5,:) = all_subjects{1, 9}.trialinfo.Data(2,:);
all_subjects{1, 9}.trialinfo.Data(2,:) = all_subjects{1, 9}.trialinfo.Data(3,:);
all_subjects{1, 9}.trialinfo.Data(3,:) = all_subjects{1, 9}.trialinfo.Data(4,:);
all_subjects{1, 9}.trialinfo.Data(4,:) = all_subjects{1, 9}.trialinfo.Data(5,:);
% Subject 10 transformation
all_subjects{1, 10}.trialinfo.Data(5,:) = all_subjects{1, 10}.trialinfo.Data(2,:);
all_subjects{1, 10}.trialinfo.Data(2,:) = all_subjects{1, 10}.trialinfo.Data(3,:);
all_subjects{1, 10}.trialinfo.Data(3,:) = all_subjects{1, 10}.trialinfo.Data(4,:);
all_subjects{1, 10}.trialinfo.Data(4,:) = all_subjects{1, 10}.trialinfo.Data(5,:);
%% Manuel Visual Artifact Rejection
cfg = [];
cfg.method = 'trial';
cfg.keepchannel = 'repair';
cfg.neighbours = neighbours;
cfg.layout = layout_capa;
ft_databrowser(cfg,all_subjects{1, 6}.selected_trials_agg_nogo)
all_subjects{1, 6}.selected_trials_agg_nogo = ft_rejectvisual(cfg, all_subjects{1, 6}.selected_trials_agg_nogo);
save ALL_SUBJECTS_artfree all_subjects
%% Bad channel repair sj 16 ch 02
cfg=[];
cfg.method = 'weighted';
cfg.badchannel = {'O2'};
cfg.neighbours = neighbours;
cfg.layout = layout_capa;
cfg.trials = 'all' ;
cfg.lambda = 1e-5 ;
cfg.order = 4;
all_subjects{16}.preprocessed_data= ft_channelrepair(cfg, all_subjects{16}.preprocessed_data);
%High agg SS 8 18 21 bad channel interpolation
cfg=[];
cfg.method = 'weighted';
cfg.badchannel = {'F4'};
cfg.neighbours = neighbours;
cfg.layout = layout_capa;
cfg.trials = 'all' ;
cfg.lambda = 1e-5 ;
cfg.order = 4;
for i = [8 18 21 ]
all_subjects{1,i}.preprocessed_data= ft_channelrepair(cfg, all_subjects{1,i}.preprocessed_data);
end
%check rejection
cfg=[];
cfg.viewmode = 'vertical';
cfg.layout = layout_capa;
cfg.channel = {'all', '-eog', '-emg'};
ft_databrowser(cfg, all_subjects{1, 9}.selected_trials_agg_nogo)
hold on
ft_databrowser(cfg,grandavg_low_agg_nogo)
% %% %% ICA
cfg = [];
cfg.channel = {'all' '-emg' '-eog'};
cfg.method = 'runica';
all_subjects{22}.ica = ft_componentanalysis(cfg,all_subjects{22}.preprocessed_data);
% plot the components for visual inspection
figure
cfg = [];
cfg.component = 1:14; % specify the component(s) that should be plotted
cfg.layout = layout_capa; % specify the layout file that should be used for plotting
cfg.comment = 'no';
ft_topoplotIC(cfg, all_subjects{22}.ica);
cfg.viewmode = 'component';
ft_databrowser(cfg, all_subjects{22}.ica);
%Reject components if necessary
cfg.component = [1]; % to be removed component(s)
all_subjects{22}.preprocessed_data = ft_rejectcomponent(cfg, all_subjects{22}.ica);
save all_subjects all_subjects
% Trial Selection based on experimental condition and groups
ft_defaults
for i=1:22
all_subjects{1,i}.cfg=[];
all_subjects{1,i}.cfg.trials = find(all_subjects{1,i}.trialinfo.Data(1,:) == 1 & all_subjects{1,i}.trialinfo.Data(2,:) == 1 & all_subjects{1,i}.trialinfo.Data(4,:) == 0 );
[all_subjects{1,i}.selected_trials_agg_nogo] = ft_selectdata(all_subjects{1,i}.cfg,all_subjects{1, i}.preprocessed_data);
end
for i=1:22
all_subjects{i}.cfg.trials = find(all_subjects{1,i}.trialinfo.Data(1,:) == 1 & all_subjects{i}.trialinfo.Data(3,:) == 1 & all_subjects{1,i}.trialinfo.Data(4,:) == 1 );
[all_subjects{i}.selected_trials_agg_go] = ft_selectdata(all_subjects{i}.cfg,all_subjects{1, i}.preprocessed_data);
end
for i=1:22
all_subjects{i}.cfg.trials = find(all_subjects{1,i}.trialinfo.Data(1,:) == 1 & all_subjects{i}.trialinfo.Data(2,:) == 1 & all_subjects{1,i}.trialinfo.Data(4,:) == 0 );
[all_subjects{i}.selected_trials_phy_nogo] = ft_selectdata(all_subjects{i}.cfg,all_subjects{1, i}.preprocessed_data);
end
for i=1:22
all_subjects{i}.cfg.trials = find(all_subjects{1,i}.trialinfo.Data(1,:) == 1 & all_subjects{i}.trialinfo.Data(3,:) == 1 & all_subjects{1,i}.trialinfo.Data(4,:) == 1 );
[all_subjects{i}.selected_trials_phy_go] = ft_selectdata(all_subjects{i}.cfg,all_subjects{1, i}.preprocessed_data);
end
save all_subjects all_subjects
%% Inter-trial averaging per subject
for i=1:22;
all_subjects{i}.cfg=[];
[all_subjects{i}.timelocked_data_agg_nogo] = ft_timelockanalysis(all_subjects{i}.cfg,all_subjects{i}.selected_trials_agg_nogo);
end
for i=1:22;
all_subjects{i}.cfg=[];
[all_subjects{i}.timelocked_data_agg_go] = ft_timelockanalysis(all_subjects{i}.cfg,all_subjects{i}.selected_trials_agg_go);
end
for i=1:22;
all_subjects{i}.cfg=[];
[all_subjects{i}.timelocked_data_phy_nogo] = ft_timelockanalysis(all_subjects{i}.cfg,all_subjects{i}.selected_trials_phy_nogo);
end
for i=1:22;
all_subjects{i}.cfg=[];
[all_subjects{i}.timelocked_data_phy_go] = ft_timelockanalysis(all_subjects{i}.cfg,all_subjects{i}.selected_trials_phy_go);
end
%% Grouping subjects based on experimental condition and behavioral results
% Based on total aggression
high_aggr_go = {};
for i=1:22
if all_subjects{1, i}.agg_condition == 2
high_aggr_go{i}= all_subjects{1, i}.timelocked_data_agg_go;
end
end
low_aggr_go = {};
for i=1:22
if all_subjects{1, i}.agg_condition == 1
low_aggr_go{i}= all_subjects{1, i}.timelocked_data_agg_go;
end
end
low_aggr_nogo = {};
for i=1:22
if all_subjects{1, i}.agg_condition == 1
low_aggr_nogo{i}= all_subjects{1, i}.timelocked_data_agg_nogo;
end
end
high_aggr_nogo = {};
for i=1:22
if all_subjects{1, i}.agg_condition == 2
high_aggr_nogo{i}= all_subjects{1, i}.timelocked_data_agg_nogo;
end
end
%Based on physical aggression
high_phyaggr_go = {};
for i=1:22
if all_subjects{1, i}.phy_condition == 2
high_phyaggr_go{i}= all_subjects{1, i}.timelocked_data_phy_go;
end
end
low_phyaggr_go = {};
for i=1:22
if all_subjects{1, i}.phy_condition == 1
low_phyaggr_go{i}= all_subjects{1, i}.timelocked_data_phy_go;
end
end
low_phyaggr_nogo = {};
for i=1:22
if all_subjects{1, i}.phy_condition == 1
low_phyaggr_nogo{i}= all_subjects{1, i}.timelocked_data_phy_nogo;
end
end
high_phyaggr_nogo = {};
for i=1:22
if all_subjects{1, i}.phy_condition == 2
high_phyaggr_nogo{i}= all_subjects{1, i}.timelocked_data_phy_nogo;
end
end
%Remove empty cells
empties = find(cellfun(@isempty, high_phyaggr_nogo)); % identify the empty cells
high_phyaggr_nogo(empties) = [] ;
empties = find(cellfun(@isempty, high_phyaggr_go)); % identify the empty cells
high_phyaggr_go(empties) = [] ;
empties = find(cellfun(@isempty, low_phyaggr_go)); % identify the empty cells
low_phyaggr_go(empties) = [] ;
empties = find(cellfun(@isempty, low_phyaggr_nogo)); % identify the empty cells
low_phyaggr_nogo(empties) = [] ;
empties = find(cellfun(@isempty, high_aggr_nogo)); % identify the empty cells
high_aggr_nogo(empties) = [] ;
empties = find(cellfun(@isempty, high_aggr_go)); % identify the empty cells
high_aggr_go(empties) = [] ;
empties = find(cellfun(@isempty, low_aggr_nogo)); % identify the empty cells
low_aggr_nogo(empties) = [] ;
empties = find(cellfun(@isempty, low_aggr_go)); % identify the empty cells
low_aggr_go(empties) = [] ;
%% Grand Average
cfg=[];
[grandavg_low_agg_go] = ft_timelockgrandaverage(cfg, low_aggr_go{:});
[grandavg_high_agg_go] = ft_timelockgrandaverage(cfg, high_aggr_go {:});
[grandavg_low_agg_nogo] = ft_timelockgrandaverage(cfg, low_aggr_nogo {:});
[grandavg_high_agg_nogo] = ft_timelockgrandaverage(cfg, high_aggr_nogo {:});
[grandavg_low_phyagg_go] = ft_timelockgrandaverage(cfg, low_phyaggr_go {:});
[grandavg_high_phyagg_go] = ft_timelockgrandaverage(cfg, high_phyaggr_go {:});
[grandavg_high_phyagg_nogo] = ft_timelockgrandaverage(cfg, high_phyaggr_nogo {:});
[grandavg_low_phyagg_nogo] = ft_timelockgrandaverage(cfg, low_phyaggr_nogo {:});
save grandavg_high_agg_go grandavg_high_agg_go
save grandavg_high_agg_nogo grandavg_high_agg_nogo
save grandavg_low_agg_go grandavg_low_agg_go
save grandavg_low_agg_nogo grandavg_low_agg_nogo
save grandavg_high_phyagg_go grandavg_high_phyagg_go
save grandavg_high_phyagg_nogo grandavg_high_phyagg_nogo
save grandavg_low_phyagg_go grandavg_low_phyagg_go
save grandavg_low_phyagg_nogo grandavg_low_phyagg_nogo
%% Visualize the results
cfg=[];
cfg.channel= {'all','-eog','-emg'} ;
cfg.layout = layout_capa;
ft_multiplotER(cfg,grandavg_low_agg_nogo,grandavg_high_agg_nogo)
plot(grandavg_low_agg_nogo.time,mean(grandavg_low_agg_go.avg(:,:)),'color','b');
hold on
plot(grandavg_high_agg_nogo.time,mean(grandavg_high_agg_go.avg(:,:)),'color','r');
%% Statistical Analysis
%For total aggression
cfg = [];
cfg.correcttail = 'alpha';
cfg.channel = {'all', '-eog', '-emg'};
cfg.layout = layout_capa;
cfg.neighbours = neighbours; %
cfg.latency = [0.2 0.5];
cfg.avgovertime = 'no';
cfg.parameter = 'avg';
cfg.method = 'montecarlo';
cfg.statistic = 'ft_statfun_indepsamplesT';
cfg.alpha = 0.05;
cfg.correctm = 'cluster';
cfg.clusteralpha = 0.05;
cfg.numrandomization = 1000;
cfg.minnbchan = 2;
cfg.clusterstatistic = 'maxsum';
design = zeros(1,size(high_aggr_nogo(:),1) + size(low_aggr_nogo(:),1));
design(1,1:size(high_aggr_nogo(:),1)) = 1;
design(1,(size(high_aggr_nogo(:),1)+1):(size(high_aggr_nogo(:),1) + size(low_aggr_nogo(:),1)))= 2;
cfg.design = design; % design matrix
cfg.ivar = 1;
stat_agg_nogo = ft_timelockstatistics(cfg,high_aggr_nogo{:}, low_aggr_nogo{:});
stat_agg_go = ft_timelockstatistics(cfg, high_aggr_go{:}, low_aggr_go{:});
% For physical agression
cfg = [];
cfg.channel = {'all', '-eog', '-emg'};
cfg.layout = layout_capa;
cfg.neighbours = neighbours; % defined as above
cfg.latency = 'all';
cfg.avgovertime = 'no';
cfg.parameter = 'avg';
cfg.method = 'montecarlo';
cfg.statistic = 'ft_statfun_indepsamplesT';
cfg.alpha = 0.05;
cfg.correctm = 'cluster';
cfg.clusteralpha = 0.05;
cfg.correcttail = 'alpha';
cfg.numrandomization = 1000;
cfg.minnbchan = 1;
cfg.clusterstatistic = 'maxsum';
design = zeros(1,size(high_phyaggr_nogo(:),1) + size(low_phyaggr_nogo(:),1));
design(1,1:size(high_phyaggr_nogo(:),1)) = 1;
design(1,(size(high_phyaggr_nogo(:),1)+1):(size(high_phyaggr_nogo(:),1) + size(low_phyaggr_nogo(:),1)))= 2;
cfg.design = design; % design matrix
cfg.ivar = 1;
stat_phyagg_nogo = ft_timelockstatistics(cfg, high_phyaggr_nogo{:}, low_phyaggr_nogo{:});
stat_phyagg_go = ft_timelockstatistics(cfg, high_phyaggr_go{:}, low_phyaggr_go{:});
%%Visualization of results
cfg = [];
cfg.style = 'blank';
cfg.layout = layout_capa;
cfg.highlight = 'on';
cfg.highlightchannel = find(stat_agg_nogo.mask);
cfg.comment = 'no';
figure; ft_topoplotER(cfg, grandavg_high_agg_go, grandavg_low_agg_go)
title('Nonparametric: significant without multiple comparison correction')
%%Visualization of results
cfg = [];
cfg.highlightsymbolseries = ['*','*','.','.','.'];
cfg.layout = layout_capa;
cfg.contournum = 0;
cfg.markersymbol = '.';
cfg.alpha = 0.25;
cfg.parameter= 'stat';
cfg.zlim = [-10 10];
ft_clusterplot(cfg, stat_agg_go);