Working with Local Dataset#

In this tutorial, we will show how to use your own local dataset with the Dataset class. The Dataset class can help you to manage and process your eyetracking data.

Preparations#

We import pymovements as the alias pm for convenience.

[1]:
import pymovements as pm
/home/docs/checkouts/readthedocs.org/user_builds/pymovements/envs/v0.15.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

For demonstration purposes, we will use the raw data provided by the Toy dataset, a sample dataset that comes with pymovements.

We will download the resources of this dataset the directory to simulate a local dataset for you. All downloaded archive files are automatically extracted and then removed. The directory of the dataset will be data/my_dataset.

After that we won’t use the python class anymore and delete the object (the files on your system will stay in place). Don’t worry if you’re confused about these lines as they are not relevant to your use case.

Just keep in mind that we now have some files with gaze data in the directory data/my_dataset.

[2]:
toy_dataset = pm.Dataset('ToyDataset', path='data/my_dataset')
toy_dataset.download(remove_finished=True)

del toy_dataset
Downloading http://github.com/aeye-lab/pymovements-toy-dataset/zipball/6cb5d663317bf418cec0c9abe1dde5085a8a8ebd/ to data/my_dataset/downloads/pymovements-toy-dataset.zip
pymovements-toy-dataset.zip: 100%|██████████| 3.06M/3.06M [00:00<00:00, 23.6MB/s]
Checking integrity of pymovements-toy-dataset.zip
Extracting pymovements-toy-dataset.zip to data/my_dataset/raw

Define your Experiment#

To use the Dataset class, we first need to create an Experiment instance. This class represents the properties of the experiment, such as the screen dimensions and sampling rate.

[3]:
experiment = pm.gaze.Experiment(
    screen_width_px=1280,
    screen_height_px=1024,
    screen_width_cm=38,
    screen_height_cm=30.2,
    distance_cm=68,
    origin='lower left',
    sampling_rate=1000,
)

Parameters for File Parsing#

We also define a filename_format which is a pattern expression used to match and extract values from filenames of data files in the dataset. For example, r'trial_{text_id:d}_{page_id:d}.csv' will match filenames that follow the pattern trial_{text_id}_{page_id}.csv and extract the values of text_id and page_id for each file.

[4]:
filename_format = r'trial_{text_id:d}_{page_id:d}.csv'

Both values of text_id and page_id are numeric. We can use a map to define the casting of these values.

[5]:
filename_format_dtypes = {
    'text_id': int,
    'page_id': int,
}

We can also adjust how the CSV files are read.

The column_map dictionary maps the original column names in the CSV files to the desired column names. Here the original column names are ‘timestamp’, ‘x’, and ‘y’, and the desired column names are ‘time’, ‘x_pix’, and ‘y_pix’, respectively.

[6]:
column_map = {
    'timestamp': 'time',
    'x': 'x_pix',
    'y': 'y_pix',
}

Here, we specify that the separator in the CSV files is a tab (‘:nbsphinx-math:`t’`).

[7]:
custom_read_kwargs = {
    'separator': '\t',
}

Define and load the Dataset#

Next we use all these definitions and create a DatasetDefinition by passing in the root directory, Experiment instance, and other optional parameters such as the filename regular expression and custom CSV reading parameters.

[8]:
dataset_definition = pm.DatasetDefinition(
    name='my_dataset',
    experiment=experiment,
    filename_format=filename_format,
    filename_format_dtypes=filename_format_dtypes,
    column_map=column_map,
    custom_read_kwargs=custom_read_kwargs,
)

Finally we create a Dataset instance by using the DatasetDefinition and specifying the directory path.

[9]:
dataset = pm.Dataset(
    definition=dataset_definition,
    path='data/my_dataset/',
)

If we have a root data directory which holds all your local datasets we can further need to define the paths of the dataset.

The dataset, raw, preprocessed, and events parameters define the names of the directories for the dataset, raw data, preprocessed data, and events data, respectively.

[10]:
dataset_paths = pm.DatasetPaths(
    root='data/',
    raw='raw',
    preprocessed='preprocessed',
    events='events',
)

dataset = pm.Dataset(
    definition=dataset_definition,
    path=dataset_paths,
)

Now let’s load the dataset into memory. Here we select a subset including the first page of texts with ID 1 and 2.

[11]:
subset = {
    'text_id': [1, 2],
    'page_id': 1,
}

dataset.load(subset=subset)
100%|██████████| 2/2 [00:00<00:00, 180.26it/s]
[11]:
<pymovements.dataset.dataset.Dataset at 0x7f8d41ee4100>

Use the Dataset#

Once we have created the Dataset instance, we can use its methods to preprocess and analyze data in our local dataset.

[12]:
dataset.gaze[0].frame
[12]:
shape: (23_054, 5)
text_idpage_idtimex_pixy_pix
i64i64f64f64f64
112.415266e6176.8140.2
112.415267e6176.7139.8
112.415268e6176.7139.3
112.415269e6176.6139.3
112.41527e6176.7139.3
112.415271e6176.8139.5
112.415272e6177.3139.8
112.415273e6177.8140.0
112.415274e6178.3140.0
112.415275e6178.3139.9
112.415276e6178.0140.2
112.415277e6177.7140.4
112.438308e6649.1633.7
112.438309e6648.8633.9
112.43831e6649.1634.1
112.438311e6649.6634.2
112.438312e6650.1634.1
112.438313e6650.0634.0
112.438314e6649.9633.9
112.438315e6649.9633.9
112.438316e6650.1633.7
112.438317e6650.2633.5
112.438318e6650.0633.2
112.438319e6649.7633.1

Here we use the pix2deg method to convert the pixel coordinates to degrees of visual angle.

[13]:
dataset.pix2deg()

dataset.gaze[0].frame
100%|██████████| 2/2 [00:00<00:00, 536.94it/s]
[13]:
shape: (23_054, 7)
text_idpage_idtimex_pixy_pixx_posy_pos
i64i64f64f64f64f64f64
112.415266e6176.8140.2-11.420403-9.148145
112.415267e6176.7139.8-11.422806-9.157834
112.415268e6176.7139.3-11.422806-9.169943
112.415269e6176.6139.3-11.42521-9.169943
112.41527e6176.7139.3-11.422806-9.169943
112.415271e6176.8139.5-11.420403-9.1651
112.415272e6177.3139.8-11.408386-9.157834
112.415273e6177.8140.0-11.396367-9.15299
112.415274e6178.3140.0-11.384348-9.15299
112.415275e6178.3139.9-11.384348-9.155412
112.415276e6178.0140.2-11.39156-9.148145
112.415277e6177.7140.4-11.398771-9.143301
112.438308e6649.1633.70.2401353.033792
112.438309e6648.8633.90.2326313.038748
112.43831e6649.1634.10.2401353.043704
112.438311e6649.6634.20.2526423.046182
112.438312e6650.1634.10.2651493.043704
112.438313e6650.0634.00.2626483.041226
112.438314e6649.9633.90.2601463.038748
112.438315e6649.9633.90.2601463.038748
112.438316e6650.1633.70.2651493.033792
112.438317e6650.2633.50.267653.028836
112.438318e6650.0633.20.2626483.021402
112.438319e6649.7633.10.2551443.018924

We can use the pos2vel method to calculate the velocity of the gaze position.

[14]:
dataset.pos2vel(method='savitzky_golay', window_length=7, polyorder=2)

dataset.gaze[0].frame
100%|██████████| 2/2 [00:00<00:00, 308.14it/s]
[14]:
shape: (23_054, 9)
text_idpage_idtimex_pixy_pixx_posy_posy_velx_vel
i64i64f64f64f64f64f64f64f64
112.415266e6176.8140.2-11.420403-9.148145-13.666945-5.235971
112.415267e6176.7139.8-11.422806-9.157834-9.630308-3.004237
112.415268e6176.7139.3-11.422806-9.169943-5.59367-0.772503
112.415269e6176.6139.3-11.42521-9.169943-1.5570321.459231
112.41527e6176.7139.3-11.422806-9.1699431.5569834.034446
112.415271e6176.8139.5-11.420403-9.16513.4599566.695697
112.415272e6177.3139.8-11.408386-9.1578343.200467.983442
112.415273e6177.8140.0-11.396367-9.152993.2005076.78167
112.415274e6178.3140.0-11.384348-9.152992.9410923.948804
112.415275e6178.3139.9-11.384348-9.1554123.4602540.343335
112.415276e6178.0140.2-11.39156-9.1481454.152379-1.717019
112.415277e6177.7140.4-11.398771-9.1433015.36358-1.974598
112.438308e6649.1633.70.2401353.0337920.708004-0.268006
112.438309e6648.8633.90.2326313.0387482.5664882.23337
112.43831e6649.1634.10.2401353.0437042.5664964.109403
112.438311e6649.6634.20.2526423.0461820.7079985.181423
112.438312e6650.1634.10.2651493.043704-0.5309934.73475
112.438313e6650.0634.00.2626483.041226-1.7699843.037385
112.438314e6649.9633.90.2601463.038748-2.6549871.518691
112.438315e6649.9633.90.2601463.038748-3.4515120.268004
112.438316e6650.1633.70.2651493.033792-3.982536-0.357339
112.438317e6650.2633.50.267653.028836-4.867566-1.667582
112.438318e6650.0633.20.2626483.021402-5.752595-2.977824
112.438319e6649.7633.10.2551443.018924-6.637625-4.288067