{
  "cells": [
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "%matplotlib inline"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "\n# Running the scoreboard model\n\n\nThis example shows how to apply the\n:py:class:`~pulse2percept.models.ScoreboardModel` to an\n:py:class:`~pulse2percept.implants.ArgusII` implant.\n\nThe scoreboard model is a standard baseline model of retinal prosthesis\nstimulation, which assumes that electrical stimulation leads to the percept\nof focal dots of light, centered over the visual field location associated with\nthe stimulated retinal field location $(x_{stim}, y_{stim})$, whose\nspatial intensity decays with a Gaussian profile [Hayes2003]_, [Thompson2003]_:\n\n\\begin{align}I_{score}(x,y; \\rho) = \\exp \\Big(\n    -\\frac{(x-x_{stim})^2 + (y-y_{stim})^2}{2 \\rho^2} \\Big)\\end{align}\n\nwhere $\\rho$ is the spatial decay constant.\n\n.. important::\n\n    As pointed out by [Beyeler2019]_, the scoreboard model does not well\n    account for percepts generated by **epiretinal** implants, where\n    incidental stimulation of retinal nerve fiber bundles leads to elongated,\n    'streaky' percepts.\n\n    In that case, use :py:class:`~pulse2percept.models.AxonMapModel` instead.\n\nThe scoreboard model can be instantiated and run in three simple steps.\n\n1. Creating the model\n---------------------\n\nThe first step is to instantiate the\n:py:class:`~pulse2percept.models.ScoreboardModel` class by calling its\nconstructor method.\nThe most important parameter to set is ``rho`` from the above equation (here\nset to 100 micrometers):\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pulse2percept.models import ScoreboardModel\nmodel = ScoreboardModel(rho=100)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Parameters you don't specify will take on default values. You can inspect\nall current model parameters as follows:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "print(model)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "This reveals a number of other parameters to set, such as:\n\n* ``xrange``, ``yrange``: the extent of the visual field to be simulated,\n  specified as a range of x and y coordinates (in degrees of visual angle,\n  or dva). For example, we are currently sampling x values between -20 dva\n  and +20dva, and y values between -15 dva and +15 dva.\n* ``xystep``: The resolution (in dva) at which to sample the visual field.\n  For example, we are currently sampling at 0.25 dva in both x and y\n  direction.\n* ``thresh_percept``: You can also define a brightness threshold, below which\n  the predicted output brightness will be zero. It is currently set to\n  ``1/sqrt(e)``, because that will make the radius of the predicted percept\n  equal to ``rho``.\n\nIn addition, you can choose the parallelization back end used to speed up\nsimulations:\n\n* ``engine``:\n   * 'serial': single-core processing (no parallelization)\n   * 'joblib': parallelization using the `JobLib`_ library\n   * 'dask': parallelization using the `Dask`_ library\n\n* ``scheduler``:\n   * 'threading': a scheduler backed by a thread pool\n   * 'multiprocessing': a scheduler backed by a process pool\n\n\nTo change parameter values, either pass them directly to the constructor\nabove or set them by hand, like this:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.engine = 'serial'"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "Then build the model. This is a necessary step before you can actually use\nthe model to predict a percept, as it performs a number of expensive setup\ncomputations (e.g., building the spatial reference frame, calculating\nelectric potentials):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "model.build()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>You need to build a model only once. After that, you can apply any number\n    of stimuli -- or even apply the model to different implants -- without\n    having to rebuild (which takes time).</p></div>\n\n2. Assigning a stimulus\n-----------------------\nThe second step is to specify a visual prosthesis from the\n:py:mod:`~pulse2percept.implants` module.\n\nIn the following, we will create an\n:py:class:`~pulse2percept.implants.ArgusII` implant. By default, the implant\nwill be centered over the fovea (at x=0, y=0) and aligned with the horizontal\nmeridian (rot=0):\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "from pulse2percept.implants import ArgusII\nimplant = ArgusII()"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The easiest way to assign a stimulus to the implant is to pass a NumPy array\nthat specifies the current amplitude to be applied to every electrode in the\nimplant.\n\nFor example, the following sends 10 microamps to all 60 electrodes of the\nimplant:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import numpy as np\nimplant.stim = 10 * np.ones(60)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "<div class=\"alert alert-info\"><h4>Note</h4><p>Some models can handle stimuli that have both a spatial and a temporal\n    component. the scoreboard model cannot.</p></div>\n\n3. Predicting the percept\n-------------------------\nThe third step is to apply the model to predict the percept resulting from\nthe specified stimulus. Note that this may take some time on your machine:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "percept = model.predict_percept(implant)"
      ]
    },
    {
      "cell_type": "markdown",
      "metadata": {},
      "source": [
        "The resulting percept is stored in a NumPy array whose dimensions correspond\nto the values specified by ``xrange``, ``yrange``, and ``xystep``.\n\nThe percept can be plotted using Matplotlib:\n\n"
      ]
    },
    {
      "cell_type": "code",
      "execution_count": null,
      "metadata": {
        "collapsed": false
      },
      "outputs": [],
      "source": [
        "import matplotlib.pyplot as plt\nplt.imshow(percept, cmap='gray')\nplt.xticks(np.linspace(0, percept.shape[1], num=5),\n           np.linspace(*model.xrange, num=5))\nplt.xlabel('x (dva)')\nplt.yticks(np.linspace(0, percept.shape[0], num=5),\n           np.linspace(*model.yrange, num=5))\nplt.ylabel('y (dva)')\nplt.title('Predicted percept')"
      ]
    }
  ],
  "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.3"
    }
  },
  "nbformat": 4,
  "nbformat_minor": 0
}