-
Notifications
You must be signed in to change notification settings - Fork 1
/
exp2_saccade_analysis.m
executable file
·447 lines (381 loc) · 14 KB
/
exp2_saccade_analysis.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
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
%% It's always good to start with a clean sheet
clc, clear, close all, warning('off','all')
%% set project name
projectname = 'detect gaze shift (microsaccade) from gaze position';
%% prepare Toolbox
parent_folder = '/Users/dongyu/Documents/Github/spontaneousMicrosaccade_EEGAlpha/';
package_folder = '/Users/dongyu/Documents/Github/spontaneousMicrosaccade_EEGAlpha/customToolbox/';
data_folder = '/Users/dongyu/MATLAB-Drive/exp2_eye_data';
cfg= [];
cfg.package_folder = package_folder;
cfg.futureTB_path = [package_folder 'futureToolBox'];
cd(cfg.futureTB_path);
futureTB_setup(cfg);
%% Set directions
write_dir_eye = creatDir([parent_folder 'eye_results_probe']);
write_dir_fig = creatDir([write_dir_eye filesep 'figures']);
%% get gaze shift
readDir = [write_dir_eye filesep 'epoched_data'];
nonNaNDir = [write_dir_eye filesep 'trial_ok_noNan'];
subjList = get_subFiles(readDir);
nonNaNList = get_subFiles(nonNaNDir);
output_dir = creatDir([write_dir_eye filesep 'gaze_shift']);
for subjInd = 1:length(subjList)
load(subjList{subjInd})
load(nonNaNList{subjInd})
% get data in x axis
data_shift =[];
if strcmp(subjList{subjInd}(end-7:end-4),'pp49') % deal with the missing data (this participant doesn't have the first 80 trials of learning phase)
startTrial = 1;
disp('start from trial 1');
else
startTrial = 81;
disp('start from trial 81');
end
eye_dataX = [];
for trial = startTrial:length(eye_data.trial)
trial_dataX = eye_data.trial{trial}(1,:);
eye_dataX = [eye_dataX;trial_dataX];
end
nonNaNtrials = event.sel(startTrial:length(eye_data.trial));
eye_dataX = eye_dataX(nonNaNtrials,:);
% get gaze shift
cfg = [];
cfg.threshold = 3;
[eye_shift,time_shift] = PBlab_gazepos2shift_1D(cfg, eye_dataX, eye_data.time{1});
eye_shift = 2*atand(eye_shift/(2*95*28.3604));
% remove saccade whose size > 1 degree of visual angle
eye_shift(abs(eye_shift)>1) = 0;
data_shift.shift = eye_shift;
data_shift.time = time_shift;
trialInfo = eye_data.trialinfo(startTrial:length(eye_data.trial));
data_shift.trialinfo = trialInfo(nonNaNtrials);
% save data
save([output_dir filesep subjList{subjInd}(end-7:end-4)] ,'data_shift')
end
%% plot gaze magnitude x shift size figure: LTM
sublist_shift = get_subFiles([write_dir_eye filesep 'gaze_shift']);
for subjInd = 1:length(sublist_shift)
load(sublist_shift{subjInd})
GA_struct = [];
cfg = [];
cfg.size_range = [0.05 5.5];
cfg.binWin = 0.25;
cfg.binstep = 0.05;
subNumber = str2double(sublist_shift{subjInd}(end-5:end-4));
disp(['subNumber:' num2str(subNumber)]);
if mod(subNumber,2) == 0
cfg.trigs_left = 721;
cfg.trigs_right = 722;
elseif mod(subNumber,2) == 1
cfg.trigs_left = 722;
cfg.trigs_right = 721;
end
[rate_size,bin_range]=gazeShiftRateOverSize(cfg, data_shift.shift, data_shift.trialinfo);
GA_struct.toward(subjInd,:,:) = rate_size.toward;
GA_struct.away(subjInd,:,:) = rate_size.away;
GA_struct.diff(subjInd,:,:) = rate_size.diff;
end
GA_struct.bin_range = bin_range;
GA_struct.time = data_shift.time;
% save the group data
save([write_dir_eye filesep 'GA_shift_rateAndsize_LTM'] ,'GA_struct')
%% plot gaze magnitude x shift size figure: WM
sublist_shift = get_subFiles([write_dir_eye filesep 'gaze_shift']);
for subjInd = 1:length(sublist_shift)
load(sublist_shift{subjInd})
GA_struct = [];
cfg = [];
cfg.size_range = [0.05 5.5];
cfg.binWin = 0.25;
cfg.binstep = 0.05;
subNumber = str2double(sublist_shift{subjInd}(end-5:end-4));
disp(['subNumber:' num2str(subNumber)]);
if mod(subNumber,2) == 0
cfg.trigs_left = 712;
cfg.trigs_right = 711;
elseif mod(subNumber,2) == 1
cfg.trigs_left = 711;
cfg.trigs_right = 712;
end
[rate_size,bin_range]=gazeShiftRateOverSize(cfg, data_shift.shift, data_shift.trialinfo);
GA_struct.toward(subjInd,:,:) = rate_size.toward;
GA_struct.away(subjInd,:,:) = rate_size.away;
GA_struct.diff(subjInd,:,:) = rate_size.diff;
end
GA_struct.bin_range = bin_range;
GA_struct.time = data_shift.time;
% save the group data
save([write_dir_eye filesep 'GA_shift_rateAndsize_WM'] ,'GA_struct')
%% plotting the figure: LTM
load([write_dir_eye filesep 'GA_shift_rateAndsize_LTM']);
cmap = brewermap([],'*RdBu');
xli = [-0.3 2];
figure('position', [100 100 1200 300])
subplot(1,3,1)
% difference figure
hz2plot= squeeze(nanmean(GA_struct.diff,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
maxValue = max(max(max(hz2plot)), abs(min(min(hz2plot))));
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('Toward - Away')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
% toward
subplot(1,3,2)
hz2plot= squeeze(nanmean(GA_struct.toward,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
maxValue = max([max(max(squeeze(nanmean(GA_struct.toward,1)))) max(max(squeeze(nanmean(GA_struct.away,1))))]);
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('Toward')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
% away
subplot(1,3,3)
hz2plot= squeeze(nanmean(GA_struct.away,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('away')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
%% plotting the figure: WM
load([write_dir_eye filesep 'GA_shift_rateAndsize_WM']);
cmap = brewermap([],'*RdBu');
xli = [-0.3 2];
figure('position', [100 100 1200 300])
subplot(1,3,1)
% difference figure
hz2plot= squeeze(nanmean(GA_struct.diff,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
maxValue = max(max(max(hz2plot)), abs(min(min(hz2plot))));
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('Toward - Away')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
% toward
subplot(1,3,2)
hz2plot= squeeze(nanmean(GA_struct.toward,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
maxValue = max([max(max(squeeze(nanmean(GA_struct.toward,1)))) max(max(squeeze(nanmean(GA_struct.away,1))))]);
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('Toward')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
% away
subplot(1,3,3)
hz2plot= squeeze(nanmean(GA_struct.away,1));
contourf(GA_struct.time,GA_struct.bin_range,hz2plot,50,'linecolor','none')
% maxValue = 0.1;
caxis([-maxValue maxValue])
colorbar
colormap(cmap);
xlabel('Time since cue onset (s)')
ylabel('Shift size (degree)')
title('away')
hold on
plot([0 0], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
plot([1 1], [GA_struct.bin_range(1), GA_struct.bin_range(end)], '--k')
xlim(xli)
%% plot average toward rate and away rate: LTM
toward_mean = [];
away_mean = [];
sublist_shift = get_subFiles([write_dir_eye filesep 'gaze_shift']);
slideWin4rate = 50;
for subjInd = 1:length(sublist_shift)
load(sublist_shift{subjInd})
subNumber = str2double(sublist_shift{subjInd}(end-5:end-4));
disp(['subNumber:' num2str(subNumber)]);
if mod(subNumber,2) == 0
trigs_left = 721;
trigs_right = 722;
elseif mod(subNumber,2) == 1
trigs_left = 722;
trigs_right = 721;
end
% select left and right trial
sel_left = ismember(data_shift.trialinfo,trigs_left);
sel_right = ismember(data_shift.trialinfo,trigs_right);
% select left and right shift
shift_left = data_shift.shift < 0;
shift_right = data_shift.shift > 0;
% get toward and away saccade
toward = (mean(shift_left(sel_left,:)) + mean(shift_right(sel_right,:))) ./ 2;
away = (mean(shift_left(sel_right,:)) + mean(shift_right(sel_left,:))) ./ 2;
% get the shift rate through movemean method
toward_rate = smoothdata(toward,2,'movmean',slideWin4rate)*1000;
away_rate = smoothdata(away,2,'movmean',slideWin4rate)*1000;
toward_mean = [toward_mean;toward_rate];
away_mean = [away_mean;away_rate];
end
% Calculate the mean and 95% CI
mean_toward = mean(toward_mean, 1); % Mean across subjects, size (1, 1149)
mean_away = mean(away_mean, 1); % Mean across subjects, size (1, 1149)
% Standard error of the mean
sem_toward = std(toward_mean, 0, 1) / sqrt(size(toward_mean, 1));
sem_away = std(away_mean, 0, 1) / sqrt(size(away_mean, 1));
% 95% Confidence Interval
ci_toward = 1.96 * sem_toward;
ci_away = 1.96 * sem_away;
% Time points
times = 1:size(toward_mean, 2); %(1:2149)
real_time_points = times - 250; %(-250:1900)
toi = 50:1250; %(-200:1000)
times_to_plot = real_time_points(toi);
mean_toward_plot = mean_toward(toi);
ci_toward_plot = ci_toward(toi);
mean_away_plot = mean_away(toi);
ci_away_plot = ci_away(toi);
toward_mean_plot = toward_mean(:,toi);
away_mean_plot = away_mean(:,toi);
% Plot
f = figure;
figureStartup;
hold on;
% Plotting toward
fill([times_to_plot, fliplr(times_to_plot)], ...
[mean_toward_plot + ci_toward_plot, fliplr(mean_toward_plot - ci_toward_plot)], ...
'b', 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Shaded area for CI
l1 = plot(times_to_plot, mean_toward_plot, 'b', 'LineWidth', 3); % Mean line
% Plotting away
fill([times_to_plot, fliplr(times_to_plot)], ...
[mean_away_plot + ci_away_plot, fliplr(mean_away_plot - ci_away_plot)], ...
'r', 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Shaded area for CI
l2 = plot(times_to_plot, mean_away_plot, 'r', 'LineWidth', 3); % Mean line
cfg = [];
cfg.xax = -200:1000;
cfg.npermutations = 5000;
cfg.clusterStatEvalaluationAlpha= 0.05;
cfg.nsub=44;
cfg.statMethod = 'montecarlo';
cfg.time_i = [-100 600];
state_t = cluster_perm_1D(cfg,toward_mean_plot,away_mean_plot);
mask_xxx = double(state_t.mask); mask_xxx(mask_xxx==0) = nan;
plot(-100:600, mask_xxx * 0.7, 'k', 'LineWidth', 3);
% change x sticks:
xticks(0:400:800);
yticks(0:0.5:2);
% Labels and legend
xlabel('Time relative to cue onset (ms)');
ylabel('Rate (Hz)');
legend([l1,l2],'Toward','Away');
legend box off
hold off;
%% plot average toward rate and away rate: WM
toward_mean = [];
away_mean = [];
sublist_shift = get_subFiles([write_dir_eye filesep 'gaze_shift']);
slideWin4rate = 50;
for subjInd = 1:length(sublist_shift)
load(sublist_shift{subjInd})
subNumber = str2double(sublist_shift{subjInd}(end-5:end-4));
disp(['subNumber:' num2str(subNumber)]);
if mod(subNumber,2) == 0
trigs_left = 712;
trigs_right = 711;
elseif mod(subNumber,2) == 1
trigs_left = 711;
trigs_right = 712;
end
% select left and right trial
sel_left = ismember(data_shift.trialinfo,trigs_left);
sel_right = ismember(data_shift.trialinfo,trigs_right);
% select left and right shift
shift_left = data_shift.shift < 0;
shift_right = data_shift.shift >0;
% get toward and away saccade
toward = (mean(shift_left(sel_left,:)) + mean(shift_right(sel_right,:))) ./ 2;
away = (mean(shift_left(sel_right,:)) + mean(shift_right(sel_left,:))) ./ 2;
% get the shift rate through movemean method
toward_rate = smoothdata(toward,2,'movmean',slideWin4rate)*1000;
away_rate = smoothdata(away,2,'movmean',slideWin4rate)*1000;
toward_mean = [toward_mean;toward_rate];
away_mean = [away_mean;away_rate];
end
% Calculate the mean and 95% CI
mean_toward = mean(toward_mean, 1); % Mean across subjects, size (1, 1149)
mean_away = mean(away_mean, 1); % Mean across subjects, size (1, 1149)
% Standard error of the mean
sem_toward = std(toward_mean, 0, 1) / sqrt(size(toward_mean, 1));
sem_away = std(away_mean, 0, 1) / sqrt(size(away_mean, 1));
% 95% Confidence Interval
ci_toward = 1.96 * sem_toward;
ci_away = 1.96 * sem_away;
% Time points
times = 1:size(toward_mean, 2); %(1:2149)
real_time_points = times - 250; %(-250:1900)
toi = 50:1250; %(-200:1000)
times_to_plot = real_time_points(toi);
mean_toward_plot = mean_toward(toi);
ci_toward_plot = ci_toward(toi);
mean_away_plot = mean_away(toi);
ci_away_plot = ci_away(toi);
toward_mean_plot = toward_mean(:,toi);
away_mean_plot = away_mean(:,toi);
% Plot
f = figure;
figureStartup;
hold on;
% Plotting toward
fill([times_to_plot, fliplr(times_to_plot)], ...
[mean_toward_plot + ci_toward_plot, fliplr(mean_toward_plot - ci_toward_plot)], ...
'b', 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Shaded area for CI
l1 = plot(times_to_plot, mean_toward_plot, 'b', 'LineWidth', 3); % Mean line
% Plotting away
fill([times_to_plot, fliplr(times_to_plot)], ...
[mean_away_plot + ci_away_plot, fliplr(mean_away_plot - ci_away_plot)], ...
'r', 'FaceAlpha', 0.3, 'EdgeColor', 'none'); % Shaded area for CI
l2 = plot(times_to_plot, mean_away_plot, 'r', 'LineWidth', 3); % Mean line
cfg = [];
cfg.xax = -200:1000;
cfg.npermutations = 5000;
cfg.clusterStatEvalaluationAlpha= 0.05;
cfg.nsub = 44;
cfg.statMethod = 'montecarlo';
cfg.time_i = [-100 600];
state_t = cluster_perm_1D(cfg,toward_mean_plot,away_mean_plot);
mask_xxx = double(state_t.mask); mask_xxx(mask_xxx==0) = nan;
plot(-100:600, mask_xxx * 0.7, 'k', 'LineWidth', 5);
% change x sticks:
xticks(0:400:800);
yticks(0:0.5:2);
% Labels and legend
xlabel('Time relative to cue onset (ms)');
ylabel('Rate (Hz)');
legend([l1,l2],'Toward','Away');
legend box off
hold off;