Mercurial > hg > absrec
comparison utils.py @ 7:b4a0b4dfe206
utils.py: added function int_setticks() to set ticks and ticklabels
author | nikcleju |
---|---|
date | Fri, 13 Jan 2012 18:44:26 +0000 |
parents | 09651b934691 |
children | 4d1bfd404f6a |
comparison
equal
deleted
inserted
replaced
6:09651b934691 | 7:b4a0b4dfe206 |
---|---|
12 import matplotlib.colors as mcolors | 12 import matplotlib.colors as mcolors |
13 | 13 |
14 # Sample call | 14 # Sample call |
15 #utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf')) | 15 #utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf')) |
16 | 16 |
17 #def loadshowmatrices(filename, algonames = None): | |
18 # mdict = scipy.io.loadmat(filename) | |
19 # if algonames == None: | |
20 # algonames = mdict['algonames'] | |
21 # | |
22 # for algonameobj in algonames: | |
23 # algoname = algonameobj[0][0] | |
24 # print algoname | |
25 # if mdict['meanmatrix'][algoname][0,0].ndim == 2: | |
26 # plt.figure() | |
27 # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') | |
28 # elif mdict['meanmatrix'][algoname][0,0].ndim == 3: | |
29 # for matrix in mdict['meanmatrix'][algoname][0,0]: | |
30 # plt.figure() | |
31 # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') | |
32 # plt.show() | |
33 # print "Finished." | |
34 # | |
35 # | |
36 #def loadsavematrices(filename, saveplotbase, saveplotexts, algonames = None): | |
37 # | |
38 # mdict = scipy.io.loadmat(filename) | |
39 # lambdas = mdict['lambdas'] | |
40 # | |
41 # if algonames is None: | |
42 # algonames = mdict['algonames'] | |
43 # | |
44 # for algonameobj in algonames: | |
45 # algoname = algonameobj[0][0] | |
46 # print algoname | |
47 # if mdict['meanmatrix'][algoname][0,0].ndim == 2: | |
48 # plt.figure() | |
49 # plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') | |
50 # for ext in saveplotexts: | |
51 # plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') | |
52 # elif mdict['meanmatrix'][algoname][0,0].ndim == 3: | |
53 # ilbd = 0 | |
54 # for matrix in mdict['meanmatrix'][algoname][0,0]: | |
55 # plt.figure() | |
56 # plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') | |
57 # for ext in saveplotexts: | |
58 # plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') | |
59 # ilbd = ilbd + 1 | |
60 # print "Finished." | |
61 | |
62 def loadmatrices(filename, algonames=None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): | |
63 | |
64 if dosave and (saveplotbase is None or saveplotexts is None): | |
65 print('Error: please specify name and extensions for saving') | |
66 raise Exception('Name or extensions for saving not specified') | |
67 | |
68 mdict = scipy.io.loadmat(filename) | |
69 | |
70 if dosave: | |
71 lambdas = mdict['lambdas'] | |
72 | |
73 if algonames is None: | |
74 algonames = mdict['algonames'] | |
75 | |
76 for algonameobj in algonames: | |
77 algoname = algonameobj[0][0] | |
78 print algoname | |
79 if mdict['meanmatrix'][algoname][0,0].ndim == 2: | |
80 plt.figure() | |
81 plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') | |
82 if dosave: | |
83 for ext in saveplotexts: | |
84 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') | |
85 elif mdict['meanmatrix'][algoname][0,0].ndim == 3: | |
86 if dosave: | |
87 ilbd = 0 | |
88 for matrix in mdict['meanmatrix'][algoname][0,0]: | |
89 plt.figure() | |
90 plt.imshow(matrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') | |
91 if dosave: | |
92 for ext in saveplotexts: | |
93 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') | |
94 ilbd = ilbd + 1 | |
95 | |
96 if doshow: | |
97 plt.show() | |
98 print "Finished." | |
99 | |
100 | |
101 def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): | 17 def loadshowmatrices_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): |
102 | 18 |
103 if dosave and (saveplotbase is None or saveplotexts is None): | 19 if dosave and (saveplotbase is None or saveplotexts is None): |
104 print('Error: please specify name and extensions for saving') | 20 print('Error: please specify name and extensions for saving') |
105 raise Exception('Name or extensions for saving not specified') | 21 raise Exception('Name or extensions for saving not specified') |
147 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh: | 63 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh: |
148 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor | 64 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor |
149 | 65 |
150 plt.figure() | 66 plt.figure() |
151 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') | 67 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower') |
152 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') | 68 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') |
69 #if algoname == 'GAP': | |
70 int_setticks() | |
71 #else: | |
72 # plt.gca().get_xaxis().set_visible(False) | |
73 # plt.gca().get_yaxis().set_visible(False) | |
74 | |
153 if dosave: | 75 if dosave: |
154 for ext in saveplotexts: | 76 for ext in saveplotexts: |
155 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') | 77 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight') |
156 elif mdict['meanmatrix'][algoname][0,0].ndim == 3: | 78 elif mdict['meanmatrix'][algoname][0,0].ndim == 3: |
157 if dosave: | 79 if dosave: |
185 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor | 107 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor |
186 | 108 |
187 plt.figure() | 109 plt.figure() |
188 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') | 110 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') |
189 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') | 111 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') |
112 plt.gca().get_xaxis().set_visible(False) | |
113 plt.gca().get_yaxis().set_visible(False) | |
114 #int_setticks() | |
190 if dosave: | 115 if dosave: |
191 for ext in saveplotexts: | 116 for ext in saveplotexts: |
192 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') | 117 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') |
193 ilbd = ilbd + 1 | 118 ilbd = ilbd + 1 |
194 if doshow: | 119 if doshow: |
227 # Right border (not very probable) | 152 # Right border (not very probable) |
228 if j<cols-1 and matrix[i,j+1] < thresh: | 153 if j<cols-1 and matrix[i,j+1] < thresh: |
229 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor | 154 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor |
230 | 155 |
231 return bigmatrix | 156 return bigmatrix |
157 | |
158 def int_setticks(): | |
159 | |
160 ticks = [10, 94, 179] | |
161 ticklabels = ["0.05", "0.5", "0.95"] | |
162 | |
163 ax = plt.gca() | |
164 ax.set_xticks(ticks) | |
165 ax.set_xticklabels(ticklabels) | |
166 ax.set_yticks(ticks) | |
167 ax.set_yticklabels(ticklabels) | |
168 | |
169 for label in ax.get_xticklabels(): | |
170 label.set_fontsize(42) | |
171 for label in ax.get_yticklabels(): | |
172 label.set_fontsize(42) | |
173 | |
174 ax.set_xlabel('\delta') |