Changeset 302

Show
Ignore:
Timestamp:
11/07/08 09:28:33 (2 months ago)
Author:
pierre
Message:

Reformat the documentation in stgen to look like something more similar to plotting and signals. It would be good if we could all agree on a standard format for the docstrings, it will simplify doc generation. Add some functions in signals also, especially a plot() one for the AnalogSignal?.

Files:

Legend:

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

    r298 r302  
    5656        dependencies[name]['check'] = True 
    5757        return dependencies[name]['is_present'] 
    58  
    59  
    60 #for name in dependencies: 
    61     #if not dependencies[name]['check']: 
    62         #is_present = check_dependency(name) 
    63         #dependencies[name]['check'] = True 
    64     #if is_present: 
    65         #if name == 'numpy': 
    66             #newnum = check_numpy_version() 
    67         #if name == 'pytables': 
    68  
    69  
    70  
    71  
    72  
    73  
    74  
    75  
    76  
    7758 
    7859 
  • trunk/src/signals.py

    r298 r302  
    21572157        return numpy.linspace(self.t_start-norm, self.t_stop-norm, len(self.signal)) 
    21582158 
     2159     
     2160    def time_offset(self, offset): 
     2161        """ 
     2162        Add a time offset to the AnalogSignal object. t_start and t_stop are 
     2163        shifted from offset. 
     2164          
     2165        Inputs: 
     2166            offset - the time offset, in ms 
     2167         
     2168        Examples: 
     2169            >> as = AnalogSignal(arange(0,100,0.1),0.1) 
     2170            >> as.t_stop 
     2171                100 
     2172            >> as.time_offset(1000) 
     2173            >> as.t_stop 
     2174                1100 
     2175        """ 
     2176        self.t_start += offset 
     2177        self.t_stop  += offset 
     2178 
     2179     
    21592180    def time_parameters(self): 
    21602181        """ 
     
    21622183        """ 
    21632184        return (self.t_start, self.t_stop, self.dt) 
     2185     
     2186     
     2187    def plot(self, ylabel="Analog Signal", display=True, kwargs={}): 
     2188        """ 
     2189        Plot the AnalogSignal 
     2190         
     2191        Inputs: 
     2192            ylabel  - A string to sepcify the label on the yaxis. 
     2193            display - if True, a new figure is created. Could also be a subplot 
     2194            kwargs  - dictionary contening extra parameters that will be sent to the plot  
     2195                      function 
     2196         
     2197        Examples: 
     2198            >> z = subplot(221) 
     2199            >> signal.plot(ylabel="Vm", display=z, kwargs={'color':'r'}) 
     2200        """ 
     2201        subplot   = get_display(display) 
     2202        time_axis = self.time_axis()   
     2203        if not subplot or not ENABLE_PLOTS: 
     2204            print MATPLOTLIB_ERROR 
     2205        else: 
     2206            xlabel = "Time (ms)" 
     2207            set_labels(subplot, xlabel, ylabel) 
     2208            subplot.plot(time_axis, self.signal, **kwargs) 
     2209            pylab.draw() 
     2210     
    21642211     
    21652212    def time_slice(self, t_start, t_stop): 
  • trunk/src/spike2/spike2channels.py

    r265 r302  
    11import numpy 
    2 import pylab 
    32import NeuroTools.signals as signals 
    43from NeuroTools.spike2.sonpy import son 
  • trunk/src/stgen.py

    r299 r302  
    44 
    55from NeuroTools import check_dependency 
    6  
    7 from signals import SpikeTrain 
    8  
     6from signals import SpikeTrain, AnalogSignal 
    97from numpy import array, log 
    108import numpy 
    119 
    1210 
    13 def gamma_hazard(x,a,b,dt=1e-4): 
    14     """Compute the hazard function for a gamma process with parameters a,b 
     11def gamma_hazard(x, a, b, dt=1e-4): 
     12    """ 
     13    Compute the hazard function for a gamma process with parameters a,b 
     14    where a and b are the parameters of the gamma PDF: 
     15    y(t) = x^(a-1) \exp(-x/b) / (\Gamma(a)*b^a) 
     16 
     17    Inputs: 
     18        x   - unit in seconds 
     19        b   - units in seconds 
     20        a   - dimensionless 
    1521     
    16     where a and b are the parameters of the gamma PDF: 
    17  
    18     y(t) = x^(a-1) \exp(-x/b) / (\Gamma(a)*b^a) 
    19  
    20     x is in units of seconds 
    21  
    22     b is in units of seconds 
    23  
    24     a is dimensionless 
    25  
    2622    """ 
    2723 
     
    3127    # and look for the kinks. 
    3228 
    33     from scipy.special import gammaincc 
     29    if check_dependency('scipy'): 
     30        from scipy.special import gammaincc 
    3431    Hpre = -log(gammaincc(a,(x-dt)/b)) 
    3532    Hpost = -log(gammaincc(a,(x+dt)/b)) 
     
    4845 
    4946    def __init__(self, rng=None, seed=None): 
    50         """ Spike Train Generator 
    51  
    52         And object to generate spiking random processes with various statistics  
     47        """  
     48        Spike Train Generator 
     49        Object to generate spiking random processes with various statistics  
    5350        (inhomogeneous poisson, inhomogeneous gamma, etc.) as SpikeTrain objects. 
    5451 
    55         rng should be None, or a numpy.random.RandomState object,  
    56         or an object with the same interface. 
    57  
    58         If rng is not None, the provided rng will be used 
    59         to generate random numbers, otherwise StGen will create  
    60         its own random number generator. 
    61  
     52        Inputs: 
     53            rng - Seed for the random number generator. Can be None, or  
     54                  a numpy.random.RandomState object, or an object with the same  
     55                  interface. 
     56 
     57        If rng is not None, the provided rng will be used to generate random numbers,  
     58        otherwise StGen will create its own random number generator. 
    6259        If a seed is provided, it is passed to rng.seed(seed) 
    6360 
     61        Examples: 
     62            >> x = StGen() 
    6463 
    6564        """ 
     
    7877 
    7978 
    80     def poisson_generator(self,rate,t_start=0.0,t_stop=1000.0,array=False): 
    81         """Returns a SpikeList whose spikes are a realization of a Poisson process 
    82            with the given rate (Hz) and stopping time t_stop (milliseconds). 
    83  
    84            Note: t_start is always 0.0, thus all realizations are as if  
    85            they spiked at t=0.0, though this spike is not included in the SpikeList. 
    86  
    87            if array is True, as numpy array of sorted spikes is returned, 
    88            rather than a SpikeList object. 
    89  
    90            """ 
     79    def poisson_generator(self, rate, t_start=0.0, t_stop=1000.0, array=False): 
     80        """ 
     81        Returns a SpikeList whose spikes are a realization of a Poisson process 
     82        with the given rate (Hz) and stopping time t_stop (milliseconds). 
     83 
     84        Note: t_start is always 0.0, thus all realizations are as if  
     85        they spiked at t=0.0, though this spike is not included in the SpikeList. 
     86 
     87        Inputs: 
     88            rate    - the rate of the discharge (in Hz) 
     89            t_start - the beginning of the final SpikeTrain object (in ms) 
     90            t_stop  - the end of the final SpikeTrain object (in ms) 
     91            array   - if True, a numpy array of sorted spikes is returned, 
     92                      rather than a SpikeList object. 
     93 
     94        Examples: 
     95            >> gen.poisson_generator(50, 0, 1000) 
     96            >> gen.poisson_generator(20, 5000, 10000, array=True) 
     97          
     98        See also: 
     99            inh_poisson_generator, inh_gamma_generator 
     100        """ 
    91101 
    92102        number = int((t_stop-t_start)/1000.0*2.0*rate) 
     
    112122 
    113123 
    114     def inh_poisson_generator(self,rate,t,t_stop,array=False): 
    115         """Returns a SpikeList whose spikes are a realization of an  
    116         inhomogeneous poisson process (dynamic rate). 
    117  
    118         The implementation uses the thinning method, as presented in the  
    119         references. 
    120  
    121         t: an array specifying the time bins (in milliseconds) 
    122         at which to specify the rate 
    123  
    124         NOTE: t_start=t[0] 
    125  
    126         rate: the rate (Hz), where rate[i] is active on the  
    127         interval [t[i],t[i+1]) (numpy array) 
    128         t_stop: length of time to simulate process 
    129  
     124    def inh_poisson_generator(self, rate, t, t_stop, array=False): 
     125        """ 
     126        Returns a SpikeList whose spikes are a realization of an inhomogeneous  
     127        poisson process (dynamic rate). The implementation uses the thinning  
     128        method, as presented in the references. 
     129 
     130        Inputs: 
     131            t      - an array specifying the time bins (in milliseconds) at which to  
     132                     specify the rate 
     133            rate   - an array of the rates (Hz) where rate[i] is active on interval  
     134                     [t[i],t[i+1]] 
     135            t_stop - lenght of time to simulate process (in ms) 
     136            array  - if True, a numpy array of sorted spikes is returned, 
     137                     rather than a SpikeList object. 
     138 
     139        Note: 
     140            t_start=t[0] 
    130141 
    131142        References: 
     
    137148        Devroye, L. (1986). Non-uniform random variate generation. New York: Springer-Verlag. 
    138149 
    139  
     150        Examples: 
     151            >> time = arange(0,1000) 
     152            >> stgen.inh_poisson_generator(time,sin(time), 1000) 
     153 
     154        See also: 
     155            poisson_generator, inh_gamma_generator 
    140156        """ 
    141157 
     
    169185 
    170186 
    171     def _inh_gamma_generator_python(self,a,b,t,t_stop,array=False): 
    172         """Returns a SpikeList whose spikes are a realization of an  
    173         inhomogeneous gamma process (dynamic rate). 
    174  
    175         The implementation uses the thinning method, as presented in the  
     187    def _inh_gamma_generator_python(self, a, b, t, t_stop, array=False): 
     188        """ 
     189        Returns a SpikeList whose spikes are a realization of an inhomogeneous gamma process  
     190        (dynamic rate). The implementation uses the thinning method, as presented in the  
    176191        references. 
    177192 
    178         t: an array specifying the time bins (in milliseconds) 
    179         at which to specify the rate 
    180  
    181         NOTE: t_start=t[0] 
    182  
    183         a and b are the parameters of the gamma PDF,  
    184         where a[i] and b[i] are active on the  
    185         interval [t[i],t[i+1]) (numpy arrays). 
    186  
    187         a is a dimensionless quantity >0, but typically on the order of 2-10.  
    188         a=1 results in a poisson process. 
    189  
    190         b is assumed to be in units of 1/Hz (seconds). 
    191  
    192         y(t) = x^(a-1) \exp(-x/b) / (\Gamma(a)*b^a) 
    193  
    194         t_stop: length of time to simulate process 
    195  
     193        Inputs: 
     194            t      - an array specifying the time bins (in milliseconds) at which to  
     195                     specify the rate 
     196            a,b    - arrays of the parameters of the gamma PDF where a[i] and b[i]  
     197                     will be active on interval [t[i],t[i+1]] 
     198            t_stop - lenght of time to simulate process (in ms) 
     199            array  - if True, a numpy array of sorted spikes is returned, 
     200                     rather than a SpikeList object. 
     201 
     202        Note:  
     203            t_start=t[0] 
     204            a is a dimensionless quantity >0, but typically on the order of 2-10.  
     205            a = 1 results in a poisson process. 
     206            b is assumed to be in units of 1/Hz (seconds). 
    196207 
    197208        References: 
     
    203214        Devroye, L. (1986). Non-uniform random variate generation. New York: Springer-Verlag. 
    204215 
     216        Examples: 
     217         
     218        See also: 
     219            inh_poisson_generator, gamma_hazard 
    205220 
    206221        """ 
     
    261276 
    262277 
    263     def _OU_generator_python(self,dt,tau,sigma,y0,t_start=0.0,t_stop=1000.0,array=False): 
    264         """ generates an OU process using forward euler 
    265         dt = resolution in milliseconds 
    266         tau = correlation time in milliseconds 
    267         sigma = std dev of process 
    268         y0 = mean/initial value 
    269         t_stop = start time in milliseconds 
    270         t_stop = end time in milliseconds 
    271         """ 
    272  
    273  
    274         t = numpy.arange(t_start,t_stop,dt) 
    275         y = numpy.zeros(numpy.shape(t),float) 
    276         gauss = self.rng.standard_normal(len(t)-1) 
    277  
    278         y[0] = y0 
    279  
     278    def _OU_generator_python(self, dt, tau, sigma, y0, t_start=0.0, t_stop=1000.0, array=False): 
     279        """  
     280        Generates an Orstein Ulbeck process using the forward euler method. The function returns 
     281        an AnalogSignal object 
     282         
     283        Inputs: 
     284            dt      - the time resolution in milliseconds of th signal 
     285            tau     - the correlation time in milliseconds 
     286            sigma   - std dev of the process 
     287            y0      - initial value of the process, at t_start 
     288            t_start - start time in milliseconds 
     289            t_stop  - end time in milliseconds 
     290            array   - if True, a numpy array of the OU signal is returned, 
     291                      rather than an AnalogSignal object. 
     292         
     293        Examples: 
     294            >> stgen.OU_generator(0.1, 2, 3, 0, 0, 10000) 
     295        """ 
     296 
     297        t     = numpy.arange(t_start,t_stop,dt) 
     298        N     = len(t) 
     299        y     = numpy.zeros(N,float) 
     300        gauss = self.rng.standard_normal(N-1) 
     301        y[0]  = y0 
     302        fac   = dt/tau 
     303        noise = numpy.sqrt(2*fac)*sigma 
     304         
    280305        # python loop... bad+slow! 
    281         for i in xrange(1,len(t)): 
    282             y[i] = y[i-1]+dt/tau*(y0-y[i-1])+numpy.sqrt(2*dt/tau)*sigma*gauss[i-1] 
    283  
    284         #if array: 
    285          
    286         return (y,t) 
    287  
    288         #return AnalogSignal(y,dt,t_start=t_start,t_stop=t_stop) 
    289  
     306        for i in xrange(1,N): 
     307            y[i] = y[i-1]+fac*(y0-y[i-1])+noise*gauss[i-1] 
     308         
     309        if array: 
     310            return (y,t) 
     311 
     312        result = AnalogSignal(y,dt,t_start=0,t_stop=t_stop-t_start) 
     313        result.time_offset(t_start) 
     314        return result 
     315         
    290316    # use slow python implementation for the time being 
    291317    # TODO: provide optimized C/weave implementation if possible 
     
    301327 
    302328    def shotnoise_generator(self,rate,tau,q,num,t): 
    303  
    304         """ generate shotnoise 
     329        """  
     330        Generate shotnoise 
     331 
     332        Inputs: 
     333            rate - the rate (in Hz) of the shotnoise 
     334            tau  - the exponential decay of the synapse 
     335            g    - the quantal increase for a spike 
    305336 
    306337        quantal-increase-"q"-exponential-decay-"tau" synapse 
     
    308339 
    309340        g = numpy.zeros(numpy.shape(t),float) 
    310  
    311  
    312341        for i in xrange(num): 
    313342 
     
    325354 
    326355def exp_conv(poisson_train,t,q,tau,eps = 1.0e-8): 
    327  
    328     """ convolve poisson spike train 
    329     with shot decaying exponentials 
     356    """  
     357    Convolve poisson spike trains with shot decaying exponentials 
    330358    t must be equally spaced arrayrange 
    331359    poisson spike times must all in the range of t