Changeset 337
- Timestamp:
- 11/13/08 15:41:12 (2 months ago)
- Files:
-
- trunk/src/signals.py (modified) (1 diff)
- trunk/src/stgen.py (modified) (17 diffs)
- trunk/test/test_stgen.py (modified) (1 diff)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/src/signals.py
r321 r337 2288 2288 t_stop - end of the new SpikeTrain, in ms. 2289 2289 """ 2290 signal = self.signal[numpy.floor(t_start/self.dt):numpy.floor(t_stop/self.dt)] 2291 return AnalogSignal(signal, self.dt, t_start, t_stop) 2290 assert t_start>self.t_start 2291 assert t_stop<=self.t_stop 2292 assert t_stop>t_start 2293 2294 t = self.time_axis() 2295 i_start = numpy.searchsorted(t,t_start,'right')-1 2296 i_stop = numpy.searchsorted(t,t_stop,'right')-1 2297 2298 signal = self.signal[i_start:i_stop] 2299 result = AnalogSignal(signal, self.dt, 0.0, t_stop-t_start) 2300 result.time_offset(t_start) 2301 return result 2292 2302 2293 2303 def threshold_detection(self, threshold=None, format=None): trunk/src/stgen.py
r330 r337 38 38 a - dimensionless 39 39 b - in units of seconds 40 41 See also: 42 inh_gamma_generator 40 43 """ 41 44 … … 71 74 a - dimensionless 72 75 b - in units of seconds 76 77 See also: 78 inh_gamma_generator 79 73 80 """ 74 81 … … 133 140 inh_poisson_generator - inhomogeneous Poisson process (time varying rate) 134 141 inh_gamma_generator - inhomogeneous Gamma process (time varying a,b) 142 inh_adaptingmarkov_generator - inhomogeneous adapting markov process (time varying) 143 inh_2Dadaptingmarkov_generator - inhomogeneous adapting and 144 refractory markov process (time varying) 135 145 136 146 Continuous time processes: … … 178 188 179 189 See also: 180 inh_poisson_generator, inh_gamma_generator 190 inh_poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator 181 191 """ 182 192 … … 261 271 262 272 See also: 263 poisson_generator, inh_gamma_generator 273 poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator 264 274 """ 265 275 … … 323 333 324 334 Examples: 325 335 See source:trunk/examples/stgen/inh_gamma_psth.py 336 326 337 See also: 327 338 inh_poisson_generator, gamma_hazard … … 434 445 435 446 Examples: 447 See source:trunk/examples/stgen/inh_2Dmarkov_psth.py 448 436 449 437 450 See also: … … 558 571 559 572 Examples: 573 See source:trunk/examples/stgen/inh_2Dmarkov_psth.py 560 574 561 575 See also: … … 642 656 """ 643 657 Generates an Orstein Ulbeck process using the forward euler method. The function returns 644 an AnalogSignal object 658 an AnalogSignal object. 645 659 646 660 Inputs: … … 651 665 t_start - start time in milliseconds 652 666 t_stop - end time in milliseconds 653 array - if True, a numpy array of the OU signal is returned, 654 rather than an AnalogSignal object. 667 array - if True, the functions returns the tuple (y,t) 668 where y and t are the OU signal and the time bins, respectively, 669 and are both numpy arrays. 655 670 656 671 Examples: 657 672 >> stgen.OU_generator(0.1, 2, 3, 0, 0, 10000) 673 674 See also: 675 OU_generator_weave1 658 676 """ 659 677 … … 693 711 """ 694 712 Generates an Orstein Ulbeck process using the forward euler method. The function returns 695 an AnalogSignal object 713 an AnalogSignal object. 696 714 697 715 Inputs: … … 702 720 t_start - start time in milliseconds 703 721 t_stop - end time in milliseconds 704 array - if True, a numpy array of the OU signal is returned, 705 rather than an AnalogSignal object. 722 array - if True, the functions returns the tuple (y,t) 723 where y and t are the OU signal and the time bins, respectively, 724 and are both numpy arrays. 706 725 707 726 Examples: 708 727 >> stgen.OU_generator(0.1, 2, 3, 0, 0, 10000) 728 729 See also: 730 OU_generator_weave1 709 731 """ 710 732 … … 742 764 743 765 def OU_generator_weave1(self, dt,tau,sigma,y0,t_start=0.0,t_stop=1000.0,time_it=False): 744 """ generates an OU process using forward euler 745 dt = resolution 746 tau = correlation time 747 sigma = std dev of process 748 y0 = mean/initial value 749 tsim = how long to simulate 766 """ 767 Generates an Orstein Ulbeck process using the forward euler method. The function returns 768 an AnalogSignal object. 769 770 OU_generator_weave1, as opposed to OU_generator, uses scipy.weave 771 and is thus much faster. 772 773 Inputs: 774 dt - the time resolution in milliseconds of th signal 775 tau - the correlation time in milliseconds 776 sigma - std dev of the process 777 y0 - initial value of the process, at t_start 778 t_start - start time in milliseconds 779 t_stop - end time in milliseconds 780 array - if True, the functions returns the tuple (y,t) 781 where y and t are the OU signal and the time bins, respectively, 782 and are both numpy arrays. 783 784 Examples: 785 >> stgen.OU_generator_weave1(0.1, 2, 3, 0, 0, 10000) 786 787 See also: 788 OU_generator 750 789 """ 751 790 import scipy.weave … … 805 844 # TODO fix shotnoise stuff below ... and write tests 806 845 807 def shotnoise_fromspikes(self,rate,tau,q,num,t):808 """809 Generate shotnoise810 811 Inputs:812 rate - the rate (in Hz) of the shotnoise813 tau - the exponential decay of the synapse814 g - the quantal increase for a spike815 816 quantal-increase-"q"-exponential-decay-"tau" synapse817 and a poisson spike train of "rate" and "num" """818 819 g = numpy.zeros(numpy.shape(t),float)820 for i in xrange(num):821 822 spikes = poisson_generator(rate,t[-1])823 dg=exp_conv(spikes,t,q,tau)824 tmp = add(g,dg,g)825 826 return g827 828 829 830 831 846 # Operations on spike trains 832 847 833 848 834 def shotnoise_fromspikes(spike_train,q,tau,dt ,array=False, eps = 1.0e-8):849 def shotnoise_fromspikes(spike_train,q,tau,dt=0.1,t_start=None, t_stop=None,array=False, eps = 1.0e-8): 835 850 """ 836 851 Convolves the provided spike train with shot decaying exponentials … … 842 857 q - the shot jump for each spike 843 858 tau - the shot decay time constant in milliseconds 859 t_start - start time of the resulting AnalogSignal 860 If unspecified, t_start of spike_train is used 861 t_stop - stop time of the resulting AnalogSignal 862 If unspecified, t_stop of spike_train is used 844 863 dt - the resolution of the resulting shotnoise in milliseconds 845 864 array - if True, returns (shotnoise,t) as numpy arrays, otherwise an AnalogSignal. … … 847 866 the shot kernal the tail is cut. The default is usually fine. 848 867 868 Note: 869 Spikes in spike_train before t_start are taken into account in the convolution. 870 849 871 Examples: 850 872 >> stg = stgen.StGen() 851 873 >> st = stg.poisson_generator(10.0,0.0,1000.0) 852 >> g_e = shotnoise_fromspikes(st,2.0,10.0 )874 >> g_e = shotnoise_fromspikes(st,2.0,10.0,dt=0.1) 853 875 854 876 855 877 See also: 856 poisson_generator, inh_gamma_generator, OU_generator ...878 poisson_generator, inh_gamma_generator, inh_adaptingmarkov_generator, OU_generator ... 857 879 """ 858 880 859 881 st = spike_train 860 882 861 t = numpy.arange(st.t_start,st.t_stop,dt)883 assert t_stop>t_start 862 884 863 885 # time of vanishing significance 864 886 vs_t = -tau*numpy.log(eps/q) 865 887 888 889 if t_stop == None: 890 t_stop = st.t_stop 891 892 # need to be clever with start time 893 # because we want to take spikes into 894 # account which occured in spikes_times 895 # before t_start 896 if t_start == None: 897 t_start = st.t_start 898 window_start = st.t_start 899 else: 900 window_start = t_start 901 if t_start>st.t_start: 902 t_start = st.t_start 903 904 905 t = numpy.arange(t_start,t_stop,dt) 906 907 866 908 kern = q*numpy.exp(-numpy.arange(0.0,vs_t,dt)/tau) 867 909 868 idx = numpy.clip(numpy.searchsorted(t, poisson_train,'right')-1,0,len(t)-1)869 870 a = numpy.zeros( shape(t),float)910 idx = numpy.clip(numpy.searchsorted(t,st.spike_times,'right')-1,0,len(t)-1) 911 912 a = numpy.zeros(numpy.shape(t),float) 871 913 872 914 a[idx] = 1.0 873 915 874 y = convolve(a,kern)[0:len(t)] 875 876 result = AnalogSignal(y,dt,t_start=0,t_stop=st.t_stop-st.t_start) 877 result.time_offset(st.t_start) 916 y = numpy.convolve(a,kern)[0:len(t)] 917 918 result = AnalogSignal(y,dt,t_start=0.0,t_stop=t_stop-t_start) 919 result.time_offset(t_start) 920 if window_start>t_start: 921 result = result.time_slice(window_start,t_stop) 878 922 return result 879 923 … … 882 926 883 927 884 def gen_g_add(spikes,tau,q,t,eps = 1.0e-8):928 def _gen_g_add(spikes,tau,q,t,eps = 1.0e-8): 885 929 886 930 #spikes = poisson_generator(rate,t[-1]) trunk/test/test_stgen.py
r323 r337 294 294 295 295 296 def testShotNoiseFromSpikes(self): 297 298 299 stg = stgen.StGen() 300 301 from numpy import array 302 303 st = stg.poisson_generator(10.0,0.0,1000.0) 304 305 ge = shotnoise_fromspikes(st,2.0,10.0,dt=0.1) 306 307 assert ge.t_start==0.0 308 assert ge.t_stop==1000.0 309 310 st = stg.poisson_generator(10.0,0.0,1000.0) 311 ge = shotnoise_fromspikes(st,2.0,10.0,dt=0.1,t_start=500.0,t_stop=1500.0) 312 313 assert ge.t_start==500.0 314 assert ge.t_stop==1500.0 315 296 316 297 317

