Plot Heatmap from Eye Tracking Data

there are many ways/toolbox to plot a heat map from eye tracking data. but I prefer DIY.

here is the draft plot, source code can be found in the end.


here is the source code:

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
%% Data Structure Explanation
% Explanation from Eyelink Programers Guide 3.0

%--------------------------------------------------------
% dataEyelink =
%
% samples: [401341x4 double]()
% fixations: [3965x6 double]()
% saccades: [3964x9 double]()
% blinks: [1720x3 double]()
% triggers: [1080x3 double]()

%--------------------------------------------------------
% dataEyelink.samples
% column1 column2 column3 column4
% timepoint x y pupil size
%--------------------------------------------------------
% dataEyelink.fixations
% column1 column2 column3 column4 column5 column6
% tmepoint_start timepoint_end duration x y avg pupil size
%--------------------------------------------------------
% dataEyelink.saccades
% column1 column2 column3 column4 column5 column6
% tmepoint_start timepoint_end duration x_from y_from x_to
% column7 column8 column9
% y_to amplitude peak velocity
% in degrees degr/sec
%--------------------------------------------------------
% dataEyelink.blinks
% column1 column2 column3
% tmepoint_start timepoint_end duration
%--------------------------------------------------------
% dataEyelink.triggers
% column1 column2 column3
% tmepoint 1=SYNCON Trial(I also wrote trial number)
% 0=SYNCOFF
%--------------------------------------------------------



clear;
load xxxxEyelink.mat

%% variables
gaussSigma = 0.05;
posX = round(dataEyelink.fixations(:,4));
posY = round(dataEyelink.fixations(:,5));
gazeDuration = dataEyelink.fixations(:,3) / max(dataEyelink.fixations(:,3)); % rescale to 0-1

%% generating data for heatmap
gazedata = [posX/1024, posY/768](); % rescale to 0-1
gazedata = gazedata((gazedata(:, 1))\>0, :); % remove possible negative value...

%% make gaussians
figure;
[X,Y]() = meshgrid(0:0.001:1, 0:0.001:1);
z = zeros(size(X,1),size(X,2));

for i = 1:length(gazedata)
z = z + gazeDuration(i) * exp(-( ((X - gazedata(i,1)).^2 ./ (2*gaussSigma^2)) + ((Y - gazedata(i,2)).^2 ./ (2*gaussSigma^2)) ) );
end

mesh(X,Y,z); % plot the heatmap
colorbar;
caxis([0,300]());
view(0,90);


print('heatmap', '-dtiff','-r300');