annotate utils.py @ 21:d395461b92ae tip

Lots and lots of modifications. Approximate recovery script working.
author Nic Cleju <nikcleju@gmail.com>
date Mon, 23 Apr 2012 10:54:57 +0300
parents 2837cfeaf353
children
rev   line source
nikcleju@0 1 # -*- coding: utf-8 -*-
nikcleju@0 2 """
nikcleju@17 3 Some utility functions.
nikcleju@0 4
nikcleju@17 5 Author: Nicolae Cleju
nikcleju@0 6 """
nikcleju@0 7
nikcleju@0 8 import numpy
nikcleju@0 9 import scipy.io
nikcleju@0 10 import matplotlib.pyplot as plt
nikcleju@0 11 import matplotlib.cm as cm
nikcleju@0 12 import matplotlib.colors as mcolors
nikcleju@0 13
nikcleju@0 14 # Sample call
nikcleju@0 15 #utils.loadshowmatrices_multipixels('H:\\CS\\Python\\Results\\pt_std1\\approx_pt_std1.mat', dosave=True, saveplotbase='approx_pt_std1_',saveplotexts=('png','eps','pdf'))
nikcleju@0 16
nikcleju@19 17 def replot_exact(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
nikcleju@19 18 """
nikcleju@19 19 Replot exact recovery results from mat file, with better axis ticks and
nikcleju@19 20 other custom tweaked options.
nikcleju@19 21 """
nikcleju@19 22
nikcleju@19 23 mdict = scipy.io.loadmat(filename)
nikcleju@19 24
nikcleju@19 25 if algonames == None:
nikcleju@19 26 if 'algonames' in mdict:
nikcleju@19 27 algonames = mdict['algonames']
nikcleju@19 28 else:
nikcleju@19 29 print "No algonames given, and couldn't find them in mat file."
nikcleju@19 30 print "Exiting."
nikcleju@19 31 return
nikcleju@19 32
nikcleju@21 33 withticks = ['GAP']
nikcleju@21 34 withnoaxes = [algoname[0][0] for algoname in algonames if algoname not in withticks]
nikcleju@21 35 loadshowmatrices_multipixels(filename, algonames, [], [], withticks, withnoaxes, doshow, dosave, saveplotbase, saveplotexts)
nikcleju@19 36
nikcleju@19 37 def replot_approx(filename, algonames = None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
nikcleju@19 38 """
nikcleju@19 39 Replot exact recovery results from mat file, with better axis ticks and
nikcleju@19 40 other custom tweaked options.
nikcleju@19 41 """
nikcleju@19 42
nikcleju@19 43 mdict = scipy.io.loadmat(filename)
nikcleju@19 44
nikcleju@19 45 if algonames == None:
nikcleju@19 46 if 'algosNnames' in mdict and 'algosLnames' in mdict:
nikcleju@21 47
nikcleju@21 48 if mdict['algosLnames'].size is 0:
nikcleju@21 49 algonames = mdict['algosNnames']
nikcleju@21 50 elif mdict['algosNnames'].size is 0:
nikcleju@21 51 algonames = mdict['algosLnames']
nikcleju@21 52 else:
nikcleju@21 53 algonames = numpy.vstack((mdict['algosNnames'], mdict['algosLnames']))
nikcleju@19 54 else:
nikcleju@19 55 print "No algonames given, and couldn't find them in mat file."
nikcleju@19 56 print "Exiting."
nikcleju@19 57 return
nikcleju@19 58
nikcleju@19 59 if dosave:
nikcleju@19 60 lambdas = mdict['lambdas']
nikcleju@19 61 threshs = [(0.85,2,0),(0.8,2,0.4),(0.5,2,1)]
nikcleju@19 62 withticks = ['GAP']
nikcleju@19 63 withnoaxes = [algoname[0][0] for algoname in algonames if algoname not in withticks]
nikcleju@19 64 #withnoaxes.remove('GAP')
nikcleju@19 65 loadshowmatrices_multipixels(filename, algonames, lambdas, threshs, withticks, withnoaxes, doshow, dosave, saveplotbase, saveplotexts)
nikcleju@19 66
nikcleju@19 67 def loadshowmatrices_multipixels(filename, algonames, lambdas, threshs = [], withticks = [], withnoaxes = [], doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
nikcleju@0 68
nikcleju@0 69 if dosave and (saveplotbase is None or saveplotexts is None):
nikcleju@0 70 print('Error: please specify name and extensions for saving')
nikcleju@0 71 raise Exception('Name or extensions for saving not specified')
nikcleju@0 72
nikcleju@0 73 mdict = scipy.io.loadmat(filename)
nikcleju@19 74
nikcleju@19 75 N = 10 # one data box = NxN
nikcleju@19 76
nikcleju@0 77 for algonameobj in algonames:
nikcleju@0 78 algoname = algonameobj[0][0]
nikcleju@0 79 print algoname
nikcleju@0 80 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
nikcleju@0 81
nikcleju@0 82 # Prepare bigger matrix
nikcleju@0 83 rows,cols = mdict['meanmatrix'][algoname][0,0].shape
nikcleju@0 84 bigmatrix = numpy.zeros((N*rows,N*cols))
nikcleju@0 85 for i in numpy.arange(rows):
nikcleju@0 86 for j in numpy.arange(cols):
nikcleju@0 87 bigmatrix[i*N:i*N+N,j*N:j*N+N] = mdict['meanmatrix'][algoname][0,0][i,j]
nikcleju@19 88
nikcleju@19 89 for thrval,width,color in threshs:
nikcleju@19 90 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,N,thrval,width,color)
nikcleju@19 91
nikcleju@19 92 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.95,2,0)
nikcleju@19 93 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.8, 2,0.4)
nikcleju@19 94 #bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0],bigmatrix,10,0.5, 2,1)
nikcleju@19 95
nikcleju@0 96 # # Mark 95% border
nikcleju@0 97 # if mdict['meanmatrix'][algoname][0,0][i,j] > thresh:
nikcleju@0 98 # # Top border
nikcleju@0 99 # if mdict['meanmatrix'][algoname][0,0][i-1,j] < thresh and i>0:
nikcleju@0 100 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 101 # # Bottom border
nikcleju@0 102 # if mdict['meanmatrix'][algoname][0,0][i+1,j] < thresh and i<rows-1:
nikcleju@0 103 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 104 # # Left border
nikcleju@0 105 # if mdict['meanmatrix'][algoname][0,0][i,j-1] < thresh and j>0:
nikcleju@0 106 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 107 # # Right border (not very probable)
nikcleju@0 108 # if j<cols-1 and mdict['meanmatrix'][algoname][0,0][i,j+1] < thresh:
nikcleju@0 109 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 110
nikcleju@0 111 plt.figure()
nikcleju@7 112 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@19 113
nikcleju@19 114 if algoname in withticks:
nikcleju@8 115 int_setticks()
nikcleju@19 116 if algoname in withnoaxes:
nikcleju@8 117 plt.gca().get_xaxis().set_visible(False)
nikcleju@8 118 plt.gca().get_yaxis().set_visible(False)
nikcleju@7 119
nikcleju@0 120 if dosave:
nikcleju@0 121 for ext in saveplotexts:
nikcleju@0 122 plt.savefig(saveplotbase + algoname + '.' + ext, bbox_inches='tight')
nikcleju@0 123 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
nikcleju@0 124 if dosave:
nikcleju@0 125 ilbd = 0
nikcleju@0 126
nikcleju@0 127 for matrix in mdict['meanmatrix'][algoname][0,0]:
nikcleju@0 128
nikcleju@0 129 # Prepare bigger matrix
nikcleju@0 130 rows,cols = matrix.shape
nikcleju@0 131 bigmatrix = numpy.zeros((N*rows,N*cols))
nikcleju@0 132 for i in numpy.arange(rows):
nikcleju@0 133 for j in numpy.arange(cols):
nikcleju@0 134 bigmatrix[i*N:i*N+N,j*N:j*N+N] = matrix[i,j]
nikcleju@19 135
nikcleju@19 136 for thrval,width,color in threshs:
nikcleju@19 137 bigmatrix = int_drawseparation(mdict['meanmatrix'][algoname][0,0][ilbd],bigmatrix,N,thrval,width,color)
nikcleju@19 138
nikcleju@19 139 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.95,2,0)
nikcleju@19 140 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.8, 2,0.4)
nikcleju@19 141 #bigmatrix = int_drawseparation(matrix,bigmatrix,10,0.5, 2,1)
nikcleju@0 142 # # Mark 95% border
nikcleju@0 143 # if matrix[i,j] > thresh:
nikcleju@0 144 # # Top border
nikcleju@0 145 # if matrix[i-1,j] < thresh and i>0:
nikcleju@0 146 # bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 147 # # Bottom border
nikcleju@0 148 # if matrix[i+1,j] < thresh and i<rows-1:
nikcleju@0 149 # bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 150 # # Left border
nikcleju@0 151 # if matrix[i,j-1] < thresh and j>0:
nikcleju@0 152 # bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 153 # # Right border (not very probable)
nikcleju@0 154 # if j<cols-1 and matrix[i,j+1] < thresh:
nikcleju@0 155 # bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 156
nikcleju@0 157 plt.figure()
nikcleju@0 158 #plt.imshow(matrix, cmap=cm.gray, interpolation='nearest',origin='lower')
nikcleju@5 159 plt.imshow(bigmatrix, cmap=cm.gray, norm=mcolors.Normalize(0,1), interpolation='nearest',origin='lower')
nikcleju@19 160
nikcleju@19 161 #plt.gca().get_xaxis().set_visible(False)
nikcleju@19 162 #plt.gca().get_yaxis().set_visible(False)
nikcleju@7 163 #int_setticks()
nikcleju@19 164 if algoname in withticks:
nikcleju@19 165 int_setticks()
nikcleju@19 166 if algoname in withnoaxes:
nikcleju@19 167 plt.gca().get_xaxis().set_visible(False)
nikcleju@19 168 plt.gca().get_yaxis().set_visible(False)
nikcleju@19 169
nikcleju@0 170 if dosave:
nikcleju@0 171 for ext in saveplotexts:
nikcleju@0 172 plt.savefig(saveplotbase + algoname + ('_lbd%.0e' % lambdas[ilbd]) + '.' + ext, bbox_inches='tight')
nikcleju@19 173
nikcleju@19 174 ilbd = ilbd + 1
nikcleju@0 175 if doshow:
nikcleju@0 176 plt.show()
nikcleju@0 177 print "Finished."
nikcleju@21 178
nikcleju@21 179 def replot_ProveEll1(filename, algonames=None, doshow=True, dosave=False, saveplotbase=None, saveplotexts=None):
nikcleju@21 180
nikcleju@21 181 """
nikcleju@21 182 Plot ...
nikcleju@21 183 The files are saved in the current folder.
nikcleju@21 184 """
nikcleju@21 185
nikcleju@21 186 mdict = scipy.io.loadmat(filename)
nikcleju@21 187
nikcleju@21 188 if algonames == None:
nikcleju@21 189 if 'algosNnames' in mdict and 'algosLnames' in mdict:
nikcleju@21 190 algonames = numpy.vstack((mdict['algosNnames'], mdict['algosLnames']))
nikcleju@21 191 else:
nikcleju@21 192 print "No algonames given, and couldn't find them in mat file."
nikcleju@21 193 print "Exiting."
nikcleju@21 194 return
nikcleju@21 195
nikcleju@21 196 lambdas = mdict['lambdas']
nikcleju@21 197
nikcleju@21 198 toplot = numpy.zeros((len(lambdas),len(algonames)))
nikcleju@21 199
nikcleju@21 200 idxcol = 0
nikcleju@21 201 for algonameobj in algonames:
nikcleju@21 202 algoname = algonameobj[0][0]
nikcleju@21 203 if mdict['meanmatrix'][algoname][0,0].ndim == 2:
nikcleju@21 204 toplot[:,idxcol] = (1 - mdict['meanmatrix'][algoname][0,0]) * numpy.ones(len(lambdas))
nikcleju@21 205 elif mdict['meanmatrix'][algoname][0,0].ndim == 3:
nikcleju@21 206 for ilbd in numpy.arange(len(lambdas)):
nikcleju@21 207 toplot[ilbd,idxcol] = 1 - mdict['meanmatrix'][algoname][0,0][ilbd][0,0]
nikcleju@21 208 idxcol = idxcol + 1
nikcleju@21 209
nikcleju@21 210 plt.figure()
nikcleju@21 211 plt.plot(toplot)
nikcleju@21 212 #plt.legend([algonameobj[0][0] for algonameobj in algonames])
nikcleju@21 213 plt.legend(['NESTA', r'ABS-$\lambda$: BP'])
nikcleju@21 214
nikcleju@21 215 ax = plt.gca()
nikcleju@21 216
nikcleju@21 217 fewerticks = [0,3,6,9]
nikcleju@21 218 xticklabels = []
nikcleju@21 219 for i in range(lambdas.size):
nikcleju@21 220 if i in fewerticks:
nikcleju@21 221 xticklabels.append('{:g}'.format(lambdas[i,0]))
nikcleju@21 222 else:
nikcleju@21 223 xticklabels.append('')
nikcleju@21 224 ax.set_xticklabels(xticklabels)
nikcleju@21 225 #ax.set_xlabel(r'Value of $\lambda$', size=60)
nikcleju@21 226 ax.set_xlabel(r'$\lambda$', size=40)
nikcleju@21 227 for label in ax.get_xticklabels():
nikcleju@21 228 label.set_fontsize(20)
nikcleju@21 229 for label in ax.get_yticklabels():
nikcleju@21 230 label.set_fontsize(20)
nikcleju@21 231 for ext in saveplotexts:
nikcleju@21 232 plt.savefig(saveplotbase + '.' + ext, bbox_inches='tight')
nikcleju@21 233
nikcleju@21 234 if doshow:
nikcleju@21 235 plt.show()
nikcleju@21 236 print "Finished."
nikcleju@0 237
nikcleju@0 238 def appendtomatfile(filename, toappend, toappendname):
nikcleju@0 239 mdict = scipy.io.loadmat(filename)
nikcleju@0 240 mdict[toappendname] = toappend
nikcleju@0 241 try:
nikcleju@0 242 scipy.io.savemat(filename, mdict)
nikcleju@0 243 except:
nikcleju@0 244 print "Save error"
nikcleju@0 245
nikcleju@0 246 # To save to a cell array, create an object array:
nikcleju@0 247 # >>> obj_arr = np.zeros((2,), dtype=np.object)
nikcleju@0 248 # >>> obj_arr[0] = 1
nikcleju@0 249 # >>> obj_arr[1] = 'a string'
nikcleju@0 250
nikcleju@0 251 def int_drawseparation(matrix,bigmatrix,N,thresh,border,bordercolor):
nikcleju@0 252 rows,cols = matrix.shape
nikcleju@0 253 for i in numpy.arange(rows):
nikcleju@0 254 for j in numpy.arange(cols):
nikcleju@0 255 # Mark border
nikcleju@0 256 # Use top-left corner of current square for reference
nikcleju@0 257 if matrix[i,j] > thresh:
nikcleju@0 258 # Top border
nikcleju@0 259 if matrix[i-1,j] < thresh and i>0:
nikcleju@0 260 bigmatrix[i*N:i*N+border,j*N:j*N+N] = bordercolor
nikcleju@0 261 # Bottom border
nikcleju@0 262 if i<rows-1 and matrix[i+1,j] < thresh:
nikcleju@0 263 bigmatrix[i*N+N-border:i*N+N,j*N:j*N+N] = bordercolor
nikcleju@0 264 # Left border
nikcleju@0 265 if matrix[i,j-1] < thresh and j>0:
nikcleju@0 266 bigmatrix[i*N:i*N+N,j*N:j*N+border] = bordercolor
nikcleju@0 267 # Right border (not very probable)
nikcleju@0 268 if j<cols-1 and matrix[i,j+1] < thresh:
nikcleju@0 269 bigmatrix[i*N:i*N+N,j*N+N-border:j*N+N] = bordercolor
nikcleju@0 270
nikcleju@7 271 return bigmatrix
nikcleju@19 272
nikcleju@7 273
nikcleju@7 274 def int_setticks():
nikcleju@7 275
nikcleju@8 276 #ticks = [10, 94, 179]
nikcleju@8 277 #ticklabels = ["0.05", "0.5", "0.95"]
nikcleju@8 278 ticks = [10, 179]
nikcleju@8 279 ticklabels = ["0.05", "0.95"]
nikcleju@7 280
nikcleju@7 281 ax = plt.gca()
nikcleju@7 282 ax.set_xticks(ticks)
nikcleju@7 283 ax.set_xticklabels(ticklabels)
nikcleju@7 284 ax.set_yticks(ticks)
nikcleju@7 285 ax.set_yticklabels(ticklabels)
nikcleju@7 286
nikcleju@7 287 for label in ax.get_xticklabels():
nikcleju@7 288 label.set_fontsize(42)
nikcleju@7 289 for label in ax.get_yticklabels():
nikcleju@7 290 label.set_fontsize(42)
nikcleju@7 291
nikcleju@8 292 ax.set_xlabel(r'$\delta$', size=60)
nikcleju@8 293 ax.set_ylabel(r'$\rho$', size=60)
nikcleju@8 294