
.. DO NOT EDIT.
.. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY.
.. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE:
.. "examples\datasets\plot_data_nanduri2012.py"
.. LINE NUMBERS ARE GIVEN BELOW.

.. only:: html

    .. note::
        :class: sphx-glr-download-link-note

        :ref:`Go to the end <sphx_glr_download_examples_datasets_plot_data_nanduri2012.py>`
        to download the full example code.

.. rst-class:: sphx-glr-example-title

.. _sphx_glr_examples_datasets_plot_data_nanduri2012.py:


===============================================================================
 Data from Nanduri et al. (2012)
===============================================================================

This example shows how to use the Nanduri et al. (2012) dataset.

[Nanduri2012]_ used a set of psychophysical detection tasks to determine
size and brightness of phosphenes by modulating current amplitude and stimulating frequency in one Argus I user.

.. important ::

        You will need to install `Pandas <https://pandas.pydata.org>`_
        (``pip install pandas``) for this dataset.

Loading the dataset
-------------------

The dataset can be loaded as a Pandas ``DataFrame``:

.. GENERATED FROM PYTHON SOURCE LINES 21-26

.. code-block:: Python


    from pulse2percept.datasets import load_nanduri2012
    data = load_nanduri2012()
    print(data)





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

        subject implant electrode  task   stim_class  stim_dur  freq  amp_factor  ... ref_amp_factor  ref_freq  brightness      size  pulse_dur  interphase_dur     pulse_type varied_param
    0       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0   10.402049  0.000000       0.45            0.45  cathodicfirst          amp
    1       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0   10.771684  0.000000       0.45            0.45  cathodicfirst          amp
    2       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0   11.976927  0.000000       0.45            0.45  cathodicfirst          amp
    3       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0   14.543276  0.000000       0.45            0.45  cathodicfirst          amp
    4       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0   16.702165  0.000000       0.45            0.45  cathodicfirst          amp
    ..      ...     ...       ...   ...          ...       ...   ...         ...  ...            ...       ...         ...       ...        ...             ...            ...          ...
    123     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0    0.000000  1.055371       0.45            0.45  cathodicfirst          amp
    124     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0    0.000000  1.126202       0.45            0.45  cathodicfirst          amp
    125     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0    0.000000  1.267865       0.45            0.45  cathodicfirst          amp
    126     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0    0.000000  2.205215       0.45            0.45  cathodicfirst          amp
    127     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0    0.000000  4.160986       0.45            0.45  cathodicfirst          amp

    [128 rows x 17 columns]




.. GENERATED FROM PYTHON SOURCE LINES 28-33

Inspecting the DataFrame tells us that there are 128 measurements
(the rows) each with 17 different attributes (the columns).

These attributes include specifiers such as "subject", "electrode", and
"freq". We can print all column names using:

.. GENERATED FROM PYTHON SOURCE LINES 33-36

.. code-block:: Python


    data.columns





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Index(['subject', 'implant', 'electrode', 'task', 'stim_class', 'stim_dur',
           'freq', 'amp_factor', 'ref_stim_class', 'ref_amp_factor', 'ref_freq',
           'brightness', 'size', 'pulse_dur', 'interphase_dur', 'pulse_type',
           'varied_param'],
          dtype='object')



.. GENERATED FROM PYTHON SOURCE LINES 37-44

.. note ::

    The meaning of all column names is explained in the docstring of
    the :py:func:`~pulse2percept.datasets.load_nanduri2012` function.

For example, "freq" corresponds to the different stimulation frequency (hz) that
were used in the paper:

.. GENERATED FROM PYTHON SOURCE LINES 44-47

.. code-block:: Python


    data.freq.unique()





.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    array([ 20.,  15.,  25.,  40.,  80., 120.])



.. GENERATED FROM PYTHON SOURCE LINES 48-50

To select all the rows where the stimulation frequency was 20hz, we can index into the DataFrame as
follows:

.. GENERATED FROM PYTHON SOURCE LINES 50-53

.. code-block:: Python


    print(data[data.freq == 20.0])





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

        subject implant electrode  task   stim_class  stim_dur  freq  amp_factor  ... ref_amp_factor  ref_freq  brightness      size  pulse_dur  interphase_dur     pulse_type varied_param
    0       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0   10.402049  0.000000       0.45            0.45  cathodicfirst          amp
    1       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0   10.771684  0.000000       0.45            0.45  cathodicfirst          amp
    2       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0   11.976927  0.000000       0.45            0.45  cathodicfirst          amp
    3       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0   14.543276  0.000000       0.45            0.45  cathodicfirst          amp
    4       S06  ArgusI        A2  rate  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0   16.702165  0.000000       0.45            0.45  cathodicfirst          amp
    ..      ...     ...       ...   ...          ...       ...   ...         ...  ...            ...       ...         ...       ...        ...             ...            ...          ...
    123     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0    0.000000  1.055371       0.45            0.45  cathodicfirst          amp
    124     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0    0.000000  1.126202       0.45            0.45  cathodicfirst          amp
    125     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0    0.000000  1.267865       0.45            0.45  cathodicfirst          amp
    126     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0    0.000000  2.205215       0.45            0.45  cathodicfirst          amp
    127     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0    0.000000  4.160986       0.45            0.45  cathodicfirst          amp

    [88 rows x 17 columns]




.. GENERATED FROM PYTHON SOURCE LINES 54-59

This leaves us with 88 rows.

One of the important points of the paper is to investigate the relationship between
phosphene brightness and size as either the stimulation amplitude factor or frequency varies.
We can easily load in all data points where phosphene brightness was recorded when initially loading in the data set.

.. GENERATED FROM PYTHON SOURCE LINES 59-62

.. code-block:: Python


    print(load_nanduri2012(task='rate'))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

       subject implant electrode  task   stim_class  stim_dur   freq  amp_factor ref_stim_class  ref_amp_factor  ref_freq  brightness  size  pulse_dur  interphase_dur     pulse_type varied_param
    0      S06  ArgusI        A2  rate  Nanduri2012       0.5   20.0        1.25    Nanduri2012            1.25      20.0   10.402049   0.0       0.45            0.45  cathodicfirst          amp
    1      S06  ArgusI        A2  rate  Nanduri2012       0.5   20.0        1.50    Nanduri2012            1.25      20.0   10.771684   0.0       0.45            0.45  cathodicfirst          amp
    2      S06  ArgusI        A2  rate  Nanduri2012       0.5   20.0        2.00    Nanduri2012            1.25      20.0   11.976927   0.0       0.45            0.45  cathodicfirst          amp
    3      S06  ArgusI        A2  rate  Nanduri2012       0.5   20.0        4.00    Nanduri2012            1.25      20.0   14.543276   0.0       0.45            0.45  cathodicfirst          amp
    4      S06  ArgusI        A2  rate  Nanduri2012       0.5   20.0        6.00    Nanduri2012            1.25      20.0   16.702165   0.0       0.45            0.45  cathodicfirst          amp
    ..     ...     ...       ...   ...          ...       ...    ...         ...            ...             ...       ...         ...   ...        ...             ...            ...          ...
    83     S06  ArgusI        D4  rate  Nanduri2012       0.5   20.0        1.25    Nanduri2012            1.25      20.0   11.502798  -1.0       0.45            0.45  cathodicfirst         freq
    84     S06  ArgusI        D4  rate  Nanduri2012       0.5   25.0        1.25    Nanduri2012            1.25      20.0   13.510182  -1.0       0.45            0.45  cathodicfirst         freq
    85     S06  ArgusI        D4  rate  Nanduri2012       0.5   40.0        1.25    Nanduri2012            1.25      20.0   23.744559  -1.0       0.45            0.45  cathodicfirst         freq
    86     S06  ArgusI        D4  rate  Nanduri2012       0.5   80.0        1.25    Nanduri2012            1.25      20.0   37.604071  -1.0       0.45            0.45  cathodicfirst         freq
    87     S06  ArgusI        D4  rate  Nanduri2012       0.5  120.0        1.25    Nanduri2012            1.25      20.0   51.548559  -1.0       0.45            0.45  cathodicfirst         freq

    [88 rows x 17 columns]




.. GENERATED FROM PYTHON SOURCE LINES 63-64

Likewise, we can load in all data points where phosphene size was recorded when initially loading in the data set.

.. GENERATED FROM PYTHON SOURCE LINES 64-67

.. code-block:: Python


    print(load_nanduri2012(task='size'))





.. rst-class:: sphx-glr-script-out

 .. code-block:: none

       subject implant electrode  task   stim_class  stim_dur  freq  amp_factor  ... ref_amp_factor  ref_freq  brightness      size  pulse_dur  interphase_dur     pulse_type varied_param
    0      S06  ArgusI        A2  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.172134       0.45            0.45  cathodicfirst          amp
    1      S06  ArgusI        A2  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.350005       0.45            0.45  cathodicfirst          amp
    2      S06  ArgusI        A2  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  1.705748       0.45            0.45  cathodicfirst          amp
    3      S06  ArgusI        A2  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  4.630283       0.45            0.45  cathodicfirst          amp
    4      S06  ArgusI        A2  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  7.692566       0.45            0.45  cathodicfirst          amp
    5      S06  ArgusI        A4  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.165483       0.45            0.45  cathodicfirst          amp
    6      S06  ArgusI        A4  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.314960       0.45            0.45  cathodicfirst          amp
    7      S06  ArgusI        A4  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  1.613913       0.45            0.45  cathodicfirst          amp
    8      S06  ArgusI        A4  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  4.310587       0.45            0.45  cathodicfirst          amp
    9      S06  ArgusI        A4  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  6.606188       0.45            0.45  cathodicfirst          amp
    10     S06  ArgusI        B1  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.047553       0.45            0.45  cathodicfirst          amp
    11     S06  ArgusI        B1  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.088356       0.45            0.45  cathodicfirst          amp
    12     S06  ArgusI        B1  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  1.169960       0.45            0.45  cathodicfirst          amp
    13     S06  ArgusI        B1  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  2.143532       0.45            0.45  cathodicfirst          amp
    14     S06  ArgusI        B1  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  4.004566       0.45            0.45  cathodicfirst          amp
    15     S06  ArgusI        C1  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  0.975384       0.45            0.45  cathodicfirst          amp
    16     S06  ArgusI        C1  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  0.982281       0.45            0.45  cathodicfirst          amp
    17     S06  ArgusI        C1  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  0.996075       0.45            0.45  cathodicfirst          amp
    18     S06  ArgusI        C1  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  0.927821       0.45            0.45  cathodicfirst          amp
    19     S06  ArgusI        C1  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  4.572084       0.45            0.45  cathodicfirst          amp
    20     S06  ArgusI        C4  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.529552       0.45            0.45  cathodicfirst          amp
    21     S06  ArgusI        C4  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.976673       0.45            0.45  cathodicfirst          amp
    22     S06  ArgusI        C4  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  2.217563       0.45            0.45  cathodicfirst          amp
    23     S06  ArgusI        C4  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  3.681949       0.45            0.45  cathodicfirst          amp
    24     S06  ArgusI        C4  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  5.956451       0.45            0.45  cathodicfirst          amp
    25     S06  ArgusI        D2  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.272748       0.45            0.45  cathodicfirst          amp
    26     S06  ArgusI        D2  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.484798       0.45            0.45  cathodicfirst          amp
    27     S06  ArgusI        D2  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  1.908898       0.45            0.45  cathodicfirst          amp
    28     S06  ArgusI        D2  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  5.021398       0.45            0.45  cathodicfirst          amp
    29     S06  ArgusI        D2  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  9.994357       0.45            0.45  cathodicfirst          amp
    30     S06  ArgusI        D3  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.292815       0.45            0.45  cathodicfirst          amp
    31     S06  ArgusI        D3  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.552115       0.45            0.45  cathodicfirst          amp
    32     S06  ArgusI        D3  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  2.070715       0.45            0.45  cathodicfirst          amp
    33     S06  ArgusI        D3  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  6.635823       0.45            0.45  cathodicfirst          amp
    34     S06  ArgusI        D3  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  7.570393       0.45            0.45  cathodicfirst          amp
    35     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.25  ...           1.25      20.0         0.0  1.055371       0.45            0.45  cathodicfirst          amp
    36     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        1.50  ...           1.25      20.0         0.0  1.126202       0.45            0.45  cathodicfirst          amp
    37     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        2.00  ...           1.25      20.0         0.0  1.267865       0.45            0.45  cathodicfirst          amp
    38     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        4.00  ...           1.25      20.0         0.0  2.205215       0.45            0.45  cathodicfirst          amp
    39     S06  ArgusI        D4  size  Nanduri2012       0.5  20.0        6.00  ...           1.25      20.0         0.0  4.160986       0.45            0.45  cathodicfirst          amp

    [40 rows x 17 columns]




.. GENERATED FROM PYTHON SOURCE LINES 68-73

.. note ::

    Please see the documentation for :py:func:`~pulse2percept.datasets.load_nanduri2012`
    to see all available parameters for data subset loading.


.. GENERATED FROM PYTHON SOURCE LINES 75-82

Plotting the data
-----------------

To see the relationship between phosphene brightness as the amplitude factor varies,
we can recreate figure 4 a, from the paper.
Furthermore, the dataset available in :py:func:`~pulse2percept.datasets.load_nanduri2012`
is used to create figures 4 and 5, a-d in the paper.

.. GENERATED FROM PYTHON SOURCE LINES 82-125

.. code-block:: Python


    import matplotlib.pyplot as plt
    import numpy as np

    # load subset of the dataset concerning brightness data
    brightness_data = load_nanduri2012(task='rate')

    # get data where stimulation amplitude is varied
    vary_amp = brightness_data[brightness_data.varied_param == 'amp']

    # get the list of electrodes
    electrodes = data['electrode'].unique()

    # iterate over all electrodes
    for electrode in electrodes:
        # get relevant data for this specific electrode
        electrode_data = vary_amp[vary_amp.electrode == electrode]

        # normalize the amplitude
        normalized_amp = electrode_data.amp_factor / electrode_data.ref_amp_factor

        # set brightness rating
        brightness_rating = electrode_data.brightness

        # perform a first order linear best fit
        linear_fit = np.poly1d(np.polyfit(normalized_amp, brightness_rating, 1))

        # plot the linear best fit
        plt.plot(normalized_amp, linear_fit(normalized_amp), label=electrode)

    # display legend on plot
    plt.legend()

    # set plot axes
    plt.xlim(0, 7)
    plt.ylim(0, 60)

    # set plot labels and title
    plt.xlabel('Amplitude (uA) / Threshold (uA)')
    plt.ylabel('Brightness Rating')
    plt.title('Amplitude Modulation Brightness')





.. image-sg:: /examples/datasets/images/sphx_glr_plot_data_nanduri2012_001.png
   :alt: Amplitude Modulation Brightness
   :srcset: /examples/datasets/images/sphx_glr_plot_data_nanduri2012_001.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    Text(0.5, 1.0, 'Amplitude Modulation Brightness')



.. GENERATED FROM PYTHON SOURCE LINES 126-136

Using Built-In Plotting Functionality
-------------------------------------

Arguably the most important column is "freq". This is the current
amplitude of the different stimuli (single pulse, pulse trains, etc.) used
at threshold.

We might be interested in seeing how the phosphene brightness varies as a function
of pulse frequency. We could either use Matplotlib to generate a scatter plot
or use pulse2percept's own visualization function:

.. GENERATED FROM PYTHON SOURCE LINES 136-140

.. code-block:: Python


    from pulse2percept.viz import scatter_correlation
    scatter_correlation(data.freq, data.brightness)




.. image-sg:: /examples/datasets/images/sphx_glr_plot_data_nanduri2012_002.png
   :alt: plot data nanduri2012
   :srcset: /examples/datasets/images/sphx_glr_plot_data_nanduri2012_002.png
   :class: sphx-glr-single-img


.. rst-class:: sphx-glr-script-out

 .. code-block:: none


    <Axes: xlabel='freq', ylabel='brightness'>



.. GENERATED FROM PYTHON SOURCE LINES 141-147

:py:func:`~pulse2percept.viz.scatter_correlation` above generates a scatter
plot of the phosphene brightness as a function of pulse frequency, and performs
linear regression to calculate a correlation $r$ and a $p$ value.
As expected from the literature, now it becomes evident that phosphene
brightness is positively correlated with pulse frequency



.. rst-class:: sphx-glr-timing

   **Total running time of the script:** (0 minutes 0.351 seconds)


.. _sphx_glr_download_examples_datasets_plot_data_nanduri2012.py:

.. only:: html

  .. container:: sphx-glr-footer sphx-glr-footer-example

    .. container:: sphx-glr-download sphx-glr-download-jupyter

      :download:`Download Jupyter notebook: plot_data_nanduri2012.ipynb <plot_data_nanduri2012.ipynb>`

    .. container:: sphx-glr-download sphx-glr-download-python

      :download:`Download Python source code: plot_data_nanduri2012.py <plot_data_nanduri2012.py>`

    .. container:: sphx-glr-download sphx-glr-download-zip

      :download:`Download zipped: plot_data_nanduri2012.zip <plot_data_nanduri2012.zip>`


.. only:: html

 .. rst-class:: sphx-glr-signature

    `Gallery generated by Sphinx-Gallery <https://sphinx-gallery.github.io>`_
