root/trunk/doc/signals.txt

Revision 486, 11.1 KB (checked in by mpereira, 10 months ago)

major improvements of SpikeTrain.instantaneous_rate()

Line 
1======================
2The ``signals`` module
3======================
4
5This module provides key functions to manipulate and perform calculations
6on data structures like spike trains, spike lists, analog signals. 
7
8The import and export of these data structures is typically achieved using the NeuroTools :trac:`io` module.
9
10------------
11Spike Trains
12------------
13
14A spike train is a sorted vector of spike times, which is the result of a simulation or acquired by measurement.
15It has therefore some attributes, like ``t_start`` and ``t_stop``, which must in general
16be specified by the user, as they can not be inferred from the data. 
17
18**Note:** the standard time unit used by NeuroTools is milliseconds.
19
20When several spike trains are gathered, they are collected in a ``SpikeList`` object, which is effectively a dictionary
21of spike trains, the id of the cells being used as a key. See the ``SpikeList`` class for more details.
22
23
24The ``SpikeTrain`` class
25------------------------
26
27Creation
28~~~~~~~~
29
30Here are some examples of creating simple ``SpikeTrain`` objects::
31
32    >>> spk1 = SpikeTrain(arange(0,100,10), t_start = 0, t_stop=100)
33    >>> spk2 = SpikeTrain(arange(0,100,10))
34
35As you can see, defining ``t_start`` and ``t_stop`` is optional. If those attributes are not provided,
36they are inferred from the data as the min and the max of the spike times::
37
38    >>> spk1.t_stop
39        100
40    >>> spk2.t_stop
41        90
42   
43You can access the raw vector of the spike times with the ``spike_times`` attribute::
44   
45    >>> spk1.spike_times
46        array([  0.,  10.,  20.,  30.,  40.,  50.,  60.,  70.,  80.,  90.])
47   
48The ``SpikeTrain`` object by itself may be interesting, but it will be more often used within a
49``SpikeList`` object, as we'll see later.
50
51The ``SpikeTrain`` object has many useful methods such as ``mean_rate()``, ``copy()``, ``isi()``, ``cv_isi()``, ``raster_plot()``. See the ``signals`` API documentation (:trac:`api:signals`) for more
52details.
53
54An example of basic use of a ``SpikeTrain`` object is as follows::
55
56    >>> spk1.raster_plot()  # Generates a raster  plot between t_start and t_stop
57    >>> spk1.isi()
58        array([ 10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.,  10.])
59    >>> spk1. mean_rate()
60        100.0
61
62
63.. _SpikeList:
64
65The ``SpikeList`` class
66-----------------------
67
68A ``SpikeList`` is effectively a dictionary of ``SpikeTrains``, and can be thought of conceptually as the output of a network simulation,
69when one records the spikes of a given population.  The ``SpikeList`` is an object to organize such a recording
70as a tuple of key-value pairs ``( spike_source_id, spike_train)``, that is, a ``SpikeList`` object acts as a dictionary created as follows:
71``{'id' : SpikeTrain, ...}``
72
73Creation
74~~~~~~~~
75
76The constructor of a ``SpikeList`` is works as follows: it will accepts a list of tuples ``(id, spike_time)``,
77parameters ``t_start`` and ``t_stop``, and the list of all the recorded ids. The last three parameters
78can't be inferred from the data safely, so it's better if they are specified by the user.  Here is an simple example of how to create a ``SpikeList``::
79
80    >>> list = [(numpy.random.random_integers(0,10,1)[0],1000*numpy.random.rand()) for i in xrange(1000)]
81    >>> spklist = SpikeList(list, range(11), t_start=0, t_stop=1000)
82
83Here, ``range(11)`` is used to specify that in the lists we will have cells with ids between 0 and 10 (this is needed
84because we can have silent cells in the ``SpikeList``), and that we will keep spikes only between ``t_start`` and ``t_stop``.
85All the ``SpikeTrain`` objects within the ``SpikeList`` will share the same ``t_start`` and ``t_stop``.
86
87Rather than calling the ``SpikeList`` constructor,  a more common way to create a ``SpikeList`` in NeuroTools is to use the ``load_spikelist()`` or the ``load()`` functions.
88If you have generated your data with PyNN_, you can use the loading functions made for this purpose. For example if
89you have recorded the spikes of a population in a file "spikes.dat", then one can load it as a ``SpikeList`` as follows::
90   
91    >>> spklist = load("spikes.dat",'spikes')
92
93Using this syntax, the header information contained in the file is used to create the population, and ``t_start`` and ``t_stop`` are
94inferred automatically as the min and the max of all the ``SpikeTrains`` within the ``SpikeList``.
95If you want to keep the control on the parameters while creating the ``SpikeList``, do the following::
96   
97    >>> spklist = load_spikelist("spikes.dat", range(11), t_start=0, t_stop=1000)
98
99
100Now that the ``SpikeList`` has been created, you can start to explore it.
101
102Navigation
103~~~~~~~~~~
104
105You can access ``SpikeTrain`` objects within the ``Spikelist`` with the simple syntax ``spklist[id]``::
106
107    >>> spklist[0]
108        <NeuroTools.signals.SpikeTrain object at 0x92aac0c>
109    >>> spklist[0].mean_rate()
110        23.5
111    >>> for spiketrain in spklist:
112            print spktrain.isi()
113
114As you can see in the example, one can navigate and iterate over a ``SpikeList`` object and have access to
115all the ``SpikeTrain``\s within the object. To have an explicit list of all the ids contained in the ``SpikeList``,
116use the function ``id_list()``::
117
118    >>> spklist.id_list()
119        array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
120    >>> spklist[15]
121        <type 'exceptions.Exception'>: id 15 is not present in the SpikeList. See id_list()
122
123You can't access ``SpikeTrains`` of non recorded cells :-)
124
125
126Slices
127~~~~~~
128
129You can do slices of your ``SpikeList`` object, either according to the time axis or to selected ids::
130
131    >>> subspklist = spklist.time_slice(500,1500)
132    >>> subspklist = spklist.id_slice(50) # Will select 50 random id within spklist.id_list()
133    >>> subspklist = spklist.id_slice([2,3,5,6])
134
135If you want to select only cells matching a particular criteria within the ``SpikeList``, you can use the
136``select_ids`` method::
137
138    >>> subspklist = spklist.select_ids("cell.mean_rate() > 0")
139    >>> subspklist = spklist.select_ids("mean(cell.isi()) < 1")
140
141
142
143Viewing and saving
144~~~~~~~~~~~~~~~~~~
145
146Several signal object methods have the property of being able to generate plots. These plots are implemented to be generic and customizable.  As can be seen in the API documentation, plotting functions accept a ``display`` flag, which you can set to ``True`` if you want to generate a new figure, or you can set it to a subplot if you want to do more complicated plots.  You can also provided additional
147parameters to the plot function of pylab as extra arguments::
148
149    >>> spklist.raster_plot(100, t_start=0, t_stop=500)
150    >>> spklist.raster_plot(100, t_start=0, t_stop=500, display=subplot(221), kwargs={'color':'red'})
151
152To save the ``SpikeList``, you can use either a Standard Text file output, or a Pickle (compressed) file. To do that, just
153provide to the ``save()`` method the corresponding file objects::
154
155    >>> spklist.save("spikes.dat") # Default mode, will create a text file
156    >>> spklist.save(StandPickleFile("spikes.dat"))
157
158
159--------------
160Analog Signals
161--------------
162
163``NeuroTools.signals`` also handles analog signals. These are generally also
164recorded during a simulation or experiment, such as for example a Vm trace, a conductance or a current. Such a signal is
165defined by a number of values between ``t_start`` and ``t_stop`` with a time step ``dt``.
166
167The ``AnalogSignal`` class
168--------------------------
169
170Creation
171~~~~~~~~
172   
173When we create an ``AnalogSignal``, we have to provide the list of the data, the time step of their acquisition,
174and as an option the ``t_start`` and ``t_stop`` parameters. If ``None``, ``t_start`` will be 0 and ``t_stop`` will be ``len(data)/dt``::
175
176    >>> x = AnalogSignal(sin(arange(1000)),0.1)
177    >>> x.t_stop
178        100
179    >>> x = AnalogSignal(sin(arange(1000)),0.1,0,50)
180    >>> x.t_stop
181        50
182    >>> len(x)
183        500
184
185You can access the raw data of the ``AnalogSignal`` by just using::
186   
187    >>> x.signal
188
189Usage
190~~~~~
191
192Several functions can be applied to an ``AnalogSignal``. See the global API for an exhaustive list. You can for example
193do an ``event_trigger_average()``, slice the signal according to some events, detect areas of the signals which are
194over a certain threshold, and so on...
195       
196
197The ``AnalogSignalList`` class
198------------------------------
199
200As for the ``SpikeList``, the ``AnalogSignalList`` is a collection of ``AnalogSignal`` objects. It has the same structure as
201the ``SpikeList``, meaning this is a dictionary containing ``AnalogSignal``\s with the key being the id of the cells.
202
203Creation
204~~~~~~~~
205
206The constructor of an ``AnalogSignalList`` is made as follows: it will accept an array with all the signals, the time step,
207and additional parameters like ``t_start``, ``t_stop``, and the list of all the recorded ids. The last three parameters
208can't be inferred from the data safely, so it's better if they are specified by the user. Nevertheless, the most
209common way to create ``AnalogSignal``\s in NeuroTools is to use the ``load_analogsignal()`` or ``load()`` functions, as
210explained below. Currently, the constructor of the ``AnalogSignalList`` is mainly tuned to be used with these load functions,
211and it is therefore not so simple to create one from a list of ``AnalogSignal``\s::
212
213    >>> sig1 = AnalogSignal(sin(arange(10000),dt=0.1,t_start=0, t_stop=1000)
214    ... There seems to be something missing here?
215
216
217Navigation
218~~~~~~~~~~
219
220You can access an ``AnalogSignal`` object within the ``AnalogSignalList`` with the simple syntax ``aslist[id]``::
221
222    >>> aslist[0]
223        <NeuroTools.signals.AnalogSignal object at 0x92aac0c>
224    >>> mean(aslist[0].signal)
225        23.5
226    >>> for as in aslist:
227            print as.signal
228
229As you can see in the example, one can navigate and iterate over an ``AnalogSignalList`` object and have access to
230all the ``AnalogSignal``\s within the object. To have an explicit list of all the ids contained in the ``AnalogSignalList``,
231use the function ``id_list()``::
232
233    >>> aslist.id_list()
234        array([0, 1, 2, 3, 4, 5, 6, 7, 8, 9])
235    >>> aslist[15]
236        <type 'exceptions.Exception'>: id 15 is not present in the AnalogSignalList. See id_list()
237
238You can't access ``AnalogSignal``\s of non recorded cells :-)
239
240Viewing and saving
241~~~~~~~~~~~~~~~~~~
242
243Similarly to the ``SpikeList object``, some methods of the ``AnalogSignalList`` object can
244generate plots. Again, a plot can be either a new figure or a subplot::
245
246    >>> vmlist.plot(1, display= subplot(221), kwargs={'color':'r'})
247
248Again, also, those ``AnalogSignalList`` objects can be saved to file. You just have to use the already implemented ``FileHandler`` (see io.py) ``StandardTextFile`` (default) or ``StandardPickleFile`` and use the ``save()`` method::
249
250    >>> vmlist.save("vm.dat") # Will create a text file by default
251    >>> vmlist.save(StandardTextFile("vm.dat")) # save as before
252    >>> vmlist.save(StandardPickleFile("vm.dat")) # Saved as pickled object
253    >>> vm = load(StandardPickleFile("vm.dat"),'v) # Read the pickle file
254
255Note that for the moment, there is a slight distinction for the conductance files, since the ``load`` function is
256tuned for PyNN. Since PyNN saves exc/inh conductances in the same file, the ``load`` function, called on a file
257generated by PyNN, will return two ``AnalogSignalList`` ::
258
259    >>> ge, gi = load("conductance.dat",'g')
260
261.. _PyNN: http://neuralensemble.org/PyNN/
Note: See TracBrowser for help on using the browser.