| 13 | | def gamma_hazard(x,a,b,dt=1e-4): |
|---|
| 14 | | """Compute the hazard function for a gamma process with parameters a,b |
|---|
| | 11 | def 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 |
|---|
| 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. |
|---|
| 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 | """ |
|---|
| 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] |
|---|
| 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 |
|---|
| 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). |
|---|
| 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 | |
|---|
| 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 | |
|---|