## Attachment 'experiment_whitening.py'

```   1 #/usr/bin/python
2 # -*- coding: utf8 -*-
3 """
4 experiment_whitening.py
5
6 Testing the whitening strategy.
7
8 Generates 2 figures:
9
10  - one which compares Olshausen's formulation and the actual statistical measure
11  - one which shows the actual effect of whitening
12
13
14 """
15 __author__ = "Laurent Perrinet INCM - CNRS"
16 __revision__ = "\$Id: experiment_whitening.py 2526 2009-01-22 08:32:16Z lup \$"
17
18 debug = False#
19 ##debug = True#
20
21
22 import matplotlib
23 matplotlib.use('agg')
24
25 import os, pylab, numpy
26 import progressbar # see http://projects.scipy.org/pipermail/scipy-dev/2008-January/008200.html
27 from image import load_in_database, patch, normalize, coco, retina, whitening, whitening_filt, olshausen_whitening_filt
28 import shelve
29
30 ext = '.svg' # type of output figure
31
32 def fig_corr(K,n_learning,url_database):
33     global ext,figure_height,results_folder
34     print 'Figure Corr'
35     bord = .03
36
37     middle = numpy.ceil(K.shape[0]/2)
38     K_axis= numpy.arange(-middle,middle)
39     vec = .5 * (K[middle,:] + K[middle+1,:])
40     hor = .5 * (K[:,middle] + K[:,middle+1])
41
42     K_ols = olshausen_whitening_filt(size = K.shape)
43     vec_ols = .5 * (K_ols[middle,:] + K_ols[middle+1,:])
44
45     #axes(rect, axisbg='w') where rect=[left, bottom, width, height] in
46     #normalized (0,1) units.  axisbg is the background color for the
47     #axis, default white
48
49     fig = pylab.figure(figsize=(3*figure_height,figure_height))
50     a = pylab.axes([bord, bord, 1/3.-bord,  1-bord])
51     a.semilogy(K_axis,1/vec,c='b')
52     a.semilogy(K_axis,1/hor,c='g')
53     a.semilogy(K_axis,1/vec_ols,c='k', ls='--',label='ols')
54     a.semilogy([],c='k', ls='-',label='stat')
55     a.legend(loc="upper right")
56
57     a = pylab.axes([1/3.+bord, bord, 1/3.-2*bord , 1. -2*bord])
58     a.matshow(numpy.log(K))
59     a.axis('off')
60
61     a = pylab.axes([2/3.+bord, bord, 1/3.-bord, 1-bord])
62     a.semilogy(K_axis,vec,c='b')
63     a.semilogy(K_axis,hor,c='g')
64     a.semilogy(K_axis,vec_ols,c='k', ls='--')
65     a.semilogy([],c='b',label='vert')
66     a.semilogy([],c='g',label='hor')
67     a.legend(loc="upper right")
68     fig.savefig(results_folder + '/'  + 'whitening_corr' + ext)
69
70     #pylab.show()
71     pylab.close(fig)
72
73
74 def fig_whitening(image,K,n_learning,url_database):
75     global ext,figure_height,results_folder
76
77     # makes or load results
78     results = shelve.open(os.path.join(results_folder,'mat-xcorr'))
79     try:
80         xcorr = results['xcorr']
81         xcorr_white = results['xcorr_white']
82     except:
83         print ' Testing that whitening effectively works... '
84         size_patch = (128,128)
85         xcorr, xcorr_white = numpy.zeros(size_patch), numpy.zeros(size_patch)
86         pbar=progressbar.ProgressBar(widgets=["calculating", " ", progressbar.Percentage(), ' ',
87             progressbar.Bar(), ' ', progressbar.ETA()], maxval=n_learning)
88
89         for i_learning in range(n_learning):
90
91             image_ = normalize(patch(load_in_database(url_database = url_database),image.shape))
92             image_patch = patch(image_, size_patch)#
93             xcorr += coco(image_patch, image_patch)/n_learning
94             white_ = retina(image_)
95             image_patch = patch(white_, size_patch)#
96             xcorr_white += coco(image_patch, image_patch)/n_learning
97
98             pbar.update(i_learning)
99         pbar.finish()
100         results['xcorr'] = xcorr
101         results['xcorr_white'] = xcorr_white
102     results.close()
103
104
105     middle = numpy.ceil(K.shape[0]/2)
106     xcorr_middle = .5 * (K[middle,:] + K[middle+1,:])
107
108
109     print 'Figure whitening'
110     fig = pylab.figure(figsize=(1.618*figure_height,figure_height))
111     bord = .03
112     # top left
113     a = pylab.axes([0,  1/2., 1/3., 1/2.])
114     a.matshow(image, cmap=pylab.gray())
115     a.axis('off')
116     # top right
117     a = pylab.axes([2/3., 1/2., 1/3., 1/2.])
118     white = retina(image)
119     a.matshow(white, cmap=pylab.gray())
120     a.axis('off')
121     # middle
122     a = pylab.axes([1/3.+bord, .25, 1/3.-2*bord , 1/2.])
123     impulse = numpy.zeros(image.shape)#(25,25))
124     impulse[24,24] = 1
125     K = retina(impulse)[:49,:49]
126     middle = numpy.ceil(K.shape[0]/2)
127     K_middle = .5 * (K[middle,:] + K[middle+1,:])
128     a.plot([24,24],[numpy.min(K_middle), numpy.max(K_middle)], c='k')
129     a.plot([0, 49],[0,0], c='k')
130     a.plot(K_middle, c='b')
131     a.axis('off')
132     # bottom left
133     a = pylab.axes([0,  0, 1/3., 1/2.])
134     a.matshow(xcorr, cmap=pylab.jet())#
135     a.axis('off')
136     #bottom right
137     a = pylab.axes([2/3., 0, 1/3., 1/2.])
138     a.matshow(xcorr_white, cmap=pylab.jet())
139     a.axis('off')
140     # gid save the figure
141     fig.savefig(results_folder + '/'  + 'whitening' + ext)
142     pylab.close(fig)
143
144
145
146 def fig_atick(K):
147     """ This is just to remember that we use a simpler fitering technique.
148
149     In the Attick LGN, the gain is changed according to an estimation of the SNR.
150
151     """
152
153     global ext,figure_height,results_folder
154     print 'Figure Atick'
155
156     contrasts = 1. / 2**numpy.arange(6)
157     freqs = numpy.linspace(0.,.49, 40)
158     size  = K.shape
159     from scipy import mgrid
160     x, y = mgrid[0:size[0],0:size[1]]
161
162     response = numpy.zeros((len(freqs), len(contrasts)))
163     for i_contrast, contrast in enumerate(contrasts):
164         for i_freq, freq in enumerate(freqs):
165             image = contrast * numpy.sin( 2* numpy.pi * x * freq)
166             white = whitening(image)
167             response[i_freq,i_contrast] = numpy.std(white)
168     response.shape
169
170     fig = pylab.figure(figsize=(1.618*figure_height,figure_height))
171     a = pylab.subplot(111)
172     a.loglog(freqs,response,c='b')
173     pylab.xlabel('frequency (cycles / pixel)')
174     pylab.axis('tight')
175     fig.savefig(results_folder + '/'  + 'whitening_atick' + ext)
176     pylab.close(fig)
177
178
179 ###########################################################################
180 if __name__ == '__main__':
181
182     url_database = 'database/Yelmo'
183
184     if not(debug):
185         pylab.ioff()
186         #####################
187         figures_corr = True
188         figures_whitening = True
189         figures_atick = True
190         #####################
191         image = pylab.imread('database/gris512.png')[:,:,0]
192         n_learning = 200
193         figure_height = 6.  #inches
194         results_folder  = 'figures/'
195         recompute = False
196     else:
197         print ' /!\ DEBUGGING /!\ '
198         pylab.ion()
199         ##################### True False
200         figures_corr = True
201         figures_whitening = False
202         figures_atick = True
203         #####################
204         image = pylab.imread('database/gris128.png')[:,:,0]
205         n_learning = 10
206         figure_height = 6.  #inches
207         results_folder = 'figures_debug/'
208         recompute = True
209
210     if not(os.path.isdir(results_folder)):  os.mkdir(results_folder)
211
212     K = whitening_filt(size = image.shape,
213                        url_database = url_database,
214                        n_learning = n_learning*5,
215                        recompute = recompute,
216                        )
217
218     if figures_corr:
219         fig_corr(K,n_learning,url_database)
220
221     #image = normalize(image)
222     #print image.shape
223
224     if figures_whitening:
225         fig_whitening(image,K,n_learning,url_database)
226     if figures_atick:
227         fig_atick(K)
228
229     print 'Done!'
```

## Attached Files

To refer to attachments on a page, use attachment:filename, as shown below in the list of files. Do NOT use the URL of the [get] link, since this is subject to change and can break easily.
• [get | view] (2014-10-25 14:02:40, 16.6 KB) [[attachment:GoldenPyramid.py]]
• [get | view] (2014-10-25 14:02:40, 3.3 KB) [[attachment:Makefile]]
• [get | view] (2014-10-25 14:02:42, 8.4 KB) [[attachment:SpikeCoding.tex]]
• [get | view] (2012-03-17 18:25:02, 80198.6 KB) [[attachment:database.zip]]
• [get | view] (2014-10-25 14:14:14, 22.4 KB) [[attachment:experiment_SpikeCoding.py]]
• [get | view] (2014-10-25 14:14:14, 6.9 KB) [[attachment:experiment_whitening.py]]
• [get | view] (2014-10-25 14:14:15, 11.7 KB) [[attachment:image.py]]
• [get | view] (2012-03-17 18:27:17, 918.3 KB) [[attachment:perrinet08spie.pdf]]
• [get | view] (2014-10-25 14:14:25, 54.8 KB) [[attachment:perrinet08spie.tex]]
• [get | view] (2012-03-17 18:27:07, 14240.3 KB) [[attachment:perrinet08spie_talk.pdf]]
All files | Selected Files: delete move to page

You are not allowed to attach a file to this page.

welcome:
• getACL = 0.003s
• i18n_init = 0.015s
• init = 0.015s
• loadLanguage = 0.000s
• load_multi_cfg = 0.011s
• run = 0.677s
• total = 0.693s