Detecting Gaze Events#

What you will learn in this tutorial:#

  • how to detect saccades using the microsaccades algorithm

  • how to detect fixations using the I-DT and I-VT algorithms

Preparations#

We import pymovements as the alias pm for convenience.

[1]:
import polars as pl

import pymovements as pm
/home/docs/checkouts/readthedocs.org/user_builds/pymovements/envs/v0.17.2/lib/python3.9/site-packages/tqdm/auto.py:21: TqdmWarning: IProgress not found. Please update jupyter and ipywidgets. See https://ipywidgets.readthedocs.io/en/stable/user_install.html
  from .autonotebook import tqdm as notebook_tqdm

Let’s start by downloading our ToyDataset and loading in its data:

[2]:
dataset = pm.Dataset('ToyDataset', path='data/ToyDataset')
dataset.download()
dataset.load()
Downloading http://github.com/aeye-lab/pymovements-toy-dataset/zipball/6cb5d663317bf418cec0c9abe1dde5085a8a8ebd/ to data/ToyDataset/downloads/pymovements-toy-dataset.zip
pymovements-toy-dataset.zip: 100%|██████████| 3.06M/3.06M [00:01<00:00, 2.61MB/s]
Checking integrity of pymovements-toy-dataset.zip
Extracting pymovements-toy-dataset.zip to data/ToyDataset/raw
100%|██████████| 20/20 [00:00<00:00, 42.28it/s]
[2]:
<pymovements.dataset.dataset.Dataset at 0x7f165a540970>

Now let’s do some basic preprocessing:

[3]:
dataset.pix2deg()
dataset.pos2vel('smooth')

dataset.gaze[0].frame.head()
100%|██████████| 20/20 [00:01<00:00, 19.29it/s]
100%|██████████| 20/20 [00:00<00:00, 34.73it/s]
[3]:
shape: (5, 8)
text_idpage_idtimestimuli_xstimuli_ypixelpositionvelocity
i64i64f32f32f32list[f32]list[f32]list[f32]
011.988145e6-1.0-1.0[206.800003, 152.399994][-10.697598, -8.8524][null, null]
011.988146e6-1.0-1.0[206.899994, 152.100006][-10.695184, -8.859678][null, null]
011.988147e6-1.0-1.0[207.0, 151.800003][-10.692768, -8.866957][1.610438, -5.256017]
011.988148e6-1.0-1.0[207.100006, 151.699997][-10.690351, -8.869382][0.40261, -4.447301]
011.988149e6-1.0-1.0[207.0, 151.5][-10.692768, -8.874233][0.402451, -3.234386]

Detecting Events#

pymovements provides a range of event detection methods for several types of gaze events.

See the reference for pymovements.events to get an overview of all the supported methods.

For this tutorial we will use the microsaccades algorithm for detecting saccades and the I-DT and I-VT algorithms for detecting fixations.

We start out by detecting saccades.

[4]:
dataset.detect_events('microsaccades', minimum_duration=12)
20it [00:00, 65.36it/s]
[4]:
<pymovements.dataset.dataset.Dataset at 0x7f165a540970>

The detected events are added as rows with the name saccade to the event dataframe:

[5]:
dataset.events[0].frame.head()
[5]:
shape: (5, 6)
text_idpage_idnameonsetoffsetduration
i64i64stri64i64i64
01"saccade"1988323198833714
01"saccade"1988547198856720
01"saccade"1988737198876023
01"saccade"1988765198877712
01"saccade"1989014198903117

Next we will detect fixations using the I-DT and I-VT algorithms.

To be able to differentiate between the detected events, we specify custom event names for each algorithm:

[6]:
dataset.detect_events('idt', dispersion_threshold=2.7, name='fixation.idt')
dataset.detect_events('ivt', velocity_threshold=20, name='fixation.ivt')
20it [00:09,  2.08it/s]
20it [00:00, 285.21it/s]
[6]:
<pymovements.dataset.dataset.Dataset at 0x7f165a540970>

This has added new rows with the fixation events to the event dataframe.

[7]:
dataset.events[0].frame.filter(pl.col('name') == 'fixation.idt').head()
[7]:
shape: (5, 6)
text_idpage_idnameonsetoffsetduration
i64i64stri64i64i64
nullnull"fixation.idt"19881451988563418
nullnull"fixation.idt"19885641988750186
nullnull"fixation.idt"19887511989178427
nullnull"fixation.idt"19891791989436257
nullnull"fixation.idt"19894371989600163
[8]:
dataset.events[0].frame.filter(pl.col('name') == 'fixation.ivt').head()
[8]:
shape: (5, 6)
text_idpage_idnameonsetoffsetduration
i64i64stri64i64i64
nullnull"fixation.ivt"19881471988322175
nullnull"fixation.ivt"19883511988546195
nullnull"fixation.ivt"19885921988736144
nullnull"fixation.ivt"19887881989013225
nullnull"fixation.ivt"19890601989170110

What you have learned in this tutorial:#

  • detecting saccades by using the microsaccades algorithm

  • detecting fixations by using the I-DT and I-VT algorithms