Changeset 315
- Timestamp:
- 11/10/08 19:17:08 (2 months ago)
- Files:
-
- trunk/examples/single_neuron/CRF_neuron_vs_signal.py (modified) (6 diffs)
- trunk/examples/single_neuron/fiber.param (modified) (1 diff)
- trunk/examples/single_neuron/simple_single_neuron.py (modified) (7 diffs)
- trunk/src/signals.py (modified) (7 diffs)
- trunk/test/test_signals.py (modified) (4 diffs)
Legend:
- Unmodified
- Added
- Removed
- Modified
- Copied
- Moved
trunk/examples/single_neuron/CRF_neuron_vs_signal.py
r173 r315 23 23 24 24 # this is not mandatory but just a "easy_install progressbar" away 25 # else remove all corresponding lines in this code...25 # else remove all corresponding 3 lines in this code... 26 26 import progressbar # see http://projects.scipy.org/pipermail/scipy-dev/2008-January/008200.html 27 27 … … 30 30 N_exp_noise = 10 31 31 N_exp_snr = 25 32 p.snr = ParameterRange(list(numpy.linspace(-1.,4.,N_exp_snr))) 33 p.noise_std = ParameterRange(list(10.**(numpy.linspace(-.50,1.,N_exp_noise)))) 32 33 ps = ParameterSpace({ 34 'snr' : ParameterRange(list(numpy.linspace(-1.,4.,N_exp_snr))), 35 'noise_std' : ParameterRange(list(10.**(numpy.linspace(-.50,1.,N_exp_noise))))}) 36 34 37 35 38 name = sys.argv[0].split('.')[0] # name of the current script withpout the '.py' part … … 40 43 41 44 # calculates the dimension of the parameter space 42 results_dim, results_label = p .parameter_space_dimension_labels()45 results_dim, results_label = ps.parameter_space_dimension_labels() 43 46 44 47 # creates results array with size of parameter space dimension … … 49 52 pbar=progressbar.ProgressBar(widgets=[name, " ", progressbar.Percentage(), ' ', 50 53 progressbar.Bar(), ' ', progressbar.ETA()], maxval=numpy.prod(results_dim)) 51 for i_exp,experiment in enumerate(p .iter_inner()):54 for i_exp,experiment in enumerate(ps.iter_inner()): 52 55 params = myFibers.params 53 56 params.update(experiment) # updates what changed in the dictionary … … 55 58 data = myFibers.run(params,verbose=False) 56 59 # calculating the index in the parameter space 57 index = p .parameter_space_index(experiment)60 index = ps.parameter_space_index(experiment) 58 61 # put the data at the right position in the results array 59 62 CRF[index] = data.mean_rate()# … … 65 68 results.close() 66 69 67 #print CRFsr68 70 #numpy.array(p.noise_std._values),numpy.array(p.snr._values), 69 71 pylab.plot(CRF.transpose()) #color = (sin(2*pi*noise_list)**2,cos(2*pi*noise_list)**2,1)) trunk/examples/single_neuron/fiber.param
r171 r315 7 7 "connectseed": 12345789, 8 8 }, 9 "snr": 1.0,10 "noise_std": 8.0,9 "snr": 2.0, 10 "noise_std": 2.0, 11 11 "weight": 1.0, 12 "N": 50,12 "N": 100, 13 13 } trunk/examples/single_neuron/simple_single_neuron.py
r227 r315 21 21 import sys, os, tempfile 22 22 # choosing the simulator 23 import pyNN. brianas sim23 import pyNN.nest2 as sim 24 24 # the link to read SpikeList files with NeuroTools 25 25 from NeuroTools.signals import load_spikelist … … 27 27 from NeuroTools.parameters import ParameterSet 28 28 29 def define(N=100):30 """31 defines the parameter set the first time32 33 """34 #these are fixed35 simulation_params = ParameterSet({'dt' : 0.1,# discretization step in simulations (ms)36 'simtime' : 40000*0.1, # float; (ms)37 'syn_delay' : 1.0, # float; (ms)38 'kernelseed' : 4321097, # array with one element per thread39 'connectseed' : 12345789 # seed for random generator(s) used during simulation40 })41 # these may change42 P = ParameterSet({'simulation': simulation_params,43 'N' : N,44 'noise_std' : 8.0, # (nA??) standard deviation of the internal noise45 'snr' : 1.0, # (nA??) size of the input signal46 'weight' : 1.0 },47 label="fiber_params")48 49 P.save('file://' + os.getcwd() + '/fiber.param')50 29 51 30 class FiberChannel(object): … … 55 34 """ 56 35 def __init__(self,N=100): 57 try : 58 url = "https://neuralensemble.org/svn/NeuroTools/trunk/examples/single_neuron/fiber.param" 59 self.params = ParameterSet(url) 60 print "Loaded parameters from SVN" 61 except : 62 #if you are off-line takes a local copy; use define to create file 63 self.params = ParameterSet('file://' + os.getcwd() + '/fiber.param') 64 print "Loaded parameters from local file" 36 # simulator specific 37 simulation_params = ParameterSet({'dt' : 0.1,# discretization step in simulations (ms) 38 'simtime' : 40000*0.1, # float; (ms) 39 'syn_delay' : 1.0, # float; (ms) 40 'kernelseed' : 4321097, # array with one element per thread 41 'connectseed' : 12345789 # seed for random generator(s) used during simulation 42 }) 43 # these may change 44 self.params = ParameterSet({'simulation': simulation_params, 45 'N' : N, 46 'noise_std' : 6.0, # (nA??) standard deviation of the internal noise 47 'snr' : 1.0, # (nA??) size of the input signal 48 'weight' : 1.0 }, 49 label="fiber_params") 65 50 66 # keep all parameters just change the size67 self.params.N = N68 51 print self.params.pretty() 69 52 … … 79 62 N = params.N 80 63 #dc_generator 81 current_source = sim.SpikeSourcePoisson({'rate':params.snr, 82 'start':params.simulation.simtime/4, 83 'duration':params.simulation.simtime/2.}) 84 85 86 # internal noise model TODO : does not exist in pyNN! 87 noise = sim.Population(N,sim.SpikeSourcePoisson,{'rate':params.snr, 88 'start':0., 89 'duration':params.simulation.simtime}) 90 64 current_source = sim.DCSource( amplitude= params.snr, 65 start=params.simulation.simtime/4, 66 stop=params.simulation.simtime/4*3) 67 68 # internal noise model (NEST specific) 69 noise = sim.Population(N,'noise_generator',{'mean':0.,'std':params.noise_std}) 91 70 # target population 92 71 output = sim.Population(N , sim.IF_cond_exp) … … 101 80 sim.Projection(noise, output, conn) 102 81 103 #for cell in output:104 #cell.inject(current_source)82 for cell in output: 83 cell.inject(current_source) 105 84 106 85 output.record() … … 118 97 Timer.reset() # start timer on construction 119 98 120 output_filename =os.path.join(tmpdir,'output.gdf')121 print output_filename99 output_filename = os.path.join(tmpdir,'output.gdf') 100 #print output_filename 122 101 output.printSpikes(output_filename)# 123 102 output_DATA = load_spikelist(output_filename,N, … … 138 117 return output_DATA 139 118 140 141 142 119 if __name__ == '__main__': 143 define(N=50) 144 myFibers = FiberChannel() 120 myFibers = FiberChannel(N=50) 145 121 spikes = myFibers.run(myFibers.params) 146 122 spikes.raster_plot() trunk/src/signals.py
r314 r315 262 262 def mean_rate(self, t_start=None, t_stop=None): 263 263 """ 264 Return the mean firing rate between t_start and t_stop, in Hz264 Returns the mean firing rate between t_start and t_stop, in Hz 265 265 266 266 Inputs: … … 302 302 303 303 See also 304 isi 304 isi, cv_kl 305 305 306 """ 306 307 isi = self.isi() … … 310 311 logging.debug("Warning, a CV can't be computed because there are not enough spikes") 311 312 return numpy.nan 313 314 315 316 def cv_kl(self, bins=100): 317 """ 318 Provides a measure for the coefficient of variation to describe the 319 regularity in spiking networks. It is based on the Kullback-Leibler 320 divergence and decribes the difference between a given 321 interspike-interval-distribution and an exponential one (representing 322 poissonian spike trains) with equal mean. 323 It yields 1 for poissonian spike trains and 0 for regular ones. 324 325 Reference: 326 http://incm.cnrs-mrs.fr/LaurentPerrinet/Publications/Voges08fens 327 328 329 Examples: 330 >> spklist.cv_kl() 331 0.98 332 333 See also: 334 cv_isi 335 336 """ 337 isi = self.isi() / 1000. 338 if newnum: 339 proba_isi, xaxis = numpy.histogram(isi, bins=bins, normed=True, new=True) 340 #xaxis = xaxis[:len(xaxis)-1] 341 else: 342 proba_isi, xaxis = numpy.histogram(isi, bins=bins, normed=True) 343 344 proba_isi /= numpy.sum(proba_isi) 345 bin_size = xaxis[1]-xaxis[0] 346 347 # differential entropy: http://en.wikipedia.org/wiki/Differential_entropy 348 KL = - numpy.sum(proba_isi * numpy.log(proba_isi+1e-16)) + numpy.log(bin_size) 349 KL -= -numpy.log(self.mean_rate()) + 1. 350 CVkl=numpy.exp(-KL) 351 352 return CVkl 353 312 354 313 355 def fano_factor_isi(self): … … 1124 1166 pylab.draw() 1125 1167 1126 1168 1127 1169 def cv_isi(self, float_only=False): 1128 1170 """ 1129 Return the list of all the cv coefficients for all the SpikeTrains objects1171 Return the list of all the CV coefficients for each SpikeTrains object 1130 1172 within the SpikeList. Return NaN when not enough spikes are present 1131 1173 … … 1141 1183 1142 1184 See also: 1143 cv_isi_hist, cv_local, cv_kl 1144 """ 1145 cvs_isi = [] 1185 cv_isi_hist, cv_local, cv_kl, SpikeTrain.cv_isi 1186 1187 """ 1188 cvs_isi = numpy.empty(len(self.id_list())) 1146 1189 for id in self.id_list(): 1147 cvs_isi .append(self.spiketrains[id].cv_isi())1148 cvs_isi = numpy.array(cvs_isi) 1190 cvs_isi[id] = self.spiketrains[id].cv_isi() 1191 1149 1192 if float_only: 1150 1193 cvs_isi = numpy.extract(numpy.logical_not(numpy.isnan(cvs_isi)),cvs_isi) … … 1152 1195 1153 1196 1197 def cv_kl(self, bins = 50): 1198 """ 1199 Return the list of all the CV coefficients for each SpikeTrains object 1200 within the SpikeList. 1201 1202 See also: 1203 cv_isi_hist, cv_local, cv_isi, SpikeTrain.cv_kl 1204 1205 """ 1206 cvs_kl = numpy.empty(len(self.id_list())) 1207 for id in self.id_list(): 1208 cvs_kl[id] = self.spiketrains[id].cv_kl(bins = bins) 1209 return cvs_kl 1154 1210 1155 1211 def cv_isi_hist(self, bins=50, display=False, kwargs={}): … … 1188 1244 1189 1245 1190 1191 def cv_kl(self):1192 """1193 Provides another (new?) measure for the coefficient of variation to describe the1194 regularity in spiking networks. It is based on the Kullback-Leibler1195 divergence and decribes the difference between a given1196 interspike-interval-distribution and an exponential one (representing1197 poissonian spike trains) with equal mean.1198 It yields approx. 1 for poissonian spike trains and 0 for regular ones.1199 1200 Attention: Contrary to the normal cv which is calculated for each1201 single neuron and then averaged, this function needs to synchronously1202 consider the spike times of all neurons.1203 1204 Examples:1205 >> spklist.cv_kl()1206 0.981207 1208 See also:1209 cv_isi, cv_isi_hist, cv_local1210 """1211 1212 isis=numpy.concatenate(self.isi())1213 mISI=numpy.mean(isis)1214 norm=len(isis)1215 min=numpy.min(isis)1216 max=numpy.max(isis)1217 bins = numpy.arange(min,max,1)1218 #print 'CV-KL norm=',norm,' #bins=',len(bins),' ev=',mISI1219 hist = numpy.histogram(isis,bins)1220 dist = hist[0].astype(float)/norm1221 testsum = numpy.sum(dist)1222 if testsum>1.1 or testsum<0.9 :1223 print 'CV-KL testsum=',testsum1224 H=0.1225 for j in range(len(dist)):1226 if dist[j] > 0. :1227 H = H - (dist[j]*numpy.log(dist[j]))1228 KL=0.1229 KL=-H+numpy.log(mISI)+1.1230 CVkl=numpy.exp(-KL)1231 1232 return CVkl1233 1246 1234 1247 trunk/test/test_signals.py
r313 r315 133 133 assert 0.9 < spk1.cv_isi() < 1.1 134 134 self.assertEqual(spk2.cv_isi(), 0) 135 136 137 def testCvKL(self): 138 poisson_param = 1./10 # 1 / firing_frequency 139 isi = numpy.random.exponential(poisson_param, 10000) 140 poisson_times = numpy.cumsum(isi)*1000. # To convert the spikes_time in ms 141 spk1 = signals.SpikeTrain(poisson_times) 142 assert 0.9 < spk1.cv_kl(bins = 1000) < 1.1 143 # does not depend on bin size 144 assert 0.9 < spk1.cv_kl(bins = 100) < 1.1 145 # does not depend on time 146 poisson_param = 1./4 147 isi = numpy.random.exponential(poisson_param, 10000) 148 poisson_times = numpy.cumsum(isi)*1000. # To convert the spikes_time in ms 149 spk1 = signals.SpikeTrain(poisson_times) 150 assert 0.9 < spk1.cv_kl() < 1.1 151 spk2 = signals.SpikeTrain(range(10), t_stop=10) 152 self.assertEqual(spk2.cv_isi(), 0) 135 153 136 154 def testHistogram(self): 137 155 poisson_param = 1./40 138 isi = numpy.random.exponential(poisson_param, 1000 )156 isi = numpy.random.exponential(poisson_param, 10000) 139 157 poisson_times = numpy.cumsum(isi)*1000. # To convert the spikes_time in ms 140 158 spk = signals.SpikeTrain(poisson_times) … … 191 209 for idx in xrange(nb_cells): 192 210 param = 1./frequencies[idx] 193 isi = numpy.random.exponential(param, 100 )211 isi = numpy.random.exponential(param, 1000) 194 212 pspikes = numpy.cumsum(isi)*1000. # To convert the spikes_time in ms 195 213 for spike in pspikes: … … 279 297 280 298 def testCVKL(self): 281 assert 0.8 < self.spk.cv_kl() < 1.2299 assert 0.8 < numpy.mean(self.spk.cv_kl()) < 1.2 282 300 283 301 def testCVLocal(self): … … 370 388 cc1 = self.spk.pairwise_cc_zero([(i,i) for i in xrange(5)], time_bin=0.1) 371 389 cc2 = self.spk.pairwise_cc_zero(5, time_bin=0.1) 390 #print cc1, cc2 391 372 392 assert (0 <= cc1 <= 1) and (cc1 > cc2) 373 393

