This repository has been archived by the owner on Jun 19, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
infant_audiovisual_eyetrack_processing.m
1496 lines (1213 loc) · 72 KB
/
infant_audiovisual_eyetrack_processing.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
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
%% Script for data preprocessing of the infant audiovisual eyetracking
% experiment - pilot data collected Dec 21 - Mar 22
% the script was coded by Ana Maria Portugal
% it uses the hdf5 file and the trial information csv file
% the task script was implemented by Lowe Wilsson in Python
clear all
%% add necessary functions
addpath '/Users/k6-c02c12kvlvdl/Dropbox/Scripts/ET_Methods/pupil-size-master/code/helperFunctions'
% we need a function for dynamic offset mean from Kret, & Sjak-Shie, 2019
% https://github.com/ElioS-S/pupil-size
%% Set general variables
% for interpolation, both pupil and gaze, the maximum gap allowed
interp_maxGap = 150; % in ms, leave empty if no interpolation is needed
% path for stimuli that define the sound condition in the csv trial file
social = 'stimuli/audio/social/';
nonsocial = 'stimuli/audio/nonsocial/';
silent = 'silent';
% Screen limits in the gaze data (distance to screen assumed to be 65 cm)
% Monitor size is 52.69x29.64. Monitor right-center edge has
% coordinates (22.06, 0), the center-top edge has coordinates (0, 12.9).
% The left-bottom corner of the monitor has coordinates (-22.06, -12.9).
width_half = 22.06; % X from -22.06 to 22.06
height_half = 12.9; % Y from -12.9 to 12.9
% Define AOI size - during task presentation the stimuli was within a
% 9x9 visual degrees size
stim_box = 11; % AOI is increased a bit
% do we want plots and data structs to be saved?
plot_figure = 0; % 1=saves heatmap and a figure with 4 different plots
save_data = 0; % 1=saves wide and long csv files
% initiate a matrix to store pupil samples across trials and individuals
t_for_B = 1; %this is needed to collect all pupil samples in the baseline
t_for_R = 1; %this is needed to collect all pupil samples in response
%% paths for data
% this needs to be edited for other projects
resultsPath = 'C:\Users\giobu365\Documents\pupil_habituation\ET_DATA'
resultsPath_calib = '/Users/k6-c02c12kvlvdl/Documents/UU face pop out exp for Ana M/Calibration/';
cd(resultsPath)
mkdir('../results/') % make directory for plots and results
files = dir('**/*.csv'); % scan folder for all trial info csv files,
% should be one csv file per participant
for f = 1: length(files)
if f > length(files)
% this is needed because we'll remove some participants below
continue
end
[~,name,ext] = fileparts(files(f).name);
subject = name(1:3); % experiment subject codes were in the format of
% "P01" so we segment the first three characters. Might need adaptation
% in other projects
s_time = name(end-3:end);
id_name = [subject, '_', s_time] % time was saved because we had duplicates
% % uncomment this to inspect particular subjects
% if subject == "P17"
% else
% continue
% end
%% skip/ remove duplicates - this needs adaptation in case of other projects
% when specific subjects and times appear we removed their paths from
% the files list and reassigned the subject variables
if subject == "p01" && s_time == "1102"
warning('skip')
files(f)=[];
if f > length(files) % if we are at the end of the files list we
% move on in the loop / end loop
continue
else % if not we re assign the subject name and paths to the loop index
[~,name,ext] = fileparts(files(f).name);
subject = name(1:3);
s_time = name(end-3:end);
id_name = [subject, '_', s_time]
end
elseif subject == "p04" && s_time == "1227"
warning('skip')
files(f)=[];
if f > length(files)
continue
else
[~,name,ext] = fileparts(files(f).name);
subject = name(1:3);
s_time = name(end-3:end);
id_name = [subject, '_', s_time]
end
elseif subject == "p13" && s_time == "1116"
warning('skip')
files(f)=[];
if f > length(files)
continue
else
[~,name,ext] = fileparts(files(f).name);
subject = name(1:3);
s_time = name(end-3:end);
id_name = [subject, '_', s_time]
end
elseif subject == "P61" && s_time == "1219"
warning('skip')
files(f)=[];
if f > length(files)
continue
else
[~,name,ext] = fileparts(files(f).name);
subject = name(1:3);
s_time = name(end-3:end);
id_name = [subject, '_', s_time]
end
end
% trial_data_all_wide collects wide format data, one row per subject
trial_data_all_wide.subject(f,1) = convertCharsToStrings(subject);
trial_data_all_wide.id_name(f,1) = convertCharsToStrings(id_name);
%% get invidiual file paths
% find the hdf5 file - this file has the same filename as the csv file
% but ends in '_hdf5.hdf5'
Filename = dir(['**/',name,'_hdf5.hdf5']);
if isempty(Filename)
% if no file was found then move on to the next subject
disp(warning('No hdf5 file found'))
continue
else
Filename = [Filename.folder,'/', Filename.name ];
end
% find the trial information csv file - this just needs to get the
% pathname and filename already in the files struct
Filename_csv = [files(f).folder,'/', files(f).name ];
% find the calibration file - this searches based on the subject id
% (eg P01) because time was not the same as the eye tracker files
% it might give us multiple files or files that are not necessarily
% corresponding to the correct session. This was fixed after manually
Filename_calib = dir([resultsPath_calib, '**/',subject,'*ease_et_calibration*', '_validation_data.csv' ]);
if isempty(Filename_calib)
% if no file was found, write NaN in the variables field
disp(warning('No Calibration file found'))
trial_data_all_wide.calib_time(f,1) = "No file";
trial_data_all_wide.mean_distance_gaze_to_target(f,1) = NaN;
trial_data_all_wide.std_distance_gaze_to_target(f,1) = NaN;
trial_data_all_wide.proportion_of_time_gaze_on_screen(f,1) = NaN;
elseif length(Filename_calib) > 1
% if more than on file was found, write NaN in the variables field
disp(warning('More than one calibration file found'))
trial_data_all_wide.calib_time(f,1) = "More than one file, fix manually";
trial_data_all_wide.mean_distance_gaze_to_target(f,1) = NaN;
trial_data_all_wide.std_distance_gaze_to_target(f,1) = NaN;
trial_data_all_wide.proportion_of_time_gaze_on_screen(f,1) = NaN;
else
% if we find only one file, we will get the time on the file so we
% can check if the corresponds to the dataset (i.e. should be some
% minutes earlier than the eye tracking session
trial_data_all_wide.calib_time(f,1) = convertCharsToStrings(Filename_calib.name(end-33:end-20));
Filename_calib = [Filename_calib.folder,'/', Filename_calib.name ];
calib_info = ease_2_getcalibinfo(Filename_calib);
trial_data_all_wide.mean_distance_gaze_to_target(f,1) = calib_info.mean_distance_gaze_to_target;
trial_data_all_wide.std_distance_gaze_to_target(f,1) = calib_info.std_distance_gaze_to_target;
trial_data_all_wide.proportion_of_time_gaze_on_screen(f,1) = calib_info.proportion_of_time_gaze_on_screen;
end
%% load gaze and event buffer from hdf5 file
gaze_buffer = h5read(Filename,'/data_collection/events/eyetracker/BinocularEyeSampleEvent');
event_buffer = h5read(Filename, '/data_collection/events/experiment/MessageEvent');
test = h5read(Filename,'/data_collection/session_meta_data'); % this only has the date of the session
experiment_date = string(test.code(3:18)');
%test = h5read(Filename,'/data_collection/events/experiment/LogEvent'); % this only has system information
% convert char event information to strings for easy reading and access
for i=1: size(event_buffer.text,2)
idx= find(isletter(event_buffer.text(:, i)), 1, "last" );
event_buffer.event(i,1) = convertCharsToStrings(event_buffer.text(1:idx, i)');
end
%% load trial information from the csv file
% this needs to be adapted to other projects
% The pilot data collected Dec 21 - Mar 22 included 15 initial
% participants (P01-P15) who only view the pilot task and from then all
% other participants also viewed another task (naturalistic videos)
% Format of the trial information files is a bit different between
% these two batches of participants (the indexes of the relevant
% columns differ so we used two different functions to import the data)
if str2double(subject(2:3)) >= 15
% if subject id is higher than 15
trial_info = ease_2_popout_gettrialinfo(Filename_csv);
else
% if subject is one of the first 15 participants
trial_info = ease_2_popout_gettrialinfo_old(Filename_csv);
end
% remove rows that do not have task trial information / block breaks
toRemove = find(isnan(trial_info.trial_global_start_time))
trial_info(toRemove,:) = [];
%% loop across all trials
% choose which trials to plot if plot_figure = 1 above
trials_to_plot = randi([1 length(trial_info.trial_loop_thisRepN) ],1,2); % this plots two random trials
%trials_to_plot = [1, 2, 3]; % this plots specific trials
%trials_to_plot = [1:length(trial_info.trial_loop_thisRepN)]; % this plots all trials
for t= 1:length(trial_info.trial_loop_thisRepN)
% % uncomment this to inspect particular trials
% if t ~= 45
% continue
% end
% trial_data collects long format data, one row per trial per
% subject
% save trial information - id and trial nr
trial_data.id_name(t,1) = convertCharsToStrings(id_name);
trial_data.id_date(t,1) = experiment_date;
trial_data.id_trial(t,1) = t;
%% save trial conditions - type, volume, and order
if strncmp(string(trial_info.audio_filepath(t)), social, length(social))
trial_data.AudioType(t,1) = 1; % social sound
elseif strncmp(string(trial_info.audio_filepath(t)), nonsocial, length(nonsocial))
trial_data.AudioType(t,1) = 2; % nonsocial sound
elseif strncmp(string(trial_info.audio_filepath(t)), silent, length(silent))
trial_data.AudioType(t,1) = 0; % silent
end
trial_data.AudioVolume(t,1) = trial_info.audio_volume(t);
% we get the trial order using the trial number from the trial file
% - if even is the first repetition, if odd is the second
% repetition
trial_data.Order(t,1) = (~mod(trial_info.trial_loop_thisRepN(t),2) == 0) + 1 ;
%% create AOIs for each stimuli category
% we use info about position in the screen from trial info file and add the stim_box
social_AOI = [trial_info.visual_social_pos_x(t) - stim_box/2, trial_info.visual_social_pos_y(t)- stim_box/2, trial_info.visual_social_pos_x(t) + stim_box/2, trial_info.visual_social_pos_y(t) + stim_box/2];
geometric_AOI = [trial_info.visual_geometric_pos_x(t) - stim_box/2, trial_info.visual_geometric_pos_y(t)- stim_box/2, trial_info.visual_geometric_pos_x(t) + stim_box/2, trial_info.visual_geometric_pos_y(t) + stim_box/2];
manmade_AOI = [trial_info.visual_manmade_pos_x(t) - stim_box/2, trial_info.visual_manmade_pos_y(t)- stim_box/2, trial_info.visual_manmade_pos_x(t) + stim_box/2, trial_info.visual_manmade_pos_y(t) + stim_box/2];
natural_AOI = [trial_info.visual_natural_pos_x(t) - stim_box/2, trial_info.visual_natural_pos_y(t)- stim_box/2, trial_info.visual_natural_pos_x(t) + stim_box/2, trial_info.visual_natural_pos_y(t) + stim_box/2];
fixation_AOI = [ -stim_box/2, - stim_box/2, stim_box/2, stim_box/2]; % central stim
%% segment trial
% % compare global start time on csv to start of trial
% trial_global_start_time = trial_info.trial_global_start_time(t);
% trial_start_ebidx_global = find(event_buffer.event == ['exp1 trial ', num2str(t), ' start' ] );
% trial_start_ebtime_global = event_buffer.time(trial_start_ebidx_global);
% trial_global_start_time - trial_start_ebtime_global
% trial start
% the start of the trial will be 500 ms before the gaze was
% captured / the offset of the attention grabber
% trial_start_ebtime might preced trial start but we want the
% window where gaze was evaluated to be in the fixation AOI
trial_start_ebidx = find(event_buffer.event == ['exp1 trial ', num2str(t), ' attention grabber end' ] );
trial_start_event_id = event_buffer.event_id(trial_start_ebidx);
trial_gaze_captured_ebtime = event_buffer.time(trial_start_ebidx);
trial_start_ebtime = event_buffer.time(trial_start_ebidx) - 0.500;
% audio onset
trial_audio_ebidx = find(event_buffer.event == ['exp1 trial ', num2str(t), ' sound onset' ] );
trial_audio_event_id = event_buffer.event_id(trial_audio_ebidx);
trial_audio_ebtime = event_buffer.time(trial_audio_ebidx);
% visual onset
trial_visual_ebidx = find(event_buffer.event == ['exp1 trial ', num2str(t), ' visual onset' ] );
trial_visual_event_id = event_buffer.event_id(trial_visual_ebidx);
trial_visual_ebtime = event_buffer.time(trial_visual_ebidx);
% visual offset = the end of the trial
trial_end_ebidx = find(event_buffer.event == ['exp1 trial ', num2str(t), ' visual offset' ] );
trial_end_event_id = event_buffer.event_id(trial_end_ebidx);
trial_end_ebtime = event_buffer.time(trial_end_ebidx);
% save the time from audio onset to visual onset which is in
% interstimuli interval (random between 80-400 ms)
trial_data.isi(t,1) = trial_visual_ebtime - trial_audio_ebtime ;
% Find start and end of trial in indexes on Buffer
% trial based on 500 ms before gaze captured and visual offset
%trial_start_gbidx = find(gaze_buffer.event_id >= trial_start_event_id, 1, "first" )
trial_start_gbidx = find(gaze_buffer.time >= trial_start_ebtime, 1, "first" );
trial_end_gbidx = find(gaze_buffer.time <= trial_end_ebtime, 1, "last" );
% save total duration of trial - from 500 ms before gaze captured
% to visual offset
trial_data.DurationTrial(t,1) = trial_end_ebtime - trial_start_ebtime;
% segment time buffer
tb = gaze_buffer.time( trial_start_gbidx:trial_end_gbidx, 1 );
time_trial = (tb(:) - tb(1))*1000; % convert time buffer to ms from visual onset
% save sampling rate in Hz
sr = etDetermineSampleRate(tb*1000000); % see function below
trial_data.SamplingRate(t,1) = sr;
% segment X buffer
gaze_lx = gaze_buffer.left_gaze_x( trial_start_gbidx:trial_end_gbidx, 1);
gaze_rx = gaze_buffer.right_gaze_x( trial_start_gbidx:trial_end_gbidx, 1);
% segment Y buffer
gaze_ly = gaze_buffer.left_gaze_y( trial_start_gbidx:trial_end_gbidx, 1);
gaze_ry = gaze_buffer.right_gaze_y( trial_start_gbidx:trial_end_gbidx, 1);
%% Save mean distance to screen
% Lowe recommended using gaze_buffer.left_eye_cam_y and right to get distance
distance_ly = gaze_buffer.left_eye_cam_y( trial_start_gbidx:trial_end_gbidx, 1);
distance_ry = gaze_buffer.right_eye_cam_y( trial_start_gbidx:trial_end_gbidx, 1);
trial_data.DistanceScreen(t,1) = nanmean( mean( [distance_ly, distance_ry] ,2, 'omitnan') );
%% Save missing data before interpolation for the entire trial
RawMissing = (isnan(gaze_lx) & isnan(gaze_rx)) | (isnan( gaze_ly) & isnan( gaze_ry) );
trial_data.MissingRawGazeTrial(t,1) = sum(RawMissing) / length(RawMissing) ;
%% interpolate and average eyes
% Data was interpolated linearly over gaps in the data shorter
% than 150 ms (as in Kleberg 2019, same as CBCD/Luke Mason's scripts)
[mb, flags] = etInterpBuffer(gaze_lx,gaze_ly, gaze_rx,gaze_ry, tb, interp_maxGap); % see function below
lx_out = mb(:, 1);
ly_out = mb(:, 2);
rx_out = mb(:, 3);
ry_out = mb(:, 4);
gaze_x_trial = mean( [lx_out, rx_out] ,2, 'omitnan');
gaze_y_trial = mean( [ly_out, ry_out] ,2, 'omitnan');
% % get missing data after interpolation
% gaze_missing = isnan(gaze_x_trial) | isnan( gaze_y_trial) ;
%% Validate trial
% compute binary vectors for gaze on CS, on screen, outside screen
inAOIfixation =...
gaze_x_trial >= fixation_AOI(1) &...
gaze_x_trial <= fixation_AOI(3) &...
gaze_y_trial >= fixation_AOI(2) &...
gaze_y_trial <= fixation_AOI(4);
onScreen = ...
gaze_x_trial <= width_half &...
gaze_x_trial >= -width_half &...
gaze_y_trial <= height_half &...
gaze_y_trial >= -height_half;
outScreen = ...
gaze_x_trial > width_half |...
gaze_x_trial < -width_half |...
gaze_y_trial > height_half |...
gaze_y_trial < -height_half;
% 1. Trial invalid if interpolated gaze was not at the central
% attention-grabbing area/AOI for at least 40% of the 500 ms before
% sound onset
sound_start_gbidx = find(tb >= trial_audio_ebtime, 1, "first" );
check_cs_before = 500; %ms
check_cs_after = 0;
crit_minInCS = .4;
InCS_idx = sound_start_gbidx - (check_cs_before*sr/1000) : sound_start_gbidx + (check_cs_after*sr/1000);
trial_data.inCSbeforeSound(t,1) = sum( inAOIfixation(InCS_idx) )/ length(inAOIfixation(InCS_idx)) ;
trial_data.ValidInCSbeforeSound(t,1) = trial_data.inCSbeforeSound(t,1) >= crit_minInCS;
% 2. Trial invalid if interpolated gaze was not at the central
% attention-grabbing area during the 200 ms before visual onset
visual_start_gbidx = find(tb >= trial_visual_ebtime, 1, "first" );
check_cs_before = 200; %ms
check_cs_after = 0;
crit_minInCS = 1;
InCS_idx = visual_start_gbidx - (check_cs_before*sr/1000) : visual_start_gbidx + (check_cs_after*sr/1000);
trial_data.inCSbeforeArray(t,1) = sum( inAOIfixation(InCS_idx) )/ length( inAOIfixation(InCS_idx)) ;
trial_data.ValidInCSbeforeArray(t,1) = trial_data.inCSbeforeArray(t,1) >= crit_minInCS;
% 3.1 Trial invalid if interpolated gaze was missing or outside the screen
% (ie it was not inside the screen) during the 500 ms after visual
% onset
check_OnScreen_before = 0; %ms
check_OnScreen_after = 500;
crit_minOnScreen = 1;
OnScreen_idx = visual_start_gbidx - (check_OnScreen_before*sr/1000) : visual_start_gbidx + (check_OnScreen_after*sr/1000);
trial_data.onScreenAfterArray(t,1) = sum( onScreen(OnScreen_idx)) / length( onScreen(OnScreen_idx));
trial_data.ValidOnScreenAfterArray(t,1) = trial_data.onScreenAfterArray(t,1) >= crit_minOnScreen;
% 3.2 Trial invalid if interpolated gaze was missing or outside the screen
% (ie it was not inside the screen) for more than 25% during
% the subsequent 500ms,
check_OnScreen_after_start = 500; %ms
check_OnScreen_after_end = 1000;
crit_minOnScreen = .75;
OnScreen_idx = visual_start_gbidx + (check_OnScreen_after_start*sr/1000) : visual_start_gbidx + (check_OnScreen_after_end*sr/1000);
trial_data.onScreenAfterArray500to1000(t,1) = sum( onScreen(OnScreen_idx)) / length( onScreen(OnScreen_idx));
trial_data.ValidOnScreenAfterArray500to1000(t,1) = trial_data.onScreenAfterArray500to1000(t,1) >= crit_minOnScreen;
% % previous flags/criteria
% % 3. Trial invalid if any interpolated gaze was missing during the
% % 500 ms after visual onset
% check_Missing_before = 0; %ms
% check_Missing_after = 500;
% crit_maxMissing = 0;
% Missing_idx = visual_start_gbidx - (check_Missing_before*sr/1000) : visual_start_gbidx + (check_Missing_after*sr/1000);
% trial_data.MissingAfterArray500(t,1) = sum( gaze_missing(Missing_idx)) / length( gaze_missing(Missing_idx));
% trial_data.ValidMissingAfterArray500(t,1) = trial_data.MissingAfterArray500(t,1) <= crit_maxMissing;
%
% % 3. Trial invalid if interpolated gaze was missing more than .25
% % during the 500-2000 ms after visual onset
% check_Missing_after_start = 500; %ms
% check_Missing_after_end = 2000;
% crit_maxMissing = .25;
% Missing_idx = visual_start_gbidx + (check_Missing_after_start*sr/1000) : visual_start_gbidx + (check_Missing_after_end*sr/1000);
% trial_data.MissingAfterArray500to2000(t,1) = sum( gaze_missing(Missing_idx)) / length( gaze_missing(Missing_idx));
% trial_data.ValidMissingAfterArray500to2000(t,1) = trial_data.MissingAfterArray500to2000(t,1) <= crit_maxMissing;
%
%
% % 4. Trial invalid if there is interpolated gaze data outside the
% % screen in the 2000 ms after visual onset
% check_OutScreen_before = 0; %ms
% check_OutScreen_after = 1500;
% crit_minOutScreen = 0;
% OutScreen_idx = visual_start_gbidx - (check_OutScreen_before*sr/1000) : visual_start_gbidx + (check_OutScreen_after*sr/1000);
% trial_data.outScreenAfterArray(t,1) = sum( outScreen(OutScreen_idx)) / length( outScreen(OutScreen_idx));
% trial_data.ValidOutScreenAfterArray(t,1) = trial_data.outScreenAfterArray(t,1) <= crit_minOutScreen;
%% segment visual array presentation
% from visual onset to visual offset
visual_start_gbidx = find(tb >= trial_visual_ebtime, 1, "first" );
visual_end_gbidx = find(tb <= trial_end_ebtime, 1, "last" );
idx_visual = visual_start_gbidx:visual_end_gbidx;
gaze_x = gaze_x_trial(idx_visual);
gaze_y = gaze_y_trial(idx_visual);
time = tb(idx_visual);
% save total duration of presentation in seconds from number of
% samples collected and sampling rate (= 3 seconds)
trial_data.DurationArray(t,1) = size(gaze_x, 1) / sr;
% % get missing data after interpolation during the presentation
% missing = isnan(gaze_x) | isnan( gaze_y);
%% VISUAL RESPONSE STATS
% compute inAOI binary vectors
inAOIsocial =...
gaze_x >= social_AOI(1) &...
gaze_x <= social_AOI(3) &...
gaze_y >= social_AOI(2) &...
gaze_y <= social_AOI(4);
inAOIgeometric =...
gaze_x >= geometric_AOI(1) &...
gaze_x <= geometric_AOI(3) &...
gaze_y >= geometric_AOI(2) &...
gaze_y <= geometric_AOI(4);
inAOImanmade =...
gaze_x >= manmade_AOI(1) &...
gaze_x <= manmade_AOI(3) &...
gaze_y >= manmade_AOI(2) &...
gaze_y <= manmade_AOI(4);
inAOInatural =...
gaze_x >= natural_AOI(1) &...
gaze_x <= natural_AOI(3) &...
gaze_y >= natural_AOI(2) &...
gaze_y <= natural_AOI(4);
onScreen = ...
gaze_x <= width_half &...
gaze_x >= -width_half &...
gaze_y <= height_half &...
gaze_y >= -height_half;
% save total time on Screen from number of samples collected and
% sampling rate (in seconds)
trial_data.onScreenArray(t,1) = sum( onScreen) / sr;
% get time on each AOI from number of samples collected and
% sampling rate (in seconds)
trial_data.onAOI(t,1) = sum(inAOIsocial) / sr;
trial_data.onAOI(t,2) = sum(inAOIgeometric) / sr;
trial_data.onAOI(t,3) = sum(inAOImanmade) / sr;
trial_data.onAOI(t,4) = sum(inAOInatural) / sr;
% Find sample entries to each AOI then
% find contiguous sections of gaze (>= criteria in ms, critInAOI)
% in AOI during the visual presentation period to identify a "look"
critInAOI = 100; % minimum time (ms)
% this is 24 in CBCD/ Luke Mason's scripts
% Johan Kleberg used a different criteria based on 4 out of 5
% samples being in the AOI
% SOCIAL AOI
% find gaps inAOI
inAOIsocialCont = findcontig(inAOIsocial, true);
if ~isempty(inAOIsocialCont)
% save first sample in AOI
trial_data.inAOIRawLatency(t,1) = time(inAOIsocialCont(1,1)) - time(1);
% convert duration of contiguous sections to ms
inAOIsocialContMs = inAOIsocialCont(:, 3) * (1000 / sr);
% find the first section with a duration longer than the criterion
FirstAOIsocialContFound =...
find(inAOIsocialContMs >= critInAOI, 1, 'first');
if ~isempty(FirstAOIsocialContFound)
% look up the sample of the onset of the first section of gaze that
% met criterion, convert to time
trial_data.inAOILatency(t,1) = time(inAOIsocialCont(FirstAOIsocialContFound,1)) - time(1);
else
% if no sections met criterion, mark as missing
trial_data.inAOILatency(t,1) = -1;
end
else
% if no sections were found at all, mark as missing
trial_data.inAOILatency(t,1) = -1;
trial_data.inAOIRawLatency(t,1) = -1;
end
% GEOMETRIC AOI
inAOIgeometricCont = findcontig(inAOIgeometric, true);
if ~isempty(inAOIgeometricCont)
trial_data.inAOIRawLatency(t,2) = time(inAOIgeometricCont(1,1)) - time(1);
% convert duration of contiguous sections to ms
inAOIgeometricContMs = inAOIgeometricCont(:, 3) * (1000 / sr);
% find the first section with a duration longer than the criterion
FirstAOIgeometricContFound =...
find(inAOIgeometricContMs >= critInAOI, 1, 'first');
if ~isempty(FirstAOIgeometricContFound)
% look up the sample of the onset of the section of gaze that
% met criterion, convert to time
trial_data.inAOILatency(t,2) = time(inAOIgeometricCont(FirstAOIgeometricContFound,1)) - time(1);
else
% if no sections met criterion, mark as missing
trial_data.inAOILatency(t,2) = -1;
end
else
% if no sections were found at all, mark as missing
trial_data.inAOILatency(t,2) = -1;
trial_data.inAOIRawLatency(t,2) = -1;
end
% MANMADE AOI
inAOImanmadeCont = findcontig(inAOImanmade, true);
if ~isempty(inAOImanmadeCont)
trial_data.inAOIRawLatency(t,3) = time(inAOImanmadeCont(1,1)) - time(1);
% convert duration of contiguous sections to ms
inAOImanmadeContMs = inAOImanmadeCont(:, 3) * (1000 / sr);
% find the first section with a duration longer than the criterion
FirstAOImanmadeContFound =...
find(inAOImanmadeContMs >= critInAOI, 1, 'first');
if ~isempty(FirstAOImanmadeContFound)
% look up the sample of the onset of the section of gaze that
% met criterion, convert to time
trial_data.inAOILatency(t,3) = time(inAOImanmadeCont(FirstAOImanmadeContFound,1)) - time(1);
else
% if no sections met criterion, mark as missing
trial_data.inAOILatency(t,3) = -1;
end
else
% if no sections were found at all, mark as missing
trial_data.inAOILatency(t,3) = -1;
trial_data.inAOIRawLatency(t,3) = -1;
end
% NATURAL AOI
inAOInaturalCont = findcontig(inAOInatural, true);
if ~isempty(inAOInaturalCont)
trial_data.inAOIRawLatency(t,4) = time(inAOInaturalCont(1,1)) - time(1);
% convert duration of contiguous sections to ms
inAOInaturalContMs = inAOInaturalCont(:, 3) * (1000 / sr);
% find the first section with a duration longer than the criterion
FirstAOInaturalContFound =...
find(inAOInaturalContMs >= critInAOI, 1, 'first');
if ~isempty(FirstAOInaturalContFound)
% look up the sample of the onset of the section of gaze that
% met criterion, convert to time
trial_data.inAOILatency(t,4) = time(inAOInaturalCont(FirstAOInaturalContFound,1)) - time(1);
else
% if no sections met criterion, mark as missing
trial_data.inAOILatency(t,4) = -1;
end
else
% if no sections were found at all, mark as missing
trial_data.inAOILatency(t,4) = -1;
trial_data.inAOIRawLatency(t,4) = -1;
end
% Was there a look in any AOI?
% if so, save where the first latency happened (which stimuli
% category), wwhether it was the face, and save the latency
if any(trial_data.inAOILatency(t,:) > 0) % if there was a look
% which index is the first look?
target = find( trial_data.inAOILatency(t,:) > 0 &...
trial_data.inAOILatency(t,:) == min (trial_data.inAOILatency(t, trial_data.inAOILatency(t,:) > 0)) );
switch target
case 1
trial_data.FirstLook(t,1) = 'F'; % Face
trial_data.FirstLookFace(t,1) = 1;
case 2
trial_data.FirstLook(t,1) = 'G'; % Geometric
trial_data.FirstLookFace(t,1) = 0;
case 3
trial_data.FirstLook(t,1) = 'M'; % Manmade
trial_data.FirstLookFace(t,1) = 0;
case 4
trial_data.FirstLook(t,1) = 'N'; % Natural
trial_data.FirstLookFace(t,1) = 0;
end
trial_data.MinLatency(t,:) = min (trial_data.inAOILatency(t, trial_data.inAOILatency(t,:) > 0)) ;
else
% if no look was found then mark as missing
trial_data.FirstLook(t,1) = NaN;
trial_data.FirstLookFace(t,1) = NaN;
trial_data.MinLatency(t,1) = NaN;
end
%% continue to validate trial
% 4. Trial invalid if "look" was not detected at any AOI
% (entry which lasted 100 ms or more) during the entire visual
% presentation
trial_data.ValidGazeInAOI(t,1) = any(trial_data.inAOILatency(t,:) > 0);
% 5. Trial invalid if Latency to first AOI is below 200 ms or above 1 s
trial_data.ValidGazeLatency(t,1) = trial_data.MinLatency(t,1) >= .2 & trial_data.MinLatency(t,1) <= 1;
%% Validate trial based on all flags
trial_data.ValidGaze(t,1) = trial_data.ValidInCSbeforeSound(t,1) == 1 &&...
trial_data.ValidInCSbeforeArray(t,1) == 1 &&...
trial_data.ValidOnScreenAfterArray(t,1) == 1 &&...
trial_data.ValidOnScreenAfterArray500to1000(t,1) == 1 &&...
trial_data.ValidGazeInAOI(t,1) == 1 &&...
trial_data.ValidGazeLatency(t,1) == 1;
%% PUPIL RESPONSE STATS
% segment pupil data
pupilL = gaze_buffer.left_pupil_measure1( trial_start_gbidx:trial_end_gbidx, 1);
pupilR = gaze_buffer.right_pupil_measure1( trial_start_gbidx:trial_end_gbidx, 1);
% missing pupil
pupilL_miss = isnan(pupilL);
pupilR_miss = isnan(pupilR);
% smooth pupil data with a moving window average (100 ms window)
% using matlab function movmean(, 'omitnan') but replacing all
% missing data with NaN again after
mawindow_ms = 100; % 100 ms
window = (sr*mawindow_ms) / 1000; % in samples
pupilL_smooth = movmean(pupilL,window, 'omitnan');
pupilL_smooth(pupilL_miss) = NaN;
pupilR_smooth = movmean(pupilR,window, 'omitnan');
pupilR_smooth(pupilR_miss) = NaN;
% average the pupil signal
% if we have data for both eyes
if sum(~pupilL_miss) > 0 && sum(~pupilR_miss) > 0 && sum(~pupilL_miss & ~pupilR_miss) > 0
% average the pupil signal considering a dynamic offset
% using a function from pupil-size-master (Kret, & Sjak-Shie, 2019)
pupil_mean = genMeanDiaSamples(time_trial, pupilL_smooth, pupilR_smooth, ~pupilL_miss, ~pupilR_miss);
% Ana found some errors in this function when the begining of
% the signal was missing / only one eye was present so in some
% cases the normal mean was computed instead
% check which data stream has more data
streams = ['L', 'R', 'Mean'];
streams_miss = [sum(pupilL_miss), sum(pupilR_miss), sum(isnan(pupil_mean)) ];
idx_stream = find( streams_miss == min(streams_miss) );
if any(idx_stream == 3) && ~isempty(pupil_mean)
trial_data.PupilEye(t,1) = 1; % we used mean pupil with dynamic offset
else
pupil_mean = mean( [pupilL_smooth, pupilR_smooth] ,2, 'omitnan');
trial_data.PupilEye(t,1) = 2; % we used normal mean
end
else
% if one eye is completely missing do normal average which is going
% to take only one eye.
pupil_mean = mean( [pupilL_smooth, pupilR_smooth] ,2, 'omitnan');
trial_data.PupilEye(t,1) = 3; % we used only one eye
% % check which data stream as more data
% streams = ['L', 'R'];
% streams_miss = [sum(pupilL_miss) , sum(pupilR_miss)];
%
% idx_stream = find( streams_miss == min(streams_miss) );
%
% if idx_stream == 1
% trial_data.Pupil_eye(t,1) = 'Only L '; % we used left pupil
% elseif idx_stream == 2
% trial_data.Pupil_eye(t,1) = 'Only R '; % we used right pupil
% end
end
% save how much missing data before interpolation we had
trial_data.MissingRawPupil(t,1) = sum(isnan(pupil_mean)) / length(pupil_mean) ;
% Exclude invalid samples based on reasonable mm range (Kret, & Sjak-Shie, 2019)
pupil_mean(pupil_mean < 1.5) = NaN;
pupil_mean(pupil_mean > 9) = NaN;
% Exclude invalid samples based on outliers (Mathôt & Vilotijević, 2022)
Pmean = double( nanmean(pupil_mean));
P3STD = double( 3*std(pupil_mean, 'omitnan'));
pupil_mean(pupil_mean > Pmean + P3STD) = NaN;
pupil_mean(pupil_mean < Pmean - P3STD) = NaN;
% Save how much invalid data we excluded
trial_data.PupilOutside3STD(t,1) = sum( pupil_mean > Pmean + P3STD | pupil_mean < Pmean - P3STD ) / length(pupil_mean);
trial_data.PupilOutsideRange(t,1) = sum( pupil_mean < 1.5 | pupil_mean > 9 ) / length(pupil_mean);
% Exclude outside screen samples
pupil_mean(outScreen) = NaN;
% Interpolate missing/invalid pupil data
% linearly over gaps in the data shorter
% than 150 ms (Kleberg 2019, same as CBCD/Luke Mason's scripts)
pupil_miss = isnan(pupil_mean);
gaps = findcontig(pupil_miss, true);
% check there are missing gaps and also some pupil data
if size(gaps,1) > 0 && size(time_trial(~pupil_miss), 1) > 2 && ~isempty(interp_maxGap)
pupil_interp = interp1(time_trial(~pupil_miss) ...
,pupil_mean(~pupil_miss)...
,time_trial,'linear');
% select those runs that are higher than the maximum length that we
% want to interpolate over
large_gaps = gaps( gaps(:,3)* (1000 / sr) > interp_maxGap, : );
% loop through all invalid interpolated sections (i.e. missing
% data was more than criterion) and get where they happened
indexes_large_gaps = [];
for i = 1:size(large_gaps,1)
indexes_large_gaps = horzcat(indexes_large_gaps, linspace(large_gaps(i,1),large_gaps(i,2),large_gaps(i,3)));
end
pupil_interp(indexes_large_gaps) = NaN; % Set all values in large gaps to NaN again
else
% if there are no missing gaps just keep the mean pupil
pupil_interp = pupil_mean;
end
% BASELINE AND RESPONSE STATS
% save mean pupil size in the baseline
% as well as missing after interpolation
% baseline is defined as the 200 ms before audio onset
sound_start_gbidx = find(tb >= trial_audio_ebtime, 1, "first" );
duration_baseline_before = 200; %ms
duration_baseline_after = 0;
baseline_idx = sound_start_gbidx - (duration_baseline_before*sr/1000) : sound_start_gbidx + (duration_baseline_after*sr/1000);
trial_data.MeanBaseline(t,1) = mean( pupil_interp(baseline_idx) , 'omitnan');
trial_data.MissingBaseline(t,1) = sum( isnan( pupil_interp(baseline_idx) )) / size(baseline_idx,2);
% save mean pupil size in the response
% as well as missing after interpolation
% response is defined as the 2 seconds after visual onset
visual_start_gbidx = find(tb >= trial_visual_ebtime, 1, "first" );
duration_response_before = 0; %ms
duration_response_after = 2000;
response_idx = visual_start_gbidx - (duration_response_before*sr/1000) : visual_start_gbidx + (duration_response_after*sr/1000);
trial_data.MeanResponse(t,1) = mean( pupil_interp(response_idx) , 'omitnan');
trial_data.MissingResponse(t,1) = sum( isnan( pupil_interp(response_idx) )) / size(response_idx,2);
% save pupil dilation indexed as mean pupil response - mean pupil baseline
if ~isnan(trial_data.MeanBaseline(t,1)) && ~isnan(trial_data.MeanResponse(t,1))
trial_data.PupilDilation(t,1) = trial_data.MeanResponse(t,1) - trial_data.MeanBaseline(t,1);
else
% if we are missing baseline or response mark as missing
trial_data.PupilDilation(t,1) = NaN;
end
% Collect normalized pupil during baseline and during response for
% later average tracing / plots
pupil_normal = (pupil_interp - trial_data.MeanBaseline(t,1)) / trial_data.MeanBaseline(t,1);
pupil_all.response(:, t_for_B) = pupil_normal(response_idx);
pupil_all.baseline(:, t_for_R) = pupil_normal(baseline_idx);
t_for_B = t_for_B +1;
t_for_R = t_for_R +1;
%% plot trial data
if plot_figure &&...
any(t == trials_to_plot)
% plot gaze data
spx = subplot(2, 2, 1);
hold(spx, 'on')
title('Gaze plot')
plot(tb, gaze_lx, '-r', 'linewidth', 2)
plot(tb, gaze_ly, '-g', 'linewidth', 2)
plot(tb, gaze_rx, '-r', 'linewidth', 2)
plot(tb, gaze_ry, '-g', 'linewidth', 2)
plot(tb, gaze_x_trial, '-k', 'linewidth', 1)
plot(tb, gaze_y_trial, '-k', 'linewidth', 1)
% plot inCS validation period before sound onset
sound_start_gbidx = find(tb >= trial_audio_ebtime, 1, "first" );
check_cs_before = 500; %ms
check_cs_after = 0;
pos_sound = [trial_audio_ebtime - (check_cs_before/1000),...
-width_half,...
(check_cs_before/1000 + check_cs_after/1000),...
width_half*2];
rectangle('Position',pos_sound, 'FaceColor',[0.4940 0.1840 0.5560 0.20])
% plot inCS/ onScreen validation period around visual onset
check_cs_before = 200; %ms
check_cs_after = 500;
pos_visual = [trial_visual_ebtime - (check_cs_before/1000),...
-width_half,...
(check_cs_before/1000 + check_cs_after/1000),...
width_half*2];
rectangle('Position',pos_visual, 'FaceColor',[0.3010 0.7450 0.9330 0.20])
% plot sound onset
line([trial_audio_ebtime, trial_audio_ebtime],[-width_half, width_half], 'color', [0, 0, 0],...
'linewidth', 2);
% plot visual onset
line([trial_visual_ebtime, trial_visual_ebtime],[-width_half, width_half], 'color', [0, 0, 0],...
'linewidth', 2);
legend({'X gaze','Y gaze'},'Location','southwest')
ylim([-width_half, width_half]) % coordinates are X screen ones
xlim([tb(1), tb(end)])
% another plot: plot inAOI vectors
spx = subplot(2, 2, 3);
hold(spx, 'on')
title('AOI Hit plot')
scatter(time, inAOInatural*5, 100, 'm', 'filled')
scatter(time, inAOImanmade*4, 100, 'c', 'filled')
scatter(time, inAOIgeometric*3, 100, 'b', 'filled')
scatter(time, inAOIsocial*2, 100, 'g', 'filled')
scatter(tb, inAOIfixation*1, 100, 'r', 'filled')
line([trial_audio_ebtime, trial_audio_ebtime], [0.5,5.5], 'color', [0, 0, 0],...
'linewidth', 2);
line([trial_visual_ebtime, trial_visual_ebtime], [0.5,5.5], 'color', [0, 0, 0],...
'linewidth', 2);
ylim([0.5,5.5])
xlim([tb(1), tb(end)])
legend({'Natural','Manmade', 'Geometric', 'Face', 'Fixation'},'Location','northwest')
% another plot: plot pupil tracing
spx = subplot(2, 2, 2);
hold(spx, 'on')
title('Pupil size plot')
plot(tb, pupilL, '-b', 'linewidth', 2)
plot(tb, pupilR, '-m', 'linewidth', 2)
plot(tb, pupil_interp, '-k', 'linewidth', 1)
min_pupil = min([pupilL;pupilR; pupil_interp]);
max_pupil = max([pupilL;pupilR; pupil_interp]);
if ~isnan(min_pupil)
% pos = [x y w h]
% plot baseline period
pos_baseline = [trial_audio_ebtime - (duration_baseline_before/1000),...
min_pupil,...
(duration_baseline_before/1000 + duration_baseline_after/1000),...
max_pupil - min_pupil];
rectangle('Position',pos_baseline, 'FaceColor',[0.9290 0.6940 0.1250 0.20])
% plot response period
pos_response = [trial_visual_ebtime - (duration_response_before/1000),...
min_pupil,...
(duration_response_before/1000 + duration_response_after/1000),...
max_pupil - min_pupil];
rectangle('Position',pos_response, 'FaceColor',[0.8500 0.3250 0.0980 0.20])
line([trial_audio_ebtime, trial_audio_ebtime], [min_pupil-.1, max_pupil+.1], 'color', [0, 0, 0],...
'linewidth', 2);
line([trial_visual_ebtime, trial_visual_ebtime], [min_pupil-.1, max_pupil+.1], 'color', [0, 0, 0],...
'linewidth', 2);
xlim([tb(1), tb(end)])
ylim([min_pupil-.1, max_pupil+.1])
end
% another plot: plot some stats
spx = subplot(2, 2, 4);
% write down stats
hold(spx, 'on')
title('Stats')
text(0.1, 0.9, strcat('ISI = ', num2str( round(trial_data.isi(t,1)*1000)), 'ms '), 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.9, ['SR =', num2str( trial_data.SamplingRate(t,1) ), 'Hz'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.8, ['% In Fixation before Sound onset =', num2str( round( trial_data.inCSbeforeSound(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.8, ['% In Fixation before Visual onset =', num2str( round( trial_data.inCSbeforeArray(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.7, ['% On Screen after Visual onset =', num2str( round( trial_data.onScreenAfterArray(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.7, ['% On Screen 500-1000 ms =', num2str( round( trial_data.onScreenAfterArray500to1000(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.6, ['Min Latency Raw =', num2str( min( trial_data.inAOIRawLatency(t, trial_data.inAOIRawLatency(t,:) >=0 )) )], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.6, ['Min Latency =', num2str( min( trial_data.inAOILatency(t, trial_data.inAOILatency(t,:) >=0 )) )], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.5, ['% Miss Pupil during Baseline =', num2str( round( trial_data.MissingBaseline(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.5, ['% Miss Pupil during Response =', num2str( round( trial_data.MissingResponse(t,1)*100 )), '%'], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
% mean and STD of pupil size, max and min
text(0.1, 0.4, ['Pupil Mean =', num2str(mean(pupil_interp, 'omitnan'))], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.4, ['Pupil STD =', num2str(std(pupil_interp, 'omitnan'))], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.3, ['Pupil Minimum =', num2str(min(pupil_interp))], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.3, ['Pupil Maximum =', num2str(max(pupil_interp))], 'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.1, 0.2, ['Pupil outside 3STD =', num2str( round( trial_data.PupilOutside3STD(t,1) *100 )), '%'],...
'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.5, 0.2, ['Pupil outside Range (1.5-9) =', num2str( round( trial_data.PupilOutsideRange(t,1) *100 )), '%'],...
'linewidth', 1, 'edgecolor', [0, 0, 0]);
text(0.3, 0.1, ['Valid Trial? ', num2str(trial_data.ValidGaze(t,1)), ' (1=Yes, 0=No)'],...
'linewidth', 1, 'edgecolor', [0, 0, 0]);
set(gcf, 'WindowState', 'fullscreen')
saveas(gcf,['../results/', id_name, ' trial ', num2str(t),' plots.png'])
% pause;
close(gcf)
% another figure: plot "heatmap"
figure, scatter(gaze_x, gaze_y)