comparison utils.py @ 19:2837cfeaf353

Fixed plot functions in utils.py Started working for test_approx.py
author Nic Cleju <nikcleju@gmail.com>
date Thu, 05 Apr 2012 13:59:22 +0300
parents 7fdf964f4edd
children d395461b92ae
comparison
equal deleted inserted replaced
18:4a967f4f18a0 19:2837cfeaf353
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_multipixels(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None): 17 def replot_exact(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
18 """
19 Replot exact recovery results from mat file, with better axis ticks and
20 other custom tweaked options.
21 """
22
23 mdict = scipy.io.loadmat(filename)
24
25 if algonames == None:
26 if 'algonames' in mdict:
27 algonames = mdict['algonames']
28 else:
29 print "No algonames given, and couldn't find them in mat file."
30 print "Exiting."
31 return
32
33 loadshowmatrices_multipixels(filename, algonames, [], [], algonames, [], doshow, dosave, saveplotbase, saveplotexts)
34
35 def replot_approx(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
36 """
37 Replot exact recovery results from mat file, with better axis ticks and
38 other custom tweaked options.
39 """
40
41 mdict = scipy.io.loadmat(filename)
42
43 if algonames == None:
44 if 'algosNnames' in mdict and 'algosLnames' in mdict:
45 algonames = numpy.vstack((mdict['algosNnames'], mdict['algosLnames']))
46 else:
47 print "No algonames given, and couldn't find them in mat file."
48 print "Exiting."
49 return
50
51 if dosave:
52 lambdas = mdict['lambdas']
53 threshs = [(0.85,2,0),(0.8,2,0.4),(0.5,2,1)]
54 withticks = ['GAP']
55 withnoaxes = [algoname[0][0] for algoname in algonames if algoname not in withticks]
56 #withnoaxes.remove('GAP')
57 loadshowmatrices_multipixels(filename, algonames, lambdas, threshs, withticks, withnoaxes, doshow, dosave, saveplotbase, saveplotexts)
58
59 def loadshowmatrices_multipixels(filename, algonames, lambdas, threshs = [], withticks = [], withnoaxes = [], doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
18 60
19 if dosave and (saveplotbase is None or saveplotexts is None): 61 if dosave and (saveplotbase is None or saveplotexts is None):
20 print('Error: please specify name and extensions for saving') 62 print('Error: please specify name and extensions for saving')
21 raise Exception('Name or extensions for saving not specified') 63 raise Exception('Name or extensions for saving not specified')
22 64
23 mdict = scipy.io.loadmat(filename) 65 mdict = scipy.io.loadmat(filename)
24 66
25 if dosave: 67 N = 10 # one data box = NxN
26 lambdas = mdict['lambdas'] 68
27
28 if algonames == None:
29 algonames = mdict['algonames']
30
31 # thresh = 0.90
32 N = 10
33 # border = 2
34 # bordercolor = 0 # black
35
36 for algonameobj in algonames: 69 for algonameobj in algonames:
37 algoname = algonameobj[0][0] 70 algoname = algonameobj[0][0]
38 print algoname 71 print algoname
39 if mdict['meanmatrix'][algoname][0,0].ndim == 2: 72 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
40 73
42 rows,cols = mdict['meanmatrix'][algoname][0,0].shape 75 rows,cols = mdict['meanmatrix'][algoname][0,0].shape
43 bigmatrix = numpy.zeros((N*rows,N*cols)) 76 bigmatrix = numpy.zeros((N*rows,N*cols))
44 for i in numpy.arange(rows): 77 for i in numpy.arange(rows):
45 for j in numpy.arange(cols): 78 for j in numpy.arange(cols):
46 bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j] 79 bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j]
47 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0) 80
48 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.9, 2,0.2) 81 for thrval,width,color in threshs:
49 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4) 82 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,N,thrval,width,color)
50 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1) 83
84 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0)
85 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4)
86 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1)
87
51 # # Mark 95% border 88 # # Mark 95% border
52 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh: 89 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
53 # # Top border 90 # # Top border
54 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0: 91 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
55 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor 92 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
62 # # Right border (not very probable) 99 # # Right border (not very probable)
63 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh: 100 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
64 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor 101 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
65 102
66 plt.figure() 103 plt.figure()
67 #plt.imshow(mdict['meanmatrix'][algoname][0,0], cmap=cm.gray, interpolation='nearest',origin='lower')
68 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') 104 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
69 if algoname == 'GAP': 105
106 if algoname in withticks:
70 int_setticks() 107 int_setticks()
71 else: 108 if algoname in withnoaxes:
72 plt.gca().get_xaxis().set_visible(False) 109 plt.gca().get_xaxis().set_visible(False)
73 plt.gca().get_yaxis().set_visible(False) 110 plt.gca().get_yaxis().set_visible(False)
74 111
75 if dosave: 112 if dosave:
76 for ext in saveplotexts: 113 for ext in saveplotexts:
85 rows,cols = matrix.shape 122 rows,cols = matrix.shape
86 bigmatrix = numpy.zeros((N*rows,N*cols)) 123 bigmatrix = numpy.zeros((N*rows,N*cols))
87 for i in numpy.arange(rows): 124 for i in numpy.arange(rows):
88 for j in numpy.arange(cols): 125 for j in numpy.arange(cols):
89 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j] 126 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
90 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.95,2,0) 127
91 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.9, 2,0.2) 128 for thrval,width,color in threshs:
92 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8, 2,0.4) 129 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0][ilbd],bigmatrix,N,thrval,width,color)
93 bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5, 2,1) 130
131 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.95,2,0)
132 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8, 2,0.4)
133 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5, 2,1)
94 # # Mark 95% border 134 # # Mark 95% border
95 # if matrix[i,j] > thresh: 135 # if matrix[i,j] > thresh:
96 # # Top border 136 # # Top border
97 # if matrix[i-1,j] < thresh and i>0: 137 # if matrix[i-1,j] < thresh and i>0:
98 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor 138 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
107 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor 147 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
108 148
109 plt.figure() 149 plt.figure()
110 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower') 150 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
111 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower') 151 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
112 plt.gca().get_xaxis().set_visible(False) 152
113 plt.gca().get_yaxis().set_visible(False) 153 #plt.gca().get_xaxis().set_visible(False)
154 #plt.gca().get_yaxis().set_visible(False)
114 #int_setticks() 155 #int_setticks()
156 if algoname in withticks:
157 int_setticks()
158 if algoname in withnoaxes:
159 plt.gca().get_xaxis().set_visible(False)
160 plt.gca().get_yaxis().set_visible(False)
161
115 if dosave: 162 if dosave:
116 for ext in saveplotexts: 163 for ext in saveplotexts:
117 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight') 164 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
118 ilbd = ilbd + 1 165
166 ilbd = ilbd + 1
119 if doshow: 167 if doshow:
120 plt.show() 168 plt.show()
121 print "Finished." 169 print "Finished."
122 170
123 def appendtomatfile(filename, toappend, toappendname): 171 def appendtomatfile(filename, toappend, toappendname):
152 # Right border (not very probable) 200 # Right border (not very probable)
153 if j<cols-1 and matrix[i,j+1] < thresh: 201 if j<cols-1 and matrix[i,j+1] < thresh:
154 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor 202 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
155 203
156 return bigmatrix 204 return bigmatrix
205
157 206
158 def int_setticks(): 207 def int_setticks():
159 208
160 #ticks = [10, 94, 179] 209 #ticks = [10, 94, 179]
161 #ticklabels = ["0.05", "0.5", "0.95"] 210 #ticklabels = ["0.05", "0.5", "0.95"]