Exemple #1
0
    def _OU_generator_python2(self,
                              dt,
                              tau,
                              sigma,
                              y0,
                              t_start=0.0,
                              t_stop=1000.0,
                              array=False,
                              time_it=False):
        """ 
        Generates an Orstein Ulbeck process using the forward euler method. The function returns
        an AnalogSignal object.
        
        Inputs:
            dt      - the time resolution in milliseconds of th signal
            tau     - the correlation time in milliseconds
            sigma   - std dev of the process
            y0      - initial value of the process, at t_start
            t_start - start time in milliseconds
            t_stop  - end time in milliseconds
            array   - if True, the functions returns the tuple (y,t) 
            where y and t are the OU signal and the time bins, respectively,
            and are both numpy arrays.
        
        Examples:
            >> stgen.OU_generator(0.1, 2, 3, 0, 0, 10000)

        See also:
            OU_generator_weave1
        """

        import time

        if time_it:
            t1 = time.time()

        t = numpy.arange(t_start, t_stop, dt)
        N = len(t)
        y = numpy.zeros(N, float)
        y[0] = y0
        fac = dt / tau
        gauss = fac * y0 + numpy.sqrt(
            2 * fac) * sigma * self.rng.standard_normal(N - 1)
        mfac = 1 - fac

        # python loop... bad+slow!
        for i in xrange(1, N):
            idx = i - 1
            y[i] = y[idx] * mfac + gauss[idx]

        if time_it:
            print time.time() - t1

        if array:
            return (y, t)

        result = AnalogSignal(y, dt, t_start, t_stop)
        return result
Exemple #2
0
def shotnoise_fromspikes(spike_train,q,tau,dt=0.1,t_start=None, t_stop=None,array=False, eps = 1.0e-8):
    """ 
    Convolves the provided spike train with shot decaying exponentials
    yielding so called shot noise if the spike train is Poisson-like.  
    Returns an AnalogSignal if array=False, otherwise (shotnoise,t) as numpy arrays. 

   Inputs:
      spike_train - a SpikeTrain object
      q - the shot jump for each spike
      tau - the shot decay time constant in milliseconds
      dt - the resolution of the resulting shotnoise in milliseconds
      t_start - start time of the resulting AnalogSignal
                If unspecified, t_start of spike_train is used
      t_stop  - stop time of the resulting AnalogSignal
                If unspecified, t_stop of spike_train is used
      array - if True, returns (shotnoise,t) as numpy arrays, otherwise an AnalogSignal.
      eps - a numerical parameter indicating at what value of 
      the shot kernal the tail is cut.  The default is usually fine.

   Note:
      Spikes in spike_train before t_start are taken into account in the convolution.

   Examples:
      >> stg = stgen.StGen()
      >> st = stg.poisson_generator(10.0,0.0,1000.0)
      >> g_e = shotnoise_fromspikes(st,2.0,10.0,dt=0.1)


   See also:
      poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator, OU_generator ...
   """

    st = spike_train

    if t_start is not None and t_stop is not None:
        assert t_stop>t_start

    # time of vanishing significance
    vs_t = -tau*numpy.log(eps/q)


    if t_stop == None:
        t_stop = st.t_stop

    # need to be clever with start time
    # because we want to take spikes into
    # account which occured in spikes_times
    # before t_start
    if t_start == None:
        t_start = st.t_start
        window_start = st.t_start
    else:
        window_start = t_start
        if t_start>st.t_start:
            t_start = st.t_start
            

    t = numpy.arange(t_start,t_stop,dt)


    kern = q*numpy.exp(-numpy.arange(0.0,vs_t,dt)/tau)

    idx = numpy.clip(numpy.searchsorted(t,st.spike_times,'right')-1,0,len(t)-1)

    a = numpy.zeros(numpy.shape(t),float)

    a[idx] = 1.0

    y = numpy.convolve(a,kern)[0:len(t)]

    if array:
       signal_t = numpy.arange(window_start,t_stop,dt)
       signal_y = y[-len(t):]
       return (signal_y,signal_t)


    result = AnalogSignal(y,dt,t_start=0.0,t_stop=t_stop-t_start)
    result.time_offset(t_start)
    if window_start>t_start:
        result = result.time_slice(window_start,t_stop)
    return result
Exemple #3
0
    def OU_generator_weave1(self, dt,tau,sigma,y0,t_start=0.0,t_stop=1000.0,time_it=False):
        """ 
        Generates an Orstein Ulbeck process using the forward euler method. The function returns
        an AnalogSignal object.

        OU_generator_weave1, as opposed to OU_generator, uses scipy.weave
        and is thus much faster.
        
        Inputs:
        -------
            dt      - the time resolution in milliseconds of th signal
            tau     - the correlation time in milliseconds
            sigma   - std dev of the process
            y0      - initial value of the process, at t_start
            t_start - start time in milliseconds
            t_stop  - end time in milliseconds
            array   - if True, the functions returns the tuple (y,t) 
                      where y and t are the OU signal and the time bins, respectively,
                      and are both numpy arrays.
        
        Examples:
        ---------
            >> stgen.OU_generator_weave1(0.1, 2, 3, 0, 0, 10000)

        See also:
        ---------
            OU_generator
        """
        import scipy.weave
        
        import time

        if time_it:
            t1 = time.time()


        t = numpy.arange(t_start,t_stop,dt)
        N = len(t)
        y = numpy.zeros(N,float)
        y[0] = y0
        fac = dt/tau
        gauss = fac*y0+numpy.sqrt(2*fac)*sigma*self.rng.standard_normal(N-1)
        
        
        # python loop... bad+slow!
        #for i in xrange(1,len(t)):
        #    y[i] = y[i-1]+dt/tau*(y0-y[i-1])+numpy.sqrt(2*dt/tau)*sigma*numpy.random.normal()

        # use weave instead

        code = """

        double f = 1.0-fac;

        for(int i=1;i<Ny[0];i++) {
          y(i) = y(i-1)*f + gauss(i-1);
        }
        """

        scipy.weave.inline(code,['y', 'gauss', 'fac'],
                     type_converters=scipy.weave.converters.blitz)


        if time_it:
            print 'Elapsed ',time.time()-t1,' seconds.'

        if array:
            return (y,t)

        result = AnalogSignal(y,dt,t_start,t_stop)
        return result
Exemple #4
0
def shotnoise_fromspikes(spike_train,q,tau,dt=0.1,t_start=None, t_stop=None,array=False, eps = 1.0e-8):
    """ 
    Convolves the provided spike train with shot decaying exponentials
    yielding so called shot noise if the spike train is Poisson-like.  
    Returns an AnalogSignal if array=False, otherwise (shotnoise,t) as numpy arrays. 

   Inputs:
   -------
      spike_train - a SpikeTrain object
      q - the shot jump for each spike
      tau - the shot decay time constant in milliseconds
      dt - the resolution of the resulting shotnoise in milliseconds
      t_start - start time of the resulting AnalogSignal
                If unspecified, t_start of spike_train is used
      t_stop  - stop time of the resulting AnalogSignal
                If unspecified, t_stop of spike_train is used
      array - if True, returns (shotnoise,t) as numpy arrays, otherwise an AnalogSignal.
      eps - a numerical parameter indicating at what value of 
      the shot kernal the tail is cut.  The default is usually fine.

   Note:
   -----
      Spikes in spike_train before t_start are taken into account in the convolution.

   Examples:
   ---------
      >> stg = stgen.StGen()
      >> st = stg.poisson_generator(10.0,0.0,1000.0)
      >> g_e = shotnoise_fromspikes(st,2.0,10.0,dt=0.1)


   See also:
   ---------
      poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator, OU_generator ...
   """

    st = spike_train

    if t_start is not None and t_stop is not None:
        assert t_stop>t_start

    # time of vanishing significance
    vs_t = -tau*numpy.log(eps/q)


    if t_stop == None:
        t_stop = st.t_stop

    # need to be clever with start time
    # because we want to take spikes into
    # account which occured in spikes_times
    # before t_start
    if t_start == None:
        t_start = st.t_start
        window_start = st.t_start
    else:
        window_start = t_start
        if t_start>st.t_start:
            t_start = st.t_start
            

    t = numpy.arange(t_start,t_stop,dt)


    kern = q*numpy.exp(-numpy.arange(0.0,vs_t,dt)/tau)

    idx = numpy.clip(numpy.searchsorted(t,st.spike_times,'right')-1,0,len(t)-1)

    a = numpy.zeros(numpy.shape(t),float)

    a[idx] = 1.0

    y = numpy.convolve(a,kern)[0:len(t)]

    if array:
       signal_t = numpy.arange(window_start,t_stop,dt)
       signal_y = y[-len(t):]
       return (signal_y,signal_t)


    result = AnalogSignal(y,dt,t_start=0.0,t_stop=t_stop-t_start)
    result.time_offset(t_start)
    if window_start>t_start:
        result = result.time_slice(window_start,t_stop)
    return result