Changeset 101

Show
Ignore:
Timestamp:
01/15/08 17:44:22 (1 year ago)
Author:
LaurentPerrinet
Message:

Finished examples of use of NeuroTools: retina / renamd SpikeTrain.mean_firing_rate to SpikeTrain.mean_rate for consistency with SpikeList.mean_rate / bugfix in spikes.py for the definition of a range in SpikeTrain.mean_rate

Files:

Legend:

Unmodified
Added
Removed
Modified
Copied
Moved
  • trunk/benchmark.py

    r94 r101  
    203203        return unfinished_experiments 
    204204 
    205     def reset_all(self): 
     205    def reset_all(self, type='full'): 
    206206        """ Removes all experiments files 
    207207 
    208         """ 
    209  
    210         for experiment in self.experiments: 
    211             os.remove(self.experiment_filename(experiment)) 
     208        type gives the type of cleaning to do 
     209        may be 'full', 'exp' or 'corrupted' (non finished) 
     210        """ 
     211        if type == 'exp': 
     212            os.remove(self.experiment_filename()) 
     213            for experiment in self.experiments: 
     214                os.remove(self.experiment_filename(experiment)) 
     215        elif type == 'full': 
     216            for experiment in self.experiments: 
     217                os.remove(self.experiment_filename(experiment)) 
     218            os.remove(self.experiment_filename()) 
     219            #os.remove(self.filename) 
    212220 
    213221 
     
    230238            params = self.get('params') 
    231239            print "Running Experiment " + experiment + " ..." 
     240            print 'Changing :', self.get('experiments')[experiment] 
    232241            # updates params with the changed parameers from experiment 
    233242            params.update(self.get('experiments')[experiment])  # change some parameters 
  • trunk/examples/retina/benchmark_linear.py

    r97 r101  
    1717import retina as model 
    1818import NeuroTools.benchmark as benchmark 
    19  
    20  
    2119 
    2220 
     
    4341 
    4442    N, N_ret, simtime = params['N'], params['N_ret'], params['simtime'] 
    45     N_smooth = 100 # how many time bins 
     43    t_smooth = 100. #  smooting time (ms) 
    4644 
    4745    for experiment in experiment_list: 
     
    5048        except: 
    5149            print "Loading data for experiment " , experiment 
    52             out_ON_DATA = benchmark.get('out',experiment)['out_ON_DATA'] 
    53             # ON 
    54             temporal_ON , lower_edges = numpy.histogram(out_ON_DATA.as_list_id_list_time()[1], 
    55                                         bins=N_smooth, range=(out_ON_DATA.t_start,out_ON_DATA.t_stop)) 
    56             temporal_ON /= N**2 
    57             # OFF : noit very useful here 
    58             #out_OFF_DATA = benchmark.get('out',experiment)['out_OFF_DATA'] 
    59 ##            temporal_OFF , lower_edges = numpy.histogram(out_OFF_DATA.as_list_id_list_time()[1], 
    60 ##                                        bins=N_smooth, range=(out_ON_DATA.t_start,out_ON_DATA.t_stop)) 
    61 ##            temporal_OFF /= N**2 
     50            out_ON = benchmark.get('out',experiment)['out_ON_DATA'] 
     51 
    6252            # storing 
    6353            benchmark.put('firing_rate',{ 
    64                     'temporal_ON' : temporal_ON, 
    65 ##                    'temporal_OFF' : temporal_OFF
    66                     'lower_edges' : lower_edges
     54                    'temporal_ON' : sum(out_ON.firing_rate(t_smooth))/N, 
     55                    #'temporal_OFF' : out_OFF.firing_rate(t_smooth)
     56                    'lower_edges' : out_ON.time_axis(t_smooth)
    6757                    'firing_rate_ok' : True # a flag to do it once 
    6858                      }, experiment ) 
     
    8979    #pylab.legend() 
    9080 
    91     pylab.savefig(benchmark.filename + '/benchmark_linear.png') #, dpi=300) # 
     81    pylab.savefig(benchmark.filename + '/benchmark_linear.pdf') #, dpi=300) # 
    9282 
    9383 
    9484if __name__ == '__main__': 
    9585 
    96     # not working tag= '07-02-16' 
    97     # tag= '07-02-14' 
    98     # tag= '07-02-23' 
    99     tag= '07-02-27' 
    100     tag = '07-03-08' 
    101     tag = '07-05-09' 
    10286    tag = 'test' 
    10387 
     
    10690 
    10791    ret = model.Retina(21) 
    108     ret.params['simtime'] = 4000*0.1 
    109     ret.params['amplitude'] = numpy.ones((ret.params['N'],ret.params['N'] )) 
    110  
    111     run = benchmark.get_experiment_dict({'snr' : 10.**(numpy.linspace(-.50,.75,5))}) 
    112  
     92    #ret.params['simtime'] = 4000*0.1 
     93    ret.params['amplitude'] = numpy.ones(ret.params['N']) 
     94    N_exp = 15 
     95    snr  = ret.params['snr']* numpy.linspace(0.1,2.0,N_exp) 
     96    run = benchmark.get_experiment_dict({'snr':snr}) 
     97    print snr#,ret.params 
    11398    B = benchmark.Benchmark(filename,ret,run) 
    11499 
    115100    B.run_simulations() 
    116101    show(B) 
    117  
     102    #B.reset_all() 
  • trunk/examples/retina/benchmark_noise.py

    r97 r101  
    3535    noise_std, CRF = numpy.zeros(len(experiments)), numpy.zeros(len(experiments)) 
    3636    for count, experiment in enumerate(experiments): 
    37         out_ON_DATA = benchmark.get('out',experiment)['out_ON_DATA']#.as_list_id_list_time() 
     37        out_ON_DATA = benchmark.get('out',experiment)['out_ON_DATA'] 
    3838        noise_std[count] = benchmark.get('experiments')[experiment]['noise_std'] 
    39         CRF[count] = out_ON_DATA.mean_rate()#[0]) / N / N / simtime * 1000.0 
     39        CRF[count] = out_ON_DATA.mean_rate() 
    4040 
    4141 
     
    4848    #pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] 
    4949 
    50     #pylab.subplot(111) 
    5150    idx_ = numpy.argsort(noise_std) # extract lines 
    5251    pylab.plot(noise_std[idx_],CRF[idx_],'go-', label='line 1', linewidth=2) 
     
    6564    # create a Retina Model object that contains a method to init, to run, ... 
    6665    ret = model.Retina(10) 
    67     ret.params['simtime'] = 4000*0.1 
    6866 
    69     tag = '07-02-21' 
    70     tag = '07-03-08' # worked fine for D25 
    71     tag = '07-04-12' 
    72     tag = '07-04-08' # new with multiple seeds for CNS07 
    73     tag = '07-05-14' # no 
    74     tag = '07-05-16' # going OO 
    75     tag = 'test' # going OO 
     67    tag = 'test' 
    7668 
    7769    name = 'results/' + tag + '_benchmark_noise' 
    7870 
    7971    # create the list of experiments as all possibilities accross parameter vectors 
    80     run = benchmark.get_experiment_dict({'noise_std':numpy.linspace(.10,10.0,20), 'snr' : [ 0.0 ]}) 
     72    span, N_exp = 5, 2*7+1 
     73    #range = 10.**(numpy.linspace(-span,span,N_exp)) 
     74    range = numpy.linspace(1/span,span,N_exp) 
     75    run = benchmark.get_experiment_dict({'noise_std': ret.params['noise_std']* range, 'snr' : [ 0.0 ]}) 
    8176 
    8277    # create benchmark (or open it if it exists) 
     
    8681 
    8782    show(B) 
    88  
     83    #B.reset_all() # erase experimental data 
  • trunk/examples/retina/benchmark_retina.py

    r97 r101  
    2727def show(benchmark): 
    2828    from NeuroTools.plotting import pylab_params 
     29    from numpy import zeros, where, arange 
    2930 
    3031 
     
    3233 
    3334    N, N_ret, simtime, dt = params['N'], params['N_ret'], params['simtime'], params['dt'] 
    34     N_smooth = 100 # how many time bins 
    35     # TODO : print N_smooth 
    36     space , t = numpy.linspace(-N/2, N/2, N), numpy.arange(0,simtime,dt * N_smooth) 
    37     amplitude = params['amplitude'] 
    38  
    39     x = params['position'][0][out_DATA[0]] 
    40     y = params['position'][1][out_DATA[0]] 
     35    t_smooth = 100. #  smooting time (ms) 
     36    t = arange(0.0, simtime, t_smooth) 
     37    N_smooth = len(t) 
     38 
     39    x = params['position'][0] 
     40    y = params['position'][1] 
    4141    r2 = x**2 + y**2 
    42  
     42    r = numpy.sqrt(r2) 
     43    id_center = where( r2 < N_ret**2) 
     44    #print r2,id_center 
    4345    #kernel = amplitude * (amplitude > 0) 
    4446    #kernel /=  numpy.sum(numpy.sum(kernel)) 
     
    5254            out_ON = benchmark.get('out',experiment)['out_ON_DATA'] 
    5355            out_OFF = benchmark.get('out',experiment)['out_OFF_DATA'] 
    54             out_ON_DATA = out_ON.firing_rate(t_smooth) 
    55             out_OFF_DATA = out_OFF.firing_rate(t_smooth) 
    56  
    57             ##temporal_ON= numpy.sum(numpy.sum(out_ON_spikematrix, axis=0), axis=0) 
    58             temporal_ON , lower_edges = numpy.histogram(out_ON_DATA[1], 
    59                                 bins=N_smooth, range=(out_ON.t_start,out_ON.t_stop)) 
    60             ##temporal_OFF= numpy.sum(numpy.sum(out_OFF_spikematrix, axis=0), axis=0) 
    61             temporal_OFF , lower_edges = numpy.histogram(out_OFF_DATA[1], 
    62                                 bins=N_smooth, range=(out_ON.t_start,out_ON.t_stop)) 
    63  
    64  
    65             # TODO the range argument may serve to select a time window 
    66             map_spatial_ON , dump = numpy.histogram(out_ON_DATA[0],bins=N**2, range=(0,N**2)) 
    67             map_spatial_OFF, dump = numpy.histogram(out_OFF_DATA[0],bins=N**2, range=(0,N**2)) 
    6856 
    6957            benchmark.put('firing_rate',{ 
    70                     'temporal_ON' : temporal_ON, 
    71                     'temporal_OFF' : temporal_OFF
    72                     'lower_edges' : lower_edges
    73                     'map_spatial_ON' : numpy.reshape(map_spatial_ON,(N,N)), 
    74                     'map_spatial_OFF' : numpy.reshape(map_spatial_OFF,(N,N)
    75                       }, experiment ) 
     58                'temporal_ON' : sum(out_ON.firing_rate(t_smooth))/N, 
     59                'temporal_OFF' : sum(out_OFF.firing_rate(t_smooth))/N
     60                'lower_edges' : out_OFF.time_axis(t_smooth)
     61                'map_spatial_ON' : out_ON.mean_rates(t_start=simtime/4.,t_stop=3*simtime/4.), 
     62                'map_spatial_OFF' : out_OFF.mean_rates(t_start=simtime/4.,t_stop=3*simtime/4.
     63                  }, experiment ) 
    7664 
    7765    pylab.close('all') 
     
    9179        # mean activity accross kernelseeds as a function of SNR 
    9280        snr_list = benchmark.list_param('snr') 
    93  
    9481        for snr in snr_list: 
    95             temporal_ON, temporal_OFF, map_spatial_ON, map_spatial_OFF = 0, 0, 0, 0 
     82            temporal_ON, temporal_OFF, map_spatial_ON, map_spatial_OFF =zeros(N_smooth), zeros(N_smooth), zeros(N), zeros(N) 
    9683            seeds_list = benchmark.list_where('snr',snr) 
     84            n_seeds = len(seeds_list) 
    9785            for experiment in seeds_list: # TODO sum on dictionaries? 
    9886                run = benchmark.get('firing_rate',experiment) 
    99                 temporal_ON += run['temporal_ON'] / len(seeds_list) 
    100                 temporal_OFF += run['temporal_OFF']/ len(seeds_list) 
    101                 map_spatial_ON += run['map_spatial_ON']/ len(seeds_list) 
    102                 map_spatial_OFF += run['map_spatial_OFF']/ len(seeds_list) 
    103  
    104             t = run['lower_edges'] 
     87                temporal_ON += run['temporal_ON'] 
     88                temporal_OFF += run['temporal_OFF'] 
     89                map_spatial_ON += run['map_spatial_ON'] 
     90                map_spatial_OFF += run['map_spatial_OFF'] 
     91 
    10592            pylab.subplot(221) 
    106             pylab.plot(t,temporal_ON
     93            pylab.plot(t,temporal_ON/ n_seeds
    10794            pylab.subplot(223) 
    108             pylab.plot(t,temporal_OFF
     95            pylab.plot(t,temporal_OFF/ n_seeds
    10996            pylab.subplot(222) 
    110             pylab.plot(space ,  numpy.sum(map_spatial_ON[N/2-N_ret/2:N/2+N_ret/2,:], axis=0) * N / N_ret
     97            pylab.plot(r , map_spatial_ON ,'bo'
    11198            pylab.subplot(224) 
    112             pylab.plot(space ,  numpy.sum(map_spatial_OFF[N/2-N_ret/2:N/2+N_ret/2,:], axis=0) * N / N_ret
     99            pylab.plot(r ,  map_spatial_OFF , 'ro'
    113100 
    114101        pylab.subplot(221) 
     
    131118        pylab.subplot(224) 
    132119        #pylab.title('spatial distribution OFF',fontsize ='small') 
    133         pylab.xlabel('space') 
     120        pylab.xlabel('distance from center') 
    134121 
    135122        pylab.savefig(name) # 
     
    176163        pylab.figure(num = 3, dpi=dpi, facecolor='w', edgecolor='k') 
    177164 
    178         Lmargin, Rmargin, dmargin, umargin = 0.05, 0.15, 0.05,  0.05 
    179         pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] 
    180  
    181         pylab.matshow(benchmark.get('params')['amplitude']
    182  
    183         pylab.axis('off') 
    184         pylab.colorbar(shrink  = 0.7) 
     165        #Lmargin, Rmargin, dmargin, umargin = 0.05, 0.15, 0.05,  0.05 
     166        #pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] 
     167 
     168        pylab.scatter(x,y,c=benchmark.get('params')['amplitude'],faceted=False
     169 
     170        #pylab.axis('off') 
     171        #pylab.colorbar(shrink  = 0.7) 
    185172        pylab.savefig(name) 
    186173 
     
    201188    # create a Retina Model object that contains a method to init, to run, ... 
    202189    ret = model.Retina(10) 
    203     ret.params['simtime'] = 4000*0.1 
    204     #retina_default(url) 
    205     #params=retina_debug() 
     190    #ret.params['simtime'] = 4000*0.1 
    206191    ret.params['description']=  'Testing Benchmark Retina with ' + ret.params['description'], 
    207192 
    208193    # create the list of experiments as all possibilities accross parameter vectors 
    209194    N_exp_snr, N_exp_seeds = 5 , 4 
    210     snr  = ret.params['snr'] * 10.**(numpy.linspace(-.50,.75,N_exp_snr)
     195    snr  = ret.params['snr'] * numpy.linspace(0.1,2.0,N_exp_snr
    211196    seeds =  [43210987 + k for k in range(N_exp_seeds)] 
    212197    run = benchmark.get_experiment_dict({'snr':snr,'kernelseed':seeds}) 
  • trunk/examples/retina/make_all.py

    r97 r101  
    55=========== 
    66 
    7 Launches all benchmarks in B0
     7Launches all benchmarks in B1
    88 
    99Laurent Perrinet, INCM, CNRS 
     
    1818 
    1919# tag list 
    20 tag = '07-02-21' 
    21 tag = '07-03-08' # worked fine for D25 
    22 tag = '07-04-12' 
    23 tag = '07-04-08' # new with multiple seeds for CNS07 
    24 tag = '07-05-14' # no 
    25 tag= '07-04-08' # this worked nice and will be a reference for D25 
    26 tag = '07-05-17' # going OO 
    27 tag = '07-07-12' # testing the new shelve interface 
    2820tag = '08-01-04' # demo for Debrecen 
    2921 
     
    3123from NeuroTools.benchmark import * 
    3224from numpy import linspace, ones 
     25 
     26 
    3327### testing the noise response 
    3428import benchmark_noise 
     
    4236B.run_simulations() 
    4337benchmark_noise.show(B) 
    44 raise("to finish") 
     38 
    4539### testing the linear response 
    4640import benchmark_linear 
  • trunk/examples/retina/retina.py

    r84 r101  
    104104            'simtime'    : 40000*0.1,      # float; (ms) 
    105105            'syn_delay'  : 1.0,         # float; (ms) 
    106             'noise_std' : 4.0,       # (nA) standard deviation of the internal noise 
    107             'snr' : 3.0, # (nA) maximum signal 
     106            'noise_std' : 5.0,       # (nA) standard deviation of the internal noise 
     107            'snr' : 2.0, # (nA) maximum signal 
    108108            'weight'     : 1.0, # 
    109109            #'threads' : 2, 'kernelseeds' : [43210987, 394780234],      # array with one element per thread 
     
    116116 
    117117        # retinal neurons parameters 
    118         self.params['parameters_gc'] = {'Theta':-57.0, 'Vreset': -70.0,'Vinit': -63.0, 'TauR': 0.5, 'gL':28.95, 
     118        self.params['parameters_gc'] = {'Theta':-57.0, 'Vreset': -70.0,'Vinit': -70.0, 
     119        'TauR': 0.5, 'gL':28.95, 
    119120        'C':289.5, 'V_reversal_E': 0.0, 'V_reversal_I': -75.0, 'TauSyn_E':1.5, 
    120121        'TauSyn_I': 10.0, 'V_reversal_sfa': -70.0, 'q_sfa': 0., #14.48, 
     
    152153        sim.Timer.start() # start timer on construction 
    153154        sim.setup(timestep=params['dt'],max_delay=params['syn_delay']) 
    154         #sim.pynest.setDict([0],{'threads' : params['threads']}) 
    155155        sim.setRNGseeds([params['kernelseed']]) 
    156156 
     
    159159        phr_ON = sim.Population(N,'dc_generator') 
    160160        phr_OFF  = sim.Population(N,'dc_generator') 
    161         for phr in [phr_ON, phr_OFF]: 
     161        for factor, phr in [(-params['snr'],phr_OFF),(params['snr'],phr_ON)]: 
     162            phr.tset('amplitude', params['amplitude'] * factor ) 
    162163            phr.set({ 'start' : params['simtime']/4, 'stop' : params['simtime']/4*3}) 
    163164 
     
    166167        noise_OFF = sim.Population(N,'noise_generator',{'mean':0.,'std':params['noise_std']}) 
    167168 
    168         # TODO LUP : here introduce a tranformation of the image (on a grid) by a DOG and then on the specific location of the neuron 
    169  
    170  
    171         phr_ON.tset('amplitude', params['amplitude'] *  params['snr']) 
    172         phr_OFF.tset('amplitude', - params['amplitude']  * params['snr']) 
    173169 
    174170        # target ON and OFF populations (what about a tridimensional Population?) 
     
    191187        ##    >>> p1.randomInit(vinit_distr) 
    192188 
    193  
    194         #  Connecting the network 
     189        # connection matrix= 
     190##        connections = ([retina_proj_ON,phr_ON, out_ON]) 
     191##        #  Connecting the network 
     192##        for name_proj,in_pop, out_pop in connections: 
     193 
    195194        retina_proj_ON = sim.Projection(phr_ON, out_ON, 'oneToOne') 
    196195        retina_proj_ON.setWeights(params['weight'])# 
  • trunk/spikes.py

    r99 r101  
    2424    >>> s1.isi() 
    2525    array([ 0.1,  0.1,  0.3]) 
    26     >>> s1.mean_firing_rate() 
     26    >>> s1.mean_rate() 
    2727    8.0 
    2828    >>> s1.cv_isi() 
     
    145145 
    146146    # Returns the mean firing rate of the SpikeTrain 
    147     def mean_firing_rate(self, t_start=None, t_stop=None): 
     147    def mean_rate(self, t_start=None, t_stop=None): 
    148148        """ Mean firing rate between t_start and t_stop in Hz 
    149149 
     
    151151        """ 
    152152        if (t_start==None) & (t_start==None): 
     153            t_start=self.t_start 
     154            t_stop=self.t_stop 
    153155            idx = self.spike_times 
    154156        else: 
    155157            if t_start==None: t_start=self.t_start 
    156             if t_stop==None: t_start=self.t_stop 
     158            if t_stop==None: t_stop=self.t_stop 
    157159            idx = numpy.where((self.spike_times >= t_start) & (self.spike_times <= t_stop))[0] 
    158160        return 1000.*len(idx)/(t_stop-t_start) 
     
    219221        are calculated from `self.t_start` and `self.t_stop`. 
    220222        If `normalized` is True, the bin values are scaled so as to represent 
    221         firing rates in spikes/second, otherwise
     223        firing rates in spikes/second, otherwise it's the number of spikes per bin
    222224        """ 
    223225        nbins = self.time_axis(time_bin) 
     
    481483 
    482484 
    483         see mean_firing_rate 
     485        see SpikeTrain.mean_rate 
    484486        """ 
    485487        rates = [] 
    486488        for id in self.id_list: 
    487             rates.append(self.spiketrains[id].mean_firing_rate(t_start, t_stop)) 
     489            rates.append(self.spiketrains[id].mean_rate(t_start, t_stop)) 
    488490 
    489491        return rates 
     
    492494    # If display is True, then it plots the distribution of the rates 
    493495    def rate_distribution(self, nbins=25, normalize=True, display=False): 
    494         rates = numpy.zeros(self.N, float) 
    495         for idx,id in enumerate(self.id_list): 
    496             rates[idx] = self.spiketrains[id].mean_firing_rate() 
     496        #rates = numpy.zeros(self.N, float) 
     497        #for idx,id in enumerate(self.id_list): 
     498        #    rates[idx] = self.spiketrains[id].mean_firing_rate() 
     499        rates = self.mean_rates() 
    497500        if not display: 
    498501            return rates 
     
    560563        if isinstance(dims, tuple) or isinstance(dims, list): 
    561564            activity_map = numpy.zeros(dims,float) 
     565            rates = self.mean_rates() 
    562566            for id in self.id_list: 
    563567                position = self.id2position(id, dims) 
    564                 activity_map[position] = self.spiketrains[id].mean_firing_rate() 
     568                activity_map[position] = rates[id] 
    565569            if not display: 
    566570                return activity_map 
     
    575579            if not len(self.id_list) == len(dims[0]): 
    576580                raise Exception("Error, the number of positions does not match the number of cells in the SpikeList") 
    577             rates = [] 
    578             for id in self.id_list: 
    579                 rates.append(self.spiketrains[id].mean_firing_rate()) 
     581            rates = self.mean_rates() 
     582            #for id in self.id_list: 
     583            #    rates.append(self.spiketrains[id].mean_firing_rate()) 
    580584            if not display: 
    581585                return rates