-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgazeplotter.py
483 lines (393 loc) · 14.2 KB
/
gazeplotter.py
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
# -*- coding: utf-8 -*-
#
# This file is part of PyGaze - the open-source toolbox for eye tracking
#
# PyGazeAnalyser is a Python module for easily analysing eye-tracking data
# Copyright (C) 2014 Edwin S. Dalmaijer
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>
# Gaze Plotter
#
# Produces different kinds of plots that are generally used in eye movement
# research, e.g. heatmaps, scanpaths, and fixation locations as overlays of
# images.
#
# version 2 (02 Jul 2014)
__author__ = "Edwin Dalmaijer"
# native
import os
# external
import numpy
import matplotlib
from matplotlib import pyplot, image
# # # # #
# LOOK
# COLOURS
# all colours are from the Tango colourmap, see:
# http://tango.freedesktop.org/Tango_Icon_Theme_Guidelines#Color_Palette
COLS = { "butter": [ '#fce94f',
'#edd400',
'#c4a000'],
"orange": [ '#fcaf3e',
'#f57900',
'#ce5c00'],
"chocolate": [ '#e9b96e',
'#c17d11',
'#8f5902'],
"chameleon": [ '#8ae234',
'#73d216',
'#4e9a06'],
"skyblue": [ '#729fcf',
'#3465a4',
'#204a87'],
"plum": [ '#ad7fa8',
'#75507b',
'#5c3566'],
"scarletred":[ '#ef2929',
'#cc0000',
'#a40000'],
"aluminium": [ '#eeeeec',
'#d3d7cf',
'#babdb6',
'#888a85',
'#555753',
'#2e3436'],
}
# FONT
FONT = { 'family': 'Ubuntu',
'size': 12}
matplotlib.rc('font', **FONT)
# # # # #
# FUNCTIONS
def draw_fixations(fixations, dispsize, imagefile=None, durationsize=True, durationcolour=True, alpha=0.5, savefilename=None):
"""Draws circles on the fixation locations, optionally on top of an image,
with optional weigthing of the duration for circle size and colour
arguments
fixations - a list of fixation ending events from a single trial,
as produced by edfreader.read_edf, e.g.
edfdata[trialnr]['events']['Efix']
dispsize - tuple or list indicating the size of the display,
e.g. (1024,768)
keyword arguments
imagefile - full path to an image file over which the heatmap
is to be laid, or None for no image; NOTE: the image
may be smaller than the display size, the function
assumes that the image was presented at the centre of
the display (default = None)
durationsize - Boolean indicating whether the fixation duration is
to be taken into account as a weight for the circle
size; longer duration = bigger (default = True)
durationcolour - Boolean indicating whether the fixation duration is
to be taken into account as a weight for the circle
colour; longer duration = hotter (default = True)
alpha - float between 0 and 1, indicating the transparancy of
the heatmap, where 0 is completely transparant and 1
is completely untransparant (default = 0.5)
savefilename - full path to the file in which the heatmap should be
saved, or None to not save the file (default = None)
returns
fig - a matplotlib.pyplot Figure instance, containing the
fixations
"""
# FIXATIONS
fix = parse_fixations(fixations)
# IMAGE
fig, ax = draw_display(dispsize, imagefile=imagefile)
# CIRCLES
# duration weigths
if durationsize:
siz = 3 * (fix['dur']/30.0)
else:
siz = 3 * numpy.median(fix['dur']/30.0)
if durationcolour:
col = fix['dur']
else:
col = COLS['chameleon'][2]
# draw circles
ax.scatter(fix['x'],fix['y'], s=siz, c=col, marker='o', cmap='jet', alpha=alpha, edgecolors='none')
# FINISH PLOT
# invert the y axis, as (0,0) is top left on a display
ax.invert_yaxis()
# save the figure if a file name was provided
if savefilename != None:
fig.savefig(savefilename)
return fig
def draw_heatmap(fixations, dispsize, imagefile=None, durationweight=True, alpha=0.5, savefilename=None):
"""Draws a heatmap of the provided fixations, optionally drawn over an
image, and optionally allocating more weight to fixations with a higher
duration.
arguments
fixations - a list of fixation ending events from a single trial,
as produced by edfreader.read_edf, e.g.
edfdata[trialnr]['events']['Efix']
dispsize - tuple or list indicating the size of the display,
e.g. (1024,768)
keyword arguments
imagefile - full path to an image file over which the heatmap
is to be laid, or None for no image; NOTE: the image
may be smaller than the display size, the function
assumes that the image was presented at the centre of
the display (default = None)
durationweight - Boolean indicating whether the fixation duration is
to be taken into account as a weight for the heatmap
intensity; longer duration = hotter (default = True)
alpha - float between 0 and 1, indicating the transparancy of
the heatmap, where 0 is completely transparant and 1
is completely untransparant (default = 0.5)
savefilename - full path to the file in which the heatmap should be
saved, or None to not save the file (default = None)
returns
fig - a matplotlib.pyplot Figure instance, containing the
heatmap
"""
# FIXATIONS
fix = parse_fixations(fixations)
# IMAGE
fig, ax = draw_display(dispsize, imagefile=imagefile)
# HEATMAP
# Gaussian
gwh = 200
gsdwh = gwh/6
gaus = gaussian(gwh,gsdwh)
# matrix of zeroes
strt = int(gwh/2)
heatmapsize = (int(dispsize[1] + 2*strt), int(dispsize[0] + 2*strt))
heatmap = numpy.zeros(heatmapsize, dtype=float)
# create heatmap
for i in range(0,len(fix['dur'])):
# get x and y coordinates
#x and y - indexes of heatmap array. must be integers
x = strt + int(fix['x'][i]) - int(gwh/2)
y = strt + int(fix['y'][i]) - int(gwh/2)
# correct Gaussian size if either coordinate falls outside of
# display boundaries
if (not 0 < x < dispsize[0]) or (not 0 < y < dispsize[1]):
hadj=[0,gwh];vadj=[0,gwh]
if 0 > x:
hadj[0] = abs(x)
x = 0
elif dispsize[0] < x:
hadj[1] = gwh - int(x-dispsize[0])
if 0 > y:
vadj[0] = abs(y)
y = 0
elif dispsize[1] < y:
vadj[1] = gwh - int(y-dispsize[1])
# add adjusted Gaussian to the current heatmap
try:
heatmap[y:y+vadj[1],x:x+hadj[1]] += gaus[vadj[0]:vadj[1],hadj[0]:hadj[1]] * fix['dur'][i]
except:
# fixation was probably outside of display
pass
else:
# add Gaussian to the current heatmap
heatmap[y:y+gwh,x:x+gwh] += gaus * fix['dur'][i]
# resize heatmap
heatmap = heatmap[strt:dispsize[1]+strt,strt:dispsize[0]+strt]
# remove zeros
lowbound = numpy.mean(heatmap[heatmap>0])
heatmap[heatmap<lowbound] = numpy.NaN
# draw heatmap on top of image
ax.imshow(heatmap, cmap='jet', alpha=alpha)
# FINISH PLOT
# invert the y axis, as (0,0) is top left on a display
ax.invert_yaxis()
# save the figure if a file name was provided
if savefilename != None:
fig.savefig(savefilename)
return fig
def draw_raw(x, y, dispsize, imagefile=None, savefilename=None):
"""Draws the raw x and y data
arguments
x - a list of x coordinates of all samples that are to
be plotted
y - a list of y coordinates of all samples that are to
be plotted
dispsize - tuple or list indicating the size of the display,
e.g. (1024,768)
keyword arguments
imagefile - full path to an image file over which the heatmap
is to be laid, or None for no image; NOTE: the image
may be smaller than the display size, the function
assumes that the image was presented at the centre of
the display (default = None)
savefilename - full path to the file in which the heatmap should be
saved, or None to not save the file (default = None)
returns
fig - a matplotlib.pyplot Figure instance, containing the
fixations
"""
# image
fig, ax = draw_display(dispsize, imagefile=imagefile)
# plot raw data points
ax.plot(x, y, 'o', color=COLS['aluminium'][0], markeredgecolor=COLS['aluminium'][5])
# invert the y axis, as (0,0) is top left on a display
ax.invert_yaxis()
# save the figure if a file name was provided
if savefilename != None:
fig.savefig(savefilename)
return fig
def draw_scanpath(fixations, saccades, dispsize, imagefile=None, alpha=0.5, savefilename=None):
"""Draws a scanpath: a series of arrows between numbered fixations,
optionally drawn over an image
arguments
fixations - a list of fixation ending events from a single trial,
as produced by edfreader.read_edf, e.g.
edfdata[trialnr]['events']['Efix']
saccades - a list of saccade ending events from a single trial,
as produced by edfreader.read_edf, e.g.
edfdata[trialnr]['events']['Esac']
dispsize - tuple or list indicating the size of the display,
e.g. (1024,768)
keyword arguments
imagefile - full path to an image file over which the heatmap
is to be laid, or None for no image; NOTE: the image
may be smaller than the display size, the function
assumes that the image was presented at the centre of
the display (default = None)
alpha - float between 0 and 1, indicating the transparancy of
the heatmap, where 0 is completely transparant and 1
is completely untransparant (default = 0.5)
savefilename - full path to the file in which the heatmap should be
saved, or None to not save the file (default = None)
returns
fig - a matplotlib.pyplot Figure instance, containing the
heatmap
"""
# image
fig, ax = draw_display(dispsize, imagefile=imagefile)
# FIXATIONS
# parse fixations
fix = parse_fixations(fixations)
# draw fixations
ax.scatter(fix['x'],fix['y'], s=(1 * fix['dur'] / 30.0), c=COLS['chameleon'][2], marker='o', cmap='jet', alpha=alpha, edgecolors='none')
# draw annotations (fixation numbers)
for i in range(len(fixations)):
ax.annotate(str(i+1), (fix['x'][i],fix['y'][i]), color=COLS['aluminium'][5], alpha=1, horizontalalignment='center', verticalalignment='center', multialignment='center')
# SACCADES
if saccades:
# loop through all saccades
for st, et, dur, sx, sy, ex, ey in saccades:
# draw an arrow between every saccade start and ending
ax.arrow(sx, sy, ex-sx, ey-sy, alpha=alpha, fc=COLS['aluminium'][0], ec=COLS['aluminium'][5], fill=True, shape='full', width=10, head_width=20, head_starts_at_zero=False, overhang=0)
# invert the y axis, as (0,0) is top left on a display
ax.invert_yaxis()
# save the figure if a file name was provided
if savefilename != None:
fig.savefig(savefilename)
return fig
# # # # #
# HELPER FUNCTIONS
def draw_display(dispsize, imagefile=None):
"""Returns a matplotlib.pyplot Figure and its axes, with a size of
dispsize, a black background colour, and optionally with an image drawn
onto it
arguments
dispsize - tuple or list indicating the size of the display,
e.g. (1024,768)
keyword arguments
imagefile - full path to an image file over which the heatmap
is to be laid, or None for no image; NOTE: the image
may be smaller than the display size, the function
assumes that the image was presented at the centre of
the display (default = None)
returns
fig, ax - matplotlib.pyplot Figure and its axes: field of zeros
with a size of dispsize, and an image drawn onto it
if an imagefile was passed
"""
# construct screen (black background)
_, ext = os.path.splitext(imagefile)
ext = ext.lower()
data_type = 'float32' if ext == '.png' else 'uint8'
screen = numpy.zeros((dispsize[1],dispsize[0],3), dtype=data_type)
# if an image location has been passed, draw the image
if imagefile != None:
# check if the path to the image exists
if not os.path.isfile(imagefile):
raise Exception("ERROR in draw_display: imagefile not found at '%s'" % imagefile)
# load image
img = image.imread(imagefile)
# flip image over the horizontal axis
# (do not do so on Windows, as the image appears to be loaded with
# the correct side up there; what's up with that? :/)
if not os.name == 'nt':
img = numpy.flipud(img)
# width and height of the image
w, h = len(img[0]), len(img)
# x and y position of the image on the display
x = int(dispsize[0]/2 - w/2)
y = int(dispsize[1]/2 - h/2)
# draw the image on the screen
screen[int(y):int(y+h),int(x):int(x+w),:] += img
# dots per inch
dpi = 100.0
# determine the figure size in inches
figsize = (dispsize[0]/dpi, dispsize[1]/dpi)
# create a figure
fig = pyplot.figure(figsize=figsize, dpi=dpi, frameon=False)
ax = pyplot.Axes(fig, [0,0,1,1])
ax.set_axis_off()
fig.add_axes(ax)
# plot display
ax.axis([0,dispsize[0],0,dispsize[1]])
ax.imshow(screen)#, origin='upper')
return fig, ax
def gaussian(x, sx, y=None, sy=None):
"""Returns an array of numpy arrays (a matrix) containing values between
1 and 0 in a 2D Gaussian distribution
arguments
x -- width in pixels
sx -- width standard deviation
keyword argments
y -- height in pixels (default = x)
sy -- height standard deviation (default = sx)
"""
# square Gaussian if only x values are passed
if y == None:
y = x
if sy == None:
sy = sx
# centers
xo = x/2
yo = y/2
# matrix of zeros
M = numpy.zeros([y,x],dtype=float)
# gaussian matrix
for i in range(x):
for j in range(y):
M[j,i] = numpy.exp(-1.0 * (((float(i)-xo)**2/(2*sx*sx)) + ((float(j)-yo)**2/(2*sy*sy)) ) )
return M
def parse_fixations(fixations):
"""Returns all relevant data from a list of fixation ending events
arguments
fixations - a list of fixation ending events from a single trial,
as produced by edfreader.read_edf, e.g.
edfdata[trialnr]['events']['Efix']
returns
fix - a dict with three keys: 'x', 'y', and 'dur' (each contain
a numpy array) for the x and y coordinates and duration of
each fixation
"""
# empty arrays to contain fixation coordinates
fix = { 'x':numpy.zeros(len(fixations)),
'y':numpy.zeros(len(fixations)),
'dur':numpy.zeros(len(fixations))}
# get all fixation coordinates
for fixnr in range(len( fixations)):
stime, etime, dur, ex, ey = fixations[fixnr]
fix['x'][fixnr] = ex
fix['y'][fixnr] = ey
fix['dur'][fixnr] = dur
return fix