Changeset 462

Show
Ignore:
Timestamp:
09/02/10 15:42:45 (17 months ago)
Author:
pierre
Message:

Add a gamma_generator function into stgen, and simplify all the positions index in SpikeTrains? objects with dimensions

Location:
trunk/src
Files:
2 modified

Legend:

Unmodified
Added
Removed
  • trunk/src/signals/spikes.py

    r453 r462  
    915915    #def __setslice__(self, i, j): 
    916916 
    917  
    918  
    919917    def __setitem__(self, id, spktrain): 
    920918        assert isinstance(spktrain, SpikeTrain), "A SpikeList object can only contain SpikeTrain objects" 
     
    10721070         
    10731071        Examples: 
    1074             >> spklist.id_list() 
     1072            >> spklist.id_list 
    10751073                [0,2,5] 
    10761074            >> spklist.complete(arange(5)) 
    1077             >> spklist.id_list() 
     1075            >> spklist.id_list 
    10781076                [0,1,2,3,4] 
    10791077        """ 
     
    17121710            return id-offset 
    17131711        if len(self.dimensions) == 2: 
    1714             x = (id-offset) % self.dimensions[1] 
    1715             y = self.dimensions[0] - 1 - numpy.floor((id-offset)/self.dimensions[1]).astype(int) 
     1712            x = (id-offset) % self.dimensions[0] 
     1713            y = ((id-offset)/self.dimensions[0]).astype(int) 
    17161714            return (x,y) 
    17171715 
     
    17351733        assert len(position) == len(tuple(self.dimensions)), "position does not have the correct shape !" 
    17361734        if len(self.dimensions) == 1: 
    1737             return position+offset 
     1735            return position + offset 
    17381736        if len(self.dimensions) == 2: 
    1739             return (self.dimensions[0] - 1 - position[1])*self.dimensions[1] + position[0] + offset 
     1737            return position[1]*self.dimensions[1] + position[0] + offset 
    17401738 
    17411739 
     
    17861784            activity_map = numpy.zeros(self.dimensions, float) 
    17871785            rates        = spklist.mean_rates() 
    1788             id_offset = min(self.id_list()) 
     1786            #id_offset    = min(self.id_list()) 
     1787            #x,y          = spklist.id2position(spklist.id_list(), id_offset) 
     1788            x,y          = spklist.id2position(spklist.id_list())             
     1789            #j,i = x, self.dimensions[0] - 1 - y 
    17891790            for count, id in enumerate(spklist.id_list()): 
    1790                 x,y = spklist.id2position(id, id_offset) 
    1791                 j,i = x, self.dimensions[0] - 1 -y 
    1792                 activity_map[i,j] = rates[count] 
     1791                #activity_map[i[count],j[count]] = rates[count]                             
     1792                activity_map[x[count],y[count]] = rates[count] 
    17931793            if not subplot or not HAVE_PYLAB or not HAVE_MATPLOTLIB: 
    17941794                return activity_map 
     
    22472247            time      = time[sort_idx] 
    22482248            pos       = pos[sort_idx] 
     2249            x,y       = spk.id2position(pos) 
    22492250            max_idx   = len(time)-1 
    22502251            logging.info('Making movie %s - this make take a while' % output) 
     
    22542255                while (t_start < t_stop): 
    22552256                    activity_map = numpy.zeros(spk.dimensions) 
    2256                     while ((time[idx] < t_start + time_bin) and (idx < max_idx)):                         
    2257                         x,y = spk.id2position(pos[idx]) 
    2258                         j,i = x, self.dimensions[0] - 1 -y 
    2259                         activity_map[i,j] += 1 
     2257                    while ((time[idx] < t_start + time_bin) and (idx < max_idx)):                                                 
     2258                        #j,i = x, self.dimensions[0] - 1 -y 
     2259                        activity_map[x[idx],y[idx]] += 1 
    22602260                        idx += 1 
    22612261                    im.set_array(activity_map) 
  • trunk/src/stgen.py

    r448 r462  
    255255            return spikes 
    256256 
    257  
     257    def gamma_generator(self, a, b, t_start=0.0, t_stop=1000.0, array=False,debug=False): 
     258        """ 
     259        Returns a SpikeTrain whose spikes are a realization of a Poisson process 
     260        with the given rate (Hz) and stopping time t_stop (milliseconds). 
     261 
     262        Note: t_start is always 0.0, thus all realizations are as if  
     263        they spiked at t=0.0, though this spike is not included in the SpikeList. 
     264 
     265        Inputs: 
     266            a,b     - the parameters of the gamma process 
     267            t_start - the beginning of the SpikeTrain (in ms) 
     268            t_stop  - the end of the SpikeTrain (in ms) 
     269            array   - if True, a numpy array of sorted spikes is returned, 
     270                      rather than a SpikeTrain object. 
     271 
     272        Examples: 
     273            >> gen.poisson_generator(50, 0, 1000) 
     274            >> gen.poisson_generator(20, 5000, 10000, array=True) 
     275          
     276        See also: 
     277            inh_poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator 
     278        """ 
     279 
     280        #number = int((t_stop-t_start)/1000.0*2.0*rate) 
     281         
     282        # less wasteful than double length method above 
     283        n = (t_stop-t_start)/1000.0*(a*b) 
     284        number = numpy.ceil(n+3*numpy.sqrt(n)) 
     285        if number<100: 
     286            number = min(5+numpy.ceil(2*n),100) 
     287         
     288        if number > 0: 
     289            isi = self.rng.gamma(a, b, number)*1000.0 
     290            if number > 1: 
     291                spikes = numpy.add.accumulate(isi) 
     292            else: 
     293                spikes = isi 
     294        else: 
     295            spikes = numpy.array([]) 
     296 
     297        spikes+=t_start 
     298        i = numpy.searchsorted(spikes, t_stop) 
     299 
     300        extra_spikes = [] 
     301        if i==len(spikes): 
     302            # ISI buf overrun 
     303             
     304            t_last = spikes[-1] + self.rng.gamma(a, b, 1)[0]*1000.0 
     305 
     306            while (t_last<t_stop): 
     307                extra_spikes.append(t_last) 
     308                t_last += self.rng.gamma(a, b, 1)[0]*1000.0 
     309             
     310            spikes = numpy.concatenate((spikes,extra_spikes)) 
     311 
     312            if debug: 
     313                print "ISI buf overrun handled. len(spikes)=%d, len(extra_spikes)=%d" % (len(spikes),len(extra_spikes)) 
     314 
     315 
     316        else: 
     317            spikes = numpy.resize(spikes,(i,)) 
     318 
     319        if not array: 
     320            spikes = SpikeTrain(spikes, t_start=t_start,t_stop=t_stop) 
     321 
     322 
     323        if debug: 
     324            return spikes, extra_spikes 
     325        else: 
     326            return spikes 
     327             
    258328    def inh_poisson_generator(self, rate, t, t_stop, array=False): 
    259329        """ 
     
    395465            # find index in "t" time 
    396466            t_i = numpy.searchsorted(t[t_i:],ps[i],'right')-1+t_i 
    397             if rn[i]<gamma_hazard((ps[i]-t_last)/1000.0,a[t_i],b[t_i])/rmax: 
     467            if rn[i]<gamma_hazard_scipy((ps[i]-t_last)/1000.0,a[t_i],b[t_i])/rmax: 
    398468                # keep spike 
    399469                t_last = ps[i]