{ "cells": [ { "cell_type": "markdown", "id": "0", "metadata": {}, "source": [ "# Saving and Loading Event Data" ] }, { "cell_type": "markdown", "id": "1", "metadata": {}, "source": [ "## What you will learn in this tutorial:\n", "\n", "* how to save your event data\n", "* how to load your event 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": [ "Let's start by downloading our `ToyDataset` and loading in its data:" ] }, { "cell_type": "code", "execution_count": null, "id": "6", "metadata": {}, "outputs": [], "source": [ "dataset = pm.Dataset('ToyDataset', path='data/ToyDataset')\n", "dataset.download()\n", "dataset.load()" ] }, { "cell_type": "markdown", "id": "7", "metadata": {}, "source": [ "Now let's do some preprocessing:" ] }, { "cell_type": "code", "execution_count": null, "id": "8", "metadata": {}, "outputs": [], "source": [ "dataset.pix2deg()\n", "dataset.pos2vel()\n", "\n", "dataset.gaze[0].frame.head()" ] }, { "cell_type": "markdown", "id": "9", "metadata": {}, "source": [ "Detect some events:" ] }, { "cell_type": "code", "execution_count": null, "id": "10", "metadata": {}, "outputs": [], "source": [ "dataset.detect_events('ivt')\n", "\n", "dataset.events[0].frame.head()" ] }, { "cell_type": "code", "execution_count": null, "id": "11", "metadata": {}, "outputs": [], "source": [ "dataset.detect_events('microsaccades')\n", "\n", "dataset.events[0].frame.head()" ] }, { "cell_type": "markdown", "id": "12", "metadata": {}, "source": [ "We now have event dataframes available with our detected event data." ] }, { "cell_type": "markdown", "id": "13", "metadata": {}, "source": [ "## Saving" ] }, { "cell_type": "markdown", "id": "14", "metadata": {}, "source": [ "Saving your event data is as simple as:" ] }, { "cell_type": "code", "execution_count": null, "id": "15", "metadata": {}, "outputs": [], "source": [ "dataset.save_events()" ] }, { "cell_type": "markdown", "id": "16", "metadata": {}, "source": [ "All of the event data is saved into this directory:" ] }, { "cell_type": "code", "execution_count": null, "id": "17", "metadata": {}, "outputs": [], "source": [ "dataset.paths.events" ] }, { "cell_type": "markdown", "id": "18", "metadata": {}, "source": [ "Let's confirm it by printing all the new files in this directory:" ] }, { "cell_type": "code", "execution_count": null, "id": "19", "metadata": {}, "outputs": [], "source": [ "print(list(dataset.paths.events.glob('*/*/*')))" ] }, { "cell_type": "markdown", "id": "20", "metadata": {}, "source": [ "All of the files have been saved into the `Dataset.paths.events` as `feather` files.\n", "\n", "If we want to save the data into an alternative directory and also use a different file format like `csv` we can use the following:" ] }, { "cell_type": "code", "execution_count": null, "id": "21", "metadata": {}, "outputs": [], "source": [ "dataset.save_events(events_dirname='events_csv', extension='csv')" ] }, { "cell_type": "markdown", "id": "22", "metadata": {}, "source": [ "Let's confirm again by printing all the new files in this alternative directory:" ] }, { "cell_type": "code", "execution_count": null, "id": "23", "metadata": {}, "outputs": [], "source": [ "alternative_dirpath = dataset.path / 'events_csv'\n", "print(list(alternative_dirpath.glob('*/*/*')))" ] }, { "cell_type": "markdown", "id": "24", "metadata": {}, "source": [ "## Loading" ] }, { "cell_type": "markdown", "id": "25", "metadata": {}, "source": [ "Now let's imagine that this preprocessing and saving was done in another file and we only want to load the preprocessed data.\n", "\n", "We simulate this by initializing a new dataset object. We don't need to download any additional data." ] }, { "cell_type": "code", "execution_count": null, "id": "26", "metadata": {}, "outputs": [], "source": [ "preprocessed_dataset = pm.Dataset('ToyDataset', path='data/ToyDataset')" ] }, { "cell_type": "markdown", "id": "27", "metadata": {}, "source": [ "The preprocessed data can now simply be loaded by setting `preprocessed` to `True`:" ] }, { "cell_type": "code", "execution_count": null, "id": "28", "metadata": {}, "outputs": [], "source": [ "preprocessed_dataset.load(events=True)\n", "\n", "dataset.events[0].frame.head()" ] }, { "cell_type": "markdown", "id": "29", "metadata": {}, "source": [ "By default, the `events` directory and the `feather` extension will be chosen." ] }, { "cell_type": "markdown", "id": "30", "metadata": {}, "source": [ "In case of alternative directory names or other file formats you can use the following:" ] }, { "cell_type": "code", "execution_count": null, "id": "31", "metadata": {}, "outputs": [], "source": [ "preprocessed_dataset.load(\n", " events=True,\n", " events_dirname='events_csv',\n", " extension='csv',\n", ")\n", "dataset.events[0].frame.head()" ] }, { "cell_type": "markdown", "id": "32", "metadata": {}, "source": [ " " ] }, { "cell_type": "markdown", "id": "33", "metadata": {}, "source": [ "## What you have learned in this tutorial:\n", "\n", "* saving your preprocesed data using `Dataset.save_preprocessed()`\n", "* load your preprocesed data using `Dataset.load(preprocessed=True)`\n", "* using custom directory names by specifying `preprocessed_dirname`\n", "* using other file formats than the default `feather` format by specifying `extension`" ] } ], "metadata": {}, "nbformat": 4, "nbformat_minor": 5 }