| 1 | #!/usr/bin/env python |
|---|
| 2 | # -*- coding: utf8 -*- |
|---|
| 3 | """ |
|---|
| 4 | benchmark_retina.py |
|---|
| 5 | =================== |
|---|
| 6 | |
|---|
| 7 | This should feed the "Hyper-Column" for benchmark one. |
|---|
| 8 | (see https://facets.kip.uni-heidelberg.de/private/wiki/index.php/V1_hypercolumn#Benchmark_one ) |
|---|
| 9 | |
|---|
| 10 | The input is a discrete dirac simulating the response in Magnocellular Pathway |
|---|
| 11 | M to primary visual cortex' layer 4C$\alpha$ then to 4B as defined in Benchmark |
|---|
| 12 | one. The paramters tested is SNR sensitivity / spatial stability of V1 response. |
|---|
| 13 | |
|---|
| 14 | |
|---|
| 15 | Illustrates how one many use parameters to explore different sets of parameters |
|---|
| 16 | and compute their effect. See benchmark_noise and benchmark_linear for simpler |
|---|
| 17 | examples. |
|---|
| 18 | |
|---|
| 19 | Laurent Perrinet, INCM, CNRS |
|---|
| 20 | |
|---|
| 21 | $ Id $ |
|---|
| 22 | |
|---|
| 23 | """ |
|---|
| 24 | |
|---|
| 25 | import os, sys, numpy, pylab, shelve, progressbar |
|---|
| 26 | |
|---|
| 27 | N, N_snr, N_seeds = 1000, 5, 10 |
|---|
| 28 | from NeuroTools.parameters import * |
|---|
| 29 | p = ParameterSpace({ |
|---|
| 30 | 'snr' : ParameterRange(list(numpy.linspace(0.5,4.0,N_snr))), |
|---|
| 31 | 'kernelseed' : ParameterRange(list([12345 + k for k in range(N_seeds)]))}) |
|---|
| 32 | |
|---|
| 33 | name = sys.argv[0].split('.')[0] # name of the current script withpout the '.py' part |
|---|
| 34 | |
|---|
| 35 | try: |
|---|
| 36 | ############## MAKING THE SIMULATIONS ############### |
|---|
| 37 | results = shelve.open('results/mat-' + name) |
|---|
| 38 | try: |
|---|
| 39 | DATA = results['DATA'] |
|---|
| 40 | params = results['params'] |
|---|
| 41 | except: |
|---|
| 42 | from retina import * |
|---|
| 43 | retina = Retina(N) |
|---|
| 44 | # calculates the dimension of the parameter space |
|---|
| 45 | results_dim, results_label = p.parameter_space_dimension_labels() |
|---|
| 46 | |
|---|
| 47 | DATA = [] |
|---|
| 48 | pbar=progressbar.ProgressBar(widgets=[name, " ", progressbar.Percentage(), ' ', |
|---|
| 49 | progressbar.Bar(), ' ', progressbar.ETA()], maxval=N_snr*N_seeds) |
|---|
| 50 | for i_exp, experiment in enumerate(p.iter_inner()): |
|---|
| 51 | params = retina.params |
|---|
| 52 | params.update(experiment) # updates what changed in the dictionary |
|---|
| 53 | # simulate the experiment and get its data |
|---|
| 54 | data = retina.run(params)#,verbose=False) |
|---|
| 55 | # store it |
|---|
| 56 | DATA.append(data)# |
|---|
| 57 | pbar.update(i_exp) |
|---|
| 58 | |
|---|
| 59 | results['DATA'] = DATA |
|---|
| 60 | results['params'] = retina.params |
|---|
| 61 | |
|---|
| 62 | pbar.finish() |
|---|
| 63 | results.close() |
|---|
| 64 | results = shelve.open('results/mat-pre-' + name) |
|---|
| 65 | ############## PRE-PROCESSING ########################### |
|---|
| 66 | #boing # uncomment to force recomputing the pre-processing stage |
|---|
| 67 | lower_edges = results['lower_edges'] |
|---|
| 68 | temporal_ON = results['temporal_ON'] |
|---|
| 69 | map_spatial_OFF = results['map_spatial_OFF'] |
|---|
| 70 | temporal_OFF = results['temporal_OFF'] |
|---|
| 71 | map_spatial_ON = results['map_spatial_ON'] |
|---|
| 72 | lower_edges = results['lower_edges'] |
|---|
| 73 | results.close() |
|---|
| 74 | |
|---|
| 75 | except: |
|---|
| 76 | def temporal_mean(spike_list): |
|---|
| 77 | return numpy.sum(spike_list.firing_rate(t_smooth),axis=0) |
|---|
| 78 | |
|---|
| 79 | t_smooth = 100. # ms. integration time to show fiber activity |
|---|
| 80 | lower_edges = DATA[0]['out_ON_DATA'].time_axis(t_smooth) |
|---|
| 81 | N_smooth = len(lower_edges)-1 |
|---|
| 82 | |
|---|
| 83 | #N_snr = len(p.snr) |
|---|
| 84 | temporal_ON, temporal_OFF = numpy.zeros((N_smooth,N_snr)), numpy.zeros((N_smooth,N_snr)) |
|---|
| 85 | map_spatial_ON, map_spatial_OFF = numpy.zeros((N,N_snr)), numpy.zeros((N,N_snr)) |
|---|
| 86 | |
|---|
| 87 | # |
|---|
| 88 | N_ret, simtime = params['N_ret'], params['simtime'] |
|---|
| 89 | x = params['position'][0] |
|---|
| 90 | y = params['position'][1] |
|---|
| 91 | r2 = x**2 + y**2 |
|---|
| 92 | r = numpy.sqrt(r2) |
|---|
| 93 | id_center = [int(k) for k in numpy.where( r2 < N_ret**2)[0]] |
|---|
| 94 | # mean activity accross kernelseeds as a function of SNR |
|---|
| 95 | for i_exp, experiment in enumerate(p.iter_inner()): |
|---|
| 96 | # calculating the index in the parameter space |
|---|
| 97 | index = p.parameter_space_index(experiment) |
|---|
| 98 | # getting SpikeLists corresponding to the interesting parts (within the center) |
|---|
| 99 | temporal_ON[:,index[1]] += temporal_mean(DATA[i_exp]['out_ON_DATA'].id_slice(id_center))/N_seeds |
|---|
| 100 | temporal_OFF[:,index[1]] += temporal_mean(DATA[i_exp]['out_ON_DATA'].id_slice(id_center))/N_seeds |
|---|
| 101 | map_spatial_ON[:,index[1]] += DATA[i_exp]['out_ON_DATA'].mean_rates(t_start=simtime/4.,t_stop=3*simtime/4.)#/N_seeds |
|---|
| 102 | map_spatial_OFF[:,index[1]] += DATA[i_exp]['out_OFF_DATA'].mean_rates(t_start=simtime/4.,t_stop=3*simtime/4.)#/N_seeds |
|---|
| 103 | |
|---|
| 104 | results = shelve.open('results/mat-pre-' + name) |
|---|
| 105 | results['temporal_ON'] = temporal_ON |
|---|
| 106 | results['map_spatial_OFF'] = map_spatial_OFF |
|---|
| 107 | results['temporal_OFF'] = temporal_OFF |
|---|
| 108 | results['map_spatial_ON'] = map_spatial_ON |
|---|
| 109 | results['lower_edges'] = lower_edges |
|---|
| 110 | results.close() |
|---|
| 111 | |
|---|
| 112 | results.close() |
|---|
| 113 | |
|---|
| 114 | ############# MAKING FIGURE ############################ |
|---|
| 115 | from NeuroTools.plotting import pylab_params |
|---|
| 116 | from numpy import zeros, where, arange |
|---|
| 117 | |
|---|
| 118 | pylab.close('all') |
|---|
| 119 | pylab.ioff() #pylab.ion() # |
|---|
| 120 | |
|---|
| 121 | """ Figure |
|---|
| 122 | |
|---|
| 123 | Prints to a figure the mean firing rate |
|---|
| 124 | * in (x,y) accross time during the stimulation and |
|---|
| 125 | * in t accross positions within the center |
|---|
| 126 | for the output (ON and OFF) and for the different parameter values. |
|---|
| 127 | |
|---|
| 128 | """ |
|---|
| 129 | pylab.rcParams.update(pylab_params(fig_width_pt = 497.9) )#, text_fontsize=8)) |
|---|
| 130 | pylab.figure(num = 1, dpi=300, facecolor='w', edgecolor='k') |
|---|
| 131 | |
|---|
| 132 | x = params['position'][0] |
|---|
| 133 | y = params['position'][1] |
|---|
| 134 | #Lmargin, Rmargin, dmargin, umargin = 0.05, 0.15, 0.05, 0.05 |
|---|
| 135 | #pylab.axes([Lmargin, dmargin , 1.0 - Rmargin- Lmargin,1.0-umargin-dmargin]) # [left, bottom, width, height] |
|---|
| 136 | #pylab.subplot(131) |
|---|
| 137 | pylab.axes([0.1, 0.33, .3/1.61 , .3]) # [left, bottom, width, height] |
|---|
| 138 | pylab.scatter(x,y,c=params['amplitude'], faceted = False) #, edgecolors='none' |
|---|
| 139 | pylab.title('Input',fontsize ='small') |
|---|
| 140 | pylab.axis('equal') |
|---|
| 141 | pylab.subplot(232) |
|---|
| 142 | pylab.plot(lower_edges[:-1],temporal_ON) |
|---|
| 143 | pylab.title('time course (ROI) ',fontsize = 'small') |
|---|
| 144 | #pylab.title('time course ON',fontsize = 'small') |
|---|
| 145 | pylab.xticks( numpy.linspace(0, params.simtime, 5) ) |
|---|
| 146 | pylab.ylabel('ON activity (Hz / neuron)') |
|---|
| 147 | #pylab.axis('tight') |
|---|
| 148 | pylab.subplot(235) |
|---|
| 149 | pylab.plot(lower_edges[:-1],temporal_OFF) |
|---|
| 150 | #pylab.title('time course OFF',fontsize = 'small') |
|---|
| 151 | pylab.xticks( numpy.linspace(0, params.simtime, 5) ) |
|---|
| 152 | pylab.ylabel('OFF activity (Hz / neuron)') |
|---|
| 153 | #pylab.axis('tight') |
|---|
| 154 | pylab.xlabel('time (ms)') |
|---|
| 155 | pylab.subplot(233) |
|---|
| 156 | pylab.scatter(x, y, c= map_spatial_ON[:,-1], faceted = False) #, edgecolors='none' |
|---|
| 157 | #pylab.title('spatial distribution ON',fontsize ='small') |
|---|
| 158 | pylab.title('Output',fontsize ='small') |
|---|
| 159 | pylab.subplot(236) |
|---|
| 160 | pylab.scatter(x, y, c= map_spatial_OFF[:,-1], faceted = False) #, edgecolors='none' |
|---|
| 161 | #pylab.title('spatial distribution OFF',fontsize ='small') |
|---|
| 162 | |
|---|
| 163 | if 0: |
|---|
| 164 | pylab.ion() |
|---|
| 165 | pylab.show() |
|---|
| 166 | else: |
|---|
| 167 | pylab.savefig('results/fig-' + name + '.pdf') |
|---|
| 168 | pylab.savefig('results/fig-' + name + '.png', dpi = 300) |
|---|
| 169 | |
|---|
| 170 | |
|---|