Changeset 186

Show
Ignore:
Timestamp:
07/25/08 14:55:23 (4 months ago)
Author:
apdavison
Message:

In signals module:

  • renamed SpikeTrain.rescale() to SpikeTrain.relative_times(). This seems clearer to me since 'rescale' implies multiplication whereas we only do a subtraction.
  • added SpikeTrain.merge(), which adds the spike times from another spike train to this one.
  • fixed a bug in iterating over a SpikeList object.
  • added SpikeList.f1f0_ratios(). This might be a bit too vision-specific, but since it is not uncommon to have oscillatory patterns in spike trains I think it should be here.

In plotting module: minor improvement to spacing between subplots, which is now proportional to the size of the subplots and may be different for the vertical and horizontal layout.

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/src/plotting.py

    r158 r186  
    8787        bottommargin = 0.1 
    8888        leftmargin=0.1 
    89         panelsep = 0.05 
    90         panelheight = (1 - topmargin - bottommargin - (nrows-1)*panelsep)/nrows 
    91         panelwidth = (1 - leftmargin - rightmargin - (ncolumns-1)*panelsep)/ncolumns 
     89        v_panelsep = 0.1*(1 - topmargin - bottommargin)/nrows #0.05 
     90        h_panelsep = 0.1*(1 - leftmargin - rightmargin)/ncolumns 
     91        panelheight = (1 - topmargin - bottommargin - (nrows-1)*v_panelsep)/nrows 
     92        panelwidth = (1 - leftmargin - rightmargin - (ncolumns-1)*h_panelsep)/ncolumns 
    9293        assert panelheight > 0 
    9394         
    94         bottomlist = [bottommargin + i*panelsep + i*panelheight for i in range(nrows)] 
    95         leftlist = [leftmargin + j*panelsep + j*panelwidth for j in range(ncolumns)] 
     95        bottomlist = [bottommargin + i*v_panelsep + i*panelheight for i in range(nrows)] 
     96        leftlist = [leftmargin + j*h_panelsep + j*panelwidth for j in range(ncolumns)] 
    9697        bottomlist.reverse() 
    9798        for j in range(ncolumns): 
  • trunk/src/signals.py

    r185 r186  
    1111    print "Warning: pylab not present" 
    1212from NeuroTools import analysis 
    13  
     13import logging 
    1414 
    1515class SpikeTrain(object): 
     
    259259        return hist 
    260260 
    261     def rescale(self): 
    262         """Rescale spike times to make t_start = 0.""" 
     261    def relative_times(self): 
     262        """ 
     263        Rescale spike times to make them relative to t_start. 
     264        t_start becomes 0.""" 
    263265        if self.t_start != 0: 
    264266            self.spike_times -= self.t_start 
     
    339341        return F1/F0 
    340342     
     343    def merge(self, spiketrain, relative_times=False): 
     344        """ 
     345        Add the spike times from `spiketrain` to this `SpikeTrain`s spike list. 
     346        """ 
     347        if relative_times: 
     348            self.relative_times() 
     349            spiketrain.relative_times() 
     350        self.spike_times = numpy.concatenate((self.spike_times, spiketrain.spike_times)) 
     351        self.spike_times.sort() 
     352        self.t_start = min(self.t_start, spiketrain.t_start) 
     353        self.t_stop = max(self.t_stop, spiketrain.t_stop) 
     354         
    341355 
    342356class SpikeList(object): 
     
    430444        self.__calc_startstop() 
    431445 
     446    def __iter__(self): 
     447        return self.spiketrains.itervalues() 
     448 
    432449    def __len__(self): 
    433450        return len(self.spiketrains) 
     
    468485                self.append(id, sl.spiketrains[id]) 
    469486 
     487    def merge(self, spikelist, relative_times=False): 
     488        """ 
     489        For each `id` in `spikelist` that matches an `id` in this `SpikeList`, 
     490        merge the two `SpikeTrains` and save the result in this `SpikeList`. 
     491         
     492        `SpikeTrain`s with `id`s not in this `SpikeList` are appended to it. 
     493        """ 
     494        for id, spiketrain in spikelist.spiketrains.items(): 
     495            if id in self.id_list: 
     496                self.spiketrains[id].merge(spiketrain, relative_times) 
     497            else: 
     498                if relative_times: 
     499                    spiketrain.relative_times() 
     500                self.append(spiketrain) 
     501                 
    470502     
    471503    def idsubSpikeList(self, id_list): 
     
    618650        """ 
    619651        nbins = numpy.ceil((self.t_stop-self.t_start)/time_bin) 
    620         firing_rate = numpy.zeros((self.N(),nbins), float) 
    621         print "nbins = %d" % nbins 
     652        spike_hist = numpy.zeros((self.N(),nbins), float) 
     653        logging.debug("nbins = %d" % nbins) 
    622654        for idx,id in enumerate(self.id_list): 
    623             print idx, id 
    624             firing_rate[idx,:] = self.spiketrains[id].time_histogram(time_bin,normalized) 
    625         if not display: 
    626             return firing_rate 
    627         else: 
     655            spike_hist[idx,:] = self.spiketrains[id].time_histogram(time_bin,normalized) 
     656        if display: 
    628657            pylab.figure() 
    629             pylab.plot(self.time_axis(time_bin),sum(firing_rate)/self.N()) 
     658            pylab.plot(self.time_axis(time_bin),sum(spike_hist)/self.N()) 
    630659            pylab.ylabel("Mean Number of Spikes per bin", size="x-large") 
    631660            pylab.xlabel("Time (ms)", size="x-large") 
     661        return spike_hist 
    632662 
    633663    def firing_rate(self, time_bin, display=False): 
     
    894924        return cor_coef_mean, cor_coef_std 
    895925 
     926    def f1f0_ratios(self, time_bin, f_stim): 
     927        """ 
     928        Returns the F1/F0 amplitude ratios for the spike trains contained in the 
     929        spike list, where the input stimulus frequency is f_stim. 
     930        """ 
     931        f1f0_dict = {} 
     932        for id, spiketrain in self.spiketrains.items(): 
     933            f1f0_dict[id] = spiketrain.f1f0_ratio(time_bin, f_stim) 
     934        return f1f0_dict 
     935 
    896936        # methods for different output formats 
    897937    def as_ids_times(self, relative=False, quantized=False):