{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Working with Local Dataset" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "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." ] }, { "cell_type": "markdown", "id": "2", "metadata": {}, "source": [ "## Preparations" ] }, { "cell_type": "markdown", "id": "3", "metadata": {}, "source": [ "We import `pymovements` as the alias `pm` for convenience." ] }, { "cell_type": "code", "execution_count": null, "id": "4", "metadata": {}, "outputs": [], "source": [ "import pymovements as pm" ] }, { "cell_type": "markdown", "id": "5", "metadata": {}, "source": [ "For demonstration purposes, we will use the raw data provided by the Toy dataset, a sample dataset that comes with *pymovements*.\n", "\n", "We will download the resources of this dataset the directory to simulate a local dataset for you.\n", "All downloaded archive files are automatically extracted and then removed.\n", "The directory of the dataset will be `data/my_dataset`.\n", "\n", "After that we won't use the python class anymore and delete the object\n", "(the files on your system will stay in place).\n", "Don't worry if you're confused about these lines as they are not relevant to your use case.\n", "\n", "Just keep in mind that we now have some files with gaze data in the directory `data/my_dataset`." ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "toy_dataset = pm.Dataset('ToyDataset', path='data/my_dataset')\n", "toy_dataset.download(remove_finished=True)\n", "\n", "del toy_dataset" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "## Define your Experiment\n", "\n", "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." ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "experiment = pm.gaze.Experiment(\n", " screen_width_px=1280,\n", " screen_height_px=1024,\n", " screen_width_cm=38,\n", " screen_height_cm=30.2,\n", " distance_cm=68,\n", " origin='upper left',\n", " sampling_rate=1000,\n", ")" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "## Parameters for File Parsing" ] }, { "cell_type": "markdown", "id": "10", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "filename_format = r'trial_{text_id:d}_{page_id:d}.csv'" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "Both values of `text_id` and `page_id` are numeric. We can use a map to define the casting of these values." ] }, { "cell_type": "code", "execution_count": null, "id": "13", "metadata": {}, "outputs": [], "source": [ "filename_format_dtypes = {\n", " 'text_id': int,\n", " 'page_id': int,\n", "}" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "We can also adjust how the CSV files are read.\n", "Here, we specify that the separator in the CSV files is a tab (**'\\t'**)." ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "custom_read_kwargs = {\n", " 'separator': '\\t',\n", "}" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "## Column Definitions" ] }, { "cell_type": "markdown", "id": "17", "metadata": {}, "source": [ "The `trial_columns` argument can be used to specify which columns define a single trial.\n", "\n", "This is important for correctly applying all preprocessing methods.\n", "\n", "For this very small single user dataset a trial ist just defined by `text_id` and `page_id`." ] }, { "cell_type": "code", "execution_count": null, "id": "18", "metadata": {}, "outputs": [], "source": [ "trial_columns = ['text_id', 'page_id']" ] }, { "cell_type": "markdown", "id": "19", "metadata": {}, "source": [ "The `time_column` and `pixel_columns` arguments can be used to correctly map the columns in your dataframes. If the time unit differs from the default milliseconds `ms` one must also specify the `time_unit` for correct computations.\n", "\n", "Depending on the content of your dataset, you can alternatively also provide `position_columns`, `velocity_columns` and `acceleration_columns`.\n", "\n", "Specifying these columns is needed for correctly applying preprocessing methods. For example, if you want to apply the `pix2deg` method, you will need to specify `pixel_columns` accordingly.\n", "\n", "If your dataset has gaze positions available only in degrees of visual angle, you have to specify the `position_columns` instead." ] }, { "cell_type": "code", "execution_count": null, "id": "20", "metadata": {}, "outputs": [], "source": [ "time_column = 'timestamp'\n", "time_unit = 'ms'\n", "pixel_columns = ['x', 'y']" ] }, { "cell_type": "markdown", "id": "21", "metadata": {}, "source": [ "## Define and load the Dataset" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "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." ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "dataset_definition = pm.DatasetDefinition(\n", " name='my_dataset',\n", " experiment=experiment,\n", " filename_format=filename_format,\n", " filename_format_dtypes=filename_format_dtypes,\n", " custom_read_kwargs=custom_read_kwargs,\n", " time_column=time_column,\n", " time_unit=time_unit,\n", " pixel_columns=pixel_columns,\n", ")" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "Finally we create a `Dataset` instance by using the `DatasetDefinition` and specifying the directory path." ] }, { "cell_type": "code", "execution_count": null, "id": "25", "metadata": {}, "outputs": [], "source": [ "dataset = pm.Dataset(\n", " definition=dataset_definition,\n", " path='data/my_dataset/',\n", ")" ] }, { "cell_type": "markdown", "id": "26", "metadata": {}, "source": [ "If we have a root data directory which holds all your local datasets we can further need to define the paths of the dataset.\n", "\n", "The `dataset`, `raw`, `preprocessed`, and `events` parameters define the names of the directories for the dataset, raw data, preprocessed data, and events data, respectively." ] }, { "cell_type": "code", "execution_count": null, "id": "27", "metadata": {}, "outputs": [], "source": [ "dataset_paths = pm.DatasetPaths(\n", " root='data/',\n", " raw='raw',\n", " preprocessed='preprocessed',\n", " events='events',\n", ")\n", "\n", "dataset = pm.Dataset(\n", " definition=dataset_definition,\n", " path=dataset_paths,\n", ")" ] }, { "cell_type": "markdown", "id": "28", "metadata": {}, "source": [ "Now let's load the dataset into memory. Here we select a subset including the first page of texts with ID 1 and 2." ] }, { "cell_type": "code", "execution_count": null, "id": "29", "metadata": {}, "outputs": [], "source": [ "subset = {\n", " 'text_id': [1, 2],\n", " 'page_id': 1,\n", "}\n", "\n", "dataset.load(subset=subset)" ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "## Use the Dataset" ] }, { "cell_type": "markdown", "id": "31", "metadata": {}, "source": [ "Once we have created the Dataset instance, we can use its methods to preprocess and analyze data in our local dataset." ] }, { "cell_type": "code", "execution_count": null, "id": "32", "metadata": {}, "outputs": [], "source": [ "dataset.gaze[0].frame" ] }, { "cell_type": "markdown", "id": "33", "metadata": {}, "source": [ "Here we use the `pix2deg` method to convert the pixel coordinates to degrees of visual angle." ] }, { "cell_type": "code", "execution_count": null, "id": "34", "metadata": {}, "outputs": [], "source": [ "dataset.pix2deg()\n", "\n", "dataset.gaze[0].frame" ] }, { "cell_type": "markdown", "id": "35", "metadata": {}, "source": [ "We can use the `pos2vel` method to calculate the velocity of the gaze position." ] }, { "cell_type": "code", "execution_count": null, "id": "36", "metadata": {}, "outputs": [], "source": [ "dataset.pos2vel(method='savitzky_golay', degree=2, window_length=7)\n", "\n", "dataset.gaze[0].frame" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }