Show
Ignore:
Timestamp:
05/07/08 18:56:23 (4 years ago)
Author:
LaurentPerrinet
Message:

Code Jam #2 : retina/benchmark_noise and benchmark_linear should now work

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/examples/retina/benchmark_linear.py

    r165 r166  
    1414""" 
    1515 
    16 import os, datetime, numpy, pylab 
    17 import retina as model 
     16import os, datetime, numpy, pylab, shelve 
     17import progressbar # see http://projects.scipy.org/pipermail/scipy-dev/2008-January/008200.html 
     18from NeuroTools.parameters import * 
     19 
     20N, N_exp = 100, 15 
     21p = ParameterSet({}) 
     22 
     23results = shelve.open('results/benchmark_linear') 
     24try: 
     25    boing 
     26    temporal_ON = results['temporal_ON'] 
     27    temporal_OFF = results['temporal_OFF'] 
     28    lower_edges = results['lower_edges'] 
     29 
     30except: 
     31    import retina as model 
     32 
     33    retina = model.Retina(N) 
     34    snr  = retina.params['snr']* numpy.linspace(0.1,2.0,N_exp) 
     35    p.noise_std = ParameterRange(list(snr)) 
     36    # calculates the dimension of the parameter space 
     37    results_dim, results_label = p.parameter_space_dimension_labels() 
     38 
     39    # creates results array with size of parameter space dimension 
     40    t_smooth = 10. #numpy.ceil(retina.params.simtime/N_smooth) 
     41    data = retina.run(retina.params,verbose=False) 
     42    lower_edges = data['out_ON_DATA'].time_axis(t_smooth) 
     43    N_smooth = len(lower_edges) 
     44    temporal_ON, temporal_OFF = [],[]#numpy.zeros((N_smooth,N_exp)),numpy.empty((N_smooth,N_exp)) 
     45 
     46    pbar=progressbar.ProgressBar(widgets=["calculating", " ", progressbar.Percentage(), ' ', 
     47            progressbar.Bar(), ' ', progressbar.ETA()], maxval=N_exp) 
     48    for i_exp,experiment in enumerate(p.iter_inner()): 
     49        params = retina.params 
     50        params.update(experiment) # updates what changed in the dictionary 
     51        # simulate the experiment and get its data 
     52        data = retina.run(params,verbose=False) 
     53        # calculating the index in the parameter space 
     54        index = p.parameter_space_index(experiment) 
     55        # put the data at the right position in the results array 
     56        temporal_ON.append(sum(data['out_ON_DATA'].firing_rate(t_smooth))/N)# 
     57        temporal_OFF.append(sum(data['out_OFF_DATA'].firing_rate(t_smooth))/N)# 
     58        pbar.update(i_exp) 
     59 
     60 
     61    results['lower_edges'] = lower_edges 
     62    results['temporal_ON'] = temporal_ON 
     63    results['temporal_OFF'] = temporal_OFF 
     64 
     65    pbar.finish() 
     66 
     67results.close() 
     68 
     69 
     70 
    1871 
    1972############################################################################### 
    20 def show(benchmark): 
    21     from NeuroTools.plotting import pylab_params 
    2273 
    23     #from NeuroTools.analysis import spikelist2spikematrix 
     74from NeuroTools.plotting import pylab_params 
    2475 
    25     """ Figure 1 
     76#from NeuroTools.analysis import spikelist2spikematrix 
    2677 
    27     Prints to a figure the mean firing rate for the output (ON and OFF) as a function of the different parameter values. It's similar to a CRF function. 
     78""" Figure 1 
    2879 
    29     """ 
    30     pylab.close('all') 
    31     pylab.rcParams.update(pylab_params(fig_width_pt = 497.9/2., ratio = 1.)) 
     80Prints to a figure the mean firing rate for the output (ON and OFF) as a function of the different parameter values. It's similar to a CRF function. 
    3281 
    33     pylab.figure(num = 1, dpi=150, facecolor='w', edgecolor='k') 
    34     Lmargin, Rmargin, dmargin, umargin = 0.2, 0.05, 0.15,  0.05 
    35     pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] 
     82""" 
     83pylab.close('all') 
     84pylab.rcParams.update(pylab_params(fig_width_pt = 497.9/2., ratio = 1.)) 
    3685 
    37     params = benchmark.get('params') 
    38     experiment_list = benchmark.list_experiments() 
     86pylab.figure(num = 1, dpi=150, facecolor='w', edgecolor='k') 
     87Lmargin, Rmargin, dmargin, umargin = 0.2, 0.05, 0.15,  0.05 
     88pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] 
    3989 
    40     N, N_ret, simtime = params['N'], params['N_ret'], params['simtime'] 
    41     t_smooth = 100. #  smooting time (ms) 
     90pylab.plot(lower_edges,  # TODO add a half bin 
     91                    temporal_ON)#, 
     92                       # label= 'snr=' + str(benchmark.get('experiments')[experiment]['snr'])) 
     93#pylab.subplot(121) 
     94#pylab.title('time course')# ON 
     95#pylab.xticks( numpy.linspace(0, simtime, 5) ) 
     96pylab.ylabel('Firing frequency (Hz)') 
     97pylab.xlabel('time (ms)') 
     98pylab.axis('tight') 
    4299 
    43     for experiment in experiment_list: 
    44         try: 
    45             ok = benchmark.get('firing_rate',experiment) #== ['temporal_ON', 'map_spatial_OFF', 'temporal_OFF', 'map_spatial_ON'] 
    46         except: 
    47             print "Loading data for experiment " , experiment 
    48             out_ON = benchmark.get('out',experiment)['out_ON_DATA'] 
    49             # storing 
    50             benchmark.put('firing_rate',{ 
    51                     'temporal_ON' : sum(out_ON.firing_rate(t_smooth))/N, 
    52                     #'temporal_OFF' : out_OFF.firing_rate(t_smooth), 
    53                     'lower_edges' : out_ON.time_axis(t_smooth), 
    54                     'firing_rate_ok' : True # a flag to do it once 
    55                       }, experiment ) 
     100#pylab.legend() 
    56101 
    57         run = benchmark.get('firing_rate',experiment) 
    58         pylab.plot(run['lower_edges'],  # TODO add a half bin 
    59                         run['temporal_ON'], 
    60                             label= 'snr=' + str(benchmark.get('experiments')[experiment]['snr'])) 
    61     #pylab.subplot(121) 
    62     #pylab.title('time course')# ON 
    63     pylab.xticks( numpy.linspace(0, simtime, 5) ) 
    64     pylab.ylabel('Firing frequency (Hz)') 
    65     pylab.xlabel('time (ms)') 
    66     pylab.axis('tight') 
     102#pylab.subplot(122) 
     103#pylab.title('time course OFF') 
     104#pylab.xticks( numpy.linspace(0, simtime, 5) ) 
     105#pylab.ylabel('firing frequency (Hz)') 
     106#pylab.axis('tight') 
     107#pylab.xlabel('time (ms)') 
     108#pylab.legend() 
    67109 
    68     #pylab.legend() 
    69  
    70     #pylab.subplot(122) 
    71     #pylab.title('time course OFF') 
    72     #pylab.xticks( numpy.linspace(0, simtime, 5) ) 
    73     #pylab.ylabel('firing frequency (Hz)') 
    74     #pylab.axis('tight') 
    75     #pylab.xlabel('time (ms)') 
    76     #pylab.legend() 
    77  
    78     pylab.savefig(benchmark.directory + '/benchmark_linear.pdf') #, dpi=300) # 
    79  
    80  
    81 if __name__ == '__main__': 
    82  
    83     tag = 'test' 
    84  
    85     # first, set the url 
    86     filename =  'results/' + tag + '_benchmark_linear' 
    87  
    88     ret = model.Retina(21) 
    89     #ret.params['simtime'] = 4000*0.1 
    90     ret.params['amplitude'] = numpy.ones(ret.params['N']) 
    91     N_exp = 15 
    92     snr  = ret.params['snr']* numpy.linspace(0.1,2.0,N_exp) 
    93     run = benchmark.get_experiment_dict({'snr':snr}) 
    94     print snr#,ret.params 
    95     B = benchmark.Benchmark(filename,ret,run) 
    96  
    97     B.run_simulations() 
    98     show(B) 
    99     #B.reset_all() 
     110pylab.savefig('results/benchmark_linear.pdf') #, dpi=300) #