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.9.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.2MB/s]

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_regex which is a regular expression used to match and extract values from filenames of data files in the dataset. For example, r'trial_(?P<text_id>\d+)_(?P<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_regex = r'trial_(?P<text_id>\d+)_(?P<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_regex_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_regex=filename_regex,
    filename_regex_dtypes=filename_regex_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, 192.98it/s]
[11]:
<pymovements.dataset.dataset.Dataset at 0x7f41aa054df0>

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: (23054, 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, 515.75it/s]
[13]:
shape: (23054, 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, 299.00it/s]
[14]:
shape: (23054, 9)
text_idpage_idtimex_pixy_pixx_posy_posx_vely_vel
i64i64f64f64f64f64f64f64f64
112.415266e6176.8140.2-11.420403-9.148145-5.235971-13.666945
112.415267e6176.7139.8-11.422806-9.157834-3.004237-9.630308
112.415268e6176.7139.3-11.422806-9.169943-0.772503-5.59367
112.415269e6176.6139.3-11.42521-9.1699431.459231-1.557032
112.41527e6176.7139.3-11.422806-9.1699434.0344461.556983
112.415271e6176.8139.5-11.420403-9.16516.6956973.459956
112.415272e6177.3139.8-11.408386-9.1578347.9834423.20046
112.415273e6177.8140.0-11.396367-9.152996.781673.200507
112.415274e6178.3140.0-11.384348-9.152993.9488042.941092
112.415275e6178.3139.9-11.384348-9.1554120.3433353.460254
112.415276e6178.0140.2-11.39156-9.148145-1.7170194.152379
112.415277e6177.7140.4-11.398771-9.143301-1.9745985.36358
112.438308e6649.1633.70.2401353.033792-0.2680060.708004
112.438309e6648.8633.90.2326313.0387482.233372.566488
112.43831e6649.1634.10.2401353.0437044.1094032.566496
112.438311e6649.6634.20.2526423.0461825.1814230.707998
112.438312e6650.1634.10.2651493.0437044.73475-0.530993
112.438313e6650.0634.00.2626483.0412263.037385-1.769984
112.438314e6649.9633.90.2601463.0387481.518691-2.654987
112.438315e6649.9633.90.2601463.0387480.268004-3.451512
112.438316e6650.1633.70.2651493.033792-0.357339-3.982536
112.438317e6650.2633.50.267653.028836-1.667582-4.867566
112.438318e6650.0633.20.2626483.021402-2.977824-5.752595
112.438319e6649.7633.10.2551443.018924-4.288067-6.637625