{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Phosphene fading data from Perez Fornos et al. (2012)\n\nThis example shows how to use the Perez Fornos et al. (2012) dataset.\n\n[PerezFornos2012]_ had nine Argus II users report perceived phosphene brightness\nvia joystick position.\n\n.. important ::\n\n    You will need to install [Pandas](https://pandas.pydata.org)\n    (``pip install pandas``) for this dataset.\n\n## Loading the dataset\n\nThe dataset can be loaded as a Pandas ``DataFrame``:\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimport matplotlib.pyplot as plt\nfrom pulse2percept.datasets import load_perezfornos2012\n\ndata = load_perezfornos2012()\nprint(data)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Inspecting the DataFrame tells us that there are 45 measurements\n(the rows) each with 3 different attributes (the columns).\n\nThese attributes include specifiers such as \"subject\", \"figure\", and\n\"time_series\". We can print all column names using:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data.columns"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        ".. note ::\n\n    The meaning of all column names is explained in the docstring of\n    the :py:func:`~pulse2percept.datasets.load_fornos2012` function.\n\nFor example, \"subject\" corresponds to each subject used in the paper:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "data.subject.unique()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "To select all the rows where the subject frequency was 'S2', we can index\ninto the DataFrame as follows:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(data[data.subject == 'S2'])"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "However, we can do the same operation when loading in the data as follows:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(load_perezfornos2012(subjects='S2'))"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This leaves us with 4 rows, each pertaining to subject 'S2'.\n\n.. note ::\n\n    Please see the documentation for\n    :py:func:`~pulse2percept.datasets.load_perezfornos2012` to see all\n    available parameters for data subset loading.\n\n## Plotting the data\n\nOne of the important points in the paper is how the perceptual response varies\namongst subjects.\nLet us recreate Figure 3, for Subject 2, to show an example of this\ndifference:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Load in each figures data:\nfigure_3_s1_data = load_perezfornos2012(figures='fig3_S1')['time_series'][0]\nfigure_3_s2_data = load_perezfornos2012(figures='fig3_S2')['time_series'][0]"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Stimuli were delivered over a 10 second window. To recreate this, we build\na vector of time points (``time_steps``) and set stimulation value to 10\nwithin the first 10 seconds, and 0 otherwise:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "time_steps = np.arange(start=-2.0, stop=75.5, step=0.5)\n\n# Set up the pulse:\npulse_value = []\nfor pulse_step in time_steps:\n    if 10 >= pulse_step > 0:\n        pulse_value.append(10)\n    else:\n        pulse_value.append(0)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Now we can plot the stimulus alongside the subject's response:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "# Set up the subplot:\nfig, (ax1, ax2) = plt.subplots(1, 2, sharex=True, sharey=True)\nplt.xlim(-1, 30)\nplt.ylim(-11, 11)\nfig.suptitle('Average Joystick Response vs. Time')\n\n# Plot subject 1\nax1.plot(time_steps, figure_3_s1_data, 'r', label='S1 Joystick Response')\nax1.step(time_steps, pulse_value, 'k--', label='Pulse')\nax1.spines['bottom'].set_position('center')\nax1.set_title('S1')\nax1.legend(loc='lower right')\n\n# Plot subject 2\nax2.plot(time_steps, figure_3_s2_data, label='S2 Joystick Response')\nax2.step(time_steps, pulse_value, 'k--', label='Pulse')\nax2.spines['bottom'].set_position('center')\nax2.set_title('S2')\nax2.legend(loc='lower right')\n\nfig.tight_layout()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The above plot suggests that Subject 1 saw phosphenes gradually get dimmer\nover the stimulus duration. Once the stimulus was removed, phosphene\nbrightness was quickly reduced to zero.\n\nFor Subject 2, the phosphene started bright (t=0) but rapidly went dark,\neven darker than the background (y-value < 0). Then the phosphene slowly\ngot brighter again. Upon stimulus removal, the subject reported seeing\nanother flash of light.\n\nThe reason for these differences is currently unknown, but might have to do\nwith retinal adaptation.\n\n"
      ]
    }
  ],
  "metadata": {
    "kernelspec": {
      "display_name": "Python 3",
      "language": "python",
      "name": "python3"
    },
    "language_info": {
      "codemirror_mode": {
        "name": "ipython",
        "version": 3
      },
      "file_extension": ".py",
      "mimetype": "text/x-python",
      "name": "python",
      "nbconvert_exporter": "python",
      "pygments_lexer": "ipython3",
      "version": "3.7.9"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}