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.16.0/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:00<00:00, 26.1MB/s]
Checking integrity of pymovements-toy-dataset.zip
Extracting pymovements-toy-dataset.zip to data/ToyDataset/raw
100%|██████████| 20/20 [00:00<00:00, 46.34it/s]
[2]:
<pymovements.dataset.dataset.Dataset at 0x7faaf25d1e80>

Now let’s do some basic preprocessing:

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

dataset.gaze[0].frame.head()
100%|██████████| 20/20 [00:00<00:00, 22.71it/s]
100%|██████████| 20/20 [00:00<00:00, 48.88it/s]
[3]:
shape: (5, 8)
text_idpage_idtimestimuli_xstimuli_ypixelpositionvelocity
i64i64f64f64f64list[f64]list[f64]list[f64]
011.988145e6-1.0-1.0[206.8, 152.4][-10.697598, -8.852399][null, null]
011.988146e6-1.0-1.0[206.9, 152.1][-10.695183, -8.859678][null, null]
011.988147e6-1.0-1.0[207.0, 151.8][-10.692768, -8.866956][1.610194, -5.256267]
011.988148e6-1.0-1.0[207.1, 151.7][-10.690352, -8.869381][0.402548, -4.447465]
011.988149e6-1.0-1.0[207.0, 151.5][-10.692768, -8.874233][0.402561, -3.234462]

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, 21.30it/s]
[4]:
<pymovements.dataset.dataset.Dataset at 0x7faaf25d1e80>

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)
nameonsetoffsetdurationtext_idpage_id
stri64i64i64i64i64
"saccade"198832319883371401
"saccade"198854719885672001
"saccade"198873719887602301
"saccade"198876519887771201
"saccade"198901419890311701

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:08,  2.38it/s]
20it [00:00, 28.08it/s]
[6]:
<pymovements.dataset.dataset.Dataset at 0x7faaf25d1e80>

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)
nameonsetoffsetdurationtext_idpage_id
stri64i64i64i64i64
"fixation.idt"1988145198856341801
"fixation.idt"1988564198875018601
"fixation.idt"1988751198917842701
"fixation.idt"1989179198943625701
"fixation.idt"1989437198960016301
[8]:
dataset.events[0].frame.filter(pl.col('name') == 'fixation.ivt').head()
[8]:
shape: (5, 6)
nameonsetoffsetdurationtext_idpage_id
stri64i64i64i64i64
"fixation.ivt"1988147198832217501
"fixation.ivt"1988351198854619501
"fixation.ivt"1988592198873614401
"fixation.ivt"1988788198901322501
"fixation.ivt"1989060198917011001

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