root/trunk/examples/retina/benchmark_retina.py

Revision 363, 6.3 KB (checked in by LaurentPerrinet, 3 years ago)

some polishing of theretina in wiki:examples

  • Property svn:keywords set to Id
Line 
1#!/usr/bin/env python
2# -*- coding: utf8 -*-
3"""
4benchmark_retina.py
5===================
6
7This 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
10The input is a discrete dirac simulating the response in Magnocellular Pathway
11M to primary visual cortex' layer 4C$\alpha$ then to 4B as defined in Benchmark
12one. The paramters tested is SNR sensitivity / spatial stability of V1 response.
13
14
15Illustrates how one many use parameters to explore different sets of parameters
16and compute their effect. See benchmark_noise and benchmark_linear for simpler
17examples.
18
19Laurent Perrinet, INCM, CNRS
20
21$ Id $
22
23"""
24
25import os, sys, numpy, pylab, shelve, progressbar
26
27N, N_snr, N_seeds = 1000, 5, 10
28from NeuroTools.parameters import *
29p =  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
33name = sys.argv[0].split('.')[0] # name of the current script withpout the '.py' part
34
35try:
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
75except:
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
112results.close()
113
114############# MAKING FIGURE ############################
115from NeuroTools.plotting import pylab_params
116from numpy import zeros, where, arange
117
118pylab.close('all')
119pylab.ioff() #pylab.ion() #
120
121""" Figure
122
123Prints 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
126for the output (ON and OFF) and for the different parameter values.
127
128"""
129pylab.rcParams.update(pylab_params(fig_width_pt = 497.9) )#, text_fontsize=8))
130pylab.figure(num = 1, dpi=300, facecolor='w', edgecolor='k')
131
132x = params['position'][0]
133y = 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)
137pylab.axes([0.1, 0.33, .3/1.61 , .3]) # [left, bottom, width, height]
138pylab.scatter(x,y,c=params['amplitude'], faceted = False) #, edgecolors='none'
139pylab.title('Input',fontsize ='small')
140pylab.axis('equal')
141pylab.subplot(232)
142pylab.plot(lower_edges[:-1],temporal_ON)
143pylab.title('time course (ROI) ',fontsize = 'small')
144#pylab.title('time course ON',fontsize = 'small')
145pylab.xticks( numpy.linspace(0, params.simtime, 5) )
146pylab.ylabel('ON activity (Hz / neuron)')
147#pylab.axis('tight')
148pylab.subplot(235)
149pylab.plot(lower_edges[:-1],temporal_OFF)
150#pylab.title('time course OFF',fontsize = 'small')
151pylab.xticks( numpy.linspace(0, params.simtime, 5) )
152pylab.ylabel('OFF activity (Hz / neuron)')
153#pylab.axis('tight')
154pylab.xlabel('time (ms)')
155pylab.subplot(233)
156pylab.scatter(x, y, c= map_spatial_ON[:,-1], faceted = False) #, edgecolors='none'
157#pylab.title('spatial distribution ON',fontsize ='small')
158pylab.title('Output',fontsize ='small')
159pylab.subplot(236)
160pylab.scatter(x, y, c= map_spatial_OFF[:,-1], faceted = False) #, edgecolors='none'
161#pylab.title('spatial distribution OFF',fontsize ='small')
162
163if 0:
164    pylab.ion()
165    pylab.show()
166else:
167    pylab.savefig('results/fig-' + name + '.pdf')
168    pylab.savefig('results/fig-' + name + '.png', dpi = 300)
169
170
Note: See TracBrowser for help on using the browser.