tutorial.rst 4.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. *********
  2. Tutorials
  3. *********
  4. Getting Started
  5. ---------------
  6. In this first tutorial, we will go through a very simple example of how to use Elephant. We will numerically verify that the coefficient of variation (CV), a measure of the variability of inter-spike intervals, of a spike train that is modeled as a random (stochastic) Poisson process is 1.
  7. As a first step, install Elephant and its dependencies as outlined in :ref:`install`. Next, start up your Python shell. Under Windows, you can likely launch a Python shell from the Start menu. Under Linux or Mac, you may start Python by typing::
  8. $ python
  9. As a first step, we want to generate spike train data modeled as a stochastic Poisson process. For this purpose, we can use the :mod:`elephant.spike_train_generation` module, which provides the :func:`homogeneous_poisson_process` function::
  10. >>> from elephant.spike_train_generation import homogeneous_poisson_process
  11. Use the :func:`help()` function of Python to display the documentation for this function::
  12. >>> help(homogeneous_poisson_process)
  13. As you can see, the function requires three parameters: the firing rate of the Poisson process, the start time and the stop time. These three parameters are specified as :class:`Quantity` objects: these are essentially arrays or numbers with a unit of measurement attached. We will see how to use these objects in a second. You can quit the help screen by typing ``q``.
  14. Let us now generate 100 independent Poisson spike trains for 100 seconds each with a rate of 10 Hz for which we later will calculate the CV. For simplicity, we will store the spike trains in a list::
  15. >>> from quantities import Hz, s, ms
  16. >>> spiketrain_list = [
  17. ... homogeneous_poisson_process(rate=10.0*Hz, t_start=0.0*s, t_stop=100.0*s)
  18. ... for i in range(100)]
  19. Notice that the units ``s`` and ``Hz`` have both been imported from the :mod:`quantities` library and can be directly attached to the values by multiplication. The output is a list of 100 Neo :class:`SpikeTrain` objects::
  20. >>> print(len(spiketrain_list))
  21. 100
  22. >>> print(type(spiketrain_list[0]))
  23. <class 'neo.core.spiketrain.SpikeTrain'>
  24. Before we continue, let us (optionally) have a look at the spike trains in a spike raster plot. This can be created, e.g., using the `matplotlib`_ framework (you may need to install this library, as it is not one of the dependencies of Elephant)::
  25. >>> import matplotlib.pyplot as plt
  26. >>> import numpy as np
  27. >>> for i, spiketrain in enumerate(spiketrain_list):
  28. t = spiketrain.rescale(ms)
  29. plt.plot(t, i * np.ones_like(t), 'k.', markersize=2)
  30. >>> plt.axis('tight')
  31. >>> plt.xlim(0, 1000)
  32. >>> plt.xlabel('Time (ms)', fontsize=16)
  33. >>> plt.ylabel('Spike Train Index', fontsize=16)
  34. >>> plt.gca().tick_params(axis='both', which='major', labelsize=14)
  35. >>> plt.show()
  36. Notice how the spike times of each spike train are extracted from each of the spike trains in the for-loop. The :meth:`rescale` operation of the quantities library is used to transform units to milliseconds. In order to aid the visualization, we restrict the plot to the first 1000 ms (:func:`xlim` function). The :func:`show` command plots the spike raster in a new figure window on the screen.
  37. .. figure:: images/tutorials/tutorial_1_figure_1.png
  38. :width: 600 px
  39. :align: center
  40. :figwidth: 80 %
  41. Spike raster plot of the 100 Poisson spike trains showing the first second of data.
  42. From the plot you can see the random nature of each Poisson spike train. Let us now calculate the distribution of the 100 CVs obtained from inter-spike intervals (ISIs) of these spike trains. Close the graphics window to get back to the Python prompt. The functions to calculate the list of ISIs and the CV are both located in the :mod:`elephant.statistics` module. Thus, for each spike train in our list, we first call the :func:`isi` function which returns an array of all *N-1* ISIs for the *N* spikes in the input spike train (refer to the online help using ``help(isi)``). We then feed the list of ISIs into the :func:`cv` function, which returns a single value for the coefficient of variation::
  43. >>> from elephant.statistics import isi, cv
  44. >>> cv_list = [cv(isi(spiketrain)) for spiketrain in spiketrain_list]
  45. In a final step, let's plot a histogram of the obtained CVs (again illustrated using the matplotlib framework for plotting)::
  46. >>> plt.hist(cv_list)
  47. >>> plt.xlabel('CV', fontsize=16)
  48. >>> plt.ylabel('count', fontsize=16)
  49. >>> plt.gca().tick_params(axis='both', which='major', labelsize=14)
  50. >>> plt.show()
  51. As predicted by theory, the CV values are clustered around 1. This concludes our first "getting started" tutorial on the use of Elephant. More tutorials will be added soon.
  52. .. figure:: images/tutorials/tutorial_1_figure_2.png
  53. :width: 600 px
  54. :align: center
  55. :figwidth: 80 %
  56. Distribution of CV values of the ISIs of 100 Poisson spike trains.
  57. .. _`matplotlib`: http://matplotlib.org/