root/trunk/examples/retina/benchmark_linear.py

Revision 340, 3.7 KB (checked in by LaurentPerrinet, 3 years ago)

some polishing of the wiki:examples, the retina needs a bit more work...

  • Property svn:keywords set to Id
Line 
1#!/usr/bin/env python
2# -*- coding: utf8 -*-
3"""
4benchmark_linear.py
5=========================
6
7Having fixed the background noise we are just studying now how different
8signal to noise ratios are integrated by the neurons.
9
10Laurent Perrinet, INCM, CNRS
11
12$ Id $
13
14"""
15
16import os, sys, numpy, pylab, shelve
17
18N, N_exp = 1000, 6
19t_smooth = 100. # width (in ms) of the integration window
20from NeuroTools.parameters import ParameterSpace, ParameterRange
21snr  = 2.0 * numpy.linspace(0.1,2.0,N_exp)
22p = ParameterSpace({'snr' : ParameterRange(list(snr))})
23
24
25name = sys.argv[0].split('.')[0] # name of the current script withpout the '.py' part
26results = shelve.open('results/mat-' + name)
27try:
28
29    temporal_ON = results['temporal_ON']
30    temporal_OFF = results['temporal_OFF']
31    lower_edges = results['lower_edges']
32    params = results['params']
33    #if (params == retina.params): raise('Parameters have changed')
34
35except:
36    from retina import *
37    retina = Retina(N)
38    retina.params['amplitude'] = numpy.ones(retina.params['amplitude'].shape)
39   
40
41    # calculates the dimension of the parameter space
42    results_dim, results_label = p.parameter_space_dimension_labels()
43
44    # creates results array with size of parameter space dimension
45    data = retina.run(retina.params,verbose=False)
46    lower_edges = data['out_ON_DATA'].time_axis(t_smooth)
47    N_smooth = len(lower_edges)
48   
49    temporal_ON, temporal_OFF = [],[]
50    import progressbar # see http://projects.scipy.org/pipermail/scipy-dev/2008-January/008200.html
51    pbar=progressbar.ProgressBar(widgets=[name, " ", progressbar.Percentage(), ' ',
52            progressbar.Bar(), ' ', progressbar.ETA()], maxval=N_exp)
53    for i_exp,experiment in enumerate(p.iter_inner()):
54        params = retina.params
55        params.update(experiment) # updates what changed in the dictionary
56        # simulate the experiment and get its data
57        data = retina.run(params,verbose=False)
58        # calculating the index in the parameter space
59        index = p.parameter_space_index(experiment)
60        # put the data at the right position in the results array
61        temporal_ON.append(sum(data['out_ON_DATA'].firing_rate(t_smooth))/N)#
62        temporal_OFF.append(sum(data['out_OFF_DATA'].firing_rate(t_smooth))/N)#
63        pbar.update(i_exp)
64
65   
66    results['lower_edges'] = lower_edges
67    results['temporal_ON'] = temporal_ON
68    results['temporal_OFF'] = temporal_OFF
69    results['params'] = retina.params
70
71    pbar.finish()
72
73results.close()
74
75###############################################################################
76
77from NeuroTools.plotting import pylab_params
78
79""" Figure 1
80
81Prints to a figure the mean firing rate for the output (ON and OFF) as a function
82of the different parameter values. It's similar to a CRF function.
83
84"""
85#pylab.close('all')
86#pylab.rcParams.update(pylab_params(fig_width_pt = 497.9/2., ratio = 1.))
87pylab.figure(1)
88#fmax = numpy.max([numpy.max(temporal_OFF[:]),numpy.max(temporal_ON[:])])
89
90pylab.subplot(211)
91for i_exp in range(N_exp):
92    pylab.plot(lower_edges[:-1] + t_smooth/2, temporal_ON[i_exp],
93                        label= '%5.2f' % p.snr._values[i_exp])
94pylab.xticks( numpy.round(numpy.linspace(0, params.simtime, 5),0) )
95pylab.ylabel('ON Firing frequency (Hz)')
96pylab.axis([0, params.simtime, 0.0, numpy.max(temporal_ON[:])])
97pylab.legend(loc='upper right')
98pylab.subplot(212)
99for i_exp in range(N_exp):
100    pylab.plot(lower_edges[:-1] + t_smooth/2, temporal_OFF[i_exp])
101pylab.xticks( numpy.round(numpy.linspace(0, params.simtime, 5),0) )
102pylab.ylabel('OFF Firing frequency (Hz)')
103pylab.xlabel('time (ms)')
104pylab.axis([0, params.simtime, 0.0, numpy.max(temporal_OFF[:]) ])
105
106
107if 0:
108    pylab.ion()
109    #pylab.show()
110else:
111    pylab.savefig('results/fig-' + name + '.pdf')
112    pylab.savefig('results/fig-' + name + '.png', dpi = 300)
113
Note: See TracBrowser for help on using the browser.