Changeset 308

Show
Ignore:
Timestamp:
11/08/08 13:26:05 (2 months ago)
Author:
pierre
Message:

Fix some bugs in pairwise_cc_zero, as reported by nvoges, and I'm not sure it is still perfect... Nevertheless tests are passed, and the index errors seems to be solved. Add some documentation to the signals.py module, and also changes minors stuffs in doc/signals.txt. By the way, why on the Wiki all the code area are not well displayed for signals ?

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/doc/signals.txt

    r289 r308  
    1818**Note:** the standard time unit used by NeuroTools is milliseconds. 
    1919 
    20 When several spikes train are gathered, they are collected in a so-called SpikeList, which is actually a dictionnary 
     20When several spikes train are gathered, they are collected in a so-called ``SpikeList``, which is actually a dictionnary 
    2121of spike trains, the id of the cells being used as a key. See the SpikeList_ class for more details. 
    2222 
     
    4141        90 
    4242     
    43     You can access the raw vector of the spike times by the spike_times attribute:: 
     43    You can access the raw vector of the spike times by the ``spike_times`` attribute:: 
    4444     
    4545    >>> spk1.spike_times 
     
    5252details.  
    5353 
    54 An example of basic use of a SpikeTrain object is as follows:: 
     54An example of basic use of a ``SpikeTrain`` object is as follows:: 
    5555 
    5656    >>> spk1.raster_plot() # Generates a raster_plot between t_start and t_stop 
     
    6666----------------------- 
    6767 
    68 A SpikeList is actually a dictionary of SpikeTrains, and can be thought of conceptually as the output of a network simulation, 
    69 when one records the spikes of a given population.  The SpikeList is an object to organize such a recording 
    70 as 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  
    73 Creation 
    74 ~~~~~~~~ 
    75  
    76 The constructor of a SpikeList is works as follows: it will accepts a list of tuples (id, spike_time),  
     68A ``SpikeList`` is actually 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),  
    7777parameters t_start and t_stop, and the list of all the recorded ids. The last three parameters 
    78 can't be infered 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:: 
     78can't be infered 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``:: 
    7979 
    8080    >>> list = [(numpy.random.random_integers(0,10,1)[0],1000*numpy.random.rand()) for i in xrange(1000)] 
     
    8383Here, range(11) is used to specify that in the lists we will have cells with id between 0 and 10 (it is needed, 
    8484because we can have silent cells in the SpikeList), and that I will kept the spike only between t_start and t_stop. 
    85 All the SpikeTrains objects within the SpikeList will share the same t_start and t_stop 
    86  
    87 Rather than calling the SpikeList constructor,  a more commmon way to create a SpikeList in NeuroTools is to use the ``load_spikelist()`` or the ``load()`` functions. 
     85All the ``SpikeTrains`` objects within the ``SpikeList`` will share the same t_start and t_stop 
     86 
     87Rather than calling the ``SpikeList`` constructor,  a more commmon way to create a ``SpikeList`` in NeuroTools is to use the ``load_spikelist()`` or the ``load()`` functions. 
    8888If you have generated your data with pyNN, you can used the loading functions made for this purpose. For example if  
    89 you have recorded the spikes of a population in a file "spikes.dat", then one can load it as a SpikeList as follows:: 
     89you have recorded the spikes of a population in a file "spikes.dat", then one can load it as a ``SpikeList`` as follows:: 
    9090     
    9191    >>> spklist = load("spikes.dat",'spikes') 
    9292 
    9393Using this syntax, the header information contained in the file is used to create the population, and t_start and t_stop are 
    94 infered automatically as the min and the max of all the SpikeTrains within the SpikeList
     94infered automatically as the min and the max of all the ``SpikeTrains`` within the ``SpikeList``
    9595If you want to keep the control on the parameters while creating the SpikeList, do the following:: 
    9696     
     
    9898 
    9999 
    100 Now that the SpikeList has been created, you can start to explore it. 
     100Now that the ``SpikeList`` has been created, you can start to explore it. 
    101101 
    102102Navigation 
    103103~~~~~~~~~~ 
    104104 
    105 You can access SpikeTrain object within the Spikelist by the simple syntax spklist[id]:: 
     105You can access ``SpikeTrain`` objects within the ``Spikelist`` by the simple syntax spklist[id]:: 
    106106 
    107107    >>> spklist[0] 
     
    112112            print spktrain.isi() 
    113113 
    114 As you can see in the example, one can naviguate and iterate over a SpikeList object and have access to 
    115 all the SpikeTrains within the object. To have an explicite list of all the id contained in the SpikeList,  
     114As you can see in the example, one can naviguate and iterate over a ``SpikeList`` object and have access to 
     115all the ``SpikeTrains`` within the object. To have an explicite list of all the id contained in the ``SpikeList``,  
    116116use the function ``id_list()``:: 
    117117 
     
    121121        <type 'exceptions.Exception'>: id 15 is not present in the SpikeList. See id_list() 
    122122 
    123 You can't access SpikeTrains of non recorded cells :-) 
     123You can't access ``SpikeTrains`` of non recorded cells :-) 
    124124 
    125125 
     
    128128 
    129129You can do slices of your ``SpikeList`` object, either according to the time axis or to selected ids. 
     130     
    130131    >>> subspklist = spklist.time_slice(500,1500) 
    131132    >>> subspklist = spklist.id_slice(50) # Will select 50 randoms id within spklist.id_list() 
     
    149150    >>> spklist.raster_plot(100, t_start=0, t_stop=500, display=subplot(221), kwargs={'color':'red'}) 
    150151 
    151 To save the SpikeList, you can use either a Standard Text file output, or a Pickle (compressed) file. To do that, just 
     152To save the ``SpikeList``, you can use either a Standard Text file output, or a Pickle (compressed) file. To do that, just 
    152153provide to the ``save()`` method the corresponding file objects:: 
    153154 
     
    189190~~~~~ 
    190191 
    191 Several functions can be applied to an AnalogSignal. See the global API for an exhaustive list. You can for example 
     192Several functions can be applied to an ``AnalogSignal``. See the global API for an exhaustive list. You can for example 
    192193do an ``event_trigger_average()``, slice the signal according to some events, detect areas of the signals which are 
    193194over a certain threshold, and so on... 
     
    197198------------------------------ 
    198199 
    199 As for the SpikeList, the AnalogSignalList is a container of AnalogSignal objects. It has the same structure than 
    200 the SpikeList, meaning this is a dictionnary containing AnalogSignals with the key being the id of the cells.  
    201  
    202 Creation 
    203 ~~~~~~~~ 
    204  
    205 The constructor of a AnalogSignalList is made as follows: it will accept an array with all the signals, the time step, 
     200As for the ``SpikeList``, the ``AnalogSignalList`` is a container of ``AnalogSignal`` objects. It has the same structure than 
     201the ``SpikeList``, meaning this is a dictionnary containing ``AnalogSignals`` with the key being the id of the cells.  
     202 
     203Creation 
     204~~~~~~~~ 
     205 
     206The constructor of a ``AnalogSignalList`` is made as follows: it will accept an array with all the signals, the time step, 
    206207and additional parameters like t_start, t_stop, and the list of all the recorded ids. The last three parameters 
    207208can't be infered from the data safely, so it's better if they are specified by the user. Nevertheless, the most 
    208 commmon way to create SpikeList in NeuroTools is to use the ``load_analogsignal()`` or the ``load()`` functions, as 
    209 explained below. Currently, the constructor of the AnalogSignalList is mainly tunned to be used with those load functions,  
    210 and this is therefore not so simple to create one from a list of AnalogSignal
     209commmon way to create ``SpikeList`` in NeuroTools is to use the ``load_analogsignal()`` or the ``load()`` functions, as 
     210explained below. Currently, the constructor of the ``AnalogSignalList`` is mainly tunned to be used with those load functions,  
     211and this is therefore not so simple to create one from a list of ``AnalogSignal``
    211212 
    212213    >>> sig1 = AnalogSignal(sin(arange(10000),dt=0.1,t_start=0, t_stop=1000) 
     
    217218~~~~~~~~~~ 
    218219 
    219 You can access AnalogSignal object within the ``AnalogSignalList`` by the simple syntax spklist[id]: 
     220You can access ``AnalogSignal`` object within the ``AnalogSignalList`` by the simple syntax spklist[id]: 
    220221 
    221222    >>> aslist[0] 
     
    226227            print as.signal 
    227228 
    228 As you can see in the example, one can naviguate and iterate over a AnalogSignalList object and have access to 
    229 all the AnalogSignal within the object. To have an explicite list of all the id contained in the AnalogSignalList,  
     229As you can see in the example, one can naviguate and iterate over a ``AnalogSignalList`` object and have access to 
     230all the ``AnalogSignal`` within the object. To have an explicite list of all the id contained in the ``AnalogSignalList``,  
    230231use the function ``id_list()`` 
    231232 
     
    235236        <type 'exceptions.Exception'>: id 15 is not present in the AnalogSignalList. See id_list() 
    236237 
    237 You can't access AnalogSignal of non recorded cells :-) 
     238You can't access ``AnalogSignal`` of non recorded cells :-) 
    238239 
    239240Viewing and saving 
     
    252253    >>> vm = load(StandardPickleFile("vm.dat"),'v) # Read the pickle file 
    253254 
    254 Note that for the moment, there is a slight disinction for the conductance files, since the ``load`` function is 
     255Note that for the moment, there is a slight distinction for the conductance files, since the ``load`` function is 
    255256tunned for pyNN. Since pyNN saves exc/inh conductances in the same file, the ``load`` function, called on a file 
    256257generated by pyNN, will return two ``AnalogSignalList``  
  • trunk/src/signals.py

    r304 r308  
    11""" 
    2 signals.py 
    3  
    4 Routines used to defined formally spike lists and membrane traces 
     2NeuroTools.signals 
     3================== 
     4 
     5A collection of functions to create, manipulate and play with spikes and analog 
     6signals.  
     7 
     8Classes 
     9------- 
     10 
     11SpikeTrain       - An object representing a spike train, for one cells. Useful for plots,  
     12                   calculus like ISI, CV, mean rate(), ... 
     13SpikeList        - An object representing the activity of a population of neurons. It's a 
     14                   dictionnary of SpikeTrain objects, and again, the SpikeList object have 
     15                   built-in methods to compute firing_rate, ISI, CV, cross-correlations, and 
     16                   so on.  
     17AnalogSignal     - An object representing an analog signal, with its data. Can be used to do  
     18                   threshold detection, event triggered averages, ... 
     19AnalogSignalList - A list of AnalogSignal objects, again with functions such as mean, std, plot,  
     20                   and so on 
     21VmList           - An AnalogSignalList object used for Vm traces 
     22ConductanceList  - An AnalogSignalList object used for conductances traces 
     23CurrentList      - An AnalogSignalList object used for current traces 
     24 
     25Functions 
     26--------- 
     27 
     28load_spikelist       - Function to load a SpikeList object from a file that have been generated by pyNN. 
     29                       Can also have been generated by your particular simulator/format, but then you have 
     30                       to write your own File object that will know how to read the data (see io.py) 
     31load_vmlist          - Function to load a VmList object (inherits from AnalogSignalList) from a file 
     32                       generated by pyNN. Same comments that previously. Note also that as load_conductancelist 
     33                       the function returns two CurrentList, one for the exc current and one for the inh current 
     34load_currentlist     - Function to load a CurrentList object (inherits from AnalogSignalList) from a file 
     35                       generated by pyNN. Same comments that previously. Note also that as load_conductancelist 
     36                       the function returns two CurrentList, one for the exc current and one for the inh current 
     37load_conductancelist - Function to load a ConductanceList object (inherits from AnalogSignalList) from a file 
     38                       generated by pyNN. Same comments that previously. load_conductancelist returns two  
     39                       ConductanceList, one for the exc conductance and one for the inh conductance 
     40load                 - A generic loader for all those previous load method. 
    541""" 
    642 
     
    2359else: 
    2460    MATPLOTLIB_ERROR = "No pylab package have been detected" 
     61 
    2562 
    2663class SpikeTrain(object): 
     
    763800            for idx in xrange(len(pairs)): 
    764801                if (pairs[idx][0] not in cells1) or (pairs[idx][1] not in cells2): 
    765                     to_remove.append(idx) 
    766             for idx in to_remove: 
    767                 pairs.pop(idx) 
     802                    to_remove.append(pairs[idx]) 
     803            N = len(to_remove) 
     804            if N > 0: 
     805                print "%d pairs have been removed because cells were not in the SpikeList" %len(to_remove) 
     806                for item in to_remove: 
     807                    pairs.remove(item) 
    768808            pairs  = numpy.array(pairs) 
    769809            spk1   = spk1.id_slice(pairs[:,0]) 
     
    16371677            spklist = self 
    16381678        subplot = get_display(display) 
    1639  
     1679         
     1680        all_cells1 = numpy.sort(self.id_list()) 
     1681        all_cells2 = numpy.sort(self.id_list()) 
     1682         
    16401683        spk1, spk2, pairs = self.__select_with_pairs__(pairs, self, spklist) 
    16411684        N = len(pairs) 
     
    16491692        times1, ids1 = spk1.convert("times, ids") 
    16501693        times2, ids2 = spk2.convert("times, ids") 
    1651         cells_id = numpy.sort(pairs[:,0]) 
    1652         for count, cell in enumerate(cells_id): 
    1653             ids1[numpy.where(ids1 == cell)[0]] = count 
    1654         cells_id = numpy.sort(pairs[:,1]) 
    1655         for count, cell in enumerate(cells_id): 
    1656             ids2[numpy.where(ids2 == cell)[0]] = count 
     1694         
     1695        cells_id     = spk1.id_list() 
     1696        for idx in xrange(len(cells_id)): 
     1697            ids1[numpy.where(ids1 == cells_id[idx])[0]] = idx 
     1698        cells_id     = spk2.id_list() 
     1699        for idx in xrange(len(cells_id)): 
     1700            ids2[numpy.where(ids2 == cells_id[idx])[0]] = idx 
    16571701        times1  = numpy.array(((times1 - self.t_start)/time_bin),int) 
    16581702        times2  = numpy.array(((times2 - self.t_start)/time_bin),int) 
     
    21392183        return AnalogSignal(self.signal, self.dt, self.t_start, self.t_stop) 
    21402184 
    2141     def time_axis(self,normalized=False): 
     2185    def time_axis(self, normalized=False): 
    21422186        """ 
    21432187        Return the time axis of the AnalogSignal 
  • trunk/test/test_signals.py

    r298 r308  
    368368     
    369369    def testCrossCorrZero(self): 
    370         cc1 = self.spk.pairwise_cc_zero([(i,i) for i in xrange(10)], time_bin=1.) 
    371         cc2 = self.spk.pairwise_cc_zero(1000, time_bin=1.) 
     370        cc1 = self.spk.pairwise_cc_zero([(i,i) for i in xrange(5)], time_bin=0.1) 
     371        cc2 = self.spk.pairwise_cc_zero(5, time_bin=0.1) 
     372        print cc1, cc2 
    372373        assert (0 <= cc1 <= 1) and (cc1 > cc2) 
    373374