Changeset 418

Show
Ignore:
Timestamp:
08/17/09 21:30:58 (3 years ago)
Author:
lestebanez
Message:

spikes.py : fixes several bugs

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • branches/interval/src/signals/spikes.py

    r410 r418  
    2626 
    2727import os, re, numpy 
     28import copy 
    2829import logging 
    2930from NeuroTools import check_dependency, check_numpy_version, analysis 
     
    111112 
    112113 
    113  
    114114    def __str__(self): 
    115115        return str(self.spike_times) 
     116 
     117    def __format__(self): 
     118        return 'hella' 
    116119 
    117120    def __len__(self): 
     
    393396            spikes  = interval.slice_times(self.spike_times) 
    394397        else: 
    395             spikes = self 
     398            spikes = self.spike_times 
    396399 
    397400        subplot = get_display(display) 
     
    401404        else: 
    402405            if len(spikes) > 0: 
    403                 subplot.plot(spikes,numpy.ones(len(spikes)),',', **kwargs) 
     406                subplot.plot(spikes,numpy.ones(len(spikes)),',', **kwargs) 
    404407                xlabel = "Time (ms)" 
    405408                ylabel = "Neurons #" 
     
    466469        """ 
    467470        Bin the spikes with the specified bin width. The first and last bins 
    468         are calculated from `self.t_start` and `self.t_stop`. 
     471        are calculated from the interval of the spike train. 
    469472         
    470473        Inputs: 
     
    576579     
    577580     
    578     def psth(self, events, time_bin=2, t_min=50, t_max=50, display = False, kwargs={}): 
     581    def psth(self, events, time_bin=2, t_min=50, t_max=50, display = False, labels=True, kwargs={}): 
    579582        """ 
    580583        Return the psth of the spike times contained in the SpikeTrain according to selected events,  
     
    587590            t_min   - Time (>0) to average the signal before an event, in ms (default 0) 
    588591            t_max   - Time (>0) to average the signal after an event, in ms  (default 100) 
    589             display - if True, a new figure is created. Could also be a subplot. 
     592            display - if True, the psth is displayed in a new figure. If a Matplotlib Axes name, 
     593                      the psth is displayed inside this Axes. 
     594            labels  - if True, psth plot has full labels. If not, the plot is shown 'raw'  
    590595            kwargs  - dictionary contening extra parameters that will be sent to the plot  
    591596                      function 
     
    603608            events = events.spike_times 
    604609        assert (t_min >= 0) and (t_max >= 0), "t_min and t_max should be greater than 0" 
    605         assert len(events) > 0, "events should not be empty and should contained at least one element" 
    606  
     610        assert len(events) > 0, "events should not be empty" 
    607611        spk_hist = self.time_histogram(time_bin) 
    608612        subplot  = get_display(display) 
    609613        count    = 0 
    610         t_min_l  = numpy.floor(t_min/time_bin) 
    611         t_max_l  = numpy.floor(t_max/time_bin) 
     614        t_min_l  = numpy.floor(t_min/float(time_bin)) 
     615        t_max_l  = numpy.floor(t_max/float(time_bin)) 
    612616        result   = numpy.zeros((t_min_l+t_max_l), numpy.float32) 
     617        events_interval = copy.copy(self.interval) 
     618        events_interval.offset_start(t_min, colapse=True) 
     619        events_interval.offset_stop(-t_max, colapse=True) 
     620        events = events_interval.slice_times(events) 
     621        assert len(events) > 0, "the PSTH windows [event-t_min, event+t_max] should be included within the spike train interval" 
    613622        for ev in events: 
    614            itv = Interval((ev-t_min,ev+t_max)) 
    615            ev  = numpy.floor(ev/time_bin) 
    616            if itv.intersect(self.interval).total_duration() == (t_max+t_min): 
    617                count  += 1 
    618                result += spk_hist[(ev-t_min_l):ev+t_max_l] 
     623            ev  = numpy.floor((ev - self.interval.t_start())/float(time_bin)) 
     624            count  += 1.     
     625            result += spk_hist[(ev-t_min_l):ev+t_max_l] 
    619626        result /= count 
    620627         
     
    622629            return result 
    623630        else: 
    624             xlabel = "Time (ms)" 
    625             ylabel = "PSTH" 
    626             time   = numpy.linspace(-t_min, t_max, (t_min+t_max)/time_bin) 
    627             set_labels(subplot, xlabel, ylabel) 
     631            if labels : 
     632                xlabel = "Time (ms)" 
     633                ylabel = "PSTH" 
     634                set_labels(subplot, xlabel, ylabel) 
     635            else : 
     636                subplot.xaxis.set_major_locator(pylab.NullLocator()) 
     637                subplot.yaxis.set_major_locator(pylab.NullLocator()) 
     638            time   = numpy.linspace(-t_min, t_max, (t_min+t_max)/time_bin) 
    628639            subplot.plot(time, result, c='k', **kwargs) 
    629640            xmin, xmax, ymin, ymax = subplot.axis() 
    630             subplot.plot([0,0],[ymin, ymax], c='r') 
     641            subplot.axvline(0, c='r') 
    631642            set_axis_limits(subplot, -t_min, t_max, ymin, ymax) 
    632643            pylab.draw() 
     
    795806                [0,1,2,3,....,9999] 
    796807        """ 
     808        print self.spiketrains.keys() 
    797809        return numpy.array(self.spiketrains.keys(), int) 
    798810